diff --git a/code/DocumentationPage.php b/code/DocumentationPage.php index 284986f..8d612d9 100644 --- a/code/DocumentationPage.php +++ b/code/DocumentationPage.php @@ -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); } } \ No newline at end of file diff --git a/code/DocumentationSearch.php b/code/DocumentationSearch.php index b793142..176dbdd 100644 --- a/code/DocumentationSearch.php +++ b/code/DocumentationSearch.php @@ -1,6 +1,10 @@ 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()); diff --git a/code/DocumentationService.php b/code/DocumentationService.php index 4cc0ec0..ae2709e 100644 --- a/code/DocumentationService.php +++ b/code/DocumentationService.php @@ -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) { diff --git a/code/DocumentationViewer.php b/code/DocumentationViewer.php index b773814..a00d88b 100755 --- a/code/DocumentationViewer.php +++ b/code/DocumentationViewer.php @@ -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]; diff --git a/code/tasks/RebuildLuceneDocsIndex.php b/code/tasks/RebuildLuceneDocsIndex.php new file mode 100644 index 0000000..a95ebec --- /dev/null +++ b/code/tasks/RebuildLuceneDocsIndex.php @@ -0,0 +1,72 @@ + (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(); + } +} \ No newline at end of file diff --git a/tests/DocumentationViewerTest.php b/tests/DocumentationViewerTest.php index ab24ec1..bd49cf2 100644 --- a/tests/DocumentationViewerTest.php +++ b/tests/DocumentationViewerTest.php @@ -144,7 +144,7 @@ class DocumentationViewerTests extends FunctionalTest { $this->assertFalse($child1->Children); $child2 = $pagesArr[2]; - + $this->assertType('DataObjectSet', $child2->Children); $this->assertEquals(