2014-09-07 01:26:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class which builds a manifest of all documentation present in a project.
|
|
|
|
*
|
|
|
|
* The manifest is required to map the provided documentation URL rules to a
|
|
|
|
* file path on the server. The stored cache looks similar to the following:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* array(
|
|
|
|
* 'en/someniceurl/' => array(
|
|
|
|
* 'filepath' => '/path/to/docs/en/SomeniceFile.md',
|
|
|
|
* 'title' => 'Some nice URL',
|
|
|
|
* 'summary' => 'Summary Text',
|
|
|
|
* 'basename' => 'SomeniceFile.md',
|
|
|
|
* 'type' => 'DocumentationPage'
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* URL format is in the following structures:
|
|
|
|
*
|
|
|
|
* {lang}/{path}
|
|
|
|
* {lang}/{module}/{path}
|
|
|
|
* {lang}/{module}/{version}/{/path}
|
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage manifest
|
|
|
|
*/
|
|
|
|
class DocumentationManifest {
|
|
|
|
|
2014-09-07 07:09:28 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var boolean $automatic_registration
|
|
|
|
*/
|
|
|
|
private static $automatic_registration = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var array $registered_entities
|
|
|
|
*/
|
|
|
|
private static $register_entities = array();
|
|
|
|
|
2014-09-07 01:26:12 +02:00
|
|
|
protected $cache;
|
|
|
|
protected $cacheKey;
|
2014-09-15 11:47:45 +02:00
|
|
|
|
2014-09-07 01:26:12 +02:00
|
|
|
protected $inited;
|
|
|
|
protected $forceRegen;
|
2014-09-15 11:47:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $pages
|
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
protected $pages = array();
|
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
/**
|
|
|
|
* @var DocumentationEntity
|
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
private $entity;
|
|
|
|
|
2014-09-27 00:13:40 +02:00
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $automaticallyPopulated = false;
|
|
|
|
|
2014-09-07 07:09:28 +02:00
|
|
|
/**
|
2014-09-19 13:29:22 +02:00
|
|
|
* @var ArrayList
|
2014-09-07 07:09:28 +02:00
|
|
|
*/
|
2014-09-19 13:29:22 +02:00
|
|
|
private $registeredEntities;
|
2014-09-07 07:09:28 +02:00
|
|
|
|
2014-09-07 01:26:12 +02:00
|
|
|
/**
|
|
|
|
* Constructs a new template manifest. The manifest is not actually built
|
|
|
|
* or loaded from cache until needed.
|
|
|
|
*
|
|
|
|
* @param bool $includeTests Include tests in the manifest.
|
|
|
|
* @param bool $forceRegen Force the manifest to be regenerated.
|
|
|
|
*/
|
|
|
|
public function __construct($forceRegen = false) {
|
|
|
|
$this->cacheKey = 'manifest';
|
|
|
|
$this->forceRegen = $forceRegen;
|
2014-09-19 13:29:22 +02:00
|
|
|
$this->registeredEntities = new ArrayList();
|
2014-09-07 01:26:12 +02:00
|
|
|
|
|
|
|
$this->cache = SS_Cache::factory('DocumentationManifest', 'Core', array(
|
|
|
|
'automatic_serialization' => true,
|
|
|
|
'lifetime' => null
|
|
|
|
));
|
2014-09-15 11:47:45 +02:00
|
|
|
|
|
|
|
$this->setupEntities();
|
2014-09-07 01:26:12 +02:00
|
|
|
}
|
|
|
|
|
2014-09-07 07:09:28 +02:00
|
|
|
/**
|
|
|
|
* Sets up the top level entities.
|
|
|
|
*
|
|
|
|
* Either manually registered through the YAML syntax or automatically
|
|
|
|
* loaded through investigating the file system for `docs` folder.
|
|
|
|
*/
|
|
|
|
public function setupEntities() {
|
2014-09-27 00:13:40 +02:00
|
|
|
if($this->registeredEntities->Count() > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-07 07:09:28 +02:00
|
|
|
if(Config::inst()->get('DocumentationManifest', 'automatic_registration')) {
|
|
|
|
$this->populateEntitiesFromInstall();
|
|
|
|
}
|
|
|
|
|
|
|
|
$registered = Config::inst()->get('DocumentationManifest', 'register_entities');
|
|
|
|
|
|
|
|
foreach($registered as $details) {
|
|
|
|
// validate the details provided through the YAML configuration
|
2014-09-15 11:47:45 +02:00
|
|
|
$required = array('Path', 'Title');
|
2014-09-07 07:09:28 +02:00
|
|
|
|
|
|
|
foreach($required as $require) {
|
|
|
|
if(!isset($details[$require])) {
|
|
|
|
throw new Exception("$require is a required key in DocumentationManifest.register_entities");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
// if path is not an absolute value then assume it is relative from
|
|
|
|
// the BASE_PATH.
|
|
|
|
$path = $this->getRealPath($details['Path']);
|
2014-09-07 07:09:28 +02:00
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
$key = (isset($details['Key'])) ? $details['Key'] : $details['Title'];
|
|
|
|
|
|
|
|
if(!is_dir($path)) {
|
|
|
|
throw new Exception($path . ' is not a valid documentation directory');
|
2014-09-07 07:09:28 +02:00
|
|
|
}
|
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
$version = (isset($details['Version'])) ? $details['Version'] : '';
|
|
|
|
|
|
|
|
$langs = scandir($path);
|
|
|
|
|
|
|
|
if($langs) {
|
|
|
|
$possible = i18n::get_common_languages(true);
|
|
|
|
|
|
|
|
foreach($langs as $k => $lang) {
|
|
|
|
if(isset($possible[$lang])) {
|
|
|
|
$entity = Injector::inst()->create(
|
|
|
|
'DocumentationEntity', $key
|
|
|
|
);
|
|
|
|
|
|
|
|
$entity->setPath(Controller::join_links($path, $lang, '/'));
|
|
|
|
$entity->setTitle($details['Title']);
|
|
|
|
$entity->setLanguage($lang);
|
|
|
|
$entity->setVersion($version);
|
2014-09-07 07:09:28 +02:00
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
if(isset($details['Stable'])) {
|
|
|
|
$entity->setIsStable($details['Stable']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($details['DefaultEntity'])) {
|
|
|
|
$entity->setIsDefaultEntity($details['DefaultEntity']);
|
|
|
|
}
|
2014-09-07 12:14:10 +02:00
|
|
|
|
2014-09-19 13:29:22 +02:00
|
|
|
$this->registeredEntities->push($entity);
|
2014-09-15 11:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-07 12:14:10 +02:00
|
|
|
}
|
2014-09-07 07:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
public function getRealPath($path) {
|
|
|
|
if(substr($path, 0, 1) != '/') {
|
|
|
|
$path = realpath(Controller::join_links(BASE_PATH, $path));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2014-09-07 07:09:28 +02:00
|
|
|
/**
|
2014-09-19 13:29:22 +02:00
|
|
|
* @return ArrayList
|
2014-09-07 07:09:28 +02:00
|
|
|
*/
|
|
|
|
public function getEntities() {
|
|
|
|
return $this->registeredEntities;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scans the current installation and picks up all the SilverStripe modules
|
|
|
|
* that contain a `docs` folder.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function populateEntitiesFromInstall() {
|
2014-09-27 00:13:40 +02:00
|
|
|
if($this->automaticallyPopulated) {
|
|
|
|
// already run
|
|
|
|
return;
|
|
|
|
}
|
2014-09-07 07:09:28 +02:00
|
|
|
|
|
|
|
foreach(scandir(BASE_PATH) as $key => $entity) {
|
|
|
|
if($key == "themes") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-27 00:13:40 +02:00
|
|
|
$dir = Controller::join_links(BASE_PATH, $entity);
|
|
|
|
|
|
|
|
if(is_dir($dir)) {
|
2014-09-07 07:09:28 +02:00
|
|
|
// check to see if it has docs
|
|
|
|
$docs = Controller::join_links($dir, 'docs');
|
|
|
|
|
|
|
|
if(is_dir($docs)) {
|
|
|
|
$entities[] = array(
|
2014-09-27 00:13:40 +02:00
|
|
|
'Path' => $docs,
|
|
|
|
'Title' => DocumentationHelper::clean_page_name($entity),
|
2014-09-07 07:09:28 +02:00
|
|
|
'Version' => 'master',
|
|
|
|
'Stable' => true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::inst()->update(
|
2014-09-27 00:13:40 +02:00
|
|
|
'DocumentationManifest', 'register_entities', $entities
|
2014-09-07 07:09:28 +02:00
|
|
|
);
|
2014-09-27 00:13:40 +02:00
|
|
|
|
|
|
|
$this->automaticallyPopulated = true;
|
2014-09-07 07:09:28 +02:00
|
|
|
}
|
2014-09-07 01:26:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function init() {
|
|
|
|
if (!$this->forceRegen && $data = $this->cache->load($this->cacheKey)) {
|
|
|
|
$this->pages = $data;
|
|
|
|
$this->inited = true;
|
|
|
|
} else {
|
|
|
|
$this->regenerate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a map of all documentation pages.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getPages() {
|
|
|
|
if (!$this->inited) {
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a particular page for the requested URL.
|
|
|
|
*
|
|
|
|
* @return DocumentationPage
|
|
|
|
*/
|
|
|
|
public function getPage($url) {
|
|
|
|
$pages = $this->getPages();
|
2014-09-15 11:47:45 +02:00
|
|
|
$url = $this->normalizeUrl($url);
|
2014-09-07 01:26:12 +02:00
|
|
|
|
|
|
|
if(!isset($pages[$url])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-07 07:09:28 +02:00
|
|
|
$record = $pages[$url];
|
2014-09-07 01:26:12 +02:00
|
|
|
|
2014-09-07 07:09:28 +02:00
|
|
|
foreach($this->getEntities() as $entity) {
|
2014-09-15 11:47:45 +02:00
|
|
|
if(strpos($record['filepath'], $entity->getPath()) !== false) {
|
|
|
|
$page = Injector::inst()->create(
|
|
|
|
$record['type'],
|
|
|
|
$entity,
|
|
|
|
$record['basename'],
|
|
|
|
$record['filepath']
|
|
|
|
);
|
2014-09-07 01:26:12 +02:00
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
return $page;
|
2014-09-07 01:26:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerates the manifest by scanning the base path.
|
|
|
|
*
|
|
|
|
* @param bool $cache
|
|
|
|
*/
|
|
|
|
public function regenerate($cache = true) {
|
|
|
|
$finder = new DocumentationManifestFileFinder();
|
|
|
|
$finder->setOptions(array(
|
|
|
|
'dir_callback' => array($this, 'handleFolder'),
|
|
|
|
'file_callback' => array($this, 'handleFile')
|
|
|
|
));
|
|
|
|
|
2014-09-07 07:09:28 +02:00
|
|
|
foreach($this->getEntities() as $entity) {
|
2014-09-15 11:47:45 +02:00
|
|
|
$this->entity = $entity;
|
|
|
|
|
|
|
|
$this->handleFolder('', $this->entity->getPath(), 0);
|
|
|
|
$finder->find($this->entity->getPath());
|
2014-09-07 01:26:12 +02:00
|
|
|
}
|
2014-09-21 01:18:36 +02:00
|
|
|
|
|
|
|
// groupds
|
|
|
|
$grouped = array();
|
|
|
|
|
|
|
|
foreach($this->pages as $url => $page) {
|
|
|
|
if(!isset($grouped[$page['entitypath']])) {
|
|
|
|
$grouped[$page['entitypath']] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$grouped[$page['entitypath']][$url] = $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pages = array();
|
|
|
|
|
|
|
|
foreach($grouped as $entity) {
|
|
|
|
uasort($entity, function($a, $b) {
|
|
|
|
// ensure parent directories are first
|
|
|
|
$a['filepath'] = str_replace('index.md', '', $a['filepath']);
|
|
|
|
$b['filepath'] = str_replace('index.md', '', $b['filepath']);
|
|
|
|
|
|
|
|
if(strpos($b['filepath'], $a['filepath']) === 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a['filepath'] == $b['filepath']) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-09-07 01:26:12 +02:00
|
|
|
|
2014-09-21 01:18:36 +02:00
|
|
|
return ($a['filepath'] < $b['filepath']) ? -1 : 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->pages = array_merge($this->pages, $entity);
|
|
|
|
}
|
|
|
|
|
2014-09-07 01:26:12 +02:00
|
|
|
if ($cache) {
|
|
|
|
$this->cache->save($this->pages, $this->cacheKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->inited = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function handleFolder($basename, $path, $depth) {
|
|
|
|
$folder = Injector::inst()->create(
|
|
|
|
'DocumentationFolder', $this->entity, $basename, $path
|
|
|
|
);
|
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
$link = ltrim(str_replace(
|
|
|
|
Config::inst()->get('DocumentationViewer', 'link_base'),
|
|
|
|
'',
|
|
|
|
$folder->Link()
|
|
|
|
), '/');
|
|
|
|
|
|
|
|
$this->pages[$link] = array(
|
2014-09-07 01:26:12 +02:00
|
|
|
'title' => $folder->getTitle(),
|
|
|
|
'basename' => $basename,
|
|
|
|
'filepath' => $path,
|
2014-09-19 13:29:22 +02:00
|
|
|
'type' => 'DocumentationFolder',
|
2014-09-21 01:18:36 +02:00
|
|
|
'entitypath' => $this->entity->getPath(),
|
2014-09-19 13:29:22 +02:00
|
|
|
'summary' => ''
|
2014-09-07 01:26:12 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Individual files can optionally provide a nice title and a better URL
|
|
|
|
* through the use of markdown meta data. This creates a new
|
|
|
|
* {@link DocumentationPage} instance for the file.
|
|
|
|
*
|
|
|
|
* If the markdown does not specify the title in the meta data it falls back
|
|
|
|
* to using the file name.
|
|
|
|
*
|
|
|
|
* @param string $basename
|
|
|
|
* @param string $path
|
|
|
|
* @param int $depth
|
|
|
|
*/
|
|
|
|
public function handleFile($basename, $path, $depth) {
|
|
|
|
$page = Injector::inst()->create(
|
|
|
|
'DocumentationPage',
|
|
|
|
$this->entity, $basename, $path
|
|
|
|
);
|
|
|
|
|
2014-09-07 12:14:10 +02:00
|
|
|
// populate any meta data
|
|
|
|
$page->getMarkdown();
|
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
$link = ltrim(str_replace(
|
|
|
|
Config::inst()->get('DocumentationViewer', 'link_base'),
|
|
|
|
'',
|
|
|
|
$page->Link()
|
|
|
|
), '/');
|
|
|
|
|
|
|
|
$this->pages[$link] = array(
|
2014-09-07 01:26:12 +02:00
|
|
|
'title' => $page->getTitle(),
|
|
|
|
'filepath' => $path,
|
2014-09-21 01:18:36 +02:00
|
|
|
'entitypath' => $this->entity->getPath(),
|
2014-09-07 01:26:12 +02:00
|
|
|
'basename' => $basename,
|
|
|
|
'type' => 'DocumentationPage',
|
|
|
|
'summary' => $page->getSummary()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an {@link ArrayList} of the pages to the given page.
|
|
|
|
*
|
2014-09-07 12:14:10 +02:00
|
|
|
* @param DocumentationPage
|
|
|
|
* @param DocumentationEntityLanguage
|
|
|
|
*
|
2014-09-07 01:26:12 +02:00
|
|
|
* @return ArrayList
|
|
|
|
*/
|
2014-09-07 12:14:10 +02:00
|
|
|
public function generateBreadcrumbs($record, $base) {
|
2014-09-07 01:26:12 +02:00
|
|
|
$output = new ArrayList();
|
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
$parts = explode('/', trim($record->getRelativeLink(), '/'));
|
|
|
|
|
|
|
|
// Add the base link.
|
2014-09-07 12:14:10 +02:00
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Link' => $base->Link(),
|
|
|
|
'Title' => $base->Title
|
|
|
|
)));
|
|
|
|
|
|
|
|
$progress = $base->Link();
|
|
|
|
|
|
|
|
foreach($parts as $part) {
|
|
|
|
if($part) {
|
|
|
|
$progress = Controller::join_links($progress, $part, '/');
|
|
|
|
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Link' => $progress,
|
|
|
|
'Title' => DocumentationHelper::clean_page_name($part)
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
2014-09-07 01:26:12 +02:00
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the next page from the given page.
|
|
|
|
*
|
|
|
|
* Relies on the fact when the manifest was built, it was generated in
|
|
|
|
* order.
|
|
|
|
*
|
2014-09-21 01:18:36 +02:00
|
|
|
* @param string $filepath
|
|
|
|
* @param string $entityBase
|
2014-09-07 01:26:12 +02:00
|
|
|
*
|
|
|
|
* @return ArrayData
|
|
|
|
*/
|
2014-09-21 01:18:36 +02:00
|
|
|
public function getNextPage($filepath, $entityBase) {
|
2014-09-07 01:26:12 +02:00
|
|
|
$grabNext = false;
|
2014-09-21 01:18:36 +02:00
|
|
|
$fallback = null;
|
2014-09-07 01:26:12 +02:00
|
|
|
|
|
|
|
foreach($this->getPages() as $url => $page) {
|
2014-09-21 01:18:36 +02:00
|
|
|
if($grabNext && strpos($page['filepath'], $entityBase) !== false) {
|
2014-09-07 01:26:12 +02:00
|
|
|
return new ArrayData(array(
|
|
|
|
'Link' => $url,
|
|
|
|
'Title' => $page['title']
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if($filepath == $page['filepath']) {
|
|
|
|
$grabNext = true;
|
2014-09-21 01:18:36 +02:00
|
|
|
} else if(!$fallback && strpos($page['filepath'], $filepath) !== false) {
|
|
|
|
$fallback = new ArrayData(array(
|
|
|
|
'Link' => $url,
|
|
|
|
'Title' => $page['title'],
|
|
|
|
'Fallback' => true
|
|
|
|
));
|
2014-09-07 01:26:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 01:18:36 +02:00
|
|
|
if(!$grabNext) {
|
|
|
|
return $fallback;
|
|
|
|
}
|
|
|
|
|
2014-09-07 01:26:12 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the previous page from the given page.
|
|
|
|
*
|
|
|
|
* Relies on the fact when the manifest was built, it was generated in
|
|
|
|
* order.
|
|
|
|
*
|
2014-09-21 01:18:36 +02:00
|
|
|
* @param string $filepath
|
|
|
|
* @param string $entityBase
|
2014-09-07 01:26:12 +02:00
|
|
|
*
|
|
|
|
* @return ArrayData
|
|
|
|
*/
|
2014-09-21 01:18:36 +02:00
|
|
|
public function getPreviousPage($filepath, $entityPath) {
|
2014-09-07 01:26:12 +02:00
|
|
|
$previousUrl = $previousPage = null;
|
|
|
|
|
|
|
|
foreach($this->getPages() as $url => $page) {
|
|
|
|
if($filepath == $page['filepath']) {
|
|
|
|
if($previousUrl) {
|
|
|
|
return new ArrayData(array(
|
|
|
|
'Link' => $previousUrl,
|
|
|
|
'Title' => $previousPage['title']
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 01:18:36 +02:00
|
|
|
if(strpos($page['filepath'], $entityPath) !== false) {
|
|
|
|
$previousUrl = $url;
|
|
|
|
$previousPage = $page;
|
|
|
|
}
|
2014-09-07 01:26:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2014-09-07 11:28:21 +02:00
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function normalizeUrl($url) {
|
2014-09-20 03:43:22 +02:00
|
|
|
$url = trim($url, '/') .'/';
|
|
|
|
|
|
|
|
// if the page is the index page then hide it from the menu
|
|
|
|
if(strpos(strtolower($url), '/index.md/')) {
|
|
|
|
$url = substr($url, 0, strpos($url, "index.md/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
2014-09-15 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2014-09-07 11:28:21 +02:00
|
|
|
/**
|
|
|
|
* Return the children of the provided record path.
|
|
|
|
*
|
|
|
|
* Looks for any pages in the manifest which have one more slash attached.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
2014-09-20 03:43:22 +02:00
|
|
|
public function getChildrenFor($entityPath, $recordPath = null) {
|
|
|
|
if(!$recordPath) {
|
|
|
|
$recordPath = $entityPath;
|
|
|
|
}
|
|
|
|
|
2014-09-07 11:28:21 +02:00
|
|
|
$output = new ArrayList();
|
2014-09-15 11:47:45 +02:00
|
|
|
$base = Config::inst()->get('DocumentationViewer', 'link_base');
|
2014-09-20 03:43:22 +02:00
|
|
|
$entityPath = $this->normalizeUrl($entityPath);
|
|
|
|
$recordPath = $this->normalizeUrl($recordPath);
|
|
|
|
|
|
|
|
$depth = substr_count($entityPath, '/');
|
2014-09-07 11:28:21 +02:00
|
|
|
|
|
|
|
foreach($this->getPages() as $url => $page) {
|
2014-09-15 11:47:45 +02:00
|
|
|
$pagePath = $this->normalizeUrl($page['filepath']);
|
|
|
|
|
|
|
|
// check to see if this page is under the given path
|
2014-09-20 03:43:22 +02:00
|
|
|
if(strpos($pagePath, $entityPath) === false) {
|
2014-09-15 11:47:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only pull it up if it's one more level depth
|
|
|
|
if(substr_count($pagePath, DIRECTORY_SEPARATOR) == ($depth + 1)) {
|
2014-09-20 03:43:22 +02:00
|
|
|
$mode = (strpos($recordPath, $pagePath) !== false) ? 'current' : 'link';
|
2014-09-15 11:47:45 +02:00
|
|
|
$children = new ArrayList();
|
2014-09-07 12:14:10 +02:00
|
|
|
|
2014-09-20 03:43:22 +02:00
|
|
|
if($mode == 'current') {
|
|
|
|
$children = $this->getChildrenFor($pagePath, $recordPath);
|
2014-09-15 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Link' => Controller::join_links($base, $url, '/'),
|
|
|
|
'Title' => $page['title'],
|
|
|
|
'LinkingMode' => $mode,
|
2014-09-19 13:29:22 +02:00
|
|
|
'Summary' => $page['summary'],
|
2014-09-15 11:47:45 +02:00
|
|
|
'Children' => $children
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DocumentationEntity
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
2014-09-19 13:29:22 +02:00
|
|
|
public function getAllVersionsOfEntity(DocumentationEntity $entity) {
|
2014-09-15 11:47:45 +02:00
|
|
|
$all = new ArrayList();
|
|
|
|
|
|
|
|
foreach($this->getEntities() as $check) {
|
|
|
|
if($check->getKey() == $entity->getKey()) {
|
|
|
|
if($check->getLanguage() == $entity->getLanguage()) {
|
|
|
|
$all->push($check);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $all;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DocumentationEntity
|
|
|
|
*
|
|
|
|
* @return DocumentationEntity
|
|
|
|
*/
|
|
|
|
public function getStableVersion(DocumentationEntity $entity) {
|
|
|
|
foreach($this->getEntities() as $check) {
|
|
|
|
if($check->getKey() == $entity->getKey()) {
|
|
|
|
if($check->getLanguage() == $entity->getLanguage()) {
|
|
|
|
if($check->getIsStable()) {
|
|
|
|
return $check;
|
2014-09-07 12:14:10 +02:00
|
|
|
}
|
2014-09-15 11:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-07 12:14:10 +02:00
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DocumentationEntity
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function getVersions($entity) {
|
|
|
|
if(!$entity) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = new ArrayList();
|
|
|
|
|
|
|
|
foreach($this->getEntities() as $check) {
|
|
|
|
if($check->getKey() == $entity->getKey()) {
|
|
|
|
if($check->getLanguage() == $entity->getLanguage()) {
|
|
|
|
$same = ($check->getVersion() == $entity->getVersion());
|
|
|
|
|
|
|
|
$output->push(new ArrayList(array(
|
|
|
|
'Title' => $entity->getTitle(),
|
|
|
|
'Link' => $entity->getLink(),
|
|
|
|
'LinkingMode' => ($same) ? 'current' : 'link'
|
2014-09-07 11:28:21 +02:00
|
|
|
)));
|
2014-09-15 11:47:45 +02:00
|
|
|
|
2014-09-07 11:28:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2014-09-19 13:29:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a sorted array of all the unique versions registered
|
|
|
|
*/
|
|
|
|
public function getAllVersions() {
|
|
|
|
$versions = array();
|
|
|
|
|
|
|
|
foreach($this->getEntities() as $entity) {
|
2014-09-21 01:18:36 +02:00
|
|
|
if($entity->getVersion()) {
|
|
|
|
$versions[$entity->getVersion()] = $entity->getVersion();
|
|
|
|
} else {
|
|
|
|
$versions['0.0'] = _t('DocumentationManifest.MASTER', 'Master');
|
|
|
|
}
|
2014-09-19 13:29:22 +02:00
|
|
|
}
|
|
|
|
|
2014-09-21 01:18:36 +02:00
|
|
|
asort($versions);
|
2014-09-19 13:29:22 +02:00
|
|
|
|
2014-09-21 01:18:36 +02:00
|
|
|
return $versions;
|
2014-09-19 13:29:22 +02:00
|
|
|
}
|
2014-09-07 01:26:12 +02:00
|
|
|
}
|