mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 09:05:56 +00: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
code
tests
DocumentationParserTest.phpDocumentationViewerTests.php
docs-recursive/en
@ -388,40 +388,56 @@ class DocumentationParser {
|
|||||||
* It is used for building the tree of the page.
|
* It is used for building the tree of the page.
|
||||||
*
|
*
|
||||||
* @param String module name
|
* @param String module name
|
||||||
|
* @param bool Recursive search
|
||||||
|
* @param DataObjectSet set of pages matched so far
|
||||||
*
|
*
|
||||||
* @return DataObjectSet
|
* @return DataObjectSet
|
||||||
*/
|
*/
|
||||||
public static function get_pages_from_folder($folder) {
|
public static function get_pages_from_folder($folder, $recursive = false, &$pages = false) {
|
||||||
$handle = opendir($folder);
|
|
||||||
$output = new DataObjectSet();
|
$output = new DataObjectSet();
|
||||||
$files = array();
|
|
||||||
|
if(!$pages) $pages = new DataObjectSet();
|
||||||
|
|
||||||
|
$handle = opendir($folder);
|
||||||
|
|
||||||
if($handle) {
|
if($handle) {
|
||||||
$extensions = DocumentationService::get_valid_extensions();
|
$extensions = DocumentationService::get_valid_extensions();
|
||||||
$ignore = DocumentationService::get_ignored_files();
|
$ignore = DocumentationService::get_ignored_files();
|
||||||
|
$files = array();
|
||||||
|
|
||||||
while (false !== ($file = readdir($handle))) {
|
while (false !== ($file = readdir($handle))) {
|
||||||
if(!in_array($file, $ignore)) {
|
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);
|
natsort($files);
|
||||||
|
|
||||||
foreach($files as $file) {
|
if($files) {
|
||||||
$file = strtolower($file);
|
foreach($files as $file) {
|
||||||
|
$clean = ($pos = strrpos($file, '.')) ? substr($file, 0, $pos) : $file;
|
||||||
$clean = ($pos = strrpos($file, '.')) ? substr($file, 0, $pos) : $file;
|
$path = rtrim($folder, '/') . '/'. $file;
|
||||||
|
|
||||||
$output->push(new ArrayData(array(
|
$pages->push(new ArrayData(array(
|
||||||
'Title' => self::clean_page_name($file),
|
'Title' => self::clean_page_name($file),
|
||||||
'Filename' => $clean,
|
'Filename' => $clean,
|
||||||
'Path' => $folder . $file .'/'
|
'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
|
// test the order of pages
|
||||||
$pages = DocumentationParser::get_pages_from_folder(BASE_PATH . '/sapphiredocs/tests/docs/en/sort');
|
$pages = DocumentationParser::get_pages_from_folder(BASE_PATH . '/sapphiredocs/tests/docs/en/sort');
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array('1 basic', '2 intermediate', '3 advanced', '10 some page', '21 another page'),
|
array('1 basic', '2 intermediate', '3 advanced', '10 some page', '21 another page'),
|
||||||
$pages->column('Title')
|
$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/'));
|
$response = $v->handleRequest(new SS_HTTPRequest('GET', '2.4/en/DocumentationViewerTests/subfolder/'));
|
||||||
$pages = $v->getModulePages();
|
$pages = $v->getModulePages();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array('index', 'subfolder', 'test'),
|
array('index', 'sort', 'subfolder', 'test'),
|
||||||
$pages->column('Filename')
|
$pages->column('Filename')
|
||||||
);
|
);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array('link', 'current', 'link'),
|
array('link', 'link','current', 'link'),
|
||||||
$pages->column('LinkingMode')
|
$pages->column('LinkingMode')
|
||||||
);
|
);
|
||||||
$links = $pages->column('Link');
|
$links = $pages->column('Link');
|
||||||
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/', $links[0]);
|
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/', $links[0]);
|
||||||
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/subfolder/', $links[1]);
|
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/sort/', $links[1]);
|
||||||
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/test/', $links[2]);
|
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/subfolder/', $links[2]);
|
||||||
|
$this->assertStringEndsWith('2.4/en/DocumentationViewerTests/test/', $links[3]);
|
||||||
|
|
||||||
// Children
|
// Children
|
||||||
$pagesArr = $pages->toArray();
|
$pagesArr = $pages->toArray();
|
||||||
$child1 = $pagesArr[0];
|
$child1 = $pagesArr[0];
|
||||||
|
|
||||||
$this->assertFalse($child1->Children);
|
$this->assertFalse($child1->Children);
|
||||||
|
|
||||||
$child2 = $pagesArr[1];
|
$child2 = $pagesArr[2];
|
||||||
|
|
||||||
$this->assertType('DataObjectSet', $child2->Children);
|
$this->assertType('DataObjectSet', $child2->Children);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array('subpage', 'subsubfolder'),
|
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…
x
Reference in New Issue
Block a user