mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 09:05:56 +00:00
MINOR: moved building indexes out to its own tasks.
This commit is contained in:
parent
b51421d964
commit
fede2626fc
@ -68,42 +68,24 @@ class DocumentationPage extends ViewableData {
|
|||||||
*
|
*
|
||||||
* @throws InvalidArgumentException
|
* @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
|
* @return string
|
||||||
*/
|
*/
|
||||||
function getPath() {
|
function getPath($defaultFile = false) {
|
||||||
if($this->fullPath) {
|
if($this->fullPath) {
|
||||||
return $this->fullPath;
|
$path = $this->fullPath;
|
||||||
}
|
}
|
||||||
elseif($this->entity) {
|
elseif($this->entity) {
|
||||||
$path = realpath(rtrim($this->entity->getPath($this->version, $this->lang), '/') . '/' . trim($this->getRelativePath(), '/'));
|
$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 {
|
else {
|
||||||
$path = $this->relativePath;
|
$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(!is_dir($path)) return $path;
|
||||||
|
|
||||||
if($entity = $this->getEntity()) {
|
if($defaultFile && ($entity = $this->getEntity())) {
|
||||||
if($relative = $this->getRelativePath()) {
|
if($relative = $this->getRelativePath()) {
|
||||||
return DocumentationService::find_page($entity, explode($relative, '/'));
|
return DocumentationService::find_page($entity, explode($relative, '/'));
|
||||||
}
|
}
|
||||||
@ -114,7 +96,26 @@ class DocumentationPage extends ViewableData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
function setFullPath($path) {
|
||||||
@ -145,15 +146,24 @@ class DocumentationPage extends ViewableData {
|
|||||||
return $this->title;
|
return $this->title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function isFolder() {
|
||||||
|
return (is_dir($this->getPath()));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
function getMarkdown() {
|
function getMarkdown() {
|
||||||
try {
|
try {
|
||||||
$path = $this->getFilePath();
|
$path = $this->getPath(true);
|
||||||
|
|
||||||
|
if($path) {
|
||||||
return file_get_contents($path);
|
return file_get_contents($path);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch(InvalidArgumentException $e) {}
|
catch(InvalidArgumentException $e) {}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@ -165,6 +175,6 @@ class DocumentationPage extends ViewableData {
|
|||||||
*/
|
*/
|
||||||
function getHTML($baselink = null) {
|
function getHTML($baselink = null) {
|
||||||
// if this is not a directory then we can to parse the file
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,10 @@
|
|||||||
<?php
|
<?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
|
* @package sapphiredocs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -30,7 +34,7 @@ class DocumentationSearch {
|
|||||||
*
|
*
|
||||||
* @return DataObjectSet
|
* @return DataObjectSet
|
||||||
*/
|
*/
|
||||||
static function get_all_documentation_pages() {
|
public static function get_all_documentation_pages() {
|
||||||
DocumentationService::load_automatic_registration();
|
DocumentationService::load_automatic_registration();
|
||||||
|
|
||||||
$modules = DocumentationService::get_registered_modules();
|
$modules = DocumentationService::get_registered_modules();
|
||||||
@ -99,7 +103,6 @@ class DocumentationSearch {
|
|||||||
* Rebuilds the index if it out of date
|
* Rebuilds the index if it out of date
|
||||||
*/
|
*/
|
||||||
public function performSearch($query) {
|
public function performSearch($query) {
|
||||||
$this->buildindex();
|
|
||||||
$index = Zend_Search_Lucene::open(self::get_index_location());
|
$index = Zend_Search_Lucene::open(self::get_index_location());
|
||||||
|
|
||||||
Zend_Search_Lucene::setResultSetLimit(200);
|
Zend_Search_Lucene::setResultSetLimit(200);
|
||||||
@ -114,7 +117,7 @@ class DocumentationSearch {
|
|||||||
|
|
||||||
$this->results->push(new ArrayData(array(
|
$this->results->push(new ArrayData(array(
|
||||||
'Title' => DBField::create('Varchar', $data->Title),
|
'Title' => DBField::create('Varchar', $data->Title),
|
||||||
'Link' => DBField::create('Varchar',$data->Path),
|
'Link' => DBField::create('Varchar',$data->Link),
|
||||||
'Language' => DBField::create('Varchar',$data->Language),
|
'Language' => DBField::create('Varchar',$data->Language),
|
||||||
'Version' => DBField::create('Varchar',$data->Version),
|
'Version' => DBField::create('Varchar',$data->Version),
|
||||||
'Content' => DBField::create('Text', $data->content)
|
'Content' => DBField::create('Text', $data->content)
|
||||||
@ -136,65 +139,6 @@ class DocumentationSearch {
|
|||||||
return (int) $this->totalResults;
|
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() {
|
public function optimizeIndex() {
|
||||||
$index = Zend_Search_Lucene::open(self::get_index_location());
|
$index = Zend_Search_Lucene::open(self::get_index_location());
|
||||||
|
|
||||||
|
@ -509,6 +509,7 @@ class DocumentationService {
|
|||||||
$page = new DocumentationPage();
|
$page = new DocumentationPage();
|
||||||
$page->setTitle(self::clean_page_name($file));
|
$page->setTitle(self::clean_page_name($file));
|
||||||
$page->setFullPath($path);
|
$page->setFullPath($path);
|
||||||
|
|
||||||
$page->Filename = self::trim_extension_off($file);
|
$page->Filename = self::trim_extension_off($file);
|
||||||
|
|
||||||
if($module instanceof DocumentationEntity) {
|
if($module instanceof DocumentationEntity) {
|
||||||
|
@ -389,7 +389,6 @@ class DocumentationViewer extends Controller {
|
|||||||
|
|
||||||
$page->Link = $this->Link($linkParts);
|
$page->Link = $this->Link($linkParts);
|
||||||
$page->LinkingMode = 'link';
|
$page->LinkingMode = 'link';
|
||||||
|
|
||||||
$page->Children = $this->_getModulePagesNested($page);
|
$page->Children = $this->_getModulePagesNested($page);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -417,8 +416,8 @@ class DocumentationViewer extends Controller {
|
|||||||
// its either in this section or is the actual link
|
// its either in this section or is the actual link
|
||||||
$page->LinkingMode = (isset($this->Remaining[$level + 1])) ? 'section' : 'current';
|
$page->LinkingMode = (isset($this->Remaining[$level + 1])) ? 'section' : 'current';
|
||||||
|
|
||||||
if(is_dir($page->Path)) {
|
if(is_dir($page->getPath())) {
|
||||||
$children = DocumentationService::get_pages_from_folder($page->Path, false);
|
$children = DocumentationService::get_pages_from_folder($page->getPath(), false);
|
||||||
|
|
||||||
$segments = array();
|
$segments = array();
|
||||||
for($x = 0; $x <= $level; $x++) {
|
for($x = 0; $x <= $level; $x++) {
|
||||||
|
72
code/tasks/RebuildLuceneDocsIndex.php
Normal file
72
code/tasks/RebuildLuceneDocsIndex.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user