MINOR: moved building indexes out to its own tasks.

This commit is contained in:
Will Rossiter 2010-12-21 10:54:11 +00:00
parent b51421d964
commit fede2626fc
6 changed files with 125 additions and 99 deletions

View File

@ -68,42 +68,24 @@ class DocumentationPage extends ViewableData {
*
* @throws InvalidArgumentException
*
* @param bool $defaultFile - If this is a folder and this is set to true then getPath
* will return the path of the first file in the folder
* @return string
*/
function getPath() {
function getPath($defaultFile = false) {
if($this->fullPath) {
return $this->fullPath;
$path = $this->fullPath;
}
elseif($this->entity) {
$path = realpath(rtrim($this->entity->getPath($this->version, $this->lang), '/') . '/' . trim($this->getRelativePath(), '/'));
if(!file_exists($path)) {
throw new InvalidArgumentException(sprintf(
'Path could not be found. Module path: %s, file path: %s',
$this->entity->getPath(),
$this->relativePath
));
}
}
else {
$path = $this->relativePath;
}
return $path;
}
/**
* Absolute path including version and lang to the file to read
* off the file system. In the case of a folder this is the index.md file
*
* @return string
*/
function getFilePath() {
$path = $this->getPath();
if(!is_dir($path)) return $path;
if($entity = $this->getEntity()) {
if($defaultFile && ($entity = $this->getEntity())) {
if($relative = $this->getRelativePath()) {
return DocumentationService::find_page($entity, explode($relative, '/'));
}
@ -113,8 +95,27 @@ class DocumentationPage extends ViewableData {
return DocumentationService::find_page($entity, explode($parts, '/'));
}
}
return rtrim($path, '/') . '/index.md';
if(!file_exists($path)) {
throw new InvalidArgumentException(sprintf(
'Path could not be found. Module path: %s, file path: %s',
$this->entity->getPath(),
$this->relativePath
));
}
return $path;
}
/**
* Returns the link for the webbrowser
*
* @return string
*/
function getLink() {
$web = Director::absoluteBaseUrl() . DocumentationViewer::get_link_base();
return (str_replace(BASE_PATH, $web , $this->getPath(true)));
}
function setFullPath($path) {
@ -145,14 +146,23 @@ class DocumentationPage extends ViewableData {
return $this->title;
}
/**
* @return bool
*/
function isFolder() {
return (is_dir($this->getPath()));
}
/**
* @return String
*/
function getMarkdown() {
try {
$path = $this->getFilePath();
return file_get_contents($path);
$path = $this->getPath(true);
if($path) {
return file_get_contents($path);
}
}
catch(InvalidArgumentException $e) {}
@ -165,6 +175,6 @@ class DocumentationPage extends ViewableData {
*/
function getHTML($baselink = null) {
// if this is not a directory then we can to parse the file
return DocumentationParser::parse($this->getFilePath(), $baselink);
return DocumentationParser::parse($this->getPath(true), $baselink);
}
}

View File

@ -1,6 +1,10 @@
<?php
/**
* 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
*
* @package sapphiredocs
*/
@ -30,7 +34,7 @@ class DocumentationSearch {
*
* @return DataObjectSet
*/
static function get_all_documentation_pages() {
public static function get_all_documentation_pages() {
DocumentationService::load_automatic_registration();
$modules = DocumentationService::get_registered_modules();
@ -99,7 +103,6 @@ class DocumentationSearch {
* Rebuilds the index if it out of date
*/
public function performSearch($query) {
$this->buildindex();
$index = Zend_Search_Lucene::open(self::get_index_location());
Zend_Search_Lucene::setResultSetLimit(200);
@ -114,7 +117,7 @@ class DocumentationSearch {
$this->results->push(new ArrayData(array(
'Title' => DBField::create('Varchar', $data->Title),
'Link' => DBField::create('Varchar',$data->Path),
'Link' => DBField::create('Varchar',$data->Link),
'Language' => DBField::create('Varchar',$data->Language),
'Version' => DBField::create('Varchar',$data->Version),
'Content' => DBField::create('Text', $data->content)
@ -135,65 +138,6 @@ class DocumentationSearch {
public function getTotalResults() {
return (int) $this->totalResults;
}
/**
* Builds the document index
*/
public function buildIndex() {
ini_set("memory_limit", -1);
ini_set('max_execution_time', 0);
// only rebuild the index if we have to. Check for either flush or the time write.lock.file
// was last altered
$lock = self::get_index_location() .'/write.lock.file';
$lockFileFresh = (file_exists($lock) && filemtime($lock) > (time() - (60 * 60 * 24)));
if($lockFileFresh && !isset($_REQUEST['flush'])) return true;
try {
$index = Zend_Search_Lucene::open(self::get_index_location());
$index->removeReference();
}
catch (Zend_Search_Lucene_Exception $e) {
}
try {
$index = Zend_Search_Lucene::create(self::get_index_location());
}
catch(Zend_Search_Lucene_Exception $c) {
user_error($c);
}
// includes registration
$pages = self::get_all_documentation_pages();
if($pages) {
$count = 0;
foreach($pages as $page) {
$count++;
// iconv complains about all the markdown formatting
// turn off notices while we parse
$error = error_reporting();
error_reporting('E_ALL ^ E_NOTICE');
if(!is_dir($page->getPath())) {
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Text('content', $page->getMarkdown()));
$doc->addField(Zend_Search_Lucene_Field::Text('Title', $page->getTitle()));
$doc->addField(Zend_Search_Lucene_Field::Keyword('Version', $page->getVersion()));
$doc->addField(Zend_Search_Lucene_Field::Keyword('Language', $page->getLang()));
$doc->addField(Zend_Search_Lucene_Field::Keyword('Path', $page->getPath()));
$index->addDocument($doc);
}
error_reporting($error);
}
}
$index->commit();
}
public function optimizeIndex() {
$index = Zend_Search_Lucene::open(self::get_index_location());

View File

@ -508,7 +508,8 @@ class DocumentationService {
$page = new DocumentationPage();
$page->setTitle(self::clean_page_name($file));
$page->setFullPath($path);
$page->setFullPath($path);
$page->Filename = self::trim_extension_off($file);
if($module instanceof DocumentationEntity) {

View File

@ -389,7 +389,6 @@ class DocumentationViewer extends Controller {
$page->Link = $this->Link($linkParts);
$page->LinkingMode = 'link';
$page->Children = $this->_getModulePagesNested($page);
}
}
@ -416,10 +415,10 @@ class DocumentationViewer extends Controller {
// its either in this section or is the actual link
$page->LinkingMode = (isset($this->Remaining[$level + 1])) ? 'section' : 'current';
if(is_dir($page->Path)) {
$children = DocumentationService::get_pages_from_folder($page->Path, false);
if(is_dir($page->getPath())) {
$children = DocumentationService::get_pages_from_folder($page->getPath(), false);
$segments = array();
for($x = 0; $x <= $level; $x++) {
$segments[] = $this->Remaining[$x];

View File

@ -0,0 +1,72 @@
<?php
/**
* @package sapphiredocs
* @subpackage tasks
*/
class RebuildLuceneDocsIndex extends BuildTask {
/**
* Builds the document index
*
* Perhaps we run this via a hourly / daily task rather than
* based on the user. It's a
*/
function run($request) {
ini_set("memory_limit", -1);
ini_set('max_execution_time', 0);
// 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)));
if($lockFileFresh && !isset($_REQUEST['flush'])) return true;
try {
$index = Zend_Search_Lucene::open(DocumentationSearch::get_index_location());
$index->removeReference();
}
catch (Zend_Search_Lucene_Exception $e) {
}
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();
if($pages) {
$count = 0;
foreach($pages as $page) {
$count++;
// iconv complains about all the markdown formatting
// turn off notices while we parse
$error = error_reporting();
error_reporting('E_ALL ^ E_NOTICE');
if(!is_dir($page->getPath())) {
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Text('content', $page->getMarkdown()));
$doc->addField(Zend_Search_Lucene_Field::Text('Title', $page->getTitle()));
$doc->addField(Zend_Search_Lucene_Field::Keyword('Version', $page->getVersion()));
$doc->addField(Zend_Search_Lucene_Field::Keyword('Language', $page->getLang()));
$doc->addField(Zend_Search_Lucene_Field::Keyword('Link', $page->getLink()));
$index->addDocument($doc);
}
error_reporting($error);
}
}
$index->commit();
}
}

View File

@ -144,7 +144,7 @@ class DocumentationViewerTests extends FunctionalTest {
$this->assertFalse($child1->Children);
$child2 = $pagesArr[2];
$this->assertType('DataObjectSet', $child2->Children);
$this->assertEquals(