2010-10-08 05:31:19 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2010-12-21 11:54:11 +01:00
|
|
|
* Documentation Search powered by Lucene. You will need Zend_Lucene installed on your path
|
|
|
|
* to rebuild the indexes run the {@link RebuildLuceneDocsIndex} task. You may wish to setup
|
|
|
|
* a cron job to remake the indexes on a regular basis
|
|
|
|
*
|
2010-12-21 10:42:44 +01:00
|
|
|
* @package sapphiredocs
|
2010-10-08 05:31:19 +02:00
|
|
|
*/
|
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
class DocumentationSearch {
|
|
|
|
|
|
|
|
private static $enabled = false;
|
|
|
|
|
|
|
|
private $results;
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
private $totalResults;
|
2010-10-21 22:27:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-12-21 10:42:44 +01:00
|
|
|
* Folder name for indexes (in the temp folder). You can override it using
|
|
|
|
* {@link DocumentationSearch::set_index_location($)}
|
|
|
|
*
|
|
|
|
* @var string
|
2010-10-21 22:27:23 +02:00
|
|
|
*/
|
2010-12-21 10:42:44 +01:00
|
|
|
private static $index_location = 'sapphiredocs';
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
static $allowed_actions = array(
|
|
|
|
'buildindex'
|
|
|
|
);
|
2010-10-08 05:31:19 +02:00
|
|
|
|
|
|
|
/**
|
2010-12-21 10:42:44 +01:00
|
|
|
* Generate an array of every single documentation page installed on the system.
|
2010-10-08 05:31:19 +02:00
|
|
|
*
|
2010-12-21 10:42:44 +01:00
|
|
|
* @return DataObjectSet
|
2010-10-08 05:31:19 +02:00
|
|
|
*/
|
2010-12-21 11:54:11 +01:00
|
|
|
public static function get_all_documentation_pages() {
|
2010-10-08 05:31:19 +02:00
|
|
|
DocumentationService::load_automatic_registration();
|
|
|
|
|
|
|
|
$modules = DocumentationService::get_registered_modules();
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
|
|
|
|
if($modules) {
|
|
|
|
foreach($modules as $module) {
|
2010-12-21 10:42:44 +01:00
|
|
|
|
2010-10-08 05:31:19 +02:00
|
|
|
foreach($module->getLanguages() as $language) {
|
2010-10-21 22:27:23 +02:00
|
|
|
try {
|
2010-12-21 10:42:44 +01:00
|
|
|
$pages = DocumentationService::get_pages_from_folder($module);
|
|
|
|
|
2010-10-21 22:27:23 +02:00
|
|
|
if($pages) {
|
|
|
|
foreach($pages as $page) {
|
2010-12-21 10:42:44 +01:00
|
|
|
$output->push($page);
|
2010-10-21 22:27:23 +02:00
|
|
|
}
|
2010-10-08 05:31:19 +02:00
|
|
|
}
|
|
|
|
}
|
2010-12-21 10:42:44 +01:00
|
|
|
catch(Exception $e) {
|
|
|
|
user_error($e, E_USER_WARNING);
|
|
|
|
}
|
2010-10-08 05:31:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-21 10:42:44 +01:00
|
|
|
|
2010-10-08 05:31:19 +02:00
|
|
|
return $output;
|
|
|
|
}
|
2010-12-21 10:42:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable searching documentation
|
|
|
|
*/
|
|
|
|
public static function enable() {
|
|
|
|
if(!class_exists('ZendSearchLuceneSearchable')) {
|
|
|
|
return user_error('DocumentationSearch requires the ZendSearchLucene library', E_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$enabled = true;
|
|
|
|
|
|
|
|
ZendSearchLuceneSearchable::enable(array());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function enabled() {
|
|
|
|
return self::$enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
public function set_index($index) {
|
|
|
|
self::$index_location = $index;
|
|
|
|
}
|
2010-10-21 22:27:23 +02:00
|
|
|
|
|
|
|
/**
|
2010-12-21 10:42:44 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_index_location() {
|
|
|
|
return TEMP_FOLDER . '/'. trim(self::$index_location, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a search query on the index
|
2010-10-21 22:27:23 +02:00
|
|
|
*
|
2010-12-21 10:42:44 +01:00
|
|
|
* Rebuilds the index if it out of date
|
2010-10-21 22:27:23 +02:00
|
|
|
*/
|
2010-12-21 10:42:44 +01:00
|
|
|
public function performSearch($query) {
|
|
|
|
$index = Zend_Search_Lucene::open(self::get_index_location());
|
|
|
|
|
|
|
|
Zend_Search_Lucene::setResultSetLimit(200);
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
$results = $index->find($query);
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
$this->results = new DataObjectSet();
|
|
|
|
$this->totalResults = $index->numDocs();
|
|
|
|
|
|
|
|
foreach($results as $result) {
|
|
|
|
$data = $result->getDocument();
|
|
|
|
|
|
|
|
$this->results->push(new ArrayData(array(
|
|
|
|
'Title' => DBField::create('Varchar', $data->Title),
|
2010-12-21 11:54:11 +01:00
|
|
|
'Link' => DBField::create('Varchar',$data->Link),
|
2010-12-21 10:42:44 +01:00
|
|
|
'Language' => DBField::create('Varchar',$data->Language),
|
2010-12-21 11:05:24 +01:00
|
|
|
'Version' => DBField::create('Varchar',$data->Version),
|
|
|
|
'Content' => DBField::create('Text', $data->content)
|
2010-12-21 10:42:44 +01:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
public function getResults($start) {
|
|
|
|
return $this->results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getTotalResults() {
|
|
|
|
return (int) $this->totalResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function optimizeIndex() {
|
|
|
|
$index = Zend_Search_Lucene::open(self::get_index_location());
|
|
|
|
|
|
|
|
if($index) $index->optimize();
|
2010-10-21 22:27:23 +02:00
|
|
|
}
|
2010-10-08 05:31:19 +02:00
|
|
|
}
|