diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-12-06 00:49:43 -0800 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-12-06 00:49:43 -0800 |
commit | d5501950e2470d52f6bf5954d2179010cdee0475 (patch) | |
tree | c452a0cbcbe3a49fa72e5ba7e8c249302da8ae29 /inc/3rdparty/libraries/Zend/Cache/Core.php | |
parent | 0b5c6ff3195e145a1fb5edb67741b8f6ed231fa2 (diff) | |
parent | 42c80841c846610be280218d53fcde06b0f0063b (diff) | |
download | wallabag-d5501950e2470d52f6bf5954d2179010cdee0475.tar.gz wallabag-d5501950e2470d52f6bf5954d2179010cdee0475.tar.zst wallabag-d5501950e2470d52f6bf5954d2179010cdee0475.zip |
Merge pull request #353 from inthepoche/ftr
[change] we now use Full-Text RSS 3.1, thank you so much @fivefilters
Diffstat (limited to 'inc/3rdparty/libraries/Zend/Cache/Core.php')
-rw-r--r-- | inc/3rdparty/libraries/Zend/Cache/Core.php | 765 |
1 files changed, 765 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/Zend/Cache/Core.php b/inc/3rdparty/libraries/Zend/Cache/Core.php new file mode 100644 index 00000000..e3588636 --- /dev/null +++ b/inc/3rdparty/libraries/Zend/Cache/Core.php | |||
@@ -0,0 +1,765 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Zend Framework | ||
4 | * | ||
5 | * LICENSE | ||
6 | * | ||
7 | * This source file is subject to the new BSD license that is bundled | ||
8 | * with this package in the file LICENSE.txt. | ||
9 | * It is also available through the world-wide-web at this URL: | ||
10 | * http://framework.zend.com/license/new-bsd | ||
11 | * If you did not receive a copy of the license and are unable to | ||
12 | * obtain it through the world-wide-web, please send an email | ||
13 | * to license@zend.com so we can send you a copy immediately. | ||
14 | * | ||
15 | * @category Zend | ||
16 | * @package Zend_Cache | ||
17 | * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
18 | * @license http://framework.zend.com/license/new-bsd New BSD License | ||
19 | * @version $Id: Core.php 24989 2012-06-21 07:24:13Z mabe $ | ||
20 | */ | ||
21 | |||
22 | |||
23 | /** | ||
24 | * @package Zend_Cache | ||
25 | * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
26 | * @license http://framework.zend.com/license/new-bsd New BSD License | ||
27 | */ | ||
28 | class Zend_Cache_Core | ||
29 | { | ||
30 | /** | ||
31 | * Messages | ||
32 | */ | ||
33 | const BACKEND_NOT_SUPPORTS_TAG = 'tags are not supported by the current backend'; | ||
34 | const BACKEND_NOT_IMPLEMENTS_EXTENDED_IF = 'Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available'; | ||
35 | |||
36 | /** | ||
37 | * Backend Object | ||
38 | * | ||
39 | * @var Zend_Cache_Backend_Interface $_backend | ||
40 | */ | ||
41 | protected $_backend = null; | ||
42 | |||
43 | /** | ||
44 | * Available options | ||
45 | * | ||
46 | * ====> (boolean) write_control : | ||
47 | * - Enable / disable write control (the cache is read just after writing to detect corrupt entries) | ||
48 | * - Enable write control will lightly slow the cache writing but not the cache reading | ||
49 | * Write control can detect some corrupt cache files but maybe it's not a perfect control | ||
50 | * | ||
51 | * ====> (boolean) caching : | ||
52 | * - Enable / disable caching | ||
53 | * (can be very useful for the debug of cached scripts) | ||
54 | * | ||
55 | * =====> (string) cache_id_prefix : | ||
56 | * - prefix for cache ids (namespace) | ||
57 | * | ||
58 | * ====> (boolean) automatic_serialization : | ||
59 | * - Enable / disable automatic serialization | ||
60 | * - It can be used to save directly datas which aren't strings (but it's slower) | ||
61 | * | ||
62 | * ====> (int) automatic_cleaning_factor : | ||
63 | * - Disable / Tune the automatic cleaning process | ||
64 | * - The automatic cleaning process destroy too old (for the given life time) | ||
65 | * cache files when a new cache file is written : | ||
66 | * 0 => no automatic cache cleaning | ||
67 | * 1 => systematic cache cleaning | ||
68 | * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write | ||
69 | * | ||
70 | * ====> (int) lifetime : | ||
71 | * - Cache lifetime (in seconds) | ||
72 | * - If null, the cache is valid forever. | ||
73 | * | ||
74 | * ====> (boolean) logging : | ||
75 | * - If set to true, logging is activated (but the system is slower) | ||
76 | * | ||
77 | * ====> (boolean) ignore_user_abort | ||
78 | * - If set to true, the core will set the ignore_user_abort PHP flag inside the | ||
79 | * save() method to avoid cache corruptions in some cases (default false) | ||
80 | * | ||
81 | * @var array $_options available options | ||
82 | */ | ||
83 | protected $_options = array( | ||
84 | 'write_control' => true, | ||
85 | 'caching' => true, | ||
86 | 'cache_id_prefix' => null, | ||
87 | 'automatic_serialization' => false, | ||
88 | 'automatic_cleaning_factor' => 10, | ||
89 | 'lifetime' => 3600, | ||
90 | 'logging' => false, | ||
91 | 'logger' => null, | ||
92 | 'ignore_user_abort' => false | ||
93 | ); | ||
94 | |||
95 | /** | ||
96 | * Array of options which have to be transfered to backend | ||
97 | * | ||
98 | * @var array $_directivesList | ||
99 | */ | ||
100 | protected static $_directivesList = array('lifetime', 'logging', 'logger'); | ||
101 | |||
102 | /** | ||
103 | * Not used for the core, just a sort a hint to get a common setOption() method (for the core and for frontends) | ||
104 | * | ||
105 | * @var array $_specificOptions | ||
106 | */ | ||
107 | protected $_specificOptions = array(); | ||
108 | |||
109 | /** | ||
110 | * Last used cache id | ||
111 | * | ||
112 | * @var string $_lastId | ||
113 | */ | ||
114 | private $_lastId = null; | ||
115 | |||
116 | /** | ||
117 | * True if the backend implements Zend_Cache_Backend_ExtendedInterface | ||
118 | * | ||
119 | * @var boolean $_extendedBackend | ||
120 | */ | ||
121 | protected $_extendedBackend = false; | ||
122 | |||
123 | /** | ||
124 | * Array of capabilities of the backend (only if it implements Zend_Cache_Backend_ExtendedInterface) | ||
125 | * | ||
126 | * @var array | ||
127 | */ | ||
128 | protected $_backendCapabilities = array(); | ||
129 | |||
130 | /** | ||
131 | * Constructor | ||
132 | * | ||
133 | * @param array|Zend_Config $options Associative array of options or Zend_Config instance | ||
134 | * @throws Zend_Cache_Exception | ||
135 | * @return void | ||
136 | */ | ||
137 | public function __construct($options = array()) | ||
138 | { | ||
139 | if ($options instanceof Zend_Config) { | ||
140 | $options = $options->toArray(); | ||
141 | } | ||
142 | if (!is_array($options)) { | ||
143 | Zend_Cache::throwException("Options passed were not an array" | ||
144 | . " or Zend_Config instance."); | ||
145 | } | ||
146 | while (list($name, $value) = each($options)) { | ||
147 | $this->setOption($name, $value); | ||
148 | } | ||
149 | $this->_loggerSanity(); | ||
150 | } | ||
151 | |||
152 | /** | ||
153 | * Set options using an instance of type Zend_Config | ||
154 | * | ||
155 | * @param Zend_Config $config | ||
156 | * @return Zend_Cache_Core | ||
157 | */ | ||
158 | public function setConfig(Zend_Config $config) | ||
159 | { | ||
160 | $options = $config->toArray(); | ||
161 | while (list($name, $value) = each($options)) { | ||
162 | $this->setOption($name, $value); | ||
163 | } | ||
164 | return $this; | ||
165 | } | ||
166 | |||
167 | /** | ||
168 | * Set the backend | ||
169 | * | ||
170 | * @param Zend_Cache_Backend $backendObject | ||
171 | * @throws Zend_Cache_Exception | ||
172 | * @return void | ||
173 | */ | ||
174 | public function setBackend(Zend_Cache_Backend $backendObject) | ||
175 | { | ||
176 | $this->_backend= $backendObject; | ||
177 | // some options (listed in $_directivesList) have to be given | ||
178 | // to the backend too (even if they are not "backend specific") | ||
179 | $directives = array(); | ||
180 | foreach (Zend_Cache_Core::$_directivesList as $directive) { | ||
181 | $directives[$directive] = $this->_options[$directive]; | ||
182 | } | ||
183 | $this->_backend->setDirectives($directives); | ||
184 | if (in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_backend))) { | ||
185 | $this->_extendedBackend = true; | ||
186 | $this->_backendCapabilities = $this->_backend->getCapabilities(); | ||
187 | } | ||
188 | |||
189 | } | ||
190 | |||
191 | /** | ||
192 | * Returns the backend | ||
193 | * | ||
194 | * @return Zend_Cache_Backend backend object | ||
195 | */ | ||
196 | public function getBackend() | ||
197 | { | ||
198 | return $this->_backend; | ||
199 | } | ||
200 | |||
201 | /** | ||
202 | * Public frontend to set an option | ||
203 | * | ||
204 | * There is an additional validation (relatively to the protected _setOption method) | ||
205 | * | ||
206 | * @param string $name Name of the option | ||
207 | * @param mixed $value Value of the option | ||
208 | * @throws Zend_Cache_Exception | ||
209 | * @return void | ||
210 | */ | ||
211 | public function setOption($name, $value) | ||
212 | { | ||
213 | if (!is_string($name)) { | ||
214 | Zend_Cache::throwException("Incorrect option name!"); | ||
215 | } | ||
216 | $name = strtolower($name); | ||
217 | if (array_key_exists($name, $this->_options)) { | ||
218 | // This is a Core option | ||
219 | $this->_setOption($name, $value); | ||
220 | return; | ||
221 | } | ||
222 | if (array_key_exists($name, $this->_specificOptions)) { | ||
223 | // This a specic option of this frontend | ||
224 | $this->_specificOptions[$name] = $value; | ||
225 | return; | ||
226 | } | ||
227 | } | ||
228 | |||
229 | /** | ||
230 | * Public frontend to get an option value | ||
231 | * | ||
232 | * @param string $name Name of the option | ||
233 | * @throws Zend_Cache_Exception | ||
234 | * @return mixed option value | ||
235 | */ | ||
236 | public function getOption($name) | ||
237 | { | ||
238 | $name = strtolower($name); | ||
239 | |||
240 | if (array_key_exists($name, $this->_options)) { | ||
241 | // This is a Core option | ||
242 | return $this->_options[$name]; | ||
243 | } | ||
244 | |||
245 | if (array_key_exists($name, $this->_specificOptions)) { | ||
246 | // This a specic option of this frontend | ||
247 | return $this->_specificOptions[$name]; | ||
248 | } | ||
249 | |||
250 | Zend_Cache::throwException("Incorrect option name : $name"); | ||
251 | } | ||
252 | |||
253 | /** | ||
254 | * Set an option | ||
255 | * | ||
256 | * @param string $name Name of the option | ||
257 | * @param mixed $value Value of the option | ||
258 | * @throws Zend_Cache_Exception | ||
259 | * @return void | ||
260 | */ | ||
261 | private function _setOption($name, $value) | ||
262 | { | ||
263 | if (!is_string($name) || !array_key_exists($name, $this->_options)) { | ||
264 | Zend_Cache::throwException("Incorrect option name : $name"); | ||
265 | } | ||
266 | if ($name == 'lifetime' && empty($value)) { | ||
267 | $value = null; | ||
268 | } | ||
269 | $this->_options[$name] = $value; | ||
270 | } | ||
271 | |||
272 | /** | ||
273 | * Force a new lifetime | ||
274 | * | ||
275 | * The new value is set for the core/frontend but for the backend too (directive) | ||
276 | * | ||
277 | * @param int $newLifetime New lifetime (in seconds) | ||
278 | * @return void | ||
279 | */ | ||
280 | public function setLifetime($newLifetime) | ||
281 | { | ||
282 | $this->_options['lifetime'] = $newLifetime; | ||
283 | $this->_backend->setDirectives(array( | ||
284 | 'lifetime' => $newLifetime | ||
285 | )); | ||
286 | } | ||
287 | |||
288 | /** | ||
289 | * Test if a cache is available for the given id and (if yes) return it (false else) | ||
290 | * | ||
291 | * @param string $id Cache id | ||
292 | * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested | ||
293 | * @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use | ||
294 | * @return mixed|false Cached datas | ||
295 | */ | ||
296 | public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false) | ||
297 | { | ||
298 | if (!$this->_options['caching']) { | ||
299 | return false; | ||
300 | } | ||
301 | $id = $this->_id($id); // cache id may need prefix | ||
302 | $this->_lastId = $id; | ||
303 | self::_validateIdOrTag($id); | ||
304 | |||
305 | $this->_log("Zend_Cache_Core: load item '{$id}'", 7); | ||
306 | $data = $this->_backend->load($id, $doNotTestCacheValidity); | ||
307 | if ($data===false) { | ||
308 | // no cache available | ||
309 | return false; | ||
310 | } | ||
311 | if ((!$doNotUnserialize) && $this->_options['automatic_serialization']) { | ||
312 | // we need to unserialize before sending the result | ||
313 | return unserialize($data); | ||
314 | } | ||
315 | return $data; | ||
316 | } | ||
317 | |||
318 | /** | ||
319 | * Test if a cache is available for the given id | ||
320 | * | ||
321 | * @param string $id Cache id | ||
322 | * @return int|false Last modified time of cache entry if it is available, false otherwise | ||
323 | */ | ||
324 | public function test($id) | ||
325 | { | ||
326 | if (!$this->_options['caching']) { | ||
327 | return false; | ||
328 | } | ||
329 | $id = $this->_id($id); // cache id may need prefix | ||
330 | self::_validateIdOrTag($id); | ||
331 | $this->_lastId = $id; | ||
332 | |||
333 | $this->_log("Zend_Cache_Core: test item '{$id}'", 7); | ||
334 | return $this->_backend->test($id); | ||
335 | } | ||
336 | |||
337 | /** | ||
338 | * Save some data in a cache | ||
339 | * | ||
340 | * @param mixed $data Data to put in cache (can be another type than string if automatic_serialization is on) | ||
341 | * @param string $id Cache id (if not set, the last cache id will be used) | ||
342 | * @param array $tags Cache tags | ||
343 | * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime) | ||
344 | * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends | ||
345 | * @throws Zend_Cache_Exception | ||
346 | * @return boolean True if no problem | ||
347 | */ | ||
348 | public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8) | ||
349 | { | ||
350 | if (!$this->_options['caching']) { | ||
351 | return true; | ||
352 | } | ||
353 | if ($id === null) { | ||
354 | $id = $this->_lastId; | ||
355 | } else { | ||
356 | $id = $this->_id($id); | ||
357 | } | ||
358 | self::_validateIdOrTag($id); | ||
359 | self::_validateTagsArray($tags); | ||
360 | if ($this->_options['automatic_serialization']) { | ||
361 | // we need to serialize datas before storing them | ||
362 | $data = serialize($data); | ||
363 | } else { | ||
364 | if (!is_string($data)) { | ||
365 | Zend_Cache::throwException("Datas must be string or set automatic_serialization = true"); | ||
366 | } | ||
367 | } | ||
368 | |||
369 | // automatic cleaning | ||
370 | if ($this->_options['automatic_cleaning_factor'] > 0) { | ||
371 | $rand = rand(1, $this->_options['automatic_cleaning_factor']); | ||
372 | if ($rand==1) { | ||
373 | // new way || deprecated way | ||
374 | if ($this->_extendedBackend || method_exists($this->_backend, 'isAutomaticCleaningAvailable')) { | ||
375 | $this->_log("Zend_Cache_Core::save(): automatic cleaning running", 7); | ||
376 | $this->clean(Zend_Cache::CLEANING_MODE_OLD); | ||
377 | } else { | ||
378 | $this->_log("Zend_Cache_Core::save(): automatic cleaning is not available/necessary with current backend", 4); | ||
379 | } | ||
380 | } | ||
381 | } | ||
382 | |||
383 | $this->_log("Zend_Cache_Core: save item '{$id}'", 7); | ||
384 | if ($this->_options['ignore_user_abort']) { | ||
385 | $abort = ignore_user_abort(true); | ||
386 | } | ||
387 | if (($this->_extendedBackend) && ($this->_backendCapabilities['priority'])) { | ||
388 | $result = $this->_backend->save($data, $id, $tags, $specificLifetime, $priority); | ||
389 | } else { | ||
390 | $result = $this->_backend->save($data, $id, $tags, $specificLifetime); | ||
391 | } | ||
392 | if ($this->_options['ignore_user_abort']) { | ||
393 | ignore_user_abort($abort); | ||
394 | } | ||
395 | |||
396 | if (!$result) { | ||
397 | // maybe the cache is corrupted, so we remove it ! | ||
398 | $this->_log("Zend_Cache_Core::save(): failed to save item '{$id}' -> removing it", 4); | ||
399 | $this->_backend->remove($id); | ||
400 | return false; | ||
401 | } | ||
402 | |||
403 | if ($this->_options['write_control']) { | ||
404 | $data2 = $this->_backend->load($id, true); | ||
405 | if ($data!=$data2) { | ||
406 | $this->_log("Zend_Cache_Core::save(): write control of item '{$id}' failed -> removing it", 4); | ||
407 | $this->_backend->remove($id); | ||
408 | return false; | ||
409 | } | ||
410 | } | ||
411 | |||
412 | return true; | ||
413 | } | ||
414 | |||
415 | /** | ||
416 | * Remove a cache | ||
417 | * | ||
418 | * @param string $id Cache id to remove | ||
419 | * @return boolean True if ok | ||
420 | */ | ||
421 | public function remove($id) | ||
422 | { | ||
423 | if (!$this->_options['caching']) { | ||
424 | return true; | ||
425 | } | ||
426 | $id = $this->_id($id); // cache id may need prefix | ||
427 | self::_validateIdOrTag($id); | ||
428 | |||
429 | $this->_log("Zend_Cache_Core: remove item '{$id}'", 7); | ||
430 | return $this->_backend->remove($id); | ||
431 | } | ||
432 | |||
433 | /** | ||
434 | * Clean cache entries | ||
435 | * | ||
436 | * Available modes are : | ||
437 | * 'all' (default) => remove all cache entries ($tags is not used) | ||
438 | * 'old' => remove too old cache entries ($tags is not used) | ||
439 | * 'matchingTag' => remove cache entries matching all given tags | ||
440 | * ($tags can be an array of strings or a single string) | ||
441 | * 'notMatchingTag' => remove cache entries not matching one of the given tags | ||
442 | * ($tags can be an array of strings or a single string) | ||
443 | * 'matchingAnyTag' => remove cache entries matching any given tags | ||
444 | * ($tags can be an array of strings or a single string) | ||
445 | * | ||
446 | * @param string $mode | ||
447 | * @param array|string $tags | ||
448 | * @throws Zend_Cache_Exception | ||
449 | * @return boolean True if ok | ||
450 | */ | ||
451 | public function clean($mode = 'all', $tags = array()) | ||
452 | { | ||
453 | if (!$this->_options['caching']) { | ||
454 | return true; | ||
455 | } | ||
456 | if (!in_array($mode, array(Zend_Cache::CLEANING_MODE_ALL, | ||
457 | Zend_Cache::CLEANING_MODE_OLD, | ||
458 | Zend_Cache::CLEANING_MODE_MATCHING_TAG, | ||
459 | Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG, | ||
460 | Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) { | ||
461 | Zend_Cache::throwException('Invalid cleaning mode'); | ||
462 | } | ||
463 | self::_validateTagsArray($tags); | ||
464 | |||
465 | return $this->_backend->clean($mode, $tags); | ||
466 | } | ||
467 | |||
468 | /** | ||
469 | * Return an array of stored cache ids which match given tags | ||
470 | * | ||
471 | * In case of multiple tags, a logical AND is made between tags | ||
472 | * | ||
473 | * @param array $tags array of tags | ||
474 | * @return array array of matching cache ids (string) | ||
475 | */ | ||
476 | public function getIdsMatchingTags($tags = array()) | ||
477 | { | ||
478 | if (!$this->_extendedBackend) { | ||
479 | Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); | ||
480 | } | ||
481 | if (!($this->_backendCapabilities['tags'])) { | ||
482 | Zend_Cache::throwException(self::BACKEND_NOT_SUPPORTS_TAG); | ||
483 | } | ||
484 | |||
485 | $ids = $this->_backend->getIdsMatchingTags($tags); | ||
486 | |||
487 | // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600) | ||
488 | if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') { | ||
489 | $prefix = & $this->_options['cache_id_prefix']; | ||
490 | $prefixLen = strlen($prefix); | ||
491 | foreach ($ids as &$id) { | ||
492 | if (strpos($id, $prefix) === 0) { | ||
493 | $id = substr($id, $prefixLen); | ||
494 | } | ||
495 | } | ||
496 | } | ||
497 | |||
498 | return $ids; | ||
499 | } | ||
500 | |||
501 | /** | ||
502 | * Return an array of stored cache ids which don't match given tags | ||
503 | * | ||
504 | * In case of multiple tags, a logical OR is made between tags | ||
505 | * | ||
506 | * @param array $tags array of tags | ||
507 | * @return array array of not matching cache ids (string) | ||
508 | */ | ||
509 | public function getIdsNotMatchingTags($tags = array()) | ||
510 | { | ||
511 | if (!$this->_extendedBackend) { | ||
512 | Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); | ||
513 | } | ||
514 | if (!($this->_backendCapabilities['tags'])) { | ||
515 | Zend_Cache::throwException(self::BACKEND_NOT_SUPPORTS_TAG); | ||
516 | } | ||
517 | |||
518 | $ids = $this->_backend->getIdsNotMatchingTags($tags); | ||
519 | |||
520 | // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600) | ||
521 | if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') { | ||
522 | $prefix = & $this->_options['cache_id_prefix']; | ||
523 | $prefixLen = strlen($prefix); | ||
524 | foreach ($ids as &$id) { | ||
525 | if (strpos($id, $prefix) === 0) { | ||
526 | $id = substr($id, $prefixLen); | ||
527 | } | ||
528 | } | ||
529 | } | ||
530 | |||
531 | return $ids; | ||
532 | } | ||
533 | |||
534 | /** | ||
535 | * Return an array of stored cache ids which match any given tags | ||
536 | * | ||
537 | * In case of multiple tags, a logical OR is made between tags | ||
538 | * | ||
539 | * @param array $tags array of tags | ||
540 | * @return array array of matching any cache ids (string) | ||
541 | */ | ||
542 | public function getIdsMatchingAnyTags($tags = array()) | ||
543 | { | ||
544 | if (!$this->_extendedBackend) { | ||
545 | Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); | ||
546 | } | ||
547 | if (!($this->_backendCapabilities['tags'])) { | ||
548 | Zend_Cache::throwException(self::BACKEND_NOT_SUPPORTS_TAG); | ||
549 | } | ||
550 | |||
551 | $ids = $this->_backend->getIdsMatchingAnyTags($tags); | ||
552 | |||
553 | // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600) | ||
554 | if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') { | ||
555 | $prefix = & $this->_options['cache_id_prefix']; | ||
556 | $prefixLen = strlen($prefix); | ||
557 | foreach ($ids as &$id) { | ||
558 | if (strpos($id, $prefix) === 0) { | ||
559 | $id = substr($id, $prefixLen); | ||
560 | } | ||
561 | } | ||
562 | } | ||
563 | |||
564 | return $ids; | ||
565 | } | ||
566 | |||
567 | /** | ||
568 | * Return an array of stored cache ids | ||
569 | * | ||
570 | * @return array array of stored cache ids (string) | ||
571 | */ | ||
572 | public function getIds() | ||
573 | { | ||
574 | if (!$this->_extendedBackend) { | ||
575 | Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); | ||
576 | } | ||
577 | |||
578 | $ids = $this->_backend->getIds(); | ||
579 | |||
580 | // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600) | ||
581 | if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') { | ||
582 | $prefix = & $this->_options['cache_id_prefix']; | ||
583 | $prefixLen = strlen($prefix); | ||
584 | foreach ($ids as &$id) { | ||
585 | if (strpos($id, $prefix) === 0) { | ||
586 | $id = substr($id, $prefixLen); | ||
587 | } | ||
588 | } | ||
589 | } | ||
590 | |||
591 | return $ids; | ||
592 | } | ||
593 | |||
594 | /** | ||
595 | * Return an array of stored tags | ||
596 | * | ||
597 | * @return array array of stored tags (string) | ||
598 | */ | ||
599 | public function getTags() | ||
600 | { | ||
601 | if (!$this->_extendedBackend) { | ||
602 | Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); | ||
603 | } | ||
604 | if (!($this->_backendCapabilities['tags'])) { | ||
605 | Zend_Cache::throwException(self::BACKEND_NOT_SUPPORTS_TAG); | ||
606 | } | ||
607 | return $this->_backend->getTags(); | ||
608 | } | ||
609 | |||
610 | /** | ||
611 | * Return the filling percentage of the backend storage | ||
612 | * | ||
613 | * @return int integer between 0 and 100 | ||
614 | */ | ||
615 | public function getFillingPercentage() | ||
616 | { | ||
617 | if (!$this->_extendedBackend) { | ||
618 | Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); | ||
619 | } | ||
620 | return $this->_backend->getFillingPercentage(); | ||
621 | } | ||
622 | |||
623 | /** | ||
624 | * Return an array of metadatas for the given cache id | ||
625 | * | ||
626 | * The array will include these keys : | ||
627 | * - expire : the expire timestamp | ||
628 | * - tags : a string array of tags | ||
629 | * - mtime : timestamp of last modification time | ||
630 | * | ||
631 | * @param string $id cache id | ||
632 | * @return array array of metadatas (false if the cache id is not found) | ||
633 | */ | ||
634 | public function getMetadatas($id) | ||
635 | { | ||
636 | if (!$this->_extendedBackend) { | ||
637 | Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); | ||
638 | } | ||
639 | $id = $this->_id($id); // cache id may need prefix | ||
640 | return $this->_backend->getMetadatas($id); | ||
641 | } | ||
642 | |||
643 | /** | ||
644 | * Give (if possible) an extra lifetime to the given cache id | ||
645 | * | ||
646 | * @param string $id cache id | ||
647 | * @param int $extraLifetime | ||
648 | * @return boolean true if ok | ||
649 | */ | ||
650 | public function touch($id, $extraLifetime) | ||
651 | { | ||
652 | if (!$this->_extendedBackend) { | ||
653 | Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); | ||
654 | } | ||
655 | $id = $this->_id($id); // cache id may need prefix | ||
656 | |||
657 | $this->_log("Zend_Cache_Core: touch item '{$id}'", 7); | ||
658 | return $this->_backend->touch($id, $extraLifetime); | ||
659 | } | ||
660 | |||
661 | /** | ||
662 | * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...) | ||
663 | * | ||
664 | * Throw an exception if a problem is found | ||
665 | * | ||
666 | * @param string $string Cache id or tag | ||
667 | * @throws Zend_Cache_Exception | ||
668 | * @return void | ||
669 | */ | ||
670 | protected static function _validateIdOrTag($string) | ||
671 | { | ||
672 | if (!is_string($string)) { | ||
673 | Zend_Cache::throwException('Invalid id or tag : must be a string'); | ||
674 | } | ||
675 | if (substr($string, 0, 9) == 'internal-') { | ||
676 | Zend_Cache::throwException('"internal-*" ids or tags are reserved'); | ||
677 | } | ||
678 | if (!preg_match('~^[a-zA-Z0-9_]+$~D', $string)) { | ||
679 | Zend_Cache::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_]"); | ||
680 | } | ||
681 | } | ||
682 | |||
683 | /** | ||
684 | * Validate a tags array (security, reliable filenames, reserved prefixes...) | ||
685 | * | ||
686 | * Throw an exception if a problem is found | ||
687 | * | ||
688 | * @param array $tags Array of tags | ||
689 | * @throws Zend_Cache_Exception | ||
690 | * @return void | ||
691 | */ | ||
692 | protected static function _validateTagsArray($tags) | ||
693 | { | ||
694 | if (!is_array($tags)) { | ||
695 | Zend_Cache::throwException('Invalid tags array : must be an array'); | ||
696 | } | ||
697 | foreach($tags as $tag) { | ||
698 | self::_validateIdOrTag($tag); | ||
699 | } | ||
700 | reset($tags); | ||
701 | } | ||
702 | |||
703 | /** | ||
704 | * Make sure if we enable logging that the Zend_Log class | ||
705 | * is available. | ||
706 | * Create a default log object if none is set. | ||
707 | * | ||
708 | * @throws Zend_Cache_Exception | ||
709 | * @return void | ||
710 | */ | ||
711 | protected function _loggerSanity() | ||
712 | { | ||
713 | if (!isset($this->_options['logging']) || !$this->_options['logging']) { | ||
714 | return; | ||
715 | } | ||
716 | |||
717 | if (isset($this->_options['logger']) && $this->_options['logger'] instanceof Zend_Log) { | ||
718 | return; | ||
719 | } | ||
720 | |||
721 | // Create a default logger to the standard output stream | ||
722 | require_once 'Zend/Log.php'; | ||
723 | require_once 'Zend/Log/Writer/Stream.php'; | ||
724 | require_once 'Zend/Log/Filter/Priority.php'; | ||
725 | $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output')); | ||
726 | $logger->addFilter(new Zend_Log_Filter_Priority(Zend_Log::WARN, '<=')); | ||
727 | $this->_options['logger'] = $logger; | ||
728 | } | ||
729 | |||
730 | /** | ||
731 | * Log a message at the WARN (4) priority. | ||
732 | * | ||
733 | * @param string $message | ||
734 | * @throws Zend_Cache_Exception | ||
735 | * @return void | ||
736 | */ | ||
737 | protected function _log($message, $priority = 4) | ||
738 | { | ||
739 | if (!$this->_options['logging']) { | ||
740 | return; | ||
741 | } | ||
742 | if (!(isset($this->_options['logger']) || $this->_options['logger'] instanceof Zend_Log)) { | ||
743 | Zend_Cache::throwException('Logging is enabled but logger is not set'); | ||
744 | } | ||
745 | $logger = $this->_options['logger']; | ||
746 | $logger->log($message, $priority); | ||
747 | } | ||
748 | |||
749 | /** | ||
750 | * Make and return a cache id | ||
751 | * | ||
752 | * Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null | ||
753 | * | ||
754 | * @param string $id Cache id | ||
755 | * @return string Cache id (with or without prefix) | ||
756 | */ | ||
757 | protected function _id($id) | ||
758 | { | ||
759 | if (($id !== null) && isset($this->_options['cache_id_prefix'])) { | ||
760 | return $this->_options['cache_id_prefix'] . $id; // return with prefix | ||
761 | } | ||
762 | return $id; // no prefix, just return the $id passed | ||
763 | } | ||
764 | |||
765 | } | ||