Example #1
0
 /**
  * Returns a validated version of the given site configuration.
  *
  * This is exposed as a public static function for convenience (unit tests,
  * etc.)
  */
 public static function getValidatedConfig(IPage $page, $config)
 {
     if (!$config) {
         $config = array();
     }
     // Add the default page config values.
     $pieCrustConfig = $page->getApp()->getConfig();
     $blogKeys = $pieCrustConfig->getValueUnchecked('site/blogs');
     $layoutName = PieCrustDefaults::DEFAULT_PAGE_TEMPLATE_NAME;
     if (PageHelper::isPost($page)) {
         $layoutName = PieCrustDefaults::DEFAULT_POST_TEMPLATE_NAME;
         if ($page->getBlogKey()) {
             // If this is a post in a multi-blog environment, make it use
             // the `<blogname>/post.html` layout first by default, and
             // then fallback on `post.html` if that doesn't exist.
             $layoutName = $page->getBlogKey() . '/' . PieCrustDefaults::DEFAULT_POST_TEMPLATE_NAME . ',' . $layoutName;
         }
     }
     $validatedConfig = array_merge(array('layout' => $layoutName, 'format' => $pieCrustConfig->getValueUnchecked('site/default_format'), 'template_engine' => $pieCrustConfig->getValueUnchecked('site/default_template_engine'), 'content_type' => 'html', 'title' => 'Untitled Page', 'blog' => $page->getBlogKey() != null ? $page->getBlogKey() : $blogKeys[0], 'segments' => array()), $config);
     // Detect common problems.
     if (isset($validatedConfig['category'])) {
         if (is_array($validatedConfig['category'])) {
             throw new PieCrustException("Page '{$page->getUri()}': `category` is an array -- it must be a string. For multiple values, use `tags` instead.");
         }
     }
     if (isset($validatedConfig['tags'])) {
         if (!is_array($validatedConfig['tags'])) {
             $validatedConfig['tags'] = array($validatedConfig['tags']);
         }
     }
     if (isset($validatedConfig['single_page'])) {
         throw new PieCrustException("Page '{$page->getUri()}': `single_page` has been deprecated. Use `blog.posts` with some limits if you want a single page showing the most recent posts.");
     }
     return $validatedConfig;
 }
Example #2
0
 public function __construct(IPage $page, $blogKey, $timeValue, $timestamp, array $dataSource)
 {
     $this->timeValue = $timeValue;
     $this->timestamp = $timestamp;
     $this->posts = new PageIterator($page->getApp(), $blogKey, $dataSource);
     $this->posts->setCurrentPage($page);
 }
 public function addPage(IPage $page)
 {
     if (!$this->enabled) {
         return;
     }
     $this->pages[$page->getUri()] = $page;
 }
Example #4
0
 public function __construct(IPage $page, $blogKey, $propertyValue, array $dataSource)
 {
     $this->propertyValue = $propertyValue;
     $this->posts = new PageIterator($page->getApp(), $blogKey, $dataSource);
     $this->posts->setCurrentPage($page);
     $this->postCount = count($dataSource);
     // Backwards compatibility (should use posts.count)
 }
Example #5
0
 /**
  * Creates a new instance of Linker.
  */
 public function __construct(IPage $page, $dir = null)
 {
     $this->page = $page;
     $this->baseDir = $dir != null ? $dir : dirname($page->getPath());
     $this->baseDir = rtrim($this->baseDir, '/\\') . '/';
     $this->sortByName = null;
     $this->sortByReverse = false;
 }
Example #6
0
 public function postMatches(IPage $post)
 {
     $actualValue = $post->getConfig()->getValue($this->settingName);
     if ($this->coerceFunc != null) {
         $coerceFunc = $this->coerceFunc;
         $actualValue = $coerceFunc($actualValue);
     }
     return $actualValue != null && $actualValue == $this->settingValue;
 }
Example #7
0
 /**
  * Creates a new instance of PageLoader.
  */
 public function __construct(IPage $page)
 {
     $this->page = $page;
     $this->cache = null;
     if ($page->getApp()->isCachingEnabled()) {
         $this->cache = new Cache($page->getApp()->getCacheDir() . 'pages_r');
     }
     $this->pageData = null;
 }
 /**
  * Returns a validated version of the given site configuration.
  *
  * This is exposed as a public static function for convenience (unit tests,
  * etc.)
  */
 public static function getValidatedConfig(IPage $page, $config)
 {
     if (!$config) {
         $config = array();
     }
     // Add the default page config values.
     $pieCrustConfig = $page->getApp()->getConfig();
     $blogKeys = $pieCrustConfig->getValueUnchecked('site/blogs');
     $validatedConfig = array_merge(array('layout' => PageHelper::isPost($page) ? PieCrustDefaults::DEFAULT_POST_TEMPLATE_NAME : PieCrustDefaults::DEFAULT_PAGE_TEMPLATE_NAME, 'format' => $pieCrustConfig->getValueUnchecked('site/default_format'), 'template_engine' => $pieCrustConfig->getValueUnchecked('site/default_template_engine'), 'content_type' => 'html', 'title' => 'Untitled Page', 'blog' => $page->getBlogKey() != null ? $page->getBlogKey() : $blogKeys[0], 'segments' => array()), $config);
     return $validatedConfig;
 }
Example #9
0
 public function postMatches(IPage $post)
 {
     $actualValue = $post->getConfig()->getValue($this->settingName);
     if ($actualValue == null || !is_array($actualValue)) {
         return false;
     }
     if ($this->coerceFunc != null) {
         $coerceFunc = $this->coerceFunc;
         $actualValue = array_map($coerceFunc, $actualValue);
     }
     return in_array($this->settingValue, $actualValue);
 }
Example #10
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);
 }
Example #11
0
 /**
  * Creates a new instance of Linker.
  */
 public function __construct(IPage $page, $dir = null)
 {
     $this->page = $page;
     if ($dir != null) {
         $this->baseDir = $dir;
         $this->selfName = null;
     } else {
         $this->baseDir = dirname($page->getPath()) . '/';
         $this->selfName = basename($page->getPath());
     }
     $this->sortByName = null;
     $this->sortByReverse = false;
 }
Example #12
0
 public function initialize(IPage $page, $baker)
 {
     $this->path = $page->getPath();
     $this->pageType = $page->getPageType();
     $this->blogKey = $page->getBlogKey();
     $this->pageKey = $page->getPageKey();
     $this->taxonomy = array();
     $this->usedTaxonomyCombinations = array();
     $this->usedPages = false;
     $this->usedPosts = array();
     $this->outputs = array();
     if ($baker) {
         $tags = $page->getConfig()->getValue('tags');
         if ($tags) {
             $this->taxonomy['tags'] = $tags;
         }
         $category = $page->getConfig()->getValue('category');
         if ($category) {
             $this->taxonomy['category'] = $category;
         }
         $collector = $page->getApp()->getEnvironment()->getLinkCollector();
         if ($collector) {
             $tagCombinations = $collector->getAllTagCombinations();
             if ($tagCombinations) {
                 $this->usedTaxonomyCombinations['tags'] = $tagCombinations;
             }
             $collector->clearAllTagCombinations();
         }
         // TODO: remember posts used by blog.
         $this->usedPosts = $baker->wasPaginationDataAccessed();
         $this->outputs = $baker->getBakedFiles();
     }
 }
Example #13
0
 public function addPageClauses(IPage $page, array $userFilterInfo = null)
 {
     // If the current page is a tag/category page, add filtering
     // for that.
     $pageClause = null;
     $pieCrust = $page->getApp();
     $flags = $pieCrust->getConfig()->getValue('site/slugify_flags');
     switch ($page->getPageType()) {
         case IPage::TYPE_TAG:
             $pageKey = $page->getPageKey();
             if (is_array($pageKey)) {
                 $pageClause = new AndBooleanClause();
                 foreach ($pageKey as $k) {
                     $pageClause->addClause(new HasFilterClause('tags', $k, function ($t) use($flags) {
                         return UriBuilder::slugify($t, $flags);
                     }));
                 }
             } else {
                 $pageClause = new HasFilterClause('tags', $pageKey, function ($t) use($flags) {
                     return UriBuilder::slugify($t, $flags);
                 });
             }
             break;
         case IPage::TYPE_CATEGORY:
             $pageClause = new IsFilterClause('category', $page->getPageKey(), function ($c) use($flags) {
                 return UriBuilder::slugify($c, $flags);
             });
             break;
     }
     if ($pageClause != null) {
         // Combine the default page filters with some user filters,
         // if any.
         if ($userFilterInfo != null) {
             $combinedClause = new AndBooleanClause();
             $combinedClause->addClause($pageClause);
             $this->addClausesRecursive($userFilterInfo, $combinedClause);
             $this->addClause($combinedClause);
         } else {
             $this->addClause($pageClause);
         }
     }
 }
 public function addPageClauses(IPage $page)
 {
     // If the current page is a tag/category page, add filtering
     // for that.
     switch ($page->getPageType()) {
         case IPage::TYPE_TAG:
             $pageKey = $page->getPageKey();
             if (is_array($pageKey)) {
                 $wrapper = new AndBooleanClause();
                 foreach ($pageKey as $k) {
                     $wrapper->addClause(new HasFilterClause('tags', $k));
                 }
                 $this->addClause($wrapper);
             } else {
                 $this->addClause(new HasFilterClause('tags', $pageKey));
             }
             break;
         case IPage::TYPE_CATEGORY:
             $this->addClause(new IsFilterClause('category', $page->getPageKey()));
             break;
     }
 }
Example #15
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;
 }
 protected function addPageValue(IPage $page, &$dataSources)
 {
     $propertyValues = $page->getConfig()->getValue($this->propertyName);
     if ($propertyValues) {
         if (!is_array($propertyValues)) {
             $propertyValues = array($propertyValues);
         }
         foreach ($propertyValues as $v) {
             if (!isset($dataSources[$v])) {
                 $dataSources[$v] = array();
             }
             $dataSources[$v][] = $page;
         }
     }
 }
Example #17
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;
 }
Example #18
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 #19
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;
 }
 public function postMatches(IPage $post)
 {
     $actualValue = $post->getConfig()->getValue($this->settingName);
     return $actualValue != null && $actualValue == $this->settingValue;
 }
Example #21
0
 public static function mergeProviderData(IPage $page, array &$data)
 {
     foreach ($page->getApp()->getPluginLoader()->getDataProviders() as $provider) {
         $providerData = $provider->getPageData($page);
         if ($providerData !== null) {
             $endPoint = $provider->getName();
             if (isset($data[$endPoint])) {
                 throw new PieCrustException("Can't load data provider: the page configuration already has a value at'{$endPoint}'.");
             }
             $data[$endPoint] = $providerData;
         }
     }
 }
Example #22
0
 /**
  * Gets whether the given page is a category listing.
  */
 public static function isCategory(IPage $page)
 {
     return $page->getPageType() == IPage::TYPE_CATEGORY;
 }