2010-12-21 11:54:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2011-01-11 02:35:59 +01:00
|
|
|
* Rebuilds the search indexes for the documentation pages.
|
|
|
|
*
|
|
|
|
* For the hourly cron rebuild use RebuildLuceneDocusIndex_Hourly
|
|
|
|
*
|
2012-04-08 11:36:16 +02:00
|
|
|
* @package docsviewer
|
2010-12-21 11:54:11 +01:00
|
|
|
* @subpackage tasks
|
|
|
|
*/
|
|
|
|
|
|
|
|
class RebuildLuceneDocsIndex extends BuildTask {
|
|
|
|
|
2011-01-11 02:35:59 +01:00
|
|
|
protected $title = "Rebuild Documentation Search Indexes";
|
|
|
|
|
2012-04-08 11:23:49 +02:00
|
|
|
protected $description = "
|
2012-04-08 11:36:16 +02:00
|
|
|
Rebuilds the indexes used for the search engine in the docsviewer.";
|
2011-01-11 02:35:59 +01:00
|
|
|
|
2010-12-21 11:54:11 +01:00
|
|
|
function run($request) {
|
2011-01-11 02:35:59 +01:00
|
|
|
$this->rebuildIndexes();
|
|
|
|
}
|
|
|
|
|
|
|
|
function rebuildIndexes($quiet = false) {
|
2012-04-08 11:36:16 +02:00
|
|
|
require_once(DOCSVIEWER_PATH .'/thirdparty/markdown/markdown.php');
|
2011-04-29 04:41:30 +02:00
|
|
|
require_once 'Zend/Search/Lucene.php';
|
2011-01-11 02:35:59 +01:00
|
|
|
|
2010-12-21 11:54:11 +01:00
|
|
|
ini_set("memory_limit", -1);
|
|
|
|
ini_set('max_execution_time', 0);
|
2011-01-11 02:35:59 +01:00
|
|
|
|
2010-12-21 11:54:11 +01:00
|
|
|
// only rebuild the index if we have to. Check for either flush or the time write.lock.file
|
|
|
|
// was last altered
|
|
|
|
$lock = DocumentationSearch::get_index_location() .'/write.lock.file';
|
|
|
|
$lockFileFresh = (file_exists($lock) && filemtime($lock) > (time() - (60 * 60 * 24)));
|
|
|
|
|
2011-01-11 02:35:59 +01:00
|
|
|
if($lockFileFresh && !isset($_REQUEST['flush'])) {
|
|
|
|
if(!$quiet) {
|
|
|
|
echo "Index recently rebuilt. If you want to force reindex use ?flush=1";
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-12-21 11:54:11 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$index = Zend_Search_Lucene::open(DocumentationSearch::get_index_location());
|
|
|
|
$index->removeReference();
|
|
|
|
}
|
2011-02-11 02:29:25 +01:00
|
|
|
catch (Zend_Search_Lucene_Exception $e) {
|
2011-04-29 04:41:30 +02:00
|
|
|
// user_error($e);
|
2011-02-11 02:29:25 +01:00
|
|
|
}
|
2010-12-21 11:54:11 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$index = Zend_Search_Lucene::create(DocumentationSearch::get_index_location());
|
|
|
|
}
|
|
|
|
catch(Zend_Search_Lucene_Exception $c) {
|
|
|
|
user_error($c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// includes registration
|
|
|
|
$pages = DocumentationSearch::get_all_documentation_pages();
|
2011-02-11 02:29:25 +01:00
|
|
|
|
2010-12-21 11:54:11 +01:00
|
|
|
if($pages) {
|
|
|
|
$count = 0;
|
2011-01-11 02:35:59 +01:00
|
|
|
|
|
|
|
// iconv complains about all the markdown formatting
|
|
|
|
// turn off notices while we parse
|
|
|
|
$error = error_reporting();
|
|
|
|
error_reporting('E_ALL ^ E_NOTICE');
|
|
|
|
|
2010-12-21 11:54:11 +01:00
|
|
|
foreach($pages as $page) {
|
|
|
|
$count++;
|
|
|
|
|
|
|
|
if(!is_dir($page->getPath())) {
|
|
|
|
$doc = new Zend_Search_Lucene_Document();
|
2010-12-22 09:21:49 +01:00
|
|
|
$content = $page->getMarkdown();
|
|
|
|
if($content) $content = Markdown($content);
|
|
|
|
|
2011-08-04 00:04:53 +02:00
|
|
|
$entity = ($entity = $page->getEntity()) ? $entity->getTitle() : "";
|
|
|
|
|
2010-12-22 09:21:49 +01:00
|
|
|
$doc->addField(Zend_Search_Lucene_Field::Text('content', $content));
|
2011-04-29 04:41:30 +02:00
|
|
|
$doc->addField($titleField = Zend_Search_Lucene_Field::Text('Title', $page->getTitle()));
|
|
|
|
$doc->addField($breadcrumbField = Zend_Search_Lucene_Field::Text('BreadcrumbTitle', $page->getBreadcrumbTitle()));
|
2010-12-21 11:54:11 +01:00
|
|
|
$doc->addField(Zend_Search_Lucene_Field::Keyword('Version', $page->getVersion()));
|
|
|
|
$doc->addField(Zend_Search_Lucene_Field::Keyword('Language', $page->getLang()));
|
2011-08-04 00:04:53 +02:00
|
|
|
$doc->addField(Zend_Search_Lucene_Field::Keyword('Entity', $entity));
|
2010-12-22 09:21:49 +01:00
|
|
|
$doc->addField(Zend_Search_Lucene_Field::Keyword('Link', $page->Link()));
|
2011-04-29 04:41:30 +02:00
|
|
|
|
|
|
|
// custom boosts
|
2011-08-04 00:04:53 +02:00
|
|
|
$titleField->boost = 3;
|
2011-04-29 04:41:30 +02:00
|
|
|
$breadcrumbField->boost = 1.5;
|
|
|
|
foreach(DocumentationSearch::$boost_by_path as $pathExpr => $boost) {
|
|
|
|
if(preg_match($pathExpr, $page->getRelativePath())) $doc->boost = $boost;
|
|
|
|
}
|
|
|
|
|
2010-12-21 11:54:11 +01:00
|
|
|
$index->addDocument($doc);
|
|
|
|
}
|
2011-01-11 02:35:59 +01:00
|
|
|
|
2011-04-29 04:41:30 +02:00
|
|
|
if(!$quiet) echo "adding ". $page->getPath() ."\n";
|
2010-12-21 11:54:11 +01:00
|
|
|
}
|
2011-01-11 02:35:59 +01:00
|
|
|
|
|
|
|
error_reporting($error);
|
2010-12-21 11:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$index->commit();
|
2011-01-11 02:35:59 +01:00
|
|
|
|
|
|
|
if(!$quiet) echo "complete.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-08 11:36:16 +02:00
|
|
|
* @package docsviewer
|
2011-01-11 02:35:59 +01:00
|
|
|
* @subpackage tasks
|
|
|
|
*/
|
|
|
|
class RebuildLuceneDocusIndex_Hourly extends HourlyTask {
|
|
|
|
|
|
|
|
function process() {
|
|
|
|
$reindex = new RebuildLuceneDocusIndex();
|
|
|
|
|
|
|
|
$reindex->rebuildIndexes(true);
|
2010-12-21 11:54:11 +01:00
|
|
|
}
|
|
|
|
}
|