mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 11:05:56 +02:00
EHANCEMENT allow recursive gathering of files from a folder. Initial cut of implementing a XML feed of all the pages
This commit is contained in:
parent
3a5be64818
commit
ac5ce3872e
@ -388,40 +388,56 @@ class DocumentationParser {
|
||||
* It is used for building the tree of the page.
|
||||
*
|
||||
* @param String module name
|
||||
* @param bool Recursive search
|
||||
* @param DataObjectSet set of pages matched so far
|
||||
*
|
||||
* @return DataObjectSet
|
||||
*/
|
||||
public static function get_pages_from_folder($folder) {
|
||||
$handle = opendir($folder);
|
||||
public static function get_pages_from_folder($folder, $recursive = false, &$pages = false) {
|
||||
$output = new DataObjectSet();
|
||||
$files = array();
|
||||
|
||||
if(!$pages) $pages = new DataObjectSet();
|
||||
|
||||
$handle = opendir($folder);
|
||||
|
||||
if($handle) {
|
||||
$extensions = DocumentationService::get_valid_extensions();
|
||||
$ignore = DocumentationService::get_ignored_files();
|
||||
$files = array();
|
||||
|
||||
while (false !== ($file = readdir($handle))) {
|
||||
if(!in_array($file, $ignore)) {
|
||||
$files[] = $file;
|
||||
$file = trim(strtolower($file), '/');
|
||||
$path = rtrim($folder, '/') . '/'. $file;
|
||||
|
||||
if($recursive && is_dir($path)) {
|
||||
self::get_pages_from_folder($path, true, $pages);
|
||||
}
|
||||
else {
|
||||
$files[] = $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
natsort($files);
|
||||
|
||||
foreach($files as $file) {
|
||||
$file = strtolower($file);
|
||||
|
||||
$clean = ($pos = strrpos($file, '.')) ? substr($file, 0, $pos) : $file;
|
||||
|
||||
$output->push(new ArrayData(array(
|
||||
'Title' => self::clean_page_name($file),
|
||||
'Filename' => $clean,
|
||||
'Path' => $folder . $file .'/'
|
||||
)));
|
||||
if($files) {
|
||||
foreach($files as $file) {
|
||||
$clean = ($pos = strrpos($file, '.')) ? substr($file, 0, $pos) : $file;
|
||||
$path = rtrim($folder, '/') . '/'. $file;
|
||||
|
||||
$pages->push(new ArrayData(array(
|
||||
'Title' => self::clean_page_name($file),
|
||||
'Filename' => $clean,
|
||||
'Path' => $path
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $output;
|
||||
closedir($handle);
|
||||
|
||||
return $pages;
|
||||
}
|
||||
|
||||
}
|
95
code/DocumentationSearch.php
Normal file
95
code/DocumentationSearch.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @todo caching?
|
||||
*/
|
||||
|
||||
class DocumentationSearch extends Controller {
|
||||
|
||||
/**
|
||||
* Generates the XML tree for {@link Sphinx} XML Pipes
|
||||
*
|
||||
* @uses DomDocument
|
||||
*/
|
||||
function sphinxxml() {
|
||||
DocumentationService::load_automatic_registration();
|
||||
|
||||
|
||||
// generate the head of the document
|
||||
$dom = new DomDocument('1.0');
|
||||
$dom->encoding = "utf-8";
|
||||
$dom->formatOutput = true;
|
||||
$root = $dom->appendChild($dom->createElement('sphinx:docset'));
|
||||
|
||||
$schema = $dom->createElement('sphinx:schema');
|
||||
|
||||
$field = $dom->createElement('sphinx:field');
|
||||
$attr = $dom->createElement('sphinx:attr');
|
||||
|
||||
foreach(array('Title','Content', 'Language', 'Version', 'Module') as $field) {
|
||||
$node = $dom->createElement('sphinx:field');
|
||||
$node->setAttribute('name', $field);
|
||||
|
||||
$schema->appendChild($node);
|
||||
}
|
||||
|
||||
$root->appendChild($schema);
|
||||
|
||||
// go through each documentation page and add it to index
|
||||
$pages = $this->getAllDocumentationPages();
|
||||
|
||||
if($pages) {
|
||||
foreach($pages as $doc) {
|
||||
$node = $dom->createElement('sphinx:document');
|
||||
|
||||
$node->setAttribute('ID', $doc->ID);
|
||||
|
||||
foreach($doc->getArray() as $key => $value) {
|
||||
if($key == 'ID') continue;
|
||||
|
||||
$tmp = $dom->createElement($key);
|
||||
$tmp->appendChild($dom->createTextNode($value));
|
||||
|
||||
$node->appendChild($tmp);
|
||||
}
|
||||
|
||||
$root->appendChild($node);
|
||||
}
|
||||
}
|
||||
|
||||
return $dom->saveXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an array of every single documentation page installed on the system.
|
||||
*
|
||||
* @todo Add version support
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getAllDocumentationPages() {
|
||||
$modules = DocumentationService::get_registered_modules();
|
||||
$output = new DataObjectSet();
|
||||
|
||||
|
||||
if($modules) {
|
||||
foreach($modules as $module) {
|
||||
foreach($module->getLanguages() as $language) {
|
||||
$pages = DocumentationParser::get_pages_from_folder($module->getPath(false, $language));
|
||||
|
||||
if($pages) {
|
||||
foreach($pages as $page) {
|
||||
$output->push(new ArrayData(array(
|
||||
'Title' => $page->Title,
|
||||
'Content' => file_get_contents($page->Path),
|
||||
'ID' => base_convert(substr(md5($page->Path), -8), 16, 10)
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
@ -214,11 +214,24 @@ class DocumentationParserTest extends SapphireTest {
|
||||
|
||||
// test the order of pages
|
||||
$pages = DocumentationParser::get_pages_from_folder(BASE_PATH . '/sapphiredocs/tests/docs/en/sort');
|
||||
|
||||
$this->assertEquals(
|
||||
array('1 basic', '2 intermediate', '3 advanced', '10 some page', '21 another page'),
|
||||
$pages->column('Title')
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
function testGetPagesFromFolderRecursive() {
|
||||
$pages = DocumentationParser::get_pages_from_folder(BASE_PATH . '/sapphiredocs/tests/docs-recursive/en/', true);
|
||||
// check to see all the pages are found, we don't care about order
|
||||
$this->assertEquals($pages->Count(), 6);
|
||||
|
||||
$pages = $pages->column('Title');
|
||||
|
||||
foreach(array('Index', 'Subfolder testfile', 'Subsubfolder testfile', 'Testfile') as $expected) {
|
||||
$this->assertContains($expected, $pages);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -124,24 +124,27 @@ class DocumentationViewerTests extends FunctionalTest {
|
||||
$response = $v->handleRequest(new SS_HTTPRequest('GET', '2.4/en/DocumentationViewerTests/subfolder/'));
|
||||
$pages = $v->getModulePages();
|
||||
$this->assertEquals(
|
||||
array('index', 'subfolder', 'test'),
|
||||
array('index', 'sort', 'subfolder', 'test'),
|
||||
$pages->column('Filename')
|
||||
);
|
||||
$this->assertEquals(
|
||||
array('link', 'current', 'link'),
|
||||
array('link', 'link','current', 'link'),
|
||||
$pages->column('LinkingMode')
|
||||
);
|
||||
$links = $pages->column('Link');
|
||||
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/', $links[0]);
|
||||
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/subfolder/', $links[1]);
|
||||
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/test/', $links[2]);
|
||||
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/sort/', $links[1]);
|
||||
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/subfolder/', $links[2]);
|
||||
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/test/', $links[3]);
|
||||
|
||||
// Children
|
||||
$pagesArr = $pages->toArray();
|
||||
$child1 = $pagesArr[0];
|
||||
|
||||
$this->assertFalse($child1->Children);
|
||||
|
||||
$child2 = $pagesArr[1];
|
||||
$child2 = $pagesArr[2];
|
||||
|
||||
$this->assertType('DataObjectSet', $child2->Children);
|
||||
$this->assertEquals(
|
||||
array('subpage', 'subsubfolder'),
|
||||
|
0
tests/docs-recursive/en/TestFile.md
Normal file
0
tests/docs-recursive/en/TestFile.md
Normal file
0
tests/docs-recursive/en/index.md
Normal file
0
tests/docs-recursive/en/index.md
Normal file
0
tests/docs-recursive/en/subfolder/index.md
Normal file
0
tests/docs-recursive/en/subfolder/index.md
Normal file
Loading…
Reference in New Issue
Block a user