/** * Takes a file reference and extracts its meta data. * * @param \TYPO3\CMS\Core\Resource\File $file * @return array */ public function extractMetaData(File $file) { $localTempFilePath = $file->getForLocalProcessing(FALSE); $query = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Tika\\Service\\SolrCellQuery', $localTempFilePath); $query->setExtractOnly(); $response = $this->solr->extract($query); $metaData = $this->solrResponseToArray($response[1]); $this->cleanupTempFile($localTempFilePath, $file); $this->log('Meta Data Extraction using Solr', array('file' => $file, 'solr connection' => (array) $this->solr, 'query' => (array) $query, 'response' => $response, 'meta data' => $metaData)); return $metaData; }
/** * Creates a single Solr Document for an item in a specific language. * * @param Tx_Solr_IndexQueue_Item An index queue item to index. * @param integer The language to use. * @return boolean TRUE if item was indexed successfully, FALSE on failure */ protected function indexItem(Tx_Solr_IndexQueue_Item $item, $language = 0) { $itemIndexed = FALSE; $documents = array(); $itemDocument = $this->itemToDocument($item, $language); if (is_null($itemDocument)) { /* * If there is no itemDocument, this means there was no translation * for this record. This should not stop the current item to count as * being valid because not-indexing not-translated items is perfectly * fine. */ return TRUE; } $documents[] = $itemDocument; $documents = array_merge($documents, $this->getAdditionalDocuments($item, $language, $itemDocument)); $documents = $this->processDocuments($item, $documents); $documents = $this->preAddModifyDocuments($item, $language, $documents); $response = $this->solr->addDocuments($documents); if ($response->getHttpStatus() == 200) { $itemIndexed = TRUE; } $this->log($item, $documents, $response); return $itemIndexed; }
/** * Adds the collected documents to the Solr index. * * @param array $documents An array of Apache_Solr_Document objects. * @return boolean TRUE if documents were added successfully, FALSE otherwise */ protected function addDocumentsToSolrIndex(array $documents) { $documentsAdded = FALSE; if (!count($documents)) { return $documentsAdded; } try { $this->log('Adding ' . count($documents) . ' documents.', 0, $documents); // chunk adds by 20 $documentChunks = array_chunk($documents, 20); foreach ($documentChunks as $documentChunk) { $response = $this->solrConnection->addDocuments($documentChunk); if ($response->getHttpStatus() != 200) { $transportException = new Apache_Solr_HttpTransportException($response); throw new RuntimeException('Solr Request failed.', 1331834983, $transportException); } } $documentsAdded = TRUE; } catch (Exception $e) { $this->log($e->getMessage() . ' Error code: ' . $e->getCode(), 2); if ($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['logging.']['exceptions']) { GeneralUtility::devLog('Exception while adding documents', 'solr', 3, array($e->__toString())); } } return $documentsAdded; }
/** * Sends a ping to the solr server to see whether it is available. * * @return boolean Returns TRUE on successful ping. * @throws Exception Throws an exception in case ping was not successful. */ public function ping() { $solrAvailable = FALSE; try { if (!$this->solr->ping()) { throw new Exception('Solr Server not responding.', 1237475791); } $solrAvailable = TRUE; } catch (Exception $e) { if ($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['logging.']['exceptions']) { GeneralUtility::devLog('exception while trying to ping the solr server', 'solr', 3, array($e->__toString())); } } return $solrAvailable; }
/** * Renders the index field table. * * Acknowledgement: Some of the code is taken from Drupal's apachesolr module * * @param $data * @return unknown_type */ protected function renderData(Apache_Solr_Response $data) { $content = ''; $registry = t3lib_div::makeInstance('t3lib_Registry'); $limit = $registry->get('tx_solr', 'luke.limit', 20000); $numberOfDocuments = $data->index->numDocs; $content .= '<p>Number of documents in index: ' . $numberOfDocuments . '</p>'; if (isset($data->index->numDocs) && $data->index->numDocs > $limit) { $notFound = '<em>Omitted</em>'; $content .= '<p>You have more than ' . $limit . ' documents, so term frequencies are being omitted for performance reasons.</p>'; } elseif (isset($data->index->numDocs)) { $notFound = 'Nothing indexed'; // below limit, so we get more data // Note: we use 2 since 1 fails on Ubuntu Hardy. $data = $this->solr->getLukeMetaData(2); $content .= '<p>Number of terms in index: ' . $data->index->numTerms . '</p>'; } $fields = (array) $data->fields; $content .= '<p>Number of fields in index: ' . count($fields) . '</p>'; // getting fields, sorting $rows = array(); foreach ($fields as $name => $field) { $rows[$name] = array($name, $field->type, isset($field->distinct) ? $field->distinct : $notFound); } ksort($rows); // Initialise table layout $tableLayout = array('table' => array('<table border="0" cellspacing="0" cellpadding="2" class="tx_solr_index_list">', '</table>'), '0' => array('tr' => array('<tr class="bgColor2" valign="top">', '</tr>'), 'defCol' => array('<td>', '</td>')), 'defRowOdd' => array('tr' => array('<tr class="bgColor3-20">', '</tr>'), 'defCol' => array('<td>', '</td>')), 'defRowEven' => array('tr' => array('<tr>', '</tr>'), 'defCol' => array('<td>', '</td>'))); $table = array(); // header row $table[] = array('Field Name', 'Index Type', 'Distinct Terms'); foreach ($rows as $row) { $table[] = $row; } // Render table $content .= $this->reportsModule->doc->table($table, $tableLayout); return $content; }
/** * Gets the version of the installed plugin. * * @param Tx_Solr_SolrService $solrConnection Solr connection to check for the plugin. * @return string The installed plugin's version number. */ public function getInstalledPluginVersion(Tx_Solr_SolrService $solrConnection) { $version = '0.0.0'; $pluginsInformation = $solrConnection->getPluginsInformation(); $rawVersion = $pluginsInformation->plugins->OTHER->{self::PLUGIN_CLASS_NAME}->version; $explodedRawVersion = explode('-', $rawVersion); $version = $explodedRawVersion[0]; return $version; }
/** * Reloads a single Solr core. * * @param \Tx_Solr_SolrService $solrServer A Solr server connection * @param string $coreName Name of the core to reload * @return bool TRUE if reloading the core was successful, FALSE otherwise */ protected function reloadCore(\Tx_Solr_SolrService $solrServer, $coreName) { $coreReloaded = FALSE; $path = $solrServer->getPath(); $pathElements = explode('/', trim($path, '/')); $coreAdminReloadUrl = $solrServer->getScheme() . '://' . $solrServer->getHost() . ':' . $solrServer->getPort() . '/' . $pathElements[0] . '/' . 'admin/cores?action=reload&core=' . $coreName; $httpTransport = $solrServer->getHttpTransport(); $httpResponse = $httpTransport->performGetRequest($coreAdminReloadUrl); $solrResponse = new \Apache_Solr_Response($httpResponse, $solrServer->getCreateDocuments(), $solrServer->getCollapseSingleValueArrays()); if ($solrResponse->getHttpStatus() == 200) { $coreReloaded = TRUE; } return $coreReloaded; }