/**
     * @dataProvider urlFunctionsWithTwoBlogsDataProvider
     */
    public function testUrlFunctionsWithTwoBlogs($siteConfig, $expectedOutput)
    {
        if (!isset($siteConfig['site'])) {
            $siteConfig['site'] = array();
        }
        $siteConfig['site']['blogs'] = array('blog-one', 'blog-two');
        $fs = MockFileSystem::create()->withConfig($siteConfig)->withPage('test1', array('format' => 'none', 'layout' => 'none'), <<<EOD
{{pctagurl('tag1')}}
{{pctagurl('tag1', 'blog-one')}}
{{pctagurl('tag1', 'blog-two')}}
{{pccaturl('cat1')}}
{{pccaturl('cat1', 'blog-one')}}
{{pccaturl('cat1', 'blog-two')}}
{{pcposturl(2012, 1, 10, 'post1-test')}}
{{pcposturl(2012, 1, 10, 'post1-test', 'blog-one')}}
{{pcposturl(2012, 1, 10, 'post1-test', 'blog-two')}}
EOD
);
        $app = $fs->getApp();
        $page = Page::createFromUri($app, '/test1');
        $pageRenderer = new PageRenderer($page);
        $output = $pageRenderer->get();
        $lines = explode("\n", $output);
        $this->assertEquals($expectedOutput, $lines);
    }
Beispiel #2
0
 protected function bakeSinglePage(PageRenderer $pageRenderer, array $extraData = null)
 {
     $page = $pageRenderer->getPage();
     // Set the extra template data before the page's data is computed.
     if ($extraData != null) {
         $page->setExtraPageData($extraData);
     }
     // This is usually done in the PieCrustBaker, but we'll do it here too
     // because the `PageBaker` could be used on its own.
     if ($this->parameters['copy_assets']) {
         $page->setAssetUrlBaseRemap("%site_root%%uri%");
     }
     // Figure out the output HTML path.
     $bakePath = $this->getOutputPath($page);
     $this->logger->debug("  p{$page->getPageNumber()} -> {$bakePath}");
     // Figure out if we should re-bake this page.
     $doBake = true;
     if ($this->parameters['smart']) {
         // Don't rebake if the output seems up-to-date, and
         // the page isn't known to be using posts.
         $bakePathTime = @filemtime($bakePath);
         if ($bakePathTime !== false && filemtime($page->getPath()) < $bakePathTime) {
             // TODO: rebake if the page is using pagination and pages/posts were baked this time.
             $doBake = false;
         }
     }
     if (!$doBake) {
         $this->logger->debug("Not baking '{$page->getUri()}/{$page->getPageNumber()}' because '{$bakePath}' is up-to-date.");
         return false;
     }
     // If we're using portable URLs, change the site root to a relative
     // path from the page's directory.
     $savedSiteRoot = $this->setPortableSiteRoot($page->getApp(), $bakePath);
     // Render the page.
     $bakedContents = $pageRenderer->get();
     // Get some objects we need.
     $data = $page->getPageData();
     $assetor = $data['assets'];
     $paginator = $data['pagination'];
     // Copy the page.
     PathHelper::ensureDirectory(dirname($bakePath));
     file_put_contents($bakePath, $bakedContents);
     $this->bakedFiles[] = $bakePath;
     // Copy any used assets for the first sub-page.
     if ($page->getPageNumber() == 1 and $this->parameters['copy_assets']) {
         $prettyUrls = PageHelper::getConfigValue($page, 'pretty_urls', 'site');
         if ($prettyUrls) {
             $bakeAssetDir = dirname($bakePath) . '/';
         } else {
             $bakePathInfo = pathinfo($bakePath);
             $bakeAssetDir = $bakePathInfo['dirname'] . '/' . ($page->getUri() == '' ? '' : $bakePathInfo['filename']) . '/';
         }
         $assetPaths = $assetor->getAssetPathnames();
         if ($assetPaths != null) {
             PathHelper::ensureDirectory($bakeAssetDir);
             foreach ($assetPaths as $assetPath) {
                 $destinationAssetPath = $bakeAssetDir . basename($assetPath);
                 if (@copy($assetPath, $destinationAssetPath) == false) {
                     throw new PieCrustException("Can't copy '{$assetPath}' to '{$destinationAssetPath}'.");
                 }
             }
         }
     }
     // Remember a few things.
     $this->paginationDataAccessed = ($this->paginationDataAccessed or $paginator->wasPaginationDataAccessed());
     // Cleanup.
     if ($savedSiteRoot) {
         $page->getApp()->getConfig()->setValue('site/root', $savedSiteRoot);
     }
     return true;
 }
Beispiel #3
0
 protected function bakeSinglePage(PageRenderer $pageRenderer, array $extraData = null)
 {
     $page = $pageRenderer->getPage();
     // Set the extra template data before the page's data is computed.
     if ($extraData != null) {
         $page->setExtraPageData($extraData);
     }
     // This is usually done in the PieCrustBaker, but we'll do it here too
     // because the `PageBaker` could be used on its own.
     if ($this->parameters['copy_assets']) {
         $page->setAssetUrlBaseRemap("%site_root%%uri%");
     }
     // Figure out the output HTML path.
     $bakePath = $this->getOutputPath($page);
     // Figure out if we should re-bake this page.
     $doBake = true;
     if (is_file($bakePath) && $this->parameters['smart']) {
         // Don't rebake if the output seems up-to-date, and
         // the page isn't known to be using posts.
         if (filemtime($page->getPath()) < filemtime($bakePath)) {
             $bakeRecord = $this->parameters['bake_record'];
             if ($bakeRecord) {
                 $relativePath = PageHelper::getRelativePath($page);
                 if (!$bakeRecord->wasAnyPostBaked() || !$bakeRecord->isPageUsingPosts($relativePath)) {
                     $doBake = false;
                 }
             }
         }
     }
     if (!$doBake) {
         $this->logger->debug("Not baking '{$page->getUri()}/{$page->getPageNumber()}' because '{$bakePath}' is up-to-date.");
         return false;
     }
     // Backward compatibility warning and file-copy.
     // [TODO] Remove in a couple of versions.
     $copyToOldPath = false;
     $contentType = $page->getConfig()->getValue('content_type');
     $nativeExtension = pathinfo($page->getPath(), PATHINFO_EXTENSION);
     if ($contentType != 'html' && $nativeExtension == 'html') {
         $copyToOldPath = $this->bakeDir . $page->getUri();
         if ($page->getPageNumber() > 1) {
             $copyToOldPath .= $page->getPageNumber() . '/';
         }
         $copyToOldPath .= '.' . $contentType;
     }
     // If we're using portable URLs, change the site root to a relative
     // path from the page's directory.
     $savedSiteRoot = $this->setPortableSiteRoot($page->getApp(), $bakePath);
     // Render the page.
     $bakedContents = $pageRenderer->get();
     // Get some objects we need.
     $data = $page->getPageData();
     $assetor = $data['asset'];
     $paginator = $data['pagination'];
     // Copy the page.
     PathHelper::ensureDirectory(dirname($bakePath));
     file_put_contents($bakePath, $bakedContents);
     $this->bakedFiles[] = $bakePath;
     // [TODO] See previous TODO.
     if ($copyToOldPath) {
         $this->logger->warning("Page '{$page->getUri()}' has 'content_type' specified but is an HTML file.");
         $this->logger->warning("Changing a baked file's extension using 'content_type' is deprecated and will be removed in a future version.");
         $this->logger->warning("For backwards compatibility, the page will also be baked to: " . substr($copyToOldPath, strlen($this->bakeDir)));
         $this->logger->warning("To fix the problem, change the source file's extension to the desired output extension.");
         $this->logger->warning("Otherwise, just ignore these messages.");
         file_put_contents($copyToOldPath, $bakedContents);
     }
     // Copy any used assets for the first sub-page.
     if ($page->getPageNumber() == 1 and $this->parameters['copy_assets']) {
         $prettyUrls = PageHelper::getConfigValue($page, 'pretty_urls', 'site');
         if ($prettyUrls) {
             $bakeAssetDir = dirname($bakePath) . '/';
         } else {
             $bakePathInfo = pathinfo($bakePath);
             $bakeAssetDir = $bakePathInfo['dirname'] . '/' . ($page->getUri() == '' ? '' : $bakePathInfo['filename']) . '/';
         }
         $assetPaths = $assetor->getAssetPathnames();
         if ($assetPaths != null) {
             PathHelper::ensureDirectory($bakeAssetDir);
             foreach ($assetPaths as $assetPath) {
                 $destinationAssetPath = $bakeAssetDir . basename($assetPath);
                 if (@copy($assetPath, $destinationAssetPath) == false) {
                     throw new PieCrustException("Can't copy '" . $assetPath . "' to '" . $destinationAssetPath . "'.");
                 }
             }
         }
     }
     // Remember a few things.
     $this->paginationDataAccessed = ($this->paginationDataAccessed or $paginator->wasPaginationDataAccessed());
     // Cleanup.
     if ($savedSiteRoot) {
         $page->getApp()->getConfig()->setValue('site/root', $savedSiteRoot);
     }
     return true;
 }
Beispiel #4
0
 /**
  * Runs PieCrust on the given URI with the given extra page rendering data,
  * but without any error handling.
  */
 public function runUnsafe($uri = null, array $server = null, $extraPageData = null, array &$headers = null)
 {
     // Create an execution context.
     $executionContext = $this->pieCrust->getEnvironment()->getExecutionContext(true);
     // Check the cache validity, and clean it automatically.
     if ($this->pieCrust->isCachingEnabled()) {
         $cacheInfo = new PieCrustCacheInfo($this->pieCrust);
         $cacheValidity = $cacheInfo->getValidity(true);
         $executionContext->isCacheValid = $cacheValidity['is_valid'];
         $executionContext->wasCacheCleaned = $cacheValidity['was_cleaned'];
     }
     // Get the resource URI and corresponding physical path.
     if ($server == null) {
         $server = $_SERVER;
     }
     if ($uri == null) {
         $uri = ServerHelper::getRequestUri($server, $this->pieCrust->getConfig()->getValueUnchecked('site/pretty_urls'));
     }
     // Do the heavy lifting.
     $page = Page::createFromUri($this->pieCrust, $uri);
     $executionContext->pushPage($page);
     if ($extraPageData != null) {
         $page->setExtraPageData($extraPageData);
     }
     $pageRenderer = new PageRenderer($page);
     $output = $pageRenderer->get();
     // Set or return the HTML headers.
     HttpHeaderHelper::setOrAddHeaders(PageRenderer::getHeaders($page->getConfig()->getValue('content_type'), $server), $headers);
     // Handle caching.
     if (!$this->pieCrust->isDebuggingEnabled()) {
         $hash = md5($output);
         HttpHeaderHelper::setOrAddHeader('Etag', '"' . $hash . '"', $headers);
         $clientHash = null;
         if (isset($server['HTTP_IF_NONE_MATCH'])) {
             $clientHash = $server['HTTP_IF_NONE_MATCH'];
         }
         if ($clientHash != null) {
             $clientHash = trim($clientHash, '"');
             if ($hash == $clientHash) {
                 HttpHeaderHelper::setOrAddHeader(0, 304, $headers);
                 HttpHeaderHelper::setOrAddHeader('Content-Length', '0', $headers);
                 return;
             }
         }
     }
     if ($this->pieCrust->isDebuggingEnabled()) {
         HttpHeaderHelper::setOrAddHeader('Cache-Control', 'no-cache, must-revalidate', $headers);
     } else {
         $cacheTime = PageHelper::getConfigValue($page, 'cache_time', 'site');
         if ($cacheTime) {
             HttpHeaderHelper::setOrAddHeader('Cache-Control', 'public, max-age=' . $cacheTime, $headers);
         }
     }
     // Output with or without GZip compression.
     $gzipEnabled = ($this->pieCrust->getConfig()->getValueUnchecked('site/enable_gzip') === true and array_key_exists('HTTP_ACCEPT_ENCODING', $server) and strpos($server['HTTP_ACCEPT_ENCODING'], 'gzip') !== false);
     if ($gzipEnabled) {
         $zippedOutput = gzencode($output);
         if ($zippedOutput === false) {
             HttpHeaderHelper::setOrAddHeader('Content-Length', strlen($output), $headers);
             echo $output;
         } else {
             HttpHeaderHelper::setOrAddHeader('Content-Encoding', 'gzip', $headers);
             HttpHeaderHelper::setOrAddHeader('Content-Length', strlen($zippedOutput), $headers);
             echo $zippedOutput;
         }
     } else {
         HttpHeaderHelper::setOrAddHeader('Content-Length', strlen($output), $headers);
         echo $output;
     }
 }
 public function runQuery($pieCrust, $uri, $cleanCache = false)
 {
     if ($cleanCache) {
         ensure_cache(PIECRUST_BENCHMARKS_CACHE_DIR, true);
     }
     $page = Page::createFromUri($pieCrust, $uri);
     $renderer = new PageRenderer($page);
     return $renderer->get();
 }