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); } } $result->command->args['pattern'] = $pattern; $foundAny = false; // Find pages. if ($returnAllTypes or $result->command->options['pages']) { $pages = PageHelper::getPages($pieCrust); $foundAny |= $this->findPages($context, $pages); } // Find posts. 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 = PageHelper::getPosts($pieCrust, $blogKey); $pagesIterator = new \ArrayIterator($pages); $sorter = new DateSortIterator($pagesIterator); $pages = iterator_to_array($sorter); $foundAny |= $this->findPages($context, $pages, $blogKey); } } // Find templates. if ($returnAllTypes or $result->command->options['templates']) { $templatesDirs = $pieCrust->getTemplatesDirs(); foreach ($templatesDirs as $dir) { $foundAny |= $this->findTemplates($context, $dir); } } if (!$foundAny) { $pattern = $result->command->args['pattern']; $logger->info("No match found for '{$pattern}'."); } return 0; }
protected function bakePosts() { if ($this->bakeRecord == null) { throw new PieCrustException("Can't bake posts without a bake-record active."); } $blogKeys = $this->pieCrust->getConfig()->getValue('site/blogs'); foreach ($blogKeys as $blogKey) { $posts = PageHelper::getPosts($this->pieCrust, $blogKey); foreach ($posts as $post) { $this->bakePost($post); } } }
protected function bakePosts() { $blogKeys = $this->pieCrust->getConfig()->getValue('site/blogs'); foreach ($blogKeys as $blogKey) { $posts = PageHelper::getPosts($this->pieCrust, $blogKey); foreach ($posts as $post) { $this->bakePost($post); } } }
protected function ensureLoaded() { if ($this->values != null) { return; } // Gather all posts sorted by the property we want. $dataSources = array(); $posts = PageHelper::getPosts($this->page->getApp(), $this->blogKey); foreach ($posts as $post) { $this->addPageValue($post, $dataSources); } // Now for each property bucket, create a pagination iterator. $this->values = array(); foreach ($dataSources as $property => $dataSource) { $this->values[$property] = new PagePropertyData($this->page, $this->blogKey, $property, $dataSource); } ksort($this->values); }
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; }
protected function ensureMonths() { if ($this->months) { return; } // Get all the blog posts. $posts = PageHelper::getPosts($this->page->getApp(), $this->blogKey); // Sort them by month. $monthsInfos = array(); $currentMonthAndYear = null; foreach ($posts as $post) { $timestamp = $post->getDate(); $pageMonthAndYear = date('F Y', $timestamp); if (!isset($monthsInfos[$pageMonthAndYear])) { $pageYear = intval(date('Y', $timestamp)); $pageMonth = intval(date('m', $timestamp)); $monthsInfos[$pageMonthAndYear] = array('name' => $pageMonthAndYear, 'timestamp' => mktime(0, 0, 0, $pageMonth, 1, $pageYear), 'data_source' => array()); } $monthsInfos[$pageMonthAndYear]['data_source'][] = $post; } // For each month, create the data class. $this->months = array(); foreach ($monthsInfos as $month => $monthInfo) { $this->months[$month] = new PageTimeData($this->page, $this->blogKey, $monthInfo['name'], $monthInfo['timestamp'], $monthInfo['data_source']); } // Sort the months in inverse chronological order. uasort($this->months, array('PieCrust\\Data\\BlogData', 'sortByReverseTimestamp')); }
protected function ensureMonths() { if ($this->months) { return; } $blogPosts = PageHelper::getPosts($this->page->getApp(), $this->blogKey); $this->months = array(); $currentMonthAndYear = null; foreach ($blogPosts as $post) { $timestamp = $post->getDate(); $pageMonthAndYear = date('Y m', $timestamp); if ($currentMonthAndYear == null or $pageMonthAndYear != $currentMonthAndYear) { $pageYear = intval(date('Y', $timestamp)); $pageMonth = intval(date('m', $timestamp)); $this->months[$pageMonthAndYear] = array('name' => date('F Y', $timestamp), 'timestamp' => mktime(0, 0, 0, $pageMonth, 1, $pageYear), 'posts' => array()); $currentMonthAndYear = $pageMonthAndYear; } $this->months[$currentMonthAndYear]['posts'][] = new PaginationData($post); } ksort($this->months); }
protected function ensurePaginationData() { if ($this->postsIterator != null) { return; } // Get the post infos. $posts = $this->dataSource; $blogKey = $this->page->getConfig()->getValue('blog'); if ($posts === null) { $posts = PageHelper::getPosts($this->page->getApp(), $blogKey); } // Create the pagination iterator. $this->postsIterator = new PageIterator($this->page->getApp(), $blogKey, $posts); // Set our current page. $this->postsIterator->setCurrentPage($this->page); // Add the filters for the current page. $postsFilter = $this->getPaginationFilter(); if ($postsFilter->hasClauses()) { $this->postsIterator->setFilter($postsFilter); } // If the `posts_per_page` setting is valid, paginate accordingly. $postsPerPage = $this->posts_per_page(); if (is_int($postsPerPage) && $postsPerPage > 0) { // Limit to posts that should be on this page. $offset = ($this->page->getPageNumber() - 1) * $postsPerPage; $this->postsIterator->setPagination($offset, $postsPerPage); } $this->postsIterator->setLocked(); }