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
|
|
|
/**
|
2010-12-21 10:42:44 +01:00
|
|
|
* A specific page within a {@link DocumentationEntity}. Maps 1 to 1 to a file on the
|
|
|
|
* filesystem.
|
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()}
|
|
|
|
*
|
2010-08-01 06:46:32 +02:00
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $relativePath;
|
|
|
|
|
2010-10-21 22:27:23 +02:00
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
2010-08-01 06:46:32 +02:00
|
|
|
protected $version;
|
|
|
|
|
2012-11-08 17:20:55 +01:00
|
|
|
/**
|
|
|
|
* @var Boolean
|
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
protected $isFolder = false;
|
2012-11-08 17:20:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Boolean
|
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
public function setIsFolder($isFolder = false) {
|
2012-11-08 17:20:55 +01:00
|
|
|
$this->IsFolder = $isFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
2012-11-09 12:36:46 +01:00
|
|
|
public function getIsFolder($isFolder = false) {
|
2012-11-08 17:20:55 +01:00
|
|
|
return $this->IsFolder;
|
|
|
|
}
|
2012-11-09 12:36:46 +01:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function getExtension() {
|
|
|
|
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
|
|
|
*/
|
2010-12-21 11:54:11 +01:00
|
|
|
function getPath($defaultFile = false) {
|
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
|
|
|
|
|
|
|
if(!is_dir($path)) $path = realpath($path);
|
|
|
|
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
|
|
|
*/
|
|
|
|
function getBreadcrumbTitle($divider = ' - ') {
|
|
|
|
$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
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-04-14 07:00:22 +02:00
|
|
|
function getLink() {
|
2010-12-22 09:21:49 +01:00
|
|
|
if($entity = $this->getEntity()) {
|
2011-07-01 03:19:35 +02:00
|
|
|
$link = Controller::join_links($entity->Link($this->getVersion(), $this->lang), $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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $link;
|
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
|
|
|
|
*/
|
|
|
|
function getRelativeLink() {
|
|
|
|
$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
|
|
|
*/
|
2010-12-22 09:21:49 +01:00
|
|
|
function getFilename() {
|
|
|
|
$path = rtrim($this->relativePath, '/');
|
|
|
|
|
|
|
|
try {
|
|
|
|
return (is_dir($this->getPath())) ? $path . '/' : $path;
|
|
|
|
}
|
|
|
|
catch (Exception $e) {}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
2011-07-04 06:58:15 +02:00
|
|
|
* Return the raw markdown for a given documentation page. Will throw
|
|
|
|
* an error if the path isn't a file.
|
2011-07-01 03:19:35 +02:00
|
|
|
*
|
2011-07-04 06:58:15 +02:00
|
|
|
* Will return empty if the type is not readable
|
|
|
|
*
|
|
|
|
* @return string
|
2010-08-01 06:46:32 +02:00
|
|
|
*/
|
|
|
|
function getMarkdown() {
|
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();
|
|
|
|
|
|
|
|
if(DocumentationService::is_valid_extension($ext)) {
|
2012-04-14 07:00:22 +02:00
|
|
|
return file_get_contents($path);
|
2011-07-04 06:58:15 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-01 03:19:35 +02:00
|
|
|
* Parse a file (with a lang and a version).
|
|
|
|
*
|
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
|
|
|
*/
|
2011-07-01 03:19:35 +02:00
|
|
|
function getHTML($version, $lang = 'en') {
|
|
|
|
return DocumentationParser::parse($this, $this->entity->getRelativeLink($version, $lang));
|
2010-08-01 06:46:32 +02:00
|
|
|
}
|
|
|
|
}
|