mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-01-03 13:21:51 +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:
parent
9c7483166c
commit
d1f43636e5
@ -68,6 +68,7 @@ class HTMLPurifier_ConfigSchema {
|
|||||||
$this->defineNamespace('HTML', 'Configuration regarding allowed HTML.');
|
$this->defineNamespace('HTML', 'Configuration regarding allowed HTML.');
|
||||||
$this->defineNamespace('CSS', 'Configuration regarding allowed CSS.');
|
$this->defineNamespace('CSS', 'Configuration regarding allowed CSS.');
|
||||||
$this->defineNamespace('Output', 'Configuration relating to the generation of (X)HTML.');
|
$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.');
|
$this->defineNamespace('Test', 'Developer testing configuration for our unit tests.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ class HTMLPurifier_DefinitionCache
|
|||||||
/**
|
/**
|
||||||
* Clears all objects from cache
|
* Clears all objects from cache
|
||||||
*/
|
*/
|
||||||
function flush() {
|
function flush($config) {
|
||||||
trigger_error('Cannot call abstract method', E_USER_ERROR);
|
trigger_error('Cannot call abstract method', E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,8 +50,8 @@ class HTMLPurifier_DefinitionCache_Decorator extends HTMLPurifier_DefinitionCach
|
|||||||
return $this->cache->get($config);
|
return $this->cache->get($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
function flush() {
|
function flush($config) {
|
||||||
return $this->cache->flush();
|
return $this->cache->flush($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanup($config) {
|
function cleanup($config) {
|
||||||
|
@ -2,6 +2,17 @@
|
|||||||
|
|
||||||
require_once 'HTMLPurifier/DefinitionCache.php';
|
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
|
class HTMLPurifier_DefinitionCache_Serializer extends
|
||||||
HTMLPurifier_DefinitionCache
|
HTMLPurifier_DefinitionCache
|
||||||
{
|
{
|
||||||
@ -10,12 +21,14 @@ class HTMLPurifier_DefinitionCache_Serializer extends
|
|||||||
if (!$this->checkDefType($def)) return;
|
if (!$this->checkDefType($def)) return;
|
||||||
$file = $this->generateFilePath($config);
|
$file = $this->generateFilePath($config);
|
||||||
if (file_exists($file)) return false;
|
if (file_exists($file)) return false;
|
||||||
|
$this->_prepareDir($config);
|
||||||
return $this->_write($file, serialize($def));
|
return $this->_write($file, serialize($def));
|
||||||
}
|
}
|
||||||
|
|
||||||
function set($def, $config) {
|
function set($def, $config) {
|
||||||
if (!$this->checkDefType($def)) return;
|
if (!$this->checkDefType($def)) return;
|
||||||
$file = $this->generateFilePath($config);
|
$file = $this->generateFilePath($config);
|
||||||
|
$this->_prepareDir($config);
|
||||||
return $this->_write($file, serialize($def));
|
return $this->_write($file, serialize($def));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,6 +36,7 @@ class HTMLPurifier_DefinitionCache_Serializer extends
|
|||||||
if (!$this->checkDefType($def)) return;
|
if (!$this->checkDefType($def)) return;
|
||||||
$file = $this->generateFilePath($config);
|
$file = $this->generateFilePath($config);
|
||||||
if (!file_exists($file)) return false;
|
if (!file_exists($file)) return false;
|
||||||
|
$this->_prepareDir($config);
|
||||||
return $this->_write($file, serialize($def));
|
return $this->_write($file, serialize($def));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,8 +52,8 @@ class HTMLPurifier_DefinitionCache_Serializer extends
|
|||||||
return unlink($file);
|
return unlink($file);
|
||||||
}
|
}
|
||||||
|
|
||||||
function flush() {
|
function flush($config) {
|
||||||
$dir = $this->generateDirectoryPath();
|
$dir = $this->generateDirectoryPath($config);
|
||||||
$dh = opendir($dir);
|
$dh = opendir($dir);
|
||||||
while (false !== ($filename = readdir($dh))) {
|
while (false !== ($filename = readdir($dh))) {
|
||||||
if (empty($filename)) continue;
|
if (empty($filename)) continue;
|
||||||
@ -49,7 +63,8 @@ class HTMLPurifier_DefinitionCache_Serializer extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
function cleanup($config) {
|
function cleanup($config) {
|
||||||
$dir = $this->generateDirectoryPath();
|
$this->_prepareDir($config);
|
||||||
|
$dir = $this->generateDirectoryPath($config);
|
||||||
$dh = opendir($dir);
|
$dh = opendir($dir);
|
||||||
while (false !== ($filename = readdir($dh))) {
|
while (false !== ($filename = readdir($dh))) {
|
||||||
if (empty($filename)) continue;
|
if (empty($filename)) continue;
|
||||||
@ -65,15 +80,17 @@ class HTMLPurifier_DefinitionCache_Serializer extends
|
|||||||
*/
|
*/
|
||||||
function generateFilePath($config) {
|
function generateFilePath($config) {
|
||||||
$key = $this->generateKey($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
|
* Generates the path to the directory contain this cache's serial files
|
||||||
* @note No trailing slash
|
* @note No trailing slash
|
||||||
*/
|
*/
|
||||||
function generateDirectoryPath() {
|
function generateDirectoryPath($config) {
|
||||||
return dirname(__FILE__) . '/Serializer/' . $this->type;
|
$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;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
@ -32,8 +32,8 @@ class HTMLPurifier_DefinitionCache_DecoratorTest extends HTMLPurifier_Definition
|
|||||||
$mock->expectOnce('get', array($config));
|
$mock->expectOnce('get', array($config));
|
||||||
$cache->get($config);
|
$cache->get($config);
|
||||||
|
|
||||||
$mock->expectOnce('flush', array());
|
$mock->expectOnce('flush', array($config));
|
||||||
$cache->flush();
|
$cache->flush($config);
|
||||||
|
|
||||||
$mock->expectOnce('cleanup', array($config));
|
$mock->expectOnce('cleanup', array($config));
|
||||||
$cache->cleanup($config);
|
$cache->cleanup($config);
|
||||||
|
@ -102,7 +102,7 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
|
|||||||
$this->assertEqual($def2, $cache->get($config2));
|
$this->assertEqual($def2, $cache->get($config2));
|
||||||
$this->assertEqual($def3, $cache->get($config3));
|
$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($config1));
|
||||||
$this->assertFalse($cache->get($config2));
|
$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');
|
$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');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
Loading…
Reference in New Issue
Block a user