Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Improve the cache handling for empty URLs (see #7618).
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Mar 19, 2015
1 parent 16db591 commit 67b4494
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 12 deletions.
52 changes: 41 additions & 11 deletions contao/controllers/FrontendIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,7 @@ protected function outputFromCache()
return;
}

/**
* If the request string is empty, look for a cached page matching the
* primary browser language. This is a compromise between not caching
* empty requests at all and considering all browser languages, which
* is not possible for various reasons.
*/
// Try to map the empty request
if (\Environment::get('request') == '' || \Environment::get('request') == 'index.php')
{
// Return if the language is added to the URL and the empty domain will be redirected
Expand All @@ -291,8 +286,43 @@ protected function outputFromCache()
return;
}

$strCacheKey = null;
$arrLanguage = \Environment::get('httpAcceptLanguage');
$strCacheKey = \Environment::get('base') .'empty.'. $arrLanguage[0];

// Try to get the cache key from the mapper array
if (file_exists(TL_ROOT . '/system/cache/config/mapping.php'))
{
$arrMapper = include TL_ROOT . '/system/cache/config/mapping.php';

// Try the language specific keys
foreach ($arrLanguage as $strLanguage)
{
$strSpecificKey = \Environment::get('base') . 'empty.' . $strLanguage;

if (isset($arrMapper[$strSpecificKey]))
{
$strCacheKey = $arrMapper[$strSpecificKey];
break;
}
}

// Try the fallback key
if ($strCacheKey === null)
{
$strSpecificKey = \Environment::get('base') . 'empty.fallback';

if (isset($arrMapper[$strSpecificKey]))
{
$strCacheKey = $arrMapper[$strSpecificKey];
}
}
}

// Fall back to the first accepted language
if ($strCacheKey === null)
{
$strCacheKey = \Environment::get('base') . 'empty.' . $arrLanguage[0];
}
}
else
{
Expand All @@ -315,8 +345,8 @@ protected function outputFromCache()
// Check for a mobile layout
if (\Input::cookie('TL_VIEW') == 'mobile' || (\Environment::get('agent')->mobile && \Input::cookie('TL_VIEW') != 'desktop'))
{
$strCacheKey = md5($strCacheKey . '.mobile');
$strCacheFile = TL_ROOT . '/system/cache/html/' . substr($strCacheKey, 0, 1) . '/' . $strCacheKey . '.html';
$strMd5CacheKey = md5($strCacheKey . '.mobile');
$strCacheFile = TL_ROOT . '/system/cache/html/' . substr($strMd5CacheKey, 0, 1) . '/' . $strMd5CacheKey . '.html';

if (file_exists($strCacheFile))
{
Expand All @@ -327,8 +357,8 @@ protected function outputFromCache()
// Check for a regular layout
if (!$blnFound)
{
$strCacheKey = md5($strCacheKey);
$strCacheFile = TL_ROOT . '/system/cache/html/' . substr($strCacheKey, 0, 1) . '/' . $strCacheKey . '.html';
$strMd5CacheKey = md5($strCacheKey);
$strCacheFile = TL_ROOT . '/system/cache/html/' . substr($strMd5CacheKey, 0, 1) . '/' . $strMd5CacheKey . '.html';

if (file_exists($strCacheFile))
{
Expand Down
48 changes: 47 additions & 1 deletion contao/library/Contao/Automator.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,54 @@ public function generateConfigCache()
// Close the file (moves it to its final destination)
$objCacheFile->close();

// Generate the page mapping array
$arrMapper = [];
$objPages = \PageModel::findPublishedRootPages();

if ($objPages !== null)
{
while ($objPages->next())
{
if ($objPages->dns != '')
{
$strBase = $objPages->useSSL ? 'https://' : 'http://';
$strBase .= $objPages->dns . \Environment::get('path') . '/';
}
else
{
$strBase = \Environment::get('base');
}

if ($objPages->fallback)
{
$arrMapper[$strBase . 'empty.fallback'] = $strBase . 'empty.' . $objPages->language;
}

$arrMapper[$strBase . 'empty.' . $objPages->language] = $strBase . 'empty.' . $objPages->language;
}
}

// Generate the page mapper file
$objCacheFile = new \File('system/cache/config/mapping.php', true);
$objCacheFile->write('<?php '); // add one space to prevent the "unexpected $end" error

$strContent = "\n\n";
$strContent .= "return array\n";
$strContent .= "(\n";

foreach ($arrMapper as $strKey=>$strCacheKey)
{
$strContent .= "\t'$strKey' => '$strCacheKey',\n";
}

$strContent .= ");";
$objCacheFile->append($strContent);

// Close the file (moves it to its final destination)
$objCacheFile->close();

// Add a log entry
$this->log('Generated the autoload cache', __METHOD__, TL_CRON);
$this->log('Generated the config cache', __METHOD__, TL_CRON);
}


Expand Down
22 changes: 22 additions & 0 deletions contao/models/PageModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,28 @@ public static function findPublishedFallbackByHostname($strHost, array $arrOptio
}


/**
* Finds the published root pages
*
* @param array $arrOptions An optional options array
*
* @return \Model\Collection|null A collection of models or null if there are no parent pages
*/
public static function findPublishedRootPages(array $arrOptions=array())
{
$t = static::$strTable;
$arrColumns = array("$t.type=?");

if (!BE_USER_LOGGED_IN)
{
$time = time();
$arrColumns[] = "($t.start='' OR $t.start<$time) AND ($t.stop='' OR $t.stop>$time) AND $t.published=1";
}

return static::findBy($arrColumns, 'root', $arrOptions);
}


/**
* Find the parent pages of a page
*
Expand Down

0 comments on commit 67b4494

Please sign in to comment.