Skip to content
Permalink
Browse files

Fixing issues with duplicate content/empty tags with CacheHelper.

Added internal tag munging proposed by 'FelipePtChO'.
Uncommented a test that was failing.
Fixes #1857
  • Loading branch information...
markstory committed Aug 2, 2011
1 parent 416e527 commit b5a533b2871129017cb61c6ebfdf59a2a8cec074
Showing with 52 additions and 12 deletions.
  1. +36 −1 cake/libs/view/helpers/cache.php
  2. +1 −1 cake/libs/view/view.php
  3. +15 −10 cake/tests/cases/libs/view/helpers/cache.test.php
@@ -56,6 +56,13 @@ class CacheHelper extends AppHelper {
*/
var $cacheAction;
/**
* Counter used for counting nocache section tags.
*
* @var integer
*/
var $_counter = 0;
/**
* Main method used to cache a view
*
@@ -101,10 +108,13 @@ function cache($file, $out, $cache = false) {
}
if ($cacheTime != '' && $cacheTime > 0) {
$out = preg_replace_callback('/<cake\:nocache>/', array($this, '_replaceSection'), $out);
$this->__parseFile($file, $out);
if ($cache === true) {
$cached = $this->__parseOutput($out);
$this->__writeFile($cached, $cacheTime, $useCallbacks);
$out = $this->_stripTags($out);
}
return $out;
} else {
@@ -125,7 +135,8 @@ function __parseFile($file, $cache) {
} elseif ($file = fileExistsInPath($file)) {
$file = file_get_contents($file);
}
preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
preg_match_all('/(<cake:nocache:\d{3}>(?<=<cake:nocache:\d{3}>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $fileResult, PREG_PATTERN_ORDER);
$fileResult = $fileResult[0];
$outputResult = $outputResult[0];
@@ -152,6 +163,30 @@ function __parseFile($file, $cache) {
}
}
/**
* Munges the output from a view with cache tags, and numbers the sections.
* This helps solve issues with empty/duplicate content.
*
* @param string $content The content to munge.
* @return string The content with cake:nocache tags replaced.
*/
function _replaceSection($matches) {
$this->_counter += 1;
return sprintf('<cake:nocache:%03d>', $this->_counter);
}
/**
* Strip cake:nocache tags from a string. Since View::render()
* only removes un-numbered nocache tags, remove all the numbered ones.
* This is the complement to _replaceSection.
*
* @param string $content String to remove tags from.
* @return string String with tags removed.
*/
function _stripTags($content) {
return preg_replace('#<\/?cake\:nocache(\:\d{3})?>#', '', $content);
}
/**
* Parse the output and replace cache tags
*
@@ -754,7 +754,7 @@ function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = fal
$cache->layout = $this->layout;
$cache->cacheAction = $this->cacheAction;
$cache->viewVars = $this->viewVars;
$cache->cache($___viewFn, $out, $cached);
$out = $cache->cache($___viewFn, $out, $cached);
}
}
return $out;
@@ -529,11 +529,17 @@ function testCacheBaseNameControllerName() {
* This test must be uncommented/fixed in next release (1.2+)
*
* @return void
* @access public
*
function testCacheEmptySections () {
*/
function testCacheEmptySections() {
$this->Controller->cache_parsing();
$this->Controller->cacheAction = array('cacheTest' => 21600);
$this->Controller->params = array(
'controller' => 'cacheTest',
'action' => 'cache_empty_sections',
'url' => array(),
'pass' => array(),
'named' => array()
);
$this->Controller->cacheAction = array('cache_empty_sections' => 21600);
$this->Controller->here = '/cacheTest/cache_empty_sections';
$this->Controller->action = 'cache_empty_sections';
$this->Controller->layout = 'cache_empty_sections';
@@ -556,16 +562,15 @@ function testCacheEmptySections () {
$this->assertNoPattern('/cake:nocache/', $contents);
$this->assertPattern(
'@<head>\s*<title>Posts</title>\s*' .
"<\?php \$x = 1; \?>\s*" .
'<\?php \$x \= 1; \?>\s*' .
'</head>\s*' .
'<body>\s*' .
"<\?php \$x\+\+; \?>\s*" .
"<\?php \$x\+\+; \?>\s*" .
'<\?php \$x\+\+; \?>\s*' .
'<\?php \$x\+\+; \?>\s*' .
'View Content\s*' .
"<\?php \$y = 1; \?>\s*" .
"<\?php echo 'cached count is:' . \$x; \?>\s*" .
'<\?php \$y = 1; \?>\s*' .
'<\?php echo \'cached count is: \' . \$x; \?>\s*' .
'@', $contents);
@unlink($filename);
}
*/
}

0 comments on commit b5a533b

Please sign in to comment.
You can’t perform that action at this time.