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 {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DocumentationEntity
|
|
|
|
*/
|
|
|
|
protected $entity;
|
|
|
|
|
|
|
|
/**
|
2010-12-22 09:21:49 +01:00
|
|
|
* Stores the relative path (from the {@link DocumentationEntity} to
|
|
|
|
* this page. The actual file name can be accessed via {@link $this->getFilename()}
|
|
|
|
*
|
2014-09-06 01:13:12 +02:00
|
|
|
* @var string
|
2010-08-01 06:46:32 +02:00
|
|
|
*/
|
|
|
|
protected $relativePath;
|
|
|
|
|
2010-10-21 22:27:23 +02:00
|
|
|
/**
|
2014-09-06 01:13:12 +02:00
|
|
|
* @var string
|
2010-10-21 22:27:23 +02:00
|
|
|
*/
|
2010-08-01 06:46:32 +02:00
|
|
|
protected $lang = 'en';
|
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $title;
|
|
|
|
|
2010-10-21 22:27:23 +02:00
|
|
|
/**
|
2014-09-06 01:13:12 +02:00
|
|
|
* @var string
|
2010-10-21 22:27:23 +02:00
|
|
|
*/
|
2010-08-01 06:46:32 +02:00
|
|
|
protected $version;
|
|
|
|
|
2012-11-08 17:20:55 +01:00
|
|
|
/**
|
2014-09-06 01:13:12 +02:00
|
|
|
* @var boolean
|
2012-11-08 17:20:55 +01:00
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
protected $isFolder = false;
|
2012-11-08 17:20:55 +01:00
|
|
|
|
2013-05-22 18:35:26 +02:00
|
|
|
/**
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $pagenumber = 0;
|
|
|
|
|
2012-11-08 17:20:55 +01:00
|
|
|
/**
|
2014-09-06 01:13:12 +02:00
|
|
|
* @param boolean
|
2012-11-08 17:20:55 +01:00
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
public function setIsFolder($isFolder = false) {
|
2012-11-09 14:01:57 +01:00
|
|
|
$this->isFolder = $isFolder;
|
2012-11-08 17:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-06 01:13:12 +02:00
|
|
|
* @return boolean
|
2012-11-08 17:20:55 +01:00
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
public function getIsFolder($isFolder = false) {
|
2012-11-09 14:01:57 +01:00
|
|
|
return $this->isFolder;
|
2012-11-08 17:20:55 +01:00
|
|
|
}
|
2012-11-09 12:36:46 +01:00
|
|
|
|
2013-05-22 18:35:26 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $number
|
|
|
|
*/
|
|
|
|
public function setPagenumber($number = 0) {
|
|
|
|
if (is_int($number )) $this->pagenumber = $number;
|
|
|
|
}
|
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
/**
|
|
|
|
* @return DocumentationEntity
|
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
public function getEntity() {
|
2010-08-01 06:46:32 +02:00
|
|
|
return $this->entity;
|
|
|
|
}
|
2012-11-09 12:36:46 +01:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
/**
|
|
|
|
* @param DocumentationEntity
|
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
public function setEntity($entity) {
|
2010-10-21 22:27:23 +02:00
|
|
|
$this->entity = $entity;
|
|
|
|
}
|
2012-11-09 12:36:46 +01:00
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
/**
|
2010-12-21 10:42:44 +01:00
|
|
|
* @return string
|
2010-08-01 06:46:32 +02:00
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
public function getRelativePath() {
|
2010-08-01 06:46:32 +02:00
|
|
|
return $this->relativePath;
|
|
|
|
}
|
2012-11-09 12:36:46 +01:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
public function setRelativePath($path) {
|
2010-10-21 22:27:23 +02:00
|
|
|
$this->relativePath = $path;
|
|
|
|
}
|
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() {
|
2011-07-04 06:58:15 +02:00
|
|
|
return DocumentationService::get_extension($this->getRelativePath());
|
|
|
|
}
|
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
/**
|
|
|
|
* Absolute path including version and lang folder.
|
|
|
|
*
|
2010-12-21 10:42:44 +01:00
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*
|
2010-12-21 11:54:11 +01:00
|
|
|
* @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
|
2010-12-21 10:42:44 +01:00
|
|
|
* @return string
|
2010-08-01 06:46:32 +02:00
|
|
|
*/
|
2014-09-06 01:13:12 +02:00
|
|
|
public function getPath($defaultFile = false, $realpath = true) {
|
2010-12-22 09:21:49 +01:00
|
|
|
if($this->entity) {
|
2011-07-01 03:19:35 +02:00
|
|
|
$path = Controller::join_links(
|
|
|
|
$this->entity->getPath($this->getVersion(), $this->lang),
|
|
|
|
$this->getRelativePath()
|
|
|
|
);
|
2010-12-22 09:21:49 +01:00
|
|
|
|
2013-05-15 15:51:08 +02:00
|
|
|
if(!is_dir($path) && $realpath) $path = realpath($path);
|
2010-12-22 09:21:49 +01:00
|
|
|
else if($defaultFile) {
|
|
|
|
$file = DocumentationService::find_page($this->entity, explode('/', $this->getRelativePath()));
|
2010-12-21 11:54:11 +01:00
|
|
|
|
2010-12-22 09:21:49 +01:00
|
|
|
if($file) $path = $file;
|
2010-12-21 10:42:44 +01:00
|
|
|
}
|
|
|
|
}
|
2010-12-22 09:21:49 +01:00
|
|
|
else {
|
|
|
|
$path = $this->getRelativePath();
|
|
|
|
}
|
2010-12-21 11:54:11 +01:00
|
|
|
if(!file_exists($path)) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Path could not be found. Module path: %s, file path: %s',
|
|
|
|
$this->entity->getPath(),
|
2010-12-22 09:21:49 +01:00
|
|
|
$this->getRelativePath()
|
2010-12-21 11:54:11 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2010-12-22 09:21:49 +01:00
|
|
|
|
|
|
|
return (is_dir($path)) ? rtrim($path, '/') . '/' : $path;
|
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 = ' - ') {
|
2011-04-29 04:41:30 +02:00
|
|
|
$pathParts = explode('/', $this->getRelativePath());
|
2011-07-01 03:19:35 +02:00
|
|
|
|
|
|
|
// add the module to the breadcrumb trail.
|
|
|
|
array_unshift($pathParts, $this->entity->getTitle());
|
|
|
|
|
2011-04-29 04:41:30 +02:00
|
|
|
$titleParts = array_map(array('DocumentationService', '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
|
|
|
/**
|
2012-04-14 07:00:22 +02:00
|
|
|
* Returns the public accessible link for this page.
|
2010-12-21 11:54:11 +01:00
|
|
|
*
|
2013-05-29 18:09:42 +02:00
|
|
|
* @param Boolean Absolute URL (incl. domain), or relative to webroot
|
2010-12-21 11:54:11 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-09-06 01:13:12 +02:00
|
|
|
public function getLink($absolute = true) {
|
2010-12-22 09:21:49 +01:00
|
|
|
if($entity = $this->getEntity()) {
|
2013-05-29 18:09:42 +02:00
|
|
|
$link = $this->getRelativeLink();
|
2010-12-22 09:21:49 +01:00
|
|
|
$link = rtrim(DocumentationService::trim_extension_off($link), '/');
|
|
|
|
|
|
|
|
// folders should have a / on them. Looks nicer
|
|
|
|
try {
|
|
|
|
if(is_dir($this->getPath())) $link .= '/';
|
|
|
|
}
|
|
|
|
catch (Exception $e) {}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$link = $this->getPath(true);
|
|
|
|
}
|
|
|
|
|
2013-05-29 18:09:42 +02:00
|
|
|
if($absolute) {
|
|
|
|
$fullLink = Controller::join_links($entity->Link($this->getVersion(), $this->lang), $link);
|
|
|
|
} else {
|
|
|
|
$fullLink = Controller::join_links($entity->getRelativeLink($this->getVersion(), $this->lang), $link);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fullLink;
|
2010-12-21 10:42:44 +01:00
|
|
|
}
|
|
|
|
|
2011-01-13 09:27:25 +01:00
|
|
|
/**
|
2011-07-01 08:49:31 +02:00
|
|
|
* Relative to the module base, not the webroot.
|
2011-01-13 09:27:25 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-09-06 01:13:12 +02:00
|
|
|
public function getRelativeLink() {
|
2011-01-13 09:27:25 +01:00
|
|
|
$link = rtrim(DocumentationService::trim_extension_off($this->getRelativePath()), '/');
|
|
|
|
|
|
|
|
// folders should have a / on them. Looks nicer
|
|
|
|
try {
|
|
|
|
if(is_dir($this->getPath())) $link .= '/';
|
|
|
|
} catch (Exception $e) {};
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
function getLang() {
|
|
|
|
return $this->lang;
|
|
|
|
}
|
|
|
|
|
2010-10-21 22:27:23 +02:00
|
|
|
function setLang($lang) {
|
|
|
|
$this->lang = $lang;
|
|
|
|
}
|
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
function getVersion() {
|
2011-07-08 04:42:52 +02:00
|
|
|
return $this->version ? $this->version : $this->entity->getStableVersion();
|
2010-08-01 06:46:32 +02:00
|
|
|
}
|
2010-10-21 22:27:23 +02:00
|
|
|
|
|
|
|
function setVersion($version) {
|
|
|
|
$this->version = $version;
|
|
|
|
}
|
2010-12-21 10:42:44 +01:00
|
|
|
|
|
|
|
function setTitle($title) {
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTitle() {
|
|
|
|
return $this->title;
|
|
|
|
}
|
2011-07-01 03:19:35 +02:00
|
|
|
|
2011-01-14 04:29:14 +01:00
|
|
|
/**
|
|
|
|
* Set a variable from the metadata field on this class
|
|
|
|
*
|
2012-04-14 07:00:22 +02:00
|
|
|
* @param string key
|
2011-01-14 04:29:14 +01:00
|
|
|
* @param mixed value
|
|
|
|
*/
|
|
|
|
public function setMetaData($key, $value) {
|
|
|
|
$this->$key = $value;
|
|
|
|
}
|
|
|
|
|
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-06 01:13:12 +02:00
|
|
|
public function getFilename() {
|
2010-12-22 09:21:49 +01:00
|
|
|
$path = rtrim($this->relativePath, '/');
|
|
|
|
|
|
|
|
try {
|
|
|
|
return (is_dir($this->getPath())) ? $path . '/' : $path;
|
|
|
|
}
|
2014-09-06 01:13:12 +02:00
|
|
|
catch (Exception $e) {
|
|
|
|
|
|
|
|
}
|
2010-12-22 09:21:49 +01:00
|
|
|
|
|
|
|
return $path;
|
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 {
|
2010-12-21 11:54:11 +01:00
|
|
|
$path = $this->getPath(true);
|
|
|
|
|
|
|
|
if($path) {
|
2011-07-04 06:58:15 +02:00
|
|
|
$ext = $this->getExtension();
|
2013-05-22 18:35:26 +02:00
|
|
|
|
|
|
|
if(empty($ext) || DocumentationService::is_valid_extension($ext)) {
|
|
|
|
if ($md = file_get_contents($path)) {
|
2013-05-22 21:35:03 +02:00
|
|
|
if ($this->title != 'Index') $this->getMetadataFromComments($md, $removeMetaData);
|
2013-05-22 18:35:26 +02:00
|
|
|
}
|
|
|
|
return $md;
|
|
|
|
}
|
2010-12-21 11:54:11 +01:00
|
|
|
}
|
2010-10-22 03:08:44 +02:00
|
|
|
}
|
|
|
|
catch(InvalidArgumentException $e) {}
|
|
|
|
|
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-06 01:13:12 +02:00
|
|
|
public function getHTML($version, $lang = 'en') {
|
2011-07-01 03:19:35 +02:00
|
|
|
return DocumentationParser::parse($this, $this->entity->getRelativeLink($version, $lang));
|
2010-08-01 06:46:32 +02:00
|
|
|
}
|
2013-05-22 18:35:26 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-22 21:35:03 +02:00
|
|
|
* get metadata from the first html block in the page, then remove the
|
|
|
|
* 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
|
|
|
*/
|
2013-05-22 21:35:03 +02:00
|
|
|
public function getMetadataFromComments(&$md, $removeMetaData = false) {
|
2013-05-22 18:35:26 +02:00
|
|
|
if($md && DocumentationService::meta_comments_enabled()) {
|
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);
|
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)) {
|
|
|
|
$this->setMetaData($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
|
|
|
}
|
|
|
|
// optionally remove the metadata block (only on the page that is displayed)
|
|
|
|
if ($metaDataFound && $removeMetaData) {
|
|
|
|
$md = preg_replace($extPattern, '', $md);
|
|
|
|
}
|
2013-05-22 18:35:26 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-06 01:13:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next page. Either retrieves the sibling of the current page
|
|
|
|
* or return the next sibling of the parent page.
|
|
|
|
*
|
|
|
|
* @return DocumentationPage
|
|
|
|
*/
|
|
|
|
public function getNextPage() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the previous page. Either returns the previous sibling or the
|
|
|
|
* parent of this page
|
|
|
|
*
|
|
|
|
* @return DocumentationPage
|
|
|
|
*/
|
|
|
|
public function getPreviousPage() {
|
|
|
|
|
|
|
|
}
|
2010-08-01 06:46:32 +02:00
|
|
|
}
|