0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-19 10:45:18 +00:00

[1.7.0] DefinitionCache->flush() now requires configuration object. DefinitionCache_Serializer now will create directories for new types on the fly, and can accept custom directories to save serials into.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1147 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-06-16 20:46:44 +00:00
parent 9c7483166c
commit d1f43636e5
6 changed files with 58 additions and 12 deletions

View File

@ -68,6 +68,7 @@ class HTMLPurifier_ConfigSchema {
$this->defineNamespace('HTML', 'Configuration regarding allowed HTML.');
$this->defineNamespace('CSS', 'Configuration regarding allowed CSS.');
$this->defineNamespace('Output', 'Configuration relating to the generation of (X)HTML.');
$this->defineNamespace('Cache', 'Configuration for DefinitionCache and related subclasses.');
$this->defineNamespace('Test', 'Developer testing configuration for our unit tests.');
}

View File

@ -106,7 +106,7 @@ class HTMLPurifier_DefinitionCache
/**
* Clears all objects from cache
*/
function flush() {
function flush($config) {
trigger_error('Cannot call abstract method', E_USER_ERROR);
}

View File

@ -50,8 +50,8 @@ class HTMLPurifier_DefinitionCache_Decorator extends HTMLPurifier_DefinitionCach
return $this->cache->get($config);
}
function flush() {
return $this->cache->flush();
function flush($config) {
return $this->cache->flush($config);
}
function cleanup($config) {

View File

@ -2,6 +2,17 @@
require_once 'HTMLPurifier/DefinitionCache.php';
HTMLPurifier_ConfigSchema::define(
'Cache', 'SerializerPath', null, 'string/null', '
<p>
Absolute path with no trailing slash to store serialized definitions in.
Default is within the
HTML Purifier library inside DefinitionCache/Serializer. This
path must be writable by the webserver. This directive has been
available since 1.7.0.
</p>
');
class HTMLPurifier_DefinitionCache_Serializer extends
HTMLPurifier_DefinitionCache
{
@ -10,12 +21,14 @@ class HTMLPurifier_DefinitionCache_Serializer extends
if (!$this->checkDefType($def)) return;
$file = $this->generateFilePath($config);
if (file_exists($file)) return false;
$this->_prepareDir($config);
return $this->_write($file, serialize($def));
}
function set($def, $config) {
if (!$this->checkDefType($def)) return;
$file = $this->generateFilePath($config);
$this->_prepareDir($config);
return $this->_write($file, serialize($def));
}
@ -23,6 +36,7 @@ class HTMLPurifier_DefinitionCache_Serializer extends
if (!$this->checkDefType($def)) return;
$file = $this->generateFilePath($config);
if (!file_exists($file)) return false;
$this->_prepareDir($config);
return $this->_write($file, serialize($def));
}
@ -38,8 +52,8 @@ class HTMLPurifier_DefinitionCache_Serializer extends
return unlink($file);
}
function flush() {
$dir = $this->generateDirectoryPath();
function flush($config) {
$dir = $this->generateDirectoryPath($config);
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if (empty($filename)) continue;
@ -49,7 +63,8 @@ class HTMLPurifier_DefinitionCache_Serializer extends
}
function cleanup($config) {
$dir = $this->generateDirectoryPath();
$this->_prepareDir($config);
$dir = $this->generateDirectoryPath($config);
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if (empty($filename)) continue;
@ -65,15 +80,17 @@ class HTMLPurifier_DefinitionCache_Serializer extends
*/
function generateFilePath($config) {
$key = $this->generateKey($config);
return $this->generateDirectoryPath() . '/' . $key . '.ser';
return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
}
/**
* Generates the path to the directory contain this cache's serial files
* @note No trailing slash
*/
function generateDirectoryPath() {
return dirname(__FILE__) . '/Serializer/' . $this->type;
function generateDirectoryPath($config) {
$base = $config->get('Cache', 'SerializerPath');
$base = is_null($base) ? dirname(__FILE__) . '/Serializer' : $base;
return $base . '/' . $this->type;
}
/**
@ -97,6 +114,16 @@ class HTMLPurifier_DefinitionCache_Serializer extends
return $status;
}
/**
* Prepares the directory that this type stores the serials in
*/
function _prepareDir($config) {
$directory = $this->generateDirectoryPath($config);
if (!is_dir($directory)) {
mkdir($directory);
}
}
}
?>

View File

@ -32,8 +32,8 @@ class HTMLPurifier_DefinitionCache_DecoratorTest extends HTMLPurifier_Definition
$mock->expectOnce('get', array($config));
$cache->get($config);
$mock->expectOnce('flush', array());
$cache->flush();
$mock->expectOnce('flush', array($config));
$cache->flush($config);
$mock->expectOnce('cleanup', array($config));
$cache->cleanup($config);

View File

@ -102,7 +102,7 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
$this->assertEqual($def2, $cache->get($config2));
$this->assertEqual($def3, $cache->get($config3));
$cache->flush();
$cache->flush($config1); // only essential directive is %Cache.SerializerPath
$this->assertFalse($cache->get($config1));
$this->assertFalse($cache->get($config2));
@ -153,6 +153,24 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
$this->assertFalse(file_exists($file), 'Expected ' . $file . ' does not exist');
}
function testAlternatePath() {
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
$config = $this->generateConfigMock('serial');
$config->version = '1.0.0';
$config->revision = 1;
$dir = dirname(__FILE__) . '/SerializerTest';
$config->setReturnValue('get', $dir, array('Cache', 'SerializerPath'));
$def_original = $this->generateDefinition();
$cache->add($def_original, $config);
$this->assertFileExist($dir . '/Test/1.0.0-1-serial.ser');
unlink($dir . '/Test/1.0.0-1-serial.ser');
rmdir( $dir . '/Test');
}
}
?>