/**
  * Processes a physical unit for the Solr index
  *
  * @access	protected
  *
  * @param	tx_dlf_document		&$doc: The METS document
  * @param	integer		$page: The page number
  * @param	array		$physicalUnit: Array of the physical unit to process
  *
  * @return	integer		0 on success or 1 on failure
  */
 protected static function processPhysical(tx_dlf_document &$doc, $page, array $physicalUnit)
 {
     $errors = 0;
     // Read extension configuration.
     $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]);
     if (!empty($physicalUnit['files'][$extConf['fileGrpFulltext']])) {
         $file = $doc->getFileLocation($physicalUnit['files'][$extConf['fileGrpFulltext']]);
         // Load XML file.
         if (\TYPO3\CMS\Core\Utility\GeneralUtility::isValidUrl($file) || version_compare(phpversion(), '5.3.3', '<')) {
             // Set user-agent to identify self when fetching XML data.
             if (!empty($extConf['useragent'])) {
                 @ini_set('user_agent', $extConf['useragent']);
             }
             // Turn off libxml's error logging.
             $libxmlErrors = libxml_use_internal_errors(TRUE);
             // disable entity loading
             $previousValueOfEntityLoader = libxml_disable_entity_loader(TRUE);
             // Load XML from file.
             $xml = simplexml_load_string(file_get_contents($file));
             // reset entity loader setting
             libxml_disable_entity_loader($previousValueOfEntityLoader);
             // Reset libxml's error logging.
             libxml_use_internal_errors($libxmlErrors);
             if ($xml === FALSE) {
                 return 1;
             }
         } else {
             return 1;
         }
         // Load class.
         if (!class_exists('Apache_Solr_Document')) {
             require_once \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:' . self::$extKey . '/lib/SolrPhpClient/Apache/Solr/Document.php');
         }
         // Create new Solr document.
         $solrDoc = new Apache_Solr_Document();
         // Create unique identifier from document's UID and unit's XML ID.
         $solrDoc->setField('id', $doc->uid . $physicalUnit['id']);
         $solrDoc->setField('uid', $doc->uid);
         $solrDoc->setField('pid', $doc->pid);
         $solrDoc->setField('page', $page);
         if (!empty($physicalUnit['files'][$extConf['fileGrpThumbs']])) {
             $solrDoc->setField('thumbnail', $doc->getFileLocation($physicalUnit['files'][$extConf['fileGrpThumbs']]));
         }
         $solrDoc->setField('partof', $doc->parentId);
         $solrDoc->setField('root', $doc->rootId);
         $solrDoc->setField('sid', $physicalUnit['id']);
         $solrDoc->setField('toplevel', FALSE);
         $solrDoc->setField('type', $physicalUnit['type'], self::$fields['fieldboost']['type']);
         $solrDoc->setField('fulltext', tx_dlf_alto::getRawText($xml));
         try {
             self::$solr->service->addDocument($solrDoc);
         } catch (Exception $e) {
             if (!defined('TYPO3_cliMode')) {
                 $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', tx_dlf_helper::getLL('flash.solrException', TRUE) . '<br />' . htmlspecialchars($e->getMessage()), tx_dlf_helper::getLL('flash.error', TRUE), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, TRUE);
                 tx_dlf_helper::addMessage($message);
             }
             return 1;
         }
     }
     return $errors;
 }
 /**
  * Processes a logical unit (and its children) for the Solr index
  *
  * @access	protected
  *
  * @param	tx_dlf_document		&$doc: The METS document
  * @param	array		$logicalUnit: Array of the logical unit to process
  *
  * @return	integer		0 on success or 1 on failure
  */
 protected static function process(tx_dlf_document &$doc, array $logicalUnit)
 {
     $errors = 0;
     // Get metadata for logical unit.
     $metadata = $doc->metadataArray[$logicalUnit['id']];
     if (!empty($metadata)) {
         // Load class.
         if (!class_exists('Apache_Solr_Document')) {
             require_once t3lib_div::getFileAbsFileName('EXT:' . self::$extKey . '/lib/SolrPhpClient/Apache/Solr/Document.php');
         }
         // Create new Solr document.
         $solrDoc = new Apache_Solr_Document();
         // Create unique identifier from document's UID and unit's XML ID.
         $solrDoc->setField('id', $doc->uid . $logicalUnit['id']);
         $solrDoc->setField('uid', $doc->uid);
         $solrDoc->setField('pid', $doc->pid);
         if (tx_dlf_helper::testInt($logicalUnit['points'])) {
             $solrDoc->setField('page', $logicalUnit['points']);
         }
         if ($logicalUnit['id'] == $doc->toplevelId) {
             $solrDoc->setField('thumbnail', $doc->thumbnail);
         } elseif (!empty($logicalUnit['thumbnailId'])) {
             $solrDoc->setField('thumbnail', $doc->getFileLocation($logicalUnit['thumbnailId']));
         }
         $solrDoc->setField('partof', $doc->parentId);
         $solrDoc->setField('sid', $logicalUnit['id']);
         $solrDoc->setField('toplevel', in_array($logicalUnit['type'], self::$toplevel));
         $solrDoc->setField('type', $logicalUnit['type'], self::$fields['fieldboost']['type']);
         $solrDoc->setField('title', $metadata['title'][0], self::$fields['fieldboost']['title']);
         $solrDoc->setField('volume', $metadata['volume'][0], self::$fields['fieldboost']['volume']);
         $autocomplete = array();
         foreach ($metadata as $index_name => $data) {
             if (!empty($data) && substr($index_name, -8) !== '_sorting') {
                 $solrDoc->setField(self::getIndexFieldName($index_name, $doc->pid), $data, self::$fields['fieldboost'][$index_name]);
                 if (in_array($index_name, self::$fields['sortables'])) {
                     // Add sortable fields to index.
                     $solrDoc->setField($index_name . '_sorting', $metadata[$index_name . '_sorting'][0]);
                 }
                 if (in_array($index_name, self::$fields['facets'])) {
                     // Add facets to index.
                     $solrDoc->setField($index_name . '_faceting', $data);
                 }
                 if (in_array($index_name, self::$fields['autocompleted'])) {
                     $autocomplete = array_merge($autocomplete, $data);
                 }
             }
         }
         // Add autocomplete values to index.
         if (!empty($autocomplete)) {
             $solrDoc->setField('autocomplete', $autocomplete);
         }
         try {
             self::$solr->service->addDocument($solrDoc);
         } catch (Exception $e) {
             if (!defined('TYPO3_cliMode')) {
                 $message = t3lib_div::makeInstance('t3lib_FlashMessage', tx_dlf_helper::getLL('flash.solrException', TRUE) . '<br />' . htmlspecialchars($e->getMessage()), tx_dlf_helper::getLL('flash.error', TRUE), t3lib_FlashMessage::ERROR, TRUE);
                 t3lib_FlashMessageQueue::addMessage($message);
             }
             return 1;
         }
     }
     // Check for child elements...
     if (!empty($logicalUnit['children'])) {
         foreach ($logicalUnit['children'] as $child) {
             if (!$errors) {
                 // ...and process them, too.
                 $errors = self::process($doc, $child);
             } else {
                 break;
             }
         }
     }
     return $errors;
 }