Exemplo n.º 1
0
 public function registerTagCombination($blogKey, $tags)
 {
     if (!is_array($tags)) {
         // Temporary warning for a change in how multi-tags
         // are specified.
         $log = $this->pieCrust->getEnvironment()->getLog();
         if (strpos($tags, '/') !== false) {
             $log->warning("A link to tag {$tags} was specified in this page. " . "If this is a tag that contains a slash character ('/') then ignore this warning. " . "However, if this was intended to be a multi-tags link, you'll need to " . "now pass an array of tags like so: `{{pctagurl(['tag1', 'tag2'])}}`. " . "Your current link won't work!");
         }
         return;
     }
     if ($blogKey == null) {
         $blogKey = PieCrustDefaults::DEFAULT_BLOG_KEY;
     }
     if (!array_key_exists($blogKey, $this->tagCombinations)) {
         $this->tagCombinations[$blogKey] = array();
     }
     // Slugify tags and sort them alphabetically.
     $flags = $this->pieCrust->getConfig()->getValue('site/slugify_flags');
     $tags = array_map(function ($t) use($flags) {
         return UriBuilder::slugify($t, $flags);
     }, $tags);
     sort($tags);
     // Only add combination if it's not already there.
     if (!in_array($tags, $this->tagCombinations[$blogKey])) {
         $this->tagCombinations[$blogKey][] = $tags;
     }
 }
Exemplo n.º 2
0
 /**
  * @dataProvider tagSlugifyDataProvider
  */
 public function testTagSlugify($value, $expectedValue, $slugifyMode = null, $locale = null)
 {
     $fs = MockFileSystem::create();
     if ($slugifyMode) {
         $fs->withConfig(array('site' => array('slugify' => $slugifyMode)));
     }
     $pc = $fs->getApp();
     $flags = $pc->getConfig()->getValue('site/slugify_flags');
     $actualValue = UriBuilder::slugify($value, $flags);
     $this->assertEquals($expectedValue, $actualValue);
 }
Exemplo n.º 3
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);
         }
     }
 }
Exemplo n.º 4
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);
         }
     }
 }
Exemplo n.º 5
0
 private static function tryParseCategoryUri(IPieCrust $pieCrust, $blogKey, $uri, array &$pageInfo)
 {
     $blogKeyDir = '';
     if ($blogKey != PieCrustDefaults::DEFAULT_BLOG_KEY) {
         $blogKeyDir = $blogKey . '/';
     }
     $categoryPageName = array();
     $themeCategoryPageName = array();
     $autoFormats = $pieCrust->getConfig()->getValueUnchecked('site/auto_formats');
     foreach ($autoFormats as $ext => $format) {
         $categoryPageName[] = $blogKeyDir . PieCrustDefaults::CATEGORY_PAGE_NAME . '.' . $ext;
         $themeCategoryPageName[] = PieCrustDefaults::CATEGORY_PAGE_NAME . '.' . $ext;
     }
     $path = PathHelper::getUserOrThemePath($pieCrust, $categoryPageName, $themeCategoryPageName);
     if ($path === false) {
         return false;
     }
     $flags = $pieCrust->getConfig()->getValueUnchecked('site/slugify_flags');
     $categoryPattern = UriBuilder::buildCategoryUriPattern($pieCrust->getConfig()->getValueUnchecked($blogKey . '/category_url'));
     if (preg_match($categoryPattern, $uri, $matches)) {
         $cat = rawurldecode($matches['cat']);
         $cat = UriBuilder::slugify($cat, $flags);
         $pageInfo['type'] = IPage::TYPE_CATEGORY;
         $pageInfo['blogKey'] = $blogKey;
         $pageInfo['key'] = $cat;
         $pageInfo['path'] = $path;
         $pageInfo['was_path_checked'] = true;
         return true;
     }
     return false;
 }
Exemplo n.º 6
0
 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}]" : "")));
             }
         }
     }
 }
 protected function ensurePostsCached($blogKey)
 {
     if ($this->posts == null) {
         $this->posts = array();
     }
     if (!isset($this->posts[$blogKey])) {
         $pageRepository = $this->getPageRepository();
         $postInfos = $this->getPostInfos($blogKey);
         $postUrlFormat = $this->pieCrust->getConfig()->getValue($blogKey . '/post_url');
         $posts = array();
         foreach ($postInfos as $postInfo) {
             $uri = UriBuilder::buildPostUri($postUrlFormat, $postInfo);
             $page = $pageRepository->getOrCreatePage($uri, $postInfo['path'], IPage::TYPE_POST, $blogKey);
             $page->setDate(PageHelper::getPostDate($postInfo));
             $posts[] = $page;
         }
         $this->posts[$blogKey] = $posts;
     }
 }
Exemplo n.º 8
0
 /**
  * Gets a tag listing page.
  */
 public static function getTagPage(IPieCrust $pieCrust, $tag, $blogKey = null)
 {
     if ($blogKey == null) {
         $blogKeys = $pieCrust->getConfig()->getValueUnchecked('site/blogs');
         $blogKey = $blogKeys[0];
     }
     $pathPrefix = '';
     if ($blogKey != PieCrustDefaults::DEFAULT_BLOG_KEY) {
         $pathPrefix = $blogKey . DIRECTORY_SEPARATOR;
     }
     $pageRepository = $pieCrust->getEnvironment()->getPageRepository();
     $uri = UriBuilder::buildTagUri($pieCrust->getConfig()->getValue($blogKey . '/tag_url'), $tag);
     $path = $pieCrust->getPagesDir() . $pathPrefix . PieCrustDefaults::TAG_PAGE_NAME . '.html';
     if (!is_file($path)) {
         return null;
     }
     $page = $pageRepository->getOrCreatePage($uri, $tagPagePath, IPage::TYPE_TAG, $blogKey, $tag);
     return $page;
 }
Exemplo n.º 9
0
 /**
  * Creates a new Page instance given a path.
  */
 public static function createFromPath(IPieCrust $pieCrust, $path, $pageType = IPage::TYPE_REGULAR, $pageNumber = 1, $blogKey = null, $pageKey = null, $date = null)
 {
     if ($path == null) {
         throw new InvalidArgumentException("The given path is null.");
     }
     if (!is_file($path)) {
         throw new InvalidArgumentException("The given path does not exist: " . $path);
     }
     $relativePath = PieCrustHelper::getRelativePath($pieCrust, $path, true);
     $uri = UriBuilder::buildUri($relativePath);
     return new Page($pieCrust, $uri, $path, $pageType, $blogKey, $pageKey, $pageNumber, $date);
 }
Exemplo n.º 10
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;
 }
Exemplo n.º 11
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;
 }
Exemplo n.º 12
0
 protected function ensurePostsCached($blogKey)
 {
     if ($this->posts == null) {
         $this->posts = array();
     }
     if (!isset($this->posts[$blogKey])) {
         $this->getLog()->debug("Indexing '{$blogKey}' posts...");
         $fs = $this->getFileSystem();
         $postInfos = $fs->getPostFiles($blogKey);
         $this->getLog()->debug("Creating '{$blogKey}' posts...");
         $pageRepository = $this->getPageRepository();
         $posts = array();
         foreach ($postInfos as $postInfo) {
             $uri = UriBuilder::buildPostUri($this->pieCrust, $blogKey, $postInfo);
             $page = $pageRepository->getOrCreatePage($uri, $postInfo->path, IPage::TYPE_POST, $blogKey);
             $page->setDate(PageHelper::getPostDate($postInfo));
             $posts[] = $page;
         }
         $this->posts[$blogKey] = $posts;
     }
 }
Exemplo n.º 13
0
 protected function load()
 {
     try {
         $pieCrust = $this->page->getApp();
         $pageRepository = $pieCrust->getEnvironment()->getPageRepository();
         $items = array();
         $skipNames = array('Thumbs.db');
         $it = new FilesystemIterator($this->baseDir);
         foreach ($it as $item) {
             $filename = $item->getFilename();
             // Skip dot files, Thumbs.db, etc.
             if (!$filename or $filename[0] == '.') {
                 continue;
             }
             if (in_array($filename, $skipNames)) {
                 continue;
             }
             if ($item->isDir()) {
                 // Skip "asset" directories.
                 if (preg_match('/\\-assets$/', $filename)) {
                     continue;
                 }
                 $linker = new Linker($this->page, $item->getPathname());
                 $items[$filename . '_'] = $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 file 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 {
                     // 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 (str_replace('\\', '/', $path) == str_replace('\\', '/', $this->page->getPath())) {
                         $page = $this->page;
                     } else {
                         $relativePath = PathHelper::getRelativePath($pieCrust->getPagesDir(), $path);
                         $uri = UriBuilder::buildUri($pieCrust, $relativePath);
                         $page = $pageRepository->getOrCreatePage($uri, $path);
                     }
                     $key = preg_replace('/\\.[a-zA-Z0-9]+$/', '', $filename);
                     $key = str_replace('.', '_', $key);
                     $items[$key] = new LinkData($page, array('name' => $key, 'is_dir' => false, 'is_self' => $page == $this->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($items, array($this, 'sortByCustom'))) {
                 throw new PieCrustException("Error while sorting pages with the specified setting: {$this->sortByName}");
             }
         }
         return $items;
     } catch (Exception $e) {
         throw new PieCrustException("Error while building the links from page '{$this->page->getUri()}': " . $e->getMessage(), 0, $e);
     }
 }
Exemplo n.º 14
0
 /**
  * Builds the URL of a category listing.
  */
 public static function buildCategoryUri(IPieCrust $pieCrust, $blogKey, $category, $slugify = true)
 {
     $categoryUrlFormat = $pieCrust->getConfig()->getValue($blogKey . '/category_url');
     if ($slugify) {
         $flags = $pieCrust->getConfig()->getValue('site/slugify_flags');
         $category = UriBuilder::slugify($category, $flags);
     }
     return str_replace('%category%', $category, $categoryUrlFormat);
 }
Exemplo n.º 15
0
 public function getCategoryUrl($value, $blogKey = null)
 {
     $blogKey = $this->getSafeBlogKey($blogKey);
     return PieCrustHelper::formatUri($this->pieCrust, UriBuilder::buildCategoryUri($this->pieCrust, $blogKey, $value));
 }
Exemplo n.º 16
0
 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}]" : "")));
             }
         }
     }
 }
Exemplo n.º 17
0
 private static function tryParseCategoryUri(IPieCrust $pieCrust, $blogKey, $uri, array &$pageInfo)
 {
     $blogKeyDir = '';
     if ($blogKey != PieCrustDefaults::DEFAULT_BLOG_KEY) {
         $blogKeyDir = $blogKey . '/';
     }
     $relativeTagPage = $blogKeyDir . PieCrustDefaults::CATEGORY_PAGE_NAME . '.html';
     $path = PathHelper::getUserOrThemeOrResPath($pieCrust, $relativeTagPage);
     if ($path === false) {
         return false;
     }
     $categoryPattern = UriBuilder::buildCategoryUriPattern($pieCrust->getConfig()->getValueUnchecked($blogKey . '/category_url'));
     if (preg_match($categoryPattern, $uri, $matches)) {
         $pageInfo['type'] = IPage::TYPE_CATEGORY;
         $pageInfo['blogKey'] = $blogKey;
         $pageInfo['key'] = $matches['cat'];
         $pageInfo['path'] = $path;
         $pageInfo['was_path_checked'] = true;
         return true;
     }
     return false;
 }
Exemplo n.º 18
0
 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));
 }
Exemplo n.º 19
0
 /**
  * @dataProvider buildCategoryUriDataProvider
  */
 public function testBuildCategoryUri($category, $expectedUri)
 {
     $uri = UriBuilder::buildCategoryUri('/%category%', $category);
     $this->assertEquals($expectedUri, $uri);
 }