From 9a9872fe8e321747f09f5e64f38d613242745529 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Sun, 8 Oct 2017 00:36:14 +0200 Subject: [PATCH] Add Pico::getPageId() --- lib/Pico.php | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/Pico.php b/lib/Pico.php index 89e799b..24a82c8 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -1676,17 +1676,8 @@ class Pico */ protected function discoverCurrentPage() { - $contentDir = $this->getConfig('content_dir'); - $contentDirLength = strlen($contentDir); - - // 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])) { + $currentPageId = $this->getPageId($this->requestFile); + if ($currentPageId && isset($this->pages[$currentPageId])) { $this->currentPage = &$this->pages[$currentPageId]; $this->previousPage = &$this->pages[$currentPageId]['previous_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 *