2010-08-01 06:46:32 +02:00
|
|
|
<?php
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
/**
|
2014-09-06 01:22:05 +02:00
|
|
|
* A specific documentation page within a {@link DocumentationEntity}.
|
|
|
|
*
|
|
|
|
* Maps to a file on the file system. Note that the URL to access this page may
|
|
|
|
* not always be the file name. If the file contains meta data with a nicer URL
|
|
|
|
* sthen it will use that.
|
2010-08-01 06:46:32 +02:00
|
|
|
*
|
2012-04-08 11:36:16 +02:00
|
|
|
* @package docsviewer
|
2011-07-01 03:19:35 +02:00
|
|
|
* @subpackage model
|
2010-08-01 06:46:32 +02:00
|
|
|
*/
|
|
|
|
class DocumentationPage extends ViewableData {
|
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $title;
|
2012-11-08 17:20:55 +01:00
|
|
|
|
2013-05-22 18:35:26 +02:00
|
|
|
/**
|
2014-09-07 01:26:12 +02:00
|
|
|
* @var string
|
2012-11-08 17:20:55 +01:00
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
protected $summary;
|
2012-11-08 17:20:55 +01:00
|
|
|
|
|
|
|
/**
|
2014-09-07 01:26:12 +02:00
|
|
|
* @var DocumentationEntityLanguage
|
2012-11-08 17:20:55 +01:00
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
protected $entity;
|
2012-11-09 12:36:46 +01:00
|
|
|
|
2013-05-22 18:35:26 +02:00
|
|
|
/**
|
2014-09-07 01:26:12 +02:00
|
|
|
* @var string
|
2010-08-01 06:46:32 +02:00
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
protected $path, $filename;
|
2012-11-09 12:36:46 +01:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
/**
|
2014-09-07 01:26:12 +02:00
|
|
|
* @param DocumentationEntityLanguage $entity
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $path
|
2010-12-21 10:42:44 +01:00
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
public function __construct(DocumentationEntityLanguage $entity, $filename, $path) {
|
|
|
|
$this->filename = $filename;
|
|
|
|
$this->path = $path;
|
2010-10-21 22:27:23 +02:00
|
|
|
$this->entity = $entity;
|
|
|
|
}
|
2012-11-09 12:36:46 +01:00
|
|
|
|
2011-07-04 06:58:15 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-09-06 01:13:12 +02:00
|
|
|
public function getExtension() {
|
2014-09-07 07:09:28 +02:00
|
|
|
return DocumentationHelper::get_extension($this->filename);
|
2010-12-21 11:54:11 +01:00
|
|
|
}
|
|
|
|
|
2011-04-29 04:41:30 +02:00
|
|
|
/**
|
2012-04-14 07:00:22 +02:00
|
|
|
* @param string - has to be plain text for open search compatibility.
|
|
|
|
*
|
|
|
|
* @return string
|
2011-04-29 04:41:30 +02:00
|
|
|
*/
|
2014-09-06 01:13:12 +02:00
|
|
|
public function getBreadcrumbTitle($divider = ' - ') {
|
2014-09-07 01:26:12 +02:00
|
|
|
$pathParts = explode('/', $this->getRelativeLink());
|
2011-07-01 03:19:35 +02:00
|
|
|
|
|
|
|
// add the module to the breadcrumb trail.
|
|
|
|
array_unshift($pathParts, $this->entity->getTitle());
|
|
|
|
|
2014-09-07 07:09:28 +02:00
|
|
|
$titleParts = array_map(array(
|
|
|
|
'DocumentationHelper', 'clean_page_name'
|
|
|
|
), $pathParts);
|
2011-07-01 03:19:35 +02:00
|
|
|
|
2011-04-29 04:41:30 +02:00
|
|
|
return implode($divider, $titleParts + array($this->getTitle()));
|
|
|
|
}
|
|
|
|
|
2010-12-21 11:54:11 +01:00
|
|
|
/**
|
2014-09-07 01:26:12 +02:00
|
|
|
* @return DocumentationEntityLanguage
|
2010-12-21 11:54:11 +01:00
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
public function getEntity() {
|
|
|
|
return $this->entity;
|
2010-12-21 10:42:44 +01:00
|
|
|
}
|
2014-09-07 01:26:12 +02:00
|
|
|
|
2011-01-13 09:27:25 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
public function getTitle() {
|
|
|
|
if($this->title) {
|
|
|
|
return $this->title;
|
|
|
|
}
|
2011-07-01 03:19:35 +02:00
|
|
|
|
2014-09-07 11:28:21 +02:00
|
|
|
$page = DocumentationHelper::clean_page_name($this->filename);
|
|
|
|
|
|
|
|
if($page == "Index") {
|
|
|
|
// look at the folder name
|
|
|
|
$parts = explode("/", $this->getPath());
|
|
|
|
array_pop($parts); // name
|
|
|
|
|
|
|
|
$page = DocumentationHelper::clean_page_name(
|
|
|
|
array_pop($parts)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $page;
|
2011-01-14 04:29:14 +01:00
|
|
|
}
|
|
|
|
|
2010-12-21 11:54:11 +01:00
|
|
|
/**
|
2010-12-22 09:21:49 +01:00
|
|
|
* @return string
|
2010-12-21 11:54:11 +01:00
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
public function getSummary() {
|
|
|
|
return $this->summary;
|
2010-12-21 11:54:11 +01:00
|
|
|
}
|
2011-01-14 04:29:14 +01:00
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
/**
|
2014-09-06 01:13:12 +02:00
|
|
|
* Return the raw markdown for a given documentation page.
|
2011-07-01 03:19:35 +02:00
|
|
|
*
|
2014-09-06 01:13:12 +02:00
|
|
|
* @param boolean $removeMetaData
|
2011-07-04 06:58:15 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2010-08-01 06:46:32 +02:00
|
|
|
*/
|
2014-09-06 01:13:12 +02:00
|
|
|
public function getMarkdown($removeMetaData = false) {
|
2010-10-22 03:08:44 +02:00
|
|
|
try {
|
2014-09-07 01:26:12 +02:00
|
|
|
if ($md = file_get_contents($this->path)) {
|
|
|
|
if ($this->title != 'Index') {
|
|
|
|
$this->populateMetaDataFromText($md, $removeMetaData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $md;
|
2010-12-21 11:54:11 +01:00
|
|
|
}
|
2010-10-22 03:08:44 +02:00
|
|
|
}
|
2014-09-07 01:26:12 +02:00
|
|
|
catch(InvalidArgumentException $e) {
|
|
|
|
|
|
|
|
}
|
2010-10-22 03:08:44 +02:00
|
|
|
|
2011-07-01 03:19:35 +02:00
|
|
|
return false;
|
2010-08-01 06:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-06 01:13:12 +02:00
|
|
|
* Parse a file and return the parsed HTML version.
|
2011-07-01 03:19:35 +02:00
|
|
|
*
|
2012-04-14 07:00:22 +02:00
|
|
|
* @param string $baselink
|
2011-07-01 03:19:35 +02:00
|
|
|
*
|
2012-04-14 07:00:22 +02:00
|
|
|
* @return string
|
2010-08-01 06:46:32 +02:00
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
public function getHTML() {
|
|
|
|
return DocumentationParser::parse(
|
|
|
|
$this,
|
|
|
|
$this->entity->Link()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRelativeLink() {
|
|
|
|
$path = str_replace($this->entity->getPath(), '', $this->path);
|
|
|
|
$url = explode('/', $path);
|
|
|
|
|
|
|
|
$url = implode('/', array_map(function($a) {
|
|
|
|
return DocumentationHelper::clean_page_url($a);
|
|
|
|
}, $url));
|
|
|
|
|
|
|
|
$url = rtrim($url, '/') . '/';
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPath() {
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URL that will be required for the user to hit to view the
|
|
|
|
* given document base name.
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param string $path
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Link() {
|
|
|
|
return Controller::join_links(
|
|
|
|
$this->entity->Link(),
|
|
|
|
$this->getRelativeLink()
|
|
|
|
);
|
2010-08-01 06:46:32 +02:00
|
|
|
}
|
2013-05-22 18:35:26 +02:00
|
|
|
|
|
|
|
/**
|
2014-09-07 01:26:12 +02:00
|
|
|
* Return metadata from the first html block in the page, then remove the
|
2013-05-22 21:35:03 +02:00
|
|
|
* block on request
|
2013-05-22 18:35:26 +02:00
|
|
|
*
|
|
|
|
* @param DocumentationPage $md
|
2013-05-22 21:35:03 +02:00
|
|
|
* @param bool $remove
|
2013-05-22 18:35:26 +02:00
|
|
|
*/
|
2014-09-07 01:26:12 +02:00
|
|
|
public function populateMetaDataFromText(&$md, $removeMetaData = false) {
|
|
|
|
if($md) {
|
2013-05-22 21:35:03 +02:00
|
|
|
// get the text up to the first whiteline
|
|
|
|
$extPattern = "/^(.+)\n(\r)*\n/Uis";
|
|
|
|
$matches = preg_match($extPattern, $md, $block);
|
2014-09-07 01:26:12 +02:00
|
|
|
|
2013-05-22 18:35:26 +02:00
|
|
|
if($matches && $block[1]) {
|
2013-05-22 21:35:03 +02:00
|
|
|
$metaDataFound = false;
|
|
|
|
|
|
|
|
// find the key/value pairs
|
|
|
|
$intPattern = '/(?<key>[A-Za-z][A-Za-z0-9_-]+)[\t]*:[\t]*(?<value>[^:\n\r\/]+)/x';
|
|
|
|
$matches = preg_match_all($intPattern, $block[1], $meta);
|
|
|
|
|
2013-05-22 18:35:26 +02:00
|
|
|
foreach($meta['key'] as $index => $key) {
|
|
|
|
if(isset($meta['value'][$index])) {
|
2013-05-22 21:35:03 +02:00
|
|
|
|
|
|
|
// check if a property exists for this key
|
2013-05-22 18:35:26 +02:00
|
|
|
if (property_exists(get_class(), $key)) {
|
2014-09-07 01:26:12 +02:00
|
|
|
$this->$key = $meta['value'][$index];
|
|
|
|
|
2013-05-22 21:35:03 +02:00
|
|
|
$metaDataFound = true;
|
2013-05-22 18:35:26 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-22 21:35:03 +02:00
|
|
|
}
|
2014-09-07 01:26:12 +02:00
|
|
|
|
|
|
|
// optionally remove the metadata block (only on the page that
|
|
|
|
// is displayed)
|
2013-05-22 21:35:03 +02:00
|
|
|
if ($metaDataFound && $removeMetaData) {
|
|
|
|
$md = preg_replace($extPattern, '', $md);
|
|
|
|
}
|
2013-05-22 18:35:26 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-06 01:13:12 +02:00
|
|
|
}
|
2010-08-01 06:46:32 +02:00
|
|
|
}
|