/** * Processes a search request. * * @access public * * @param string $query: The search query * * @return tx_dlf_list The result list */ public function search($query = '') { // Perform search. $results = $this->service->search((string) $query, 0, $this->limit, $this->params); $this->numberOfHits = count($results->response->docs); $toplevel = array(); $checks = array(); // Get metadata configuration. if ($this->numberOfHits > 0) { $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_dlf_metadata.index_name AS index_name', 'tx_dlf_metadata', 'tx_dlf_metadata.is_sortable=1 AND tx_dlf_metadata.pid=' . intval($this->cPid) . tx_dlf_helper::whereClause('tx_dlf_metadata'), '', '', ''); $sorting = array(); while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { $sorting[$resArray['index_name']] = $resArray['index_name'] . '_sorting'; } } // Keep track of relevance. $i = 0; // Process results. foreach ($results->response->docs as $doc) { // Split toplevel documents from subparts. if ($doc->toplevel == 1) { // Prepare document's metadata for sorting. $docSorting = array(); foreach ($sorting as $index_name => $solr_name) { if (!empty($doc->{$solr_name})) { $docSorting[$index_name] = is_array($doc->{$solr_name}) ? $doc->{$solr_name}[0] : $doc->{$solr_name}; } } // Preserve relevance ranking. if (!empty($toplevel[$doc->uid]['s']['relevance'])) { $docSorting['relevance'] = $toplevel[$doc->uid]['s']['relevance']; } $toplevel[$doc->uid] = array('u' => $doc->uid, 's' => $docSorting, 'p' => !empty($toplevel[$doc->uid]['p']) ? $toplevel[$doc->uid]['p'] : array()); } else { $toplevel[$doc->uid]['p'][] = $doc->id; if (!in_array($doc->uid, $checks)) { $checks[] = $doc->uid; } } // Add relevance to sorting values. if (empty($toplevel[$doc->uid]['s']['relevance'])) { $toplevel[$doc->uid]['s']['relevance'] = str_pad($i, 6, '0', STR_PAD_LEFT); } $i++; } // Check if the toplevel documents have metadata. foreach ($checks as $check) { if (empty($toplevel[$check]['u'])) { // Get information for toplevel document. $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_dlf_documents.uid AS uid,tx_dlf_documents.metadata_sorting AS metadata_sorting', 'tx_dlf_documents', 'tx_dlf_documents.uid=' . intval($check) . tx_dlf_helper::whereClause('tx_dlf_documents'), '', '', '1'); // Process results. if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) { $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result); // Prepare document's metadata for sorting. $sorting = unserialize($resArray['metadata_sorting']); if (!empty($sorting['type']) && tx_dlf_helper::testInt($sorting['type'])) { $sorting['type'] = tx_dlf_helper::getIndexName($sorting['type'], 'tx_dlf_structures', $this->cPid); } if (!empty($sorting['owner']) && tx_dlf_helper::testInt($sorting['owner'])) { $sorting['owner'] = tx_dlf_helper::getIndexName($sorting['owner'], 'tx_dlf_libraries', $this->cPid); } if (!empty($sorting['collection']) && tx_dlf_helper::testInt($sorting['collection'])) { $sorting['collection'] = tx_dlf_helper::getIndexName($sorting['collection'], 'tx_dlf_collections', $this->cPid); } // Preserve relevance ranking. if (!empty($toplevel[$check]['s']['relevance'])) { $sorting['relevance'] = $toplevel[$check]['s']['relevance']; } $toplevel[$check] = array('u' => $resArray['uid'], 's' => $sorting, 'p' => $toplevel[$check]['p']); } else { // Clear entry if there is no (accessible) toplevel document. unset($toplevel[$check]); } } } // Save list of documents. $list = t3lib_div::makeInstance('tx_dlf_list'); $list->reset(); $list->add(array_values($toplevel)); // Set metadata for search. $list->metadata = array('label' => '', 'description' => '', 'options' => array('source' => 'search', 'select' => $query, 'userid' => 0, 'params' => $this->params, 'core' => $this->core, 'pid' => $this->cPid, 'order' => 'relevance', 'order.asc' => TRUE)); return $list; }
/** * Builds a collection's list * * @access protected * * @param integer $id: The collection's UID * * @return void */ protected function showSingleCollection($id) { // Should user-defined collections be shown? if (empty($this->conf['show_userdefined'])) { $additionalWhere = ' AND tx_dlf_collections.fe_cruser_id=0'; } elseif ($this->conf['show_userdefined'] > 0) { $additionalWhere = ' AND NOT tx_dlf_collections.fe_cruser_id=0'; } // Get all documents in collection. $result = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query('tx_dlf_collections.index_name AS index_name,tx_dlf_collections.label AS collLabel,tx_dlf_collections.description AS collDesc,tx_dlf_collections.thumbnail AS collThumb,tx_dlf_collections.fe_cruser_id AS userid,tx_dlf_documents.uid AS uid,tx_dlf_documents.metadata_sorting AS metadata_sorting,tx_dlf_documents.volume_sorting AS volume_sorting,tx_dlf_documents.partof AS partof', 'tx_dlf_documents', 'tx_dlf_relations', 'tx_dlf_collections', 'AND tx_dlf_collections.uid=' . intval($id) . ' AND tx_dlf_collections.pid=' . intval($this->conf['pages']) . ' AND tx_dlf_relations.ident=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('docs_colls', 'tx_dlf_relations') . $additionalWhere . tx_dlf_helper::whereClause('tx_dlf_documents') . tx_dlf_helper::whereClause('tx_dlf_collections'), '', 'tx_dlf_documents.title_sorting ASC', ''); $toplevel = array(); $subparts = array(); $listMetadata = array(); // Process results. while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { if (empty($listMetadata)) { $listMetadata = array('label' => htmlspecialchars($resArray['collLabel']), 'description' => $this->pi_RTEcssText($resArray['collDesc']), 'thumbnail' => htmlspecialchars($resArray['collThumb']), 'options' => array('source' => 'collection', 'select' => $id, 'userid' => $resArray['userid'], 'params' => array('fq' => array('collection_faceting:"' . $resArray['index_name'] . '"')), 'core' => '', 'pid' => $this->conf['pages'], 'order' => 'title', 'order.asc' => TRUE)); } // Split toplevel documents from volumes. if ($resArray['partof'] == 0) { // Prepare document's metadata for sorting. $sorting = unserialize($resArray['metadata_sorting']); if (!empty($sorting['type']) && tx_dlf_helper::testInt($sorting['type'])) { $sorting['type'] = tx_dlf_helper::getIndexName($sorting['type'], 'tx_dlf_structures', $this->conf['pages']); } if (!empty($sorting['owner']) && tx_dlf_helper::testInt($sorting['owner'])) { $sorting['owner'] = tx_dlf_helper::getIndexName($sorting['owner'], 'tx_dlf_libraries', $this->conf['pages']); } if (!empty($sorting['collection']) && tx_dlf_helper::testInt($sorting['collection'])) { $sorting['collection'] = tx_dlf_helper::getIndexName($sorting['collection'], 'tx_dlf_collections', $this->conf['pages']); } $toplevel[$resArray['uid']] = array('u' => $resArray['uid'], 's' => $sorting, 'p' => array()); } else { $subparts[$resArray['partof']][$resArray['volume_sorting']] = $resArray['uid']; } } // Add volumes to the corresponding toplevel documents. foreach ($subparts as $partof => $parts) { if (!empty($toplevel[$partof])) { ksort($parts); $toplevel[$partof]['p'] = array_values($parts); } } // Save list of documents. $list = t3lib_div::makeInstance('tx_dlf_list'); $list->reset(); $list->add(array_values($toplevel)); $list->metadata = $listMetadata; $list->save(); // Clean output buffer. t3lib_div::cleanOutputBuffers(); // Send headers. header('Location: ' . t3lib_div::locationHeaderUrl($this->cObj->typoLink_URL(array('parameter' => $this->conf['targetPid'])))); // Flush output buffer and end script processing. ob_end_flush(); exit; }
/** * This returns the document's thumbnail location * * @access protected * * @param boolean $forceReload: Force reloading the thumbnail instead of returning the cached value * * @return string The document's thumbnail location */ protected function _getThumbnail($forceReload = FALSE) { if (!$this->thumbnailLoaded || $forceReload) { // Retain current PID. $cPid = $this->cPid ? $this->cPid : $this->pid; if (!$cPid) { if (TYPO3_DLOG) { t3lib_div::devLog('[tx_dlf_document->_getThumbnail()] Invalid PID "' . $cPid . '" for structure definitions', self::$extKey, SYSLOG_SEVERITY_ERROR); } $this->thumbnailLoaded = TRUE; return $this->thumbnail; } // Load extension configuration. $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]); if (empty($extConf['fileGrpThumbs'])) { if (TYPO3_DLOG) { t3lib_div::devLog('[tx_dlf_document->_getThumbnail()] No fileGrp for thumbnails specified', self::$extKey, SYSLOG_SEVERITY_WARNING); } $this->thumbnailLoaded = TRUE; return $this->thumbnail; } $strctId = $this->_getToplevelId(); $metadata = $this->getTitledata($cPid); // Get structure element to get thumbnail from. $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_dlf_structures.thumbnail AS thumbnail', 'tx_dlf_structures', 'tx_dlf_structures.pid=' . intval($cPid) . ' AND tx_dlf_structures.index_name=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($metadata['type'][0], 'tx_dlf_structures') . tx_dlf_helper::whereClause('tx_dlf_structures'), '', '', '1'); if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) { $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result); // Get desired thumbnail structure if not the toplevel structure itself. if (!empty($resArray['thumbnail'])) { $strctType = tx_dlf_helper::getIndexName($resArray['thumbnail'], 'tx_dlf_structures', $cPid); // Check if this document has a structure element of the desired type. $strctIds = $this->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@TYPE="' . $strctType . '"]/@ID'); if (!empty($strctIds)) { $strctId = (string) $strctIds[0]; } } // Load smLinks. $this->_getSmLinks(); // Get thumbnail location. if ($this->_getPhysicalPages() && !empty($this->smLinks['l2p'][$strctId])) { $this->thumbnail = $this->getFileLocation($this->physicalPagesInfo[$this->smLinks['l2p'][$strctId][0]]['files'][$extConf['fileGrpThumbs']]); } else { $this->thumbnail = $this->getFileLocation($this->physicalPagesInfo[$this->physicalPages[1]]['files'][$extConf['fileGrpThumbs']]); } } elseif (TYPO3_DLOG) { t3lib_div::devLog('[tx_dlf_document->_getThumbnail()] No structure of type "' . $metadata['type'][0] . '" found in database', self::$extKey, SYSLOG_SEVERITY_ERROR); } $this->thumbnailLoaded = TRUE; } return $this->thumbnail; }
/** * The main method of the PlugIn * * @access public * * @param string $content: The PlugIn content * @param array $conf: The PlugIn configuration * * @return string The content that is displayed on the website */ public function main($content, $conf) { $this->init($conf); // Disable caching for this plugin. $this->setCache(FALSE); // Quit without doing anything if required variables are not set. if (empty($this->conf['solrcore'])) { if (TYPO3_DLOG) { \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_search->main(' . $content . ', [data])] Incomplete plugin configuration', $this->extKey, SYSLOG_SEVERITY_WARNING, $conf); } return $content; } if (!isset($this->piVars['query']) && empty($this->piVars['extQuery'])) { // Add javascript for search suggestions if enabled and jQuery autocompletion is available. if (!empty($this->conf['suggest'])) { $this->addAutocompleteJS(); } // Load template file. if (!empty($this->conf['templateFile'])) { $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATE###'); } else { $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/plugins/search/template.tmpl'), '###TEMPLATE###'); } // Configure @action URL for form. $linkConf = array('parameter' => $GLOBALS['TSFE']->id, 'forceAbsoluteUrl' => 1); // Fill markers. $markerArray = array('###ACTION_URL###' => $this->cObj->typoLink_URL($linkConf), '###LABEL_QUERY###' => $this->pi_getLL('label.query'), '###LABEL_SUBMIT###' => $this->pi_getLL('label.submit'), '###FIELD_QUERY###' => $this->prefixId . '[query]', '###QUERY###' => '', '###FULLTEXTSWITCH###' => $this->addFulltextSwitch(), '###FIELD_DOC###' => $this->conf['searchIn'] == 'document' || $this->conf['searchIn'] == 'all' ? $this->addCurrentDocument() : '', '###FIELD_COLL###' => $this->conf['searchIn'] == 'collection' || $this->conf['searchIn'] == 'all' ? $this->addCurrentCollection() : '', '###ADDITIONAL_INPUTS###' => $this->addEncryptedCoreName(), '###FACETS_MENU###' => $this->addFacetsMenu()); // Get additional fields for extended search. $extendedSearch = $this->addExtendedSearch(); // Display search form. $content .= $this->cObj->substituteSubpart($this->cObj->substituteMarkerArray($this->template, $markerArray), '###EXT_SEARCH_ENTRY###', $extendedSearch); return $this->pi_wrapInBaseClass($content); } else { // Instantiate search object. $solr = tx_dlf_solr::getInstance($this->conf['solrcore']); if (!$solr->ready) { if (TYPO3_DLOG) { \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_search->main(' . $content . ', [data])] Apache Solr not available', $this->extKey, SYSLOG_SEVERITY_ERROR, $conf); } return $content; } // Build label for result list. $label = $this->pi_getLL('search', '', TRUE); if (!empty($this->piVars['query'])) { $label .= htmlspecialchars(sprintf($this->pi_getLL('for', ''), $this->piVars['query'])); } // Prepare query parameters. $params = array(); // Set search query. if (!empty($this->conf['fulltext']) && !empty($this->piVars['fulltext'])) { // Search in fulltext field if applicable. $query = 'fulltext:(' . tx_dlf_solr::escapeQuery($this->piVars['query']) . ')'; // Add highlighting for fulltext. $params['hl'] = 'true'; $params['hl.fl'] = 'fulltext'; } else { // Retain given search field if valid. $query = tx_dlf_solr::escapeQueryKeepField($this->piVars['query'], $this->conf['pages']); } // Add extended search query. if (!empty($this->piVars['extQuery']) && is_array($this->piVars['extQuery'])) { $allowedOperators = array('AND', 'OR', 'NOT'); $allowedFields = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->conf['extendedFields'], TRUE); for ($i = 0; $i < count($this->piVars['extQuery']); $i++) { if (!empty($this->piVars['extQuery'][$i])) { if (in_array($this->piVars['extOperator'][$i], $allowedOperators) && in_array($this->piVars['extField'][$i], $allowedFields)) { if (!empty($query)) { $query .= ' ' . $this->piVars['extOperator'][$i] . ' '; } $query .= tx_dlf_indexing::getIndexFieldName($this->piVars['extField'][$i], $this->conf['pages']) . ':(' . tx_dlf_solr::escapeQuery($this->piVars['extQuery'][$i]) . ')'; } } } } // Add filter query for faceting. if (!empty($this->piVars['fq'])) { $params['fq'] = $this->piVars['fq']; } // Add filter query for in-document searching. if ($this->conf['searchIn'] == 'document' || $this->conf['searchIn'] == 'all') { if (!empty($this->piVars['id']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->piVars['id'])) { $params['fq'][] = 'uid:(' . $this->piVars['id'] . ') OR partof:(' . $this->piVars['id'] . ')'; $label .= htmlspecialchars(sprintf($this->pi_getLL('in', ''), tx_dlf_document::getTitle($this->piVars['id']))); } } // Add filter query for in-collection searching. if ($this->conf['searchIn'] == 'collection' || $this->conf['searchIn'] == 'all') { if (!empty($this->piVars['collection']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->piVars['collection'])) { $index_name = tx_dlf_helper::getIndexName($this->piVars['collection'], 'tx_dlf_collections', $this->conf['pages']); $params['fq'][] = 'collection_faceting:("' . tx_dlf_solr::escapeQuery($index_name) . '")'; $label .= sprintf($this->pi_getLL('in', '', TRUE), tx_dlf_helper::translate($index_name, 'tx_dlf_collections', $this->conf['pages'])); } } // Add filter query for collection restrictions. if ($this->conf['collections']) { $collIds = explode(',', $this->conf['collections']); $collIndexNames = array(); foreach ($collIds as $collId) { $collIndexNames[] = tx_dlf_solr::escapeQuery(tx_dlf_helper::getIndexName(intval($collId), 'tx_dlf_collections', $this->conf['pages'])); } // Last value is fake and used for distinction in $this->addCurrentCollection() $params['fq'][] = 'collection_faceting:("' . implode('" OR "', $collIndexNames) . '" OR "FakeValueForDistinction")'; } // Set search parameters. $solr->limit = max(intval($this->conf['limit']), 1); $solr->cPid = $this->conf['pages']; $solr->params = $params; // Perform search. $results = $solr->search($query); $results->metadata = array('label' => $label, 'description' => '<p class="tx-dlf-search-numHits">' . htmlspecialchars(sprintf($this->pi_getLL('hits', ''), $solr->numberOfHits, count($results))) . '</p>', 'thumbnail' => '', 'options' => $results->metadata['options']); $results->save(); // Clean output buffer. \TYPO3\CMS\Core\Utility\GeneralUtility::cleanOutputBuffers(); // Keep some plugin variables. $linkConf['parameter'] = $this->conf['targetPid']; if (!empty($this->piVars['order'])) { $linkConf['additionalParams'] = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, array('order' => $this->piVars['order'], 'asc' => !empty($this->piVars['asc']) ? '1' : '0'), '', TRUE, FALSE); } // Send headers. header('Location: ' . \TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl($this->cObj->typoLink_URL($linkConf))); // Flush output buffer and end script processing. ob_end_flush(); exit; } }
/** * This returns the full record of any list element * * @access protected * * @param mixed $element: The list element * * @return mixed The element's full record */ protected function getRecord($element) { if (is_array($element) && array_keys($element) == array('u', 'h', 's', 'p')) { // Return already processed record if possible. if (!empty($this->records[$element['u']])) { return $this->records[$element['u']]; } $record = array('uid' => $element['u'], 'page' => 1, 'preview' => '', 'subparts' => $element['p']); // Check if it's a list of database records or Solr documents. if (!empty($this->metadata['options']['source']) && $this->metadata['options']['source'] == 'collection') { // Get document's thumbnail and metadata from database. $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_dlf_documents.uid AS uid,tx_dlf_documents.thumbnail AS thumbnail,tx_dlf_documents.metadata AS metadata', 'tx_dlf_documents', '(tx_dlf_documents.uid=' . intval($record['uid']) . ' OR tx_dlf_documents.partof=' . intval($record['uid']) . ')' . tx_dlf_helper::whereClause('tx_dlf_documents'), '', '', ''); // Process results. while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { // Prepare document's metadata. $metadata = unserialize($resArray['metadata']); if (!empty($metadata['type'][0]) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($metadata['type'][0])) { $metadata['type'][0] = tx_dlf_helper::getIndexName($metadata['type'][0], 'tx_dlf_structures', $this->metadata['options']['pid']); } if (!empty($metadata['owner'][0]) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($metadata['owner'][0])) { $metadata['owner'][0] = tx_dlf_helper::getIndexName($metadata['owner'][0], 'tx_dlf_libraries', $this->metadata['options']['pid']); } if (!empty($metadata['collection']) && is_array($metadata['collection'])) { foreach ($metadata['collection'] as $i => $collection) { if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($collection)) { $metadata['collection'][$i] = tx_dlf_helper::getIndexName($metadata['collection'][$i], 'tx_dlf_collections', $this->metadata['options']['pid']); } } } // Add metadata to list element. if ($resArray['uid'] == $record['uid']) { $record['thumbnail'] = $resArray['thumbnail']; $record['metadata'] = $metadata; } elseif (($key = tx_dlf_helper::array_search_recursive($resArray['uid'], $record['subparts'], TRUE)) !== FALSE) { $record['subparts'][$key] = array('uid' => $resArray['uid'], 'page' => 1, 'preview' => !empty($record['subparts'][$key]['h']) ? $record['subparts'][$key]['h'] : '', 'thumbnail' => $resArray['thumbnail'], 'metadata' => $metadata); } } } elseif (!empty($this->metadata['options']['source']) && $this->metadata['options']['source'] == 'search') { if ($this->solrConnect()) { // Get document's thumbnail and metadata from Solr index. $result = $this->solr->service->search('uid:' . tx_dlf_solr::escapeQuery($record['uid']), 0, $this->solr->limit); // Process results. foreach ($result->response->docs as $resArray) { // Prepare document's metadata. $metadata = array(); foreach ($this->solrConfig as $index_name => $solr_name) { if (!empty($resArray->{$solr_name})) { $metadata[$index_name] = is_array($resArray->{$solr_name}) ? $resArray->{$solr_name} : array($resArray->{$solr_name}); } } // Add metadata to list elements. if ($resArray->toplevel) { $record['thumbnail'] = $resArray->thumbnail; $record['metadata'] = $metadata; } elseif (($key = tx_dlf_helper::array_search_recursive($resArray->id, $record['subparts'], TRUE)) !== FALSE) { $record['subparts'][$key] = array('uid' => $resArray->uid, 'page' => $resArray->page, 'preview' => !empty($record['subparts'][$key]['h']) ? $record['subparts'][$key]['h'] : '', 'thumbnail' => $resArray->thumbnail, 'metadata' => $metadata); } } } } // Save record for later usage. $this->records[$element['u']] = $record; } else { if (TYPO3_DLOG) { \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_list->getRecord([data])] No UID of list element to fetch full record', $this->extKey, SYSLOG_SEVERITY_NOTICE, $element); } $record = $element; } return $record; }