[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / plugins-dist / textwheel / inc / textwheel.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined("_ECRIRE_INC_VERSION")) return;
14
15 include_spip('engine/textwheel');
16 // si une regle change et rend son cache non valide
17 // incrementer ce define au numero de commit concerne
18 // (inconsistence entre la wheel et l'inclusion php)
19 if (!defined('_WHEELS_VERSION')) define('_WHEELS_VERSION',68672);
20
21 //
22 // Definition des principales wheels de SPIP
23 //
24 if (!isset($GLOBALS['spip_wheels'])) {
25 $GLOBALS['spip_wheels'] = array();
26 }
27
28 // Si le tableau des raccourcis existe déjà
29 if (!isset($GLOBALS['spip_wheels']['raccourcis']) OR !is_array($GLOBALS['spip_wheels']['raccourcis']))
30 $GLOBALS['spip_wheels']['raccourcis'] = array(
31 'spip/spip.yaml',
32 'spip/spip-paragrapher.yaml'
33 );
34 else
35 $GLOBALS['spip_wheels']['raccourcis'] = array_merge(
36 array(
37 'spip/spip.yaml',
38 'spip/spip-paragrapher.yaml'
39 ),
40 $GLOBALS['spip_wheels']['raccourcis']
41 );
42
43 if (test_espace_prive ())
44 $GLOBALS['spip_wheels']['raccourcis'][] = 'spip/ecrire.yaml';
45
46 $GLOBALS['spip_wheels']['interdire_scripts'] = array(
47 'spip/interdire-scripts.yaml'
48 );
49
50 $GLOBALS['spip_wheels']['echappe_js'] = array(
51 'spip/echappe-js.yaml'
52 );
53
54 $GLOBALS['spip_wheels']['paragrapher'] = array(
55 'spip/spip-paragrapher.yaml'
56 );
57
58 $GLOBALS['spip_wheels']['listes'] = array(
59 'spip/spip-listes.yaml'
60 );
61
62 //
63 // Methode de chargement d'une wheel SPIP
64 //
65
66 class SPIPTextWheelRuleset extends TextWheelRuleSet {
67 protected function findFile(&$file, $path=''){
68 static $default_path;
69
70 // absolute file path?
71 if (file_exists($file))
72 return $file;
73
74 // file include with texwheels, relative to calling ruleset
75 if ($path AND file_exists($f = $path.$file))
76 return $f;
77
78 return find_in_path($file,'wheels/');
79 }
80
81 public static function &loader($ruleset, $callback = '', $class = 'SPIPTextWheelRuleset') {
82
83 # memoization
84 # attention : le ruleset peut contenir apres loading des chemins relatifs
85 # il faut donc que le cache depende du chemin courant vers la racine de SPIP
86 $key = 'tw-'.md5(_WHEELS_VERSION."-".serialize($ruleset).$callback.$class._DIR_RACINE);
87
88 # lecture du cache
89 if ((!defined('_VAR_MODE') OR _VAR_MODE!='recalcul')
90 AND $cacheruleset = tw_cache_get($key))
91 return $cacheruleset;
92
93 # calcul de la wheel
94 $ruleset = parent::loader($ruleset, $callback, $class);
95
96 # ecriture du cache
97 tw_cache_set($key, $ruleset);
98
99 return $ruleset;
100 }
101 }
102
103
104 function tw_trig_purger($quoi){
105 if ($quoi=='cache')
106 purger_repertoire(_DIR_CACHE."wheels");
107 return $quoi;
108 }
109
110
111 /**
112 * Lire une valeur en cache
113 * memoization minimale
114 * (utilise le plugin memoization si disponible)
115 *
116 * @param string $key
117 * @return mixed
118 */
119 function tw_cache_get($key) {
120 if (function_exists('cache_get')){
121 return cache_get($key);
122 }
123 return @unserialize(file_get_contents(_DIR_CACHE."wheels/".$key.".txt"));
124 }
125
126 /**
127 * Ecrire une valeur en cache
128 * memoization minimale
129 * (utilise le plugin memoization si disponible)
130 *
131 * @param string $key
132 * @param mixed $value
133 * @param int|null $ttl
134 * @return bool
135 */
136 function tw_cache_set($key, $value, $ttl=null) {
137 if (function_exists('cache_set')){
138 return cache_set($key, $value, $ttl);
139 }
140 $dir = sous_repertoire(_DIR_CACHE,"wheels/");
141 return ecrire_fichier($dir.$key.".txt", serialize($value));
142 }