kitodo-presentation/dlf/common/class.tx_dlf_indexing.php

642 lines
14 KiB
PHP

<?php
/***************************************************************
* Copyright notice
*
* (c) 2011 Sebastian Meyer <sebastian.meyer@slub-dresden.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* [CLASS/FUNCTION INDEX of SCRIPT]
*/
/**
* Indexing class 'tx_dlf_indexing' for the 'dlf' extension.
*
* @author Sebastian Meyer <sebastian.meyer@slub-dresden.de>
* @copyright Copyright (c) 2011, Sebastian Meyer, SLUB Dresden
* @package TYPO3
* @subpackage tx_dlf
* @access public
*/
class tx_dlf_indexing {
/**
* The extension key
*
* @var string
* @access public
*/
public static $extKey = 'dlf';
/**
* Array of metadata fields' configuration
* @see loadIndexConf()
*
* @var array
* @access protected
*/
protected static $fields = array (
'autocompleted' => array (),
'facets' => array (),
'sortables' => array (),
'indexed' => array (),
'stored' => array (),
'tokenized' => array (),
'fieldboost' => array ()
);
/**
* List of already processed documents
*
* @var array
* @access protected
*/
protected static $processedDocs = array ();
/**
* Instance of Apache_Solr_Service class
*
* @var Apache_Solr_Service
* @access protected
*/
protected static $solr;
/**
* Array of toplevel structure elements
* @see loadIndexConf()
*
* @var array
* @access protected
*/
protected static $toplevel = array ();
/**
* Insert given document into Solr index
*
* @access public
*
* @param tx_dlf_document &$doc: The document to add
* @param integer $core: UID of the Solr core to use
*
* @return integer 0 on success or 1 on failure
*/
public static function add(tx_dlf_document &$doc, $core = 0) {
if (in_array($doc->uid, self::$processedDocs)) {
return 0;
} elseif (self::solrConnect($core, $doc->pid)) {
$errors = 0;
// Handle multi-volume documents.
if ($doc->parentId) {
$parent = tx_dlf_document::getInstance($doc->parentId, 0, TRUE);
if ($parent->ready) {
$errors = self::add($parent, $core);
} else {
if (TYPO3_DLOG) {
t3lib_div::devLog('[tx_dlf_indexing->add(['.$doc->uid.'], '.$core.')] Could not load parent document with UID "'.$doc->parentId.'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
}
return 1;
}
}
try {
// Add document to list of processed documents.
self::$processedDocs[] = $doc->uid;
// Index every logical unit as separate Solr document.
foreach ($doc->tableOfContents as $logicalUnit) {
if (!$errors) {
$errors = self::process($doc, $logicalUnit);
} else {
break;
}
}
self::$solr->service->commit();
// Get document title from database.
$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_dlf_documents.title AS title',
'tx_dlf_documents',
'tx_dlf_documents.uid='.intval($doc->uid).tx_dlf_helper::whereClause('tx_dlf_documents'),
'',
'',
'1'
);
$resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
if (!defined('TYPO3_cliMode')) {
$message = t3lib_div::makeInstance(
't3lib_FlashMessage',
htmlspecialchars(sprintf($GLOBALS['LANG']->getLL('flash.documentIndexed'), $resArray['title'], $doc->uid)),
$GLOBALS['LANG']->getLL('flash.done', TRUE),
t3lib_FlashMessage::OK,
TRUE
);
t3lib_FlashMessageQueue::addMessage($message);
}
return $errors;
} catch (Exception $e) {
if (!defined('TYPO3_cliMode')) {
$message = t3lib_div::makeInstance(
't3lib_FlashMessage',
$GLOBALS['LANG']->getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()),
$GLOBALS['LANG']->getLL('flash.error', TRUE),
t3lib_FlashMessage::ERROR,
TRUE
);
t3lib_FlashMessageQueue::addMessage($message);
}
if (TYPO3_DLOG) {
t3lib_div::devLog('[tx_dlf_indexing->add(['.$doc->uid.'], '.$core.')] Apache Solr threw exception: "'.$e->getMessage().'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
}
return 1;
}
} else {
if (!defined('TYPO3_cliMode')) {
$message = t3lib_div::makeInstance(
't3lib_FlashMessage',
$GLOBALS['LANG']->getLL('flash.solrNoConnection', TRUE),
$GLOBALS['LANG']->getLL('flash.warning', TRUE),
t3lib_FlashMessage::WARNING,
TRUE
);
t3lib_FlashMessageQueue::addMessage($message);
}
if (TYPO3_DLOG) {
t3lib_div::devLog('[tx_dlf_indexing->add(['.$doc->uid.'], '.$core.')] Could not connect to Apache Solr server', $this->extKey, SYSLOG_SEVERITY_ERROR);
}
return 1;
}
}
/**
* Delete document from Solr index
*
* @access public
*
* @param integer $uid: UID of the document to delete
*
* @return integer 0 on success or 1 on failure
*/
public static function delete($uid) {
// Save parameter for logging purposes.
$_uid = $uid;
// Sanitize input.
$uid = max(intval($uid), 0);
// Get Solr core for document.
$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_dlf_solrcores.uid AS uid,tx_dlf_documents.title AS title',
'tx_dlf_solrcores,tx_dlf_documents',
'tx_dlf_solrcores.uid=tx_dlf_documents.solrcore AND tx_dlf_documents.uid='.$uid.tx_dlf_helper::whereClause('tx_dlf_solrcores'),
'',
'',
'1'
);
if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
list ($core, $title) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
// Establish Solr connection.
if (self::solrConnect($core)) {
try {
// Delete Solr document.
self::$solr->service->deleteByQuery('uid:'.$uid);
self::$solr->service->commit();
} catch (Exception $e) {
if (!defined('TYPO3_cliMode')) {
$message = t3lib_div::makeInstance(
't3lib_FlashMessage',
$GLOBALS['LANG']->getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()),
$GLOBALS['LANG']->getLL('flash.error', TRUE),
t3lib_FlashMessage::ERROR,
TRUE
);
t3lib_FlashMessageQueue::addMessage($message);
}
if (TYPO3_DLOG) {
t3lib_div::devLog('[tx_dlf_indexing->delete('.$_uid.')] Apache Solr threw exception: "'.$e->getMessage().'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
}
return 1;
}
} else {
if (!defined('TYPO3_cliMode')) {
$message = t3lib_div::makeInstance(
't3lib_FlashMessage',
$GLOBALS['LANG']->getLL('flash.solrNoConnection', TRUE),
$GLOBALS['LANG']->getLL('flash.error', TRUE),
t3lib_FlashMessage::ERROR,
TRUE
);
t3lib_FlashMessageQueue::addMessage($message);
}
if (TYPO3_DLOG) {
t3lib_div::devLog('[tx_dlf_indexing->delete('.$_uid.')] Could not connect to Apache Solr server', $this->extKey, SYSLOG_SEVERITY_ERROR);
}
return 1;
}
if (!defined('TYPO3_cliMode')) {
$message = t3lib_div::makeInstance(
't3lib_FlashMessage',
htmlspecialchars(sprintf($GLOBALS['LANG']->getLL('flash.documentDeleted'), $title, $uid)),
$GLOBALS['LANG']->getLL('flash.done', TRUE),
t3lib_FlashMessage::OK,
TRUE
);
t3lib_FlashMessageQueue::addMessage($message);
}
return 0;
} else {
if (TYPO3_DLOG) {
t3lib_div::devLog('[tx_dlf_indexing->delete('.$_uid.')] Invalid UID "'.$uid.'" for document deletion', $this->extKey, SYSLOG_SEVERITY_ERROR);
}
return 1;
}
}
/**
* Load indexing configuration
*
* @access protected
*
* @param integer $pid: The configuration page's UID
*
* @return void
*/
protected static function loadIndexConf($pid) {
// Get the list of toplevel structures.
$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_dlf_structures.index_name AS index_name',
'tx_dlf_structures',
'tx_dlf_structures.toplevel=1 AND tx_dlf_structures.pid='.intval($pid).tx_dlf_helper::whereClause('tx_dlf_structures'),
'',
'',
''
);
while ($toplevel = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
self::$toplevel[] = $toplevel['index_name'];
}
// Get the metadata indexing options.
$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_dlf_metadata.index_name AS index_name,tx_dlf_metadata.tokenized AS tokenized,tx_dlf_metadata.stored AS stored,tx_dlf_metadata.indexed AS indexed,tx_dlf_metadata.is_sortable AS is_sortable,tx_dlf_metadata.is_facet AS is_facet,tx_dlf_metadata.is_listed AS is_listed,tx_dlf_metadata.autocomplete AS autocomplete,tx_dlf_metadata.boost AS boost',
'tx_dlf_metadata',
'tx_dlf_metadata.pid='.intval($pid).tx_dlf_helper::whereClause('tx_dlf_metadata'),
'',
'',
''
);
while ($indexing = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
if ($indexing['tokenized']) {
self::$fields['tokenized'][] = $indexing['index_name'];
}
if ($indexing['stored'] || $indexing['is_listed']) {
self::$fields['stored'][] = $indexing['index_name'];
}
if ($indexing['indexed']) {
self::$fields['indexed'][] = $indexing['index_name'];
}
if ($indexing['is_sortable']) {
self::$fields['sortables'][] = $indexing['index_name'];
}
if ($indexing['is_facet']) {
self::$fields['facets'][] = $indexing['index_name'];
}
if ($indexing['autocomplete']) {
self::$fields['autocompleted'][] = $indexing['index_name'];
}
if ($indexing['boost'] > 0.0) {
self::$fields['fieldboost'][$indexing['index_name']] = floatval($indexing['boost']);
} else {
self::$fields['fieldboost'][$indexing['index_name']] = FALSE;
}
}
}
/**
* 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 (t3lib_div::testInt($logicalUnit['points'])) {
$solrDoc->setField('page', $logicalUnit['points']);
}
$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']);
foreach ($metadata as $index_name => $data) {
if (!empty($data) && substr($index_name, -8) !== '_sorting') {
$suffix = (in_array($index_name, self::$fields['tokenized']) ? 't' : 'u');
$suffix .= (in_array($index_name, self::$fields['stored']) ? 's' : 'u');
$suffix .= (in_array($index_name, self::$fields['indexed']) ? 'i' : 'u');
$solrDoc->setField($index_name.'_'.$suffix, $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'])) {
// Add autocomplete values to index.
$solrDoc->setField($index_name.'_suggest', $data);
}
}
}
try {
self::$solr->service->addDocument($solrDoc);
} catch (Exception $e) {
if (!defined('TYPO3_cliMode')) {
$message = t3lib_div::makeInstance(
't3lib_FlashMessage',
$GLOBALS['LANG']->getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()),
$GLOBALS['LANG']->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;
}
/**
* Connects to Solr server.
*
* @access protected
*
* @param integer $core: UID of the Solr core
* @param integer $pid: UID of the configuration page
*
* @return boolean TRUE on success or FALSE on failure
*/
protected static function solrConnect($core, $pid = 0) {
if (!self::$solr) {
// Connect to Solr server.
if (self::$solr = tx_dlf_solr::getInstance($core)) {
// Load indexing configuration if needed.
if ($pid) {
self::loadIndexConf($pid);
}
} else {
return FALSE;
}
}
return TRUE;
}
/**
* This is a static class, thus no instances should be created
*
* @access protected
*/
protected function __construct() {}
}
/* No xclasses for static classes!
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dlf/common/class.tx_dlf_indexing.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dlf/common/class.tx_dlf_indexing.php']);
}
*/
?>