Fixing issues with duplicate content/empty tags with CacheHelper.

Added internal tag munging proposed by 'FelipePtChO'.
Uncommented a test that was failing.
Fixes #1857
This commit is contained in:
Mark Story 2011-08-01 22:54:49 -04:00
parent 416e527cbc
commit b5a533b287
3 changed files with 52 additions and 12 deletions

View file

@ -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 @@ class CacheHelper extends AppHelper {
}
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 @@ class CacheHelper extends AppHelper {
} 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 @@ class CacheHelper extends AppHelper {
}
}
/**
* 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
*

View file

@ -754,7 +754,7 @@ class View extends Object {
$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;

View file

@ -529,11 +529,17 @@ class CacheHelperTest extends CakeTestCase {
* This test must be uncommented/fixed in next release (1.2+)
*
* @return void
* @access public
*
*/
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 @@ class CacheHelperTest extends CakeTestCase {
$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);
}
*/
}