コード例 #1
0
 /**
  * Indexing a single file.
  * Use indexUsingCurrentSetup() or indexFiles() instead.
  *
  * @param	string		$filepath: file path or array: array('processFile' => 'path to file that should be indexed', 'metaFile' => 'additional file that holds meta data for the file to be indexed')
  * @param	integer		$crdate: timestamp of the index run
  * @param	integer		$pid: The sysfolder to store the meta data record
  * @param	mixed		$metaCallbackFunc Will be called to process the meta data
  * @param	mixed		$filePreprocessingCallbackFunc Will be called to allow preprocessing of the file before indexing
  * @param	array		$metaPreset: Meta data preset. $meta['fields'] has the record data.
  * @return	array		Meta data array. $meta['fields'] has the record data.
  */
 function indexFile($filepath, $crdate = 0, $pid = NULL, $metaCallbackFunc = NULL, $filePreprocessingCallbackFunc = NULL, $metaPreset = array())
 {
     global $TYPO3_CONF_VARS;
     $pathname = $this->getFilePath($filepath);
     $pathname = tx_dam::file_absolutePath($pathname);
     // locks the indexing for the current file
     // If the file is currently indexed this will return false
     if (!$this->lock($pathname)) {
         return FALSE;
     }
     $pid = is_null($pid) ? $this->pid : $pid;
     if ($filePreprocessingCallbackFunc) {
         call_user_func($filePreprocessingCallbackFunc, 'filePreprocessing', $pathname, $this);
         if ($this->writeDevLog) {
             t3lib_div::devLog('indexFile(): call filePreprocessingCallbackFunc', 'tx_dam_indexing', 0, $filePreprocessingCallbackFunc);
         }
     }
     // might be possible to have $pathname call by reference and change the filename - usable for copying files before indexing??? Needs to be tested.
     // Answer: Note that the parameters for call_user_func() are not passed by reference.
     $meta = $this->getFileNodeInfo($pathname, true);
     if ($metaFile = $this->getMetaFilePath($filepath)) {
         $meta['metaFile'] = $metaFile;
     }
     if ($this->skipThisFile($meta['file'])) {
         unset($meta);
         $this->log('Skipped file: ' . $pathname, 1, 0);
         if ($this->writeDevLog) {
             t3lib_div::devLog('indexFile(): file skipped: ' . $pathname, 'tx_dam_indexing');
         }
     } elseif (is_array($meta)) {
         if ($this->writeDevLog) {
             t3lib_div::devLog('indexFile() - got file node info: ' . $pathname, 'tx_dam_indexing', 0, $meta);
         }
         if ($this->writeDevLog and $metaPreset) {
             t3lib_div::devLog('indexFile: use meta preset', 'tx_dam_indexing', 0, $metaPreset);
         }
         $meta = t3lib_div::array_merge_recursive_overrule($metaPreset, $meta);
         $status = tx_dam::index_check($meta['fields'], $meta['fields']['file_hash']);
         $uid = intval($status['meta']['uid']);
         if ($uid) {
             if ($this->writeDevLog) {
                 t3lib_div::devLog('indexFile(): file already indexed (uid:' . $uid . ')', 'tx_dam_indexing', 0, $status);
             }
             $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_dam', 'uid=' . intval($uid));
             $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
             if ($this->writeDevLog) {
                 t3lib_div::devLog('indexFile(): fetch index data for reindexing', 'tx_dam_indexing', 0, $row);
             }
             // this is needed for fields like group/MM
             require_once PATH_t3lib . 'class.t3lib_transferdata.php';
             $processData = t3lib_div::makeInstance('t3lib_transferData');
             $row = $processData->renderRecordRaw('tx_dam', $row['uid'], $row['pid'], $row);
             if ($this->writeDevLog) {
                 t3lib_div::devLog('indexFile(): call t3lib_transferdata->renderRecordRaw() for data from index', 'tx_dam_indexing', 0, $row);
             }
             // index rule use 'row' for merging
             $meta['row'] = $row;
             $meta['indexExist'] = true;
             $meta['reindexed'] = $this->doReindexing;
         } else {
             $uid = 'NEW';
             $meta['indexExist'] = false;
             $meta['reindexed'] = false;
         }
         // TODO handle TXDAM_file_missing and reconnect file to index
         if ($status['__status'] == TXDAM_file_unknown or $status['__status'] > TXDAM_file_unknown and $this->doReindexing) {
             $mimeType = array();
             $mimeType['fields'] = $this->getFileMimeType($pathname);
             $meta = t3lib_div::array_merge_recursive_overrule(array('fields' => $this->getDefaultRecord()), $meta);
             $meta = t3lib_div::array_merge_recursive_overrule($meta, $mimeType);
             $meta['fields']['uid'] = $uid;
             $meta['fields']['pid'] = $pid;
             $meta['fields']['index_type'] = $this->indexRunType;
             $meta = $this->getFileMetaInfo($pathname, $meta);
             if ($meta['textExtract']) {
                 $meta['textExtract'] = $this->processTextExcerpt($meta['textExtract']);
             } else {
                 $meta['textExtract'] = $this->getFileTextExcerpt($pathname, $meta['fields']['file_type']);
             }
             $meta['fields']['search_content'] = $meta['textExtract'];
             $meta['fields']['abstract'] = $meta['fields']['abstract'] ? $meta['fields']['abstract'] : trim($meta['fields']['search_content']);
             $meta['fields']['language'] = $this->getMetaLanguage($meta);
             $meta['fields']['file_dl_name'] = $meta['fields']['file_dl_name'] ? $meta['fields']['file_dl_name'] : $meta['fields']['file_name'];
             $meta['fields']['crdate'] = $crdate ? $crdate : time();
             $meta['fields']['tstamp'] = time();
             $meta['fields']['cruser_id'] = intval($GLOBALS['BE_USER']->user['uid']);
             $meta['fields']['date_cr'] = $meta['fields']['date_cr'] ? $meta['fields']['date_cr'] : time();
             $meta['fields']['date_mod'] = $meta['fields']['date_mod'] ? $meta['fields']['date_mod'] : $meta['fields']['date_cr'];
             // TODO category handling - merging?
             # $fieldsUpdated = tx_dam_db::getUpdateData($meta['fields'], $this->replaceData, $this->appendData);
             foreach ($this->dataPreset as $field => $value) {
                 if ($value and !$meta['fields'][$field]) {
                     $meta['fields'][$field] = $value;
                 }
             }
             $fieldsUpdated = tx_dam_db::getUpdateData($meta['fields'], $this->dataPostset, $this->dataAppend);
             $meta['fields'] = array_merge($meta['fields'], $fieldsUpdated);
             $meta = $this->rulesCallback('process', $meta, $pathname);
             if ($metaCallbackFunc) {
                 $meta = call_user_func($metaCallbackFunc, 'process', $meta, $pathname, $this);
                 if ($this->writeDevLog) {
                     t3lib_div::devLog('indexFile(): call_user_func: ' . @get_class($metaCallbackFunc[0]) . '->' . $metaCallbackFunc[1] . ' (process)', 'tx_dam_indexing', 0, $meta);
                 }
             }
             $meta['failure'] = false;
             if (!$this->dryRun) {
                 if ($this->writeDevLog) {
                     t3lib_div::devLog('indexFile(): call tx_dam_db::insertUpdateData()', 'tx_dam_indexing', 0, $meta['fields']);
                 }
                 $meta['fields']['uid'] = tx_dam_db::insertUpdateData($meta['fields']);
                 if (!intval($meta['fields']['uid'])) {
                     list($error, $errorMsg) = tx_dam_db::getLastError();
                     $meta['failure'] = 'Meta record could not be inserted: ' . $errorMsg;
                     $this->log('Meta record could not be inserted: ' . $pathname . '. ' . $errorMsg, 1, 1);
                 } else {
                     if ($this->writeDevLog) {
                         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_dam', 'uid=' . intval($meta['fields']['uid']));
                     }
                     if ($this->writeDevLog) {
                         $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
                     }
                     if ($this->writeDevLog) {
                         t3lib_div::devLog('indexFile(): written to index', 'tx_dam_indexing', 0, $row);
                     }
                 }
             }
             if (intval($meta['fields']['uid'])) {
                 $meta['isIndexed'] = true;
                 $this->infoList[] = array('uid' => $meta['fields']['uid'], 'title' => $meta['fields']['title'], 'file_name' => $meta['fields']['file_name'], 'file_path' => $meta['fields']['file_path'], 'reindexed' => $meta['reindexed']);
                 $this->stat['newIndexed'] += $meta['reindexed'] ? 0 : 1;
                 $this->stat['reIndexed'] += $meta['reindexed'] ? 1 : 0;
             }
             $meta = $this->rulesCallback('post', $meta, $pathname);
             if ($metaCallbackFunc) {
                 $meta = call_user_func($metaCallbackFunc, 'post', $meta, $pathname, $this);
                 if ($this->writeDevLog) {
                     t3lib_div::devLog('indexFile(): call_user_func: ' . @get_class($metaCallbackFunc[0]) . '->' . $metaCallbackFunc[1] . ' (post)', 'tx_dam_indexing', 0, $meta);
                 }
             }
             $currentUid = intval($meta['fields']['uid']) ? $meta['fields']['uid'] : '_NO_UID_' . (string) intval($this->noIdCounter++);
             if ($this->collectMeta) {
                 $this->meta[$currentUid] = $meta;
             }
             // todo: indexing of childs to this file - eg. images from a OpenOffice file
             if (is_array($meta['childs'])) {
                 foreach ($meta['childs'] as $fileDef) {
                     $pathname = $fileDef['pathname'];
                     if (file_exists($pathname)) {
                         if ($meta['fields']['file_hash'] and $fileDef['fileStorageType'] === 'moveToInternal') {
                             $storageFolder = PATH_site . 'uploads/tx_dam/storage/' . $meta['fields']['file_hash'] . '/';
                             $targetFile = $storageFolder . tx_dam::file_basename($pathname);
                             if (!is_dir($storageFolder)) {
                                 t3lib_div::mkdir($storageFolder);
                             }
                             @unlink($targetFile);
                             rename($pathname, $targetFile);
                             $pathname = $targetFile;
                         }
                         $metaPreset = is_array($fileDef['metaPreset']) ? $fileDef['metaPreset'] : array();
                         $metaPreset['fields']['parent_id'] = $currentUid;
                         $this->indexFile($pathname, $crdate, $pid, $metaCallbackFunc, $filePreprocessingCallbackFunc, $metaPreset);
                     }
                 }
             }
             $this->statMeta($meta);
             $this->unlock();
             return $meta;
         } elseif (is_array($meta['row'])) {
             $meta['fields'] = $meta['row'];
             $this->statMeta($meta);
             $this->unlock();
             return $meta;
         }
     } else {
         if (!@is_file($pathname)) {
             $this->log('Is not a file: ' . $pathname, 1, 1);
         } elseif (!@is_readable($pathname)) {
             $this->log('Is not readable: ' . $pathname, 1, 1);
         }
     }
     $this->unlock();
     return FALSE;
 }