/**
  * 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;
 }
 /**
  * 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;
 }
Beispiel #3
0
 protected function ensureLinksCache()
 {
     if ($this->linksCache === null) {
         try {
             $pieCrust = $this->page->getApp();
             $pageRepository = $pieCrust->getEnvironment()->getPageRepository();
             $this->linksCache = array();
             $skipNames = array('Thumbs.db');
             $it = new FilesystemIterator($this->baseDir);
             foreach ($it as $item) {
                 $basename = $item->getBasename();
                 // Skip dot files, Thumbs.db, etc.
                 if (!$basename or $basename[0] == '.') {
                     continue;
                 }
                 if (in_array($item->getFilename(), $skipNames)) {
                     continue;
                 }
                 if ($item->isDir()) {
                     $linker = new Linker($this->page, $item->getPathname());
                     $this->linksCache[$basename . '_'] = $linker;
                     // We add '_' at the end of the directory name to avoid
                     // collisions with a possibly existing page with the same
                     // name (since we strip out the '.html' extension).
                     // This means the user must access directories with
                     // 'link.dirname_' instead of 'link.dirname' but hey, if
                     // you have a better idea, send me an email!
                 } else {
                     $path = $item->getPathname();
                     try {
                         $relativePath = PageHelper::getRelativePath($this->page);
                         $uri = UriBuilder::buildUri($relativePath);
                         // To get the link's page, we need to be careful with the case
                         // where that page is the currently rendering one. This is
                         // because it could be rendering a sub-page -- but we would be
                         // requesting the default first page, which would effectively
                         // change the page number *while* we're rendering, which leads
                         // to all kinds of bad things!
                         // TODO: obviously, there needs to be some design changes to
                         // prevent this kind of chaotic behaviour.
                         if ($path == $this->page->getPath()) {
                             $page = $this->page;
                         } else {
                             $page = $pageRepository->getOrCreatePage($uri, $path);
                         }
                         $key = str_replace('.', '_', $item->getBasename('.html'));
                         $this->linksCache[$key] = array('uri' => $uri, 'name' => $key, 'is_dir' => false, 'is_self' => $basename == $this->selfName, 'page' => new PaginationData($page));
                     } catch (Exception $e) {
                         throw new PieCrustException("Error while loading page '{$path}' for linking from '{$this->page->getUri()}': " . $e->getMessage(), 0, $e);
                     }
                 }
             }
             if ($this->sortByName) {
                 if (false === usort($this->linksCache, array($this, 'sortByCustom'))) {
                     throw new PieCrustException("Error while sorting pages with the specified setting: {$this->sortByName}");
                 }
             }
             if ($this->selfName != null) {
                 // Add special stuff only for the original Linker
                 // (the one directly created by the current page).
                 if (PageHelper::isRegular($this->page)) {
                     // Add a link to go up to the parent directory, but stay inside
                     // the app's pages directory.
                     $parentBaseDir = dirname($this->baseDir);
                     if (strlen($parentBaseDir) >= strlen($pieCrust->getPagesDir())) {
                         $linker = new Linker($this->page, dirname($this->baseDir));
                         $this->linksCache['_'] = $linker;
                     }
                 } else {
                     if (PageHelper::isPost($this->page)) {
                         // Add a link to go up to the parent directory, but stay inside
                         // the app's posts directory.
                         $parentBaseDir = dirname($this->baseDir);
                         if (strlen($parentBaseDir) >= strlen($pieCrust->getPostsDir())) {
                             $linker = new Linker($this->page, dirname($this->baseDir));
                             $this->linksCache['_'] = $linker;
                         }
                     }
                 }
                 if ($pieCrust->getPagesDir()) {
                     // Add a shortcut to the pages directory.
                     $linker = new Linker($this->page, $pieCrust->getPagesDir());
                     $this->linksCache['_pages_'] = $linker;
                 }
                 if ($pieCrust->getPostsDir()) {
                     // Add a shortcut to the posts directory.
                     $linker = new Linker($this->page, $pieCrust->getPostsDir());
                     $this->linksCache['_posts_'] = $linker;
                 }
             }
         } catch (Exception $e) {
             throw new PieCrustException("Error while building the links from page '{$this->page->getUri()}': " . $e->getMessage(), 0, $e);
         }
     }
 }
 public function run(ChefContext $context)
 {
     $logger = $context->getLog();
     $pieCrust = $context->getApp();
     $result = $context->getResult();
     // Get some options.
     $exact = $result->command->options['exact'];
     $fullPath = $result->command->options['full_path'];
     // If no type filters are given, return all types.
     $returnAllTypes = ($result->command->options['pages'] == false and $result->command->options['posts'] == false and $result->command->options['templates'] == false);
     // Validate the argument.
     $pattern = $result->command->args['pattern'];
     if ($exact) {
         // Check we have a path to match, and get its absolute value.
         if (!$pattern) {
             throw new PieCrustException("You need to specify a path when using the `--exact` option.");
         }
         $pattern = PathHelper::getAbsolutePath($pattern);
     } else {
         // If a pattern was given, get the Regex'd version.
         if ($pattern) {
             $pattern = PathHelper::globToRegex($pattern);
         }
     }
     // Get the pages and posts.
     $pages = array();
     if ($returnAllTypes or $result->command->options['pages']) {
         $pages = PageHelper::getPages($pieCrust);
     }
     if ($returnAllTypes or $result->command->options['posts']) {
         $blogKeys = $pieCrust->getConfig()->getValue('site/blogs');
         if ($result->command->options['blog']) {
             $blogKeys = array($result->command->options['blog']);
         }
         foreach ($blogKeys as $blogKey) {
             $pages = array_merge($pages, PageHelper::getPosts($pieCrust, $blogKey));
         }
     }
     // Get some other stuff.
     $returnComponents = $result->command->options['page_components'];
     // Get a regex for the posts file-naming convention.
     $fs = FileSystem::create($pieCrust);
     $pathComponentsRegex = preg_quote($fs->getPostPathFormat(), '/');
     $pathComponentsRegex = str_replace(array('%year%', '%month%', '%day%', '%slug%'), array('(\\d{4})', '(\\d{2})', '(\\d{2})', '(.+)'), $pathComponentsRegex);
     $pathComponentsRegex = '/' . $pathComponentsRegex . '/';
     // Print the matching pages.
     foreach ($pages as $page) {
         if ($result->command->options['no_special']) {
             // Skip special pages.
             if ($page->getUri() == PieCrustDefaults::CATEGORY_PAGE_NAME or $page->getUri() == PieCrustDefaults::TAG_PAGE_NAME) {
                 continue;
             }
         }
         if ($exact) {
             // Match the path exactly, or pass.
             if (str_replace('\\', '/', $pattern) != str_replace('\\', '/', $page->getPath())) {
                 continue;
             }
         } else {
             if ($pattern) {
                 // Match the regex, or pass.
                 if (!preg_match($pattern, $page->getUri())) {
                     continue;
                 }
             }
         }
         $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $page->getPath());
         if (!$fullPath) {
             $path = PieCrustHelper::getRelativePath($pieCrust, $path);
         }
         if ($returnComponents) {
             $components = array('path' => $path, 'type' => 'page', 'uri' => $page->getUri(), 'slug' => $page->getUri());
             if (PageHelper::isPost($page)) {
                 $matches = array();
                 if (preg_match($pathComponentsRegex, str_replace('\\', '/', $path), $matches) !== 1) {
                     throw new PieCrustException("Can't extract path components from path: {$path}");
                 }
                 $components['type'] = 'post';
                 $components['year'] = $matches[1];
                 $components['month'] = $matches[2];
                 $components['day'] = $matches[3];
                 $components['slug'] = $matches[4];
             }
             $str = '';
             foreach ($components as $k => $v) {
                 $str .= $k . ': ' . $v . PHP_EOL;
             }
             $logger->info($str);
         } else {
             $logger->info($path);
         }
     }
     // Get the template files and print them.
     if ($returnAllTypes or $result->command->options['templates']) {
         $templatesDirs = $pieCrust->getTemplatesDirs();
         foreach ($templatesDirs as $dir) {
             $dirIt = new \RecursiveDirectoryIterator($dir);
             $it = new \RecursiveIteratorIterator($dirIt);
             foreach ($it as $path) {
                 if ($it->isDot()) {
                     continue;
                 }
                 $relativePath = PieCrustHelper::getRelativePath($pieCrust, $path->getPathname());
                 if ($exact) {
                     // Match the path exactly, or pass.
                     if (str_replace('\\', '/', $pattern) != str_replace('\\', '/', $path->getPathname())) {
                         continue;
                     }
                 } else {
                     if ($pattern) {
                         // Match the regex, or pass.
                         if (!preg_match($pattern, $relativePath)) {
                             continue;
                         }
                     }
                 }
                 // Get the path to print.
                 $finalPath = $relativePath;
                 if ($fullPath) {
                     $finalPath = $path->getPathname();
                 }
                 $finalPath = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $finalPath);
                 // Print the information!
                 if ($returnComponents) {
                     $logger->info("path: {$finalPath}");
                     $logger->info("type: template");
                 } else {
                     $logger->info($finalPath);
                 }
             }
         }
     }
     return 0;
 }
Beispiel #5
0
 /**
  * Gets the total number of pages.
  *
  * This method is meant to be called from the layouts via the template engine.
  */
 public function total_page_count()
 {
     if (PageHelper::isPost($this->page) or $this->page->getConfig()->getValue('single_page')) {
         return 1;
     }
     $totalPostCount = $this->total_post_count();
     $postsPerPage = $this->posts_per_page();
     if (is_int($postsPerPage) && $postsPerPage > 0) {
         return ceil($totalPostCount / $postsPerPage);
     }
     return $totalPostCount;
 }
Beispiel #6
0
 private function findPages($context, $pages, $fromBlog = false)
 {
     $logger = $context->getLog();
     $pieCrust = $context->getApp();
     $result = $context->getResult();
     $rootDir = $pieCrust->getRootDir();
     $rootDir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $rootDir);
     $exact = $result->command->options['exact'];
     $pattern = $result->command->args['pattern'];
     $fullPath = $result->command->options['full_path'];
     $returnComponents = $result->command->options['page_components'];
     $foundAny = false;
     foreach ($pages as $page) {
         if ($result->command->options['no_special']) {
             // Skip special pages.
             if ($page->getUri() == PieCrustDefaults::CATEGORY_PAGE_NAME or $page->getUri() == PieCrustDefaults::TAG_PAGE_NAME) {
                 continue;
             }
         }
         if ($exact) {
             // Match the path exactly, or pass.
             if (str_replace('\\', '/', $pattern) != str_replace('\\', '/', $page->getPath())) {
                 continue;
             }
         } else {
             if ($pattern) {
                 // Match the regex, or pass.
                 if (!preg_match($pattern, $page->getUri())) {
                     continue;
                 }
             }
         }
         $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $page->getPath());
         if (!$fullPath) {
             if (substr($path, 0, strlen($rootDir)) == $rootDir) {
                 $path = PieCrustHelper::getRelativePath($pieCrust, $path);
             }
         }
         if ($returnComponents) {
             $components = array('path' => $path, 'type' => 'page', 'uri' => $page->getUri(), 'slug' => $page->getUri());
             if (PageHelper::isPost($page)) {
                 $timestamp = $page->getDate(true);
                 $components['type'] = 'post';
                 $components['year'] = date('Y', $timestamp);
                 $components['month'] = date('m', $timestamp);
                 $components['day'] = date('d', $timestamp);
                 $components['hour'] = date('H', $timestamp);
                 $components['minute'] = date('i', $timestamp);
                 $components['second'] = date('s', $timestamp);
                 $matches = array();
                 $postsPattern = UriBuilder::buildPostUriPattern($pieCrust->getConfig()->getValue($fromBlog . '/post_url'), $fromBlog);
                 if (preg_match($postsPattern, $page->getUri(), $matches)) {
                     $components['slug'] = $matches['slug'];
                 }
             }
             foreach ($components as $k => $v) {
                 $logger->info("{$k}: {$v}");
             }
             $logger->info("");
             $foundAny = true;
         } else {
             $logger->info($path);
             $foundAny = true;
         }
     }
     return $foundAny;
 }