Example #1
0
 public function addPage(IPage $page)
 {
     if (!$this->enabled) {
         return;
     }
     $this->pages[$page->getUri()] = $page;
     if ($page->isLoaded()) {
         $timestamp = microtime();
         $this->loadedTimes[$page->getUri()] = $timestamp;
         $this->checkLimits($page);
     }
     $page->addObserver($this);
 }
 public function addPage(IPage $page)
 {
     if (!$this->enabled) {
         return;
     }
     $this->pages[$page->getUri()] = $page;
 }
Example #3
0
 /**
  * Gets the page's data for page rendering.
  *
  * It's better to call IPage::getData, which calls this function, because it
  * will also cache the results. It's useful for example when pagination
  * results needs to be re-used.
  */
 public static function getPageData(IPage $page)
 {
     $pieCrust = $page->getApp();
     $paginator = new Paginator($page);
     $assetor = new Assetor($page);
     $linker = new Linker($page);
     if ($page->getPaginationDataSource() != null) {
         $paginator->setPaginationDataSource($page->getPaginationDataSource());
     }
     $data = array('page' => $page->getConfig()->get(), 'asset' => $assetor, 'pagination' => $paginator, 'link' => $linker);
     $data['page']['url'] = PieCrustHelper::formatUri($pieCrust, $page->getUri());
     $data['page']['slug'] = $page->getUri();
     $data['page']['timestamp'] = $page->getDate();
     $dateFormat = PageHelper::getConfigValueUnchecked($page, 'date_format', $page->getConfig()->getValueUnchecked('blog'));
     $data['page']['date'] = date($dateFormat, $page->getDate());
     switch ($page->getPageType()) {
         case IPage::TYPE_TAG:
             if (is_array($page->getPageKey())) {
                 $data['tag'] = implode(' + ', $page->getPageKey());
             } else {
                 $data['tag'] = $page->getPageKey();
             }
             break;
         case IPage::TYPE_CATEGORY:
             $data['category'] = $page->getPageKey();
             break;
     }
     $extraData = $page->getExtraPageData();
     if ($extraData) {
         if (is_array($extraData)) {
             $data = Configuration::mergeArrays($data, $extraData);
         } else {
             $data['extra'] = $extraData;
         }
     }
     return $data;
 }
Example #4
0
 protected static function buildUrlBase(IPage $page, $assetUrlBaseRemap)
 {
     $siteRoot = $page->getApp()->getConfig()->getValueUnchecked('site/root');
     $relativePath = str_replace('\\', '/', PieCrustHelper::getRelativePath($page->getApp(), $page->getPath(), true));
     $uri = $page->getUri();
     $prettyUrls = PageHelper::getConfigValue($page, 'pretty_urls', 'site');
     if (!$prettyUrls) {
         // Remove the extension from the URI (if any), because without 'pretty URLs',
         // we want to copy assets to a directory named after the page's filename
         // (without the extension). See `PageBaker` for more information.
         $uriInfo = pathinfo($uri);
         $uri = $uriInfo['dirname'];
         if ($uri == '.') {
             $uri = '';
         } else {
             $uri .= '/';
         }
         $uri .= $uriInfo['filename'];
     }
     $replacements = array('%site_root%' => $siteRoot, '%path%' => $relativePath, '%uri%' => $uri);
     return str_replace(array_keys($replacements), array_values($replacements), $assetUrlBaseRemap);
 }
Example #5
0
 /**
  * Bakes the given page. Additional template data can be provided, along with
  * a specific set of posts for the pagination data.
  */
 public function bake(IPage $page, array $extraData = null)
 {
     $didBake = false;
     try {
         $this->bakedFiles = array();
         $this->paginationDataAccessed = false;
         $this->logger->debug("Baking '{$page->getUri()}'...");
         $pageRenderer = new PageRenderer($page);
         $hasMorePages = true;
         while ($hasMorePages) {
             $didBakeThisOne = $this->bakeSinglePage($pageRenderer, $extraData);
             $didBake |= $didBakeThisOne;
             if (!$didBakeThisOne) {
                 break;
             }
             $data = $page->getPageData();
             if ($data and isset($data['pagination'])) {
                 $paginator = $data['pagination'];
                 $hasMorePages = ($paginator->wasPaginationDataAccessed() and $paginator->hasMorePages());
                 if ($hasMorePages) {
                     $page->setPageNumber($page->getPageNumber() + 1);
                     // setPageNumber() resets the page's data, so when we
                     // enter bakeSinglePage again in the next loop, we have
                     // to re-set the extraData and all other stuff.
                 }
             }
         }
     } catch (Exception $e) {
         $pageRelativePath = PageHelper::getRelativePath($page);
         throw new PieCrustException("Error baking page '{$pageRelativePath}' (p{$page->getPageNumber()})", 0, $e);
     }
     // Record our work.
     if ($this->bakeRecord) {
         $this->bakeRecord->addPageEntry($page, $didBake ? $this : null);
     }
     return $didBake;
 }
Example #6
0
 /**
  * Gets the page's data for page rendering.
  *
  * It's better to call IPage::getData, which calls this function, because it
  * will also cache the results. It's useful for example when pagination
  * results needs to be re-used.
  */
 public static function getPageData(IPage $page)
 {
     $pieCrust = $page->getApp();
     $paginator = new Paginator($page);
     $assetor = new Assetor($page);
     $linker = new Linker($page);
     $recursiveLinker = new RecursiveLinkerIterator($linker);
     if ($page->getPaginationDataSource() != null) {
         $paginator->setPaginationDataSource($page->getPaginationDataSource());
     }
     $data = array('page' => $page->getConfig()->get(), 'assets' => $assetor, 'pagination' => $paginator, 'siblings' => $linker, 'family' => $recursiveLinker);
     $data['page']['url'] = PieCrustHelper::formatUri($pieCrust, $page->getUri());
     $data['page']['slug'] = $page->getUri();
     $data['page']['timestamp'] = $page->getDate(true);
     $dateFormat = PageHelper::getConfigValueUnchecked($page, 'date_format', $page->getConfig()->getValueUnchecked('blog'));
     $data['page']['date'] = date($dateFormat, $page->getDate(true));
     switch ($page->getPageType()) {
         case IPage::TYPE_TAG:
             if (is_array($page->getPageKey())) {
                 $data['tag'] = implode(' + ', $page->getPageKey());
             } else {
                 $data['tag'] = $page->getPageKey();
             }
             if (strpos($data['tag'], '-') >= 0) {
                 // The tag may have been slugified. Let's cheat a bit by looking at
                 // the first tag that matches in the first pagination post, and
                 // using that instead.
                 $paginationPosts = $paginator->posts();
                 if (count($paginationPosts) > 0) {
                     $firstPost = $paginationPosts[0];
                     $firstPostTags = $firstPost['tags'];
                     if (!is_array($firstPostTags)) {
                         $firstPostTags = array($firstPostTags);
                     }
                     $flags = $pieCrust->getConfig()->getValue('site/slugify_flags');
                     if (is_array($page->getPageKey())) {
                         $pageKey = $page->getPageKey();
                         foreach ($firstPostTags as $t) {
                             $st = UriBuilder::slugify($t, $flags);
                             foreach ($pageKey as &$pk) {
                                 if ($st == $pk) {
                                     $pk = $t;
                                     break;
                                 }
                             }
                         }
                         if ($page->getPageKey() == null) {
                             $page->setPageKey($pageKey);
                         }
                         $data['tag'] = implode(' + ', $pageKey);
                     } else {
                         foreach ($firstPostTags as $t) {
                             if (UriBuilder::slugify($t, $flags) == $data['tag']) {
                                 if ($page->getPageKey() == null) {
                                     $page->setPageKey($t);
                                 }
                                 $data['tag'] = $t;
                                 break;
                             }
                         }
                     }
                 }
             }
             break;
         case IPage::TYPE_CATEGORY:
             $data['category'] = $page->getPageKey();
             if (strpos($page->getPageKey(), '-') >= 0) {
                 // Same remark as for tags.
                 $paginationPosts = $paginator->posts();
                 if (count($paginationPosts) > 0) {
                     $firstPost = $paginationPosts[0];
                     if ($page->getPageKey() == null) {
                         $page->setPageKey($firstPost['category']);
                     }
                     $data['category'] = $firstPost['category'];
                 }
             }
             break;
     }
     $extraData = $page->getExtraPageData();
     if ($extraData) {
         if (is_array($extraData)) {
             $data = Configuration::mergeArrays($data, $extraData);
         } else {
             $data['extra'] = $extraData;
         }
     }
     return $data;
 }
Example #7
0
 /**
  * Bakes the given page. Additional template data can be provided, along with
  * a specific set of posts for the pagination data.
  */
 public function bake(IPage $page, array $extraData = null)
 {
     $didBake = false;
     try {
         $this->bakedFiles = array();
         $this->paginationDataAccessed = false;
         $pageRenderer = new PageRenderer($page);
         $hasMorePages = true;
         while ($hasMorePages) {
             $didBake |= $this->bakeSinglePage($pageRenderer, $extraData);
             $data = $page->getPageData();
             if ($data and isset($data['pagination'])) {
                 $paginator = $data['pagination'];
                 $hasMorePages = ($paginator->wasPaginationDataAccessed() and $paginator->hasMorePages());
                 if ($hasMorePages) {
                     $page->setPageNumber($page->getPageNumber() + 1);
                     // setPageNumber() resets the page's data, so when we
                     // enter bakeSinglePage again in the next loop, we have
                     // to re-set the extraData and all other stuff.
                 }
             }
         }
     } catch (Exception $e) {
         throw new PieCrustException("Error baking page '{$page->getUri()}' (p{$page->getPageNumber()}): {$e->getMessage()}", 0, $e);
     }
     return $didBake;
 }