/** * @dataProvider buildCategoryUriDataProvider */ public function testBuildCategoryUri($category, $expectedUri) { $pc = new MockPieCrust(); $pc->getConfig()->setValue('blog/category_url', '/%category%'); $uri = UriBuilder::buildCategoryUri($pc, 'blog', $category); $this->assertEquals($expectedUri, $uri); }
public function getCategoryUrl($value, $blogKey = null) { $format = $blogKey == null ? $this->categoryUrlFormat : $pieCrust->getConfig()->getValueUnchecked($blogKey . '/category_url'); return PieCrustHelper::formatUri($this->pieCrust, UriBuilder::buildCategoryUri($format, $value)); }
protected function bakeCategories() { if ($this->bakeRecord == null) { throw new PieCrustException("Can't bake categories without a bake-record active."); } $blogKeys = $this->pieCrust->getConfig()->getValueUnchecked('site/blogs'); foreach ($blogKeys as $blogKey) { // Check that there is a category listing page to bake. $prefix = ''; if ($blogKey != PieCrustDefaults::DEFAULT_BLOG_KEY) { $prefix = $blogKey . DIRECTORY_SEPARATOR; } $categoryPageName = $prefix . PieCrustDefaults::CATEGORY_PAGE_NAME . '.html'; $categoryPagePath = PathHelper::getUserOrThemeOrResPath($this->pieCrust, $categoryPageName); if ($categoryPagePath === false) { continue; } // Order categories so it looks nicer when we bake. $categoriesToBake = $this->bakeRecord->getCategoriesToBake($blogKey); sort($categoriesToBake); // Bake! $pageRepository = $this->pieCrust->getEnvironment()->getPageRepository(); foreach ($categoriesToBake as $category) { $start = microtime(true); $postInfos = $this->bakeRecord->getPostsInCategory($blogKey, $category); if (count($postInfos) > 0) { $uri = UriBuilder::buildCategoryUri($this->pieCrust->getConfig()->getValue($blogKey . '/category_url'), $category); $page = $pageRepository->getOrCreatePage($uri, $categoryPagePath, IPage::TYPE_CATEGORY, $blogKey, $category); $baker = new PageBaker($this->getBakeDir(), $this->getPageBakerParameters()); $baker->bake($page); $pageCount = $baker->getPageCount(); $this->logger->info(self::formatTimed($start, 'category:' . $category . ($pageCount > 1 ? " [{$pageCount}]" : ""))); } } } }
protected function bakeTaxonomies() { // Get some global stuff we'll need. $slugifyFlags = $this->pieCrust->getConfig()->getValue('site/slugify_flags'); $pageRepository = $this->pieCrust->getEnvironment()->getPageRepository(); // Get the taxonomies. $taxonomies = array('tags' => array('multiple' => true, 'singular' => 'tag', 'page' => PieCrustDefaults::TAG_PAGE_NAME . '.html'), 'category' => array('multiple' => false, 'page' => PieCrustDefaults::CATEGORY_PAGE_NAME . '.html')); // Get which terms we need to bake. $allDirtyTaxonomies = $this->bakeRecord->getDirtyTaxonomies($taxonomies); $allUsedCombinations = $this->bakeRecord->getUsedTaxonomyCombinations($taxonomies); // Get the taxonomy listing pages, if they exist. $taxonomyPages = array(); $blogKeys = $this->pieCrust->getConfig()->getValue('site/blogs'); foreach ($taxonomies as $name => $taxonomyMetadata) { $taxonomyPages[$name] = array(); foreach ($blogKeys as $blogKey) { $prefix = ''; if ($blogKey != PieCrustDefaults::DEFAULT_BLOG_KEY) { $prefix = $blogKey . DIRECTORY_SEPARATOR; } $termPageName = $prefix . $taxonomyMetadata['page']; $themeTermPageName = $taxonomyMetadata['page']; $termPagePath = PathHelper::getUserOrThemePath($this->pieCrust, $termPageName, $themeTermPageName); $taxonomyPages[$name][$blogKey] = $termPagePath; } } foreach ($allDirtyTaxonomies as $name => $dirtyTerms) { $taxonomyMetadata = $taxonomies[$name]; foreach ($dirtyTerms as $blogKey => $dirtyTermsForBlog) { // Check that we have a term listing page to bake. $termPagePath = $taxonomyPages[$name][$blogKey]; if (!$termPagePath) { continue; } // We have the terms that need to be rebaked. $termsToBake = $dirtyTermsForBlog; // Look at the combinations of terms we need to consider. if ($taxonomyMetadata['multiple']) { // User-specified combinations. $forcedCombinations = array(); $forcedCombinationParameters = array($name . '_combinations'); if (isset($taxonomyMetadata['singular'])) { $forcedCombinationParameters[] = $taxonomyMetadata['singular'] . '_combinations'; } foreach ($forcedCombinationParameters as $param) { if (isset($this->parameters[$param])) { $forcedCombinations = $this->parameters[$param]; if (array_key_exists($blogKey, $forcedCombinations)) { $forcedCombinations = $forcedCombinations[$blogKey]; } elseif (count($blogKeys > 1)) { $forcedCombinations = array(); } break; } } // Collected combinations in use. $usedCombinations = array(); if (isset($allUsedCombinations[$name]) && isset($allUsedCombinations[$name][$blogKey])) { $usedCombinations = $allUsedCombinations[$name][$blogKey]; } // Get all the combinations together (forced and used) and keep // those that include a term that we have to rebake. $combinations = array_merge($forcedCombinations, $usedCombinations); if ($combinations) { $combinationsToBake = array(); foreach ($combinations as $comb) { if (count(array_intersect($comb, $termsToBake)) > 0) { $combinationsToBake[] = $comb; } } $termsToBake = array_merge($termsToBake, $combinationsToBake); } } // Order terms so it looks nice when we bake. usort($termsToBake, function ($t1, $t2) { if (is_array($t1)) { $t1 = implode('+', $t1); } if (is_array($t2)) { $t2 = implode('+', $t2); } return strcmp($t1, $t2); }); // Bake! foreach ($termsToBake as $term) { $start = microtime(true); if ($taxonomyMetadata['multiple'] && is_array($term)) { $slugifiedTerm = array_map(function ($t) use($slugifyFlags) { return UriBuilder::slugify($t, $slugifyFlags); }, $term); $formattedTerm = implode('+', array_map('rawurldecode', $term)); } else { $slugifiedTerm = UriBuilder::slugify($term, $slugifyFlags); $formattedTerm = rawurldecode($term); } if ($name == 'tags') { $uri = UriBuilder::buildTagUri($this->pieCrust, $blogKey, $slugifiedTerm, false); $pageType = IPage::TYPE_TAG; } else { if ($name == 'category') { $uri = UriBuilder::buildCategoryUri($this->pieCrust, $blogKey, $slugifiedTerm, false); $pageType = IPage::TYPE_CATEGORY; } } $page = $pageRepository->getOrCreatePage($uri, $termPagePath, $pageType, $blogKey); $page->setPageKey($slugifiedTerm); $this->callAssistants('onPageBakeStart', array($page)); $baker = $this->getPageBaker(); $baker->bake($page); $this->callAssistants('onPageBakeEnd', array($page, new BakeResult(true))); $pageCount = $baker->getPageCount(); $this->logger->info(self::formatTimed($start, "{$name}:{$formattedTerm}" . ($pageCount > 1 ? " [{$pageCount}]" : ""))); } } } }
public function getCategoryUrl($value, $blogKey = null) { $blogKey = $this->getSafeBlogKey($blogKey); return PieCrustHelper::formatUri($this->pieCrust, UriBuilder::buildCategoryUri($this->pieCrust, $blogKey, $value)); }
/** * @dataProvider buildCategoryUriDataProvider */ public function testBuildCategoryUri($category, $expectedUri) { $uri = UriBuilder::buildCategoryUri('/%category%', $category); $this->assertEquals($expectedUri, $uri); }