[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / textwheel / inc / textwheel.php
index 2e6453d..d2de775 100644 (file)
@@ -3,20 +3,24 @@
 /***************************************************************************\
  *  SPIP, Systeme de publication pour l'internet                           *
  *                                                                         *
- *  Copyright (c) 2001-2013                                                *
+ *  Copyright (c) 2001-2019                                                *
  *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
  *                                                                         *
  *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
  *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
 \***************************************************************************/
 
-if (!defined("_ECRIRE_INC_VERSION")) return;
+if (!defined("_ECRIRE_INC_VERSION")) {
+       return;
+}
 
 include_spip('engine/textwheel');
 // si une regle change et rend son cache non valide
 // incrementer ce define au numero de commit concerne
 // (inconsistence entre la wheel et l'inclusion php)
-if (!defined('_WHEELS_VERSION')) define('_WHEELS_VERSION',68672);
+if (!defined('_WHEELS_VERSION')) {
+       define('_WHEELS_VERSION', 68672);
+}
 
 //
 // Definition des principales wheels de SPIP
@@ -26,12 +30,12 @@ if (!isset($GLOBALS['spip_wheels'])) {
 }
 
 // Si le tableau des raccourcis existe déjà
-if (!isset($GLOBALS['spip_wheels']['raccourcis']) OR !is_array($GLOBALS['spip_wheels']['raccourcis']))
+if (!isset($GLOBALS['spip_wheels']['raccourcis']) or !is_array($GLOBALS['spip_wheels']['raccourcis'])) {
        $GLOBALS['spip_wheels']['raccourcis'] = array(
                'spip/spip.yaml',
                'spip/spip-paragrapher.yaml'
        );
-else
+} else {
        $GLOBALS['spip_wheels']['raccourcis'] = array_merge(
                array(
                        'spip/spip.yaml',
@@ -39,9 +43,11 @@ else
                ),
                $GLOBALS['spip_wheels']['raccourcis']
        );
+}
 
-if (test_espace_prive ())
+if (test_espace_prive()) {
        $GLOBALS['spip_wheels']['raccourcis'][] = 'spip/ecrire.yaml';
+}
 
 $GLOBALS['spip_wheels']['interdire_scripts'] = array(
        'spip/interdire-scripts.yaml'
@@ -64,18 +70,20 @@ $GLOBALS['spip_wheels']['listes'] = array(
 //
 
 class SPIPTextWheelRuleset extends TextWheelRuleSet {
-       protected function findFile(&$file, $path=''){
+       protected function findFile(&$file, $path = '') {
                static $default_path;
 
                // absolute file path?
-               if (file_exists($file))
+               if (file_exists($file)) {
                        return $file;
+               }
 
                // file include with texwheels, relative to calling ruleset
-               if ($path AND file_exists($f = $path.$file))
+               if ($path and file_exists($f = $path . $file)) {
                        return $f;
+               }
 
-               return find_in_path($file,'wheels/');
+               return find_in_path($file, 'wheels/');
        }
 
        public static function &loader($ruleset, $callback = '', $class = 'SPIPTextWheelRuleset') {
@@ -83,30 +91,70 @@ class SPIPTextWheelRuleset extends TextWheelRuleSet {
                # memoization
                # attention : le ruleset peut contenir apres loading des chemins relatifs
                # il faut donc que le cache depende du chemin courant vers la racine de SPIP
-               $key = 'tw-'.md5(_WHEELS_VERSION."-".serialize($ruleset).$callback.$class._DIR_RACINE);
+               $key = "";
+               if ($callback) {
+                       $key = $callback($key);
+               }
+               $key = 'tw-' . md5(_WHEELS_VERSION . "-" . serialize($ruleset) . $key . $class . _DIR_RACINE);
 
                # lecture du cache
-               include_spip('inc/memoization');
-               if (!function_exists('cache_get')) include_spip('inc/memoization-mini');
-               if ((!defined('_VAR_MODE') OR _VAR_MODE!='recalcul')
-                 AND $cacheruleset = cache_get($key))
+               if ((!defined('_VAR_MODE') or _VAR_MODE != 'recalcul')
+                       and $cacheruleset = tw_cache_get($key)
+               ) {
                        return $cacheruleset;
+               }
 
                # calcul de la wheel
                $ruleset = parent::loader($ruleset, $callback, $class);
 
                # ecriture du cache
-               cache_set($key, $ruleset);
+               tw_cache_set($key, $ruleset);
 
                return $ruleset;
        }
 }
 
 
-function tw_trig_purger($quoi){
-       if ($quoi=='cache')
-               purger_repertoire(_DIR_CACHE."wheels");
+function tw_trig_purger($quoi) {
+       if ($quoi == 'cache') {
+               purger_repertoire(_DIR_CACHE . "wheels");
+       }
+
        return $quoi;
 }
 
-?>
+
+/**
+ * Lire une valeur en cache
+ * memoization minimale
+ * (utilise le plugin memoization si disponible)
+ *
+ * @param string $key
+ * @return mixed
+ */
+function tw_cache_get($key) {
+       if (function_exists('cache_get')) {
+               return cache_get($key);
+       }
+
+       return @unserialize(file_get_contents(_DIR_CACHE . "wheels/" . $key . ".txt"));
+}
+
+/**
+ * Ecrire une valeur en cache
+ * memoization minimale
+ * (utilise le plugin memoization si disponible)
+ *
+ * @param string $key
+ * @param mixed $value
+ * @param int|null $ttl
+ * @return bool
+ */
+function tw_cache_set($key, $value, $ttl = null) {
+       if (function_exists('cache_set')) {
+               return cache_set($key, $value, $ttl);
+       }
+       $dir = sous_repertoire(_DIR_CACHE, "wheels/");
+
+       return ecrire_fichier($dir . $key . ".txt", serialize($value));
+}