Add Pico::getPageId()

This commit is contained in:
Daniel Rudolf 2017-10-08 00:36:14 +02:00
parent 7087573aed
commit 9a9872fe8e
No known key found for this signature in database
GPG key ID: A061F02CD8DE4538

View file

@ -1676,17 +1676,8 @@ class Pico
*/ */
protected function discoverCurrentPage() protected function discoverCurrentPage()
{ {
$contentDir = $this->getConfig('content_dir'); $currentPageId = $this->getPageId($this->requestFile);
$contentDirLength = strlen($contentDir); if ($currentPageId && isset($this->pages[$currentPageId])) {
// the requested file is not in the regular content directory, therefore its ID
// isn't specified and it's impossible to determine the current page automatically
if (substr($this->requestFile, 0, $contentDirLength) !== $contentDir) {
return;
}
$currentPageId = substr($this->requestFile, $contentDirLength, -strlen($this->getConfig('content_ext')));
if (isset($this->pages[$currentPageId])) {
$this->currentPage = &$this->pages[$currentPageId]; $this->currentPage = &$this->pages[$currentPageId];
$this->previousPage = &$this->pages[$currentPageId]['previous_page']; $this->previousPage = &$this->pages[$currentPageId]['previous_page'];
$this->nextPage = &$this->pages[$currentPageId]['next_page']; $this->nextPage = &$this->pages[$currentPageId]['next_page'];
@ -1930,6 +1921,30 @@ class Pico
} }
} }
/**
* Returns the page ID of a given content file
*
* @param string $path path to the content file
* @return string|null either the corresponding page ID or null
*/
public function getPageId($path)
{
$contentDir = $this->getConfig('content_dir');
$contentDirLength = strlen($contentDir);
$contentExt = $this->getConfig('content_ext');
$contentExtLength = strlen($contentExt);
if (substr($path, 0, $contentDirLength) !== $contentDir) {
return null;
}
if (substr($path, -$contentExtLength) !== $contentExt) {
return null;
}
return substr($path, $contentDirLength, -$contentExtLength) ?: null;
}
/** /**
* Returns the URL of the themes folder of this Pico instance * Returns the URL of the themes folder of this Pico instance
* *