@@ -23,16 +23,16 @@ discard block |
||
| 23 | 23 | * @return |
| 24 | 24 | */ |
| 25 | 25 | function iterateur_DATA_dist($b) { |
| 26 | - $b->iterateur = 'DATA'; # designe la classe d'iterateur |
|
| 27 | - $b->show = array( |
|
| 28 | - 'field' => array( |
|
| 29 | - 'cle' => 'STRING', |
|
| 30 | - 'valeur' => 'STRING', |
|
| 31 | - '*' => 'ALL' // Champ joker * |
|
| 32 | - ) |
|
| 33 | - ); |
|
| 34 | - $b->select[] = '.valeur'; |
|
| 35 | - return $b; |
|
| 26 | + $b->iterateur = 'DATA'; # designe la classe d'iterateur |
|
| 27 | + $b->show = array( |
|
| 28 | + 'field' => array( |
|
| 29 | + 'cle' => 'STRING', |
|
| 30 | + 'valeur' => 'STRING', |
|
| 31 | + '*' => 'ALL' // Champ joker * |
|
| 32 | + ) |
|
| 33 | + ); |
|
| 34 | + $b->select[] = '.valeur'; |
|
| 35 | + return $b; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | |
@@ -40,453 +40,453 @@ discard block |
||
| 40 | 40 | * IterateurDATA pour iterer sur des donnees |
| 41 | 41 | */ |
| 42 | 42 | class IterateurDATA implements Iterator { |
| 43 | - /** |
|
| 44 | - * tableau de donnees |
|
| 45 | - * @var array |
|
| 46 | - */ |
|
| 47 | - protected $tableau = array(); |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Conditions de filtrage |
|
| 51 | - * ie criteres de selection |
|
| 52 | - * @var array |
|
| 53 | - */ |
|
| 54 | - protected $filtre = array(); |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Cle courante |
|
| 59 | - * @var null |
|
| 60 | - */ |
|
| 61 | - protected $cle = null; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Valeur courante |
|
| 65 | - * @var null |
|
| 66 | - */ |
|
| 67 | - protected $valeur = null; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Erreur presente ? |
|
| 71 | - * |
|
| 72 | - * @var bool |
|
| 73 | - **/ |
|
| 74 | - public $err = false; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Calcul du total des elements |
|
| 78 | - * |
|
| 79 | - * @var int|null |
|
| 80 | - **/ |
|
| 81 | - public $total = null; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Constructeur |
|
| 85 | - * |
|
| 86 | - * @param $command |
|
| 87 | - * @param array $info |
|
| 88 | - */ |
|
| 89 | - public function __construct($command, $info=array()) { |
|
| 90 | - $this->type='DATA'; |
|
| 91 | - $this->command = $command; |
|
| 92 | - $this->info = $info; |
|
| 93 | - |
|
| 94 | - $this->select($command); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Revenir au depart |
|
| 99 | - * @return void |
|
| 100 | - */ |
|
| 101 | - public function rewind() { |
|
| 102 | - reset($this->tableau); |
|
| 103 | - list($this->cle, $this->valeur) = each($this->tableau); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Declarer les criteres exceptions |
|
| 108 | - * @return array |
|
| 109 | - */ |
|
| 110 | - public function exception_des_criteres() { |
|
| 111 | - return array('tableau'); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Recuperer depuis le cache si possible |
|
| 116 | - * @param $cle |
|
| 117 | - * @return |
|
| 118 | - */ |
|
| 119 | - protected function cache_get($cle) { |
|
| 120 | - if (!$cle) return; |
|
| 121 | - # utiliser memoization si dispo |
|
| 122 | - include_spip('inc/memoization'); |
|
| 123 | - if (!function_exists('cache_get')) return; |
|
| 124 | - return cache_get($cle); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Stocker en cache si possible |
|
| 129 | - * @param $cle |
|
| 130 | - * @param $ttl |
|
| 131 | - * @return |
|
| 132 | - */ |
|
| 133 | - protected function cache_set($cle, $ttl, $valeur = null) { |
|
| 134 | - if (!$cle) return; |
|
| 135 | - if (is_null($valeur)) { |
|
| 136 | - $valeur = $this->tableau; |
|
| 137 | - } |
|
| 138 | - # utiliser memoization si dispo |
|
| 139 | - include_spip('inc/memoization'); |
|
| 140 | - if (!function_exists('cache_set')) return; |
|
| 141 | - return cache_set($cle, |
|
| 142 | - array( |
|
| 143 | - 'data' => $valeur, |
|
| 144 | - 'time' => time(), |
|
| 145 | - 'ttl' => $ttl |
|
| 146 | - ), |
|
| 147 | - 3600 + $ttl); |
|
| 148 | - # conserver le cache 1h de plus que la validite demandee, |
|
| 149 | - # pour le cas ou le serveur distant ne reponde plus |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Aller chercher les donnees de la boucle DATA |
|
| 154 | - * |
|
| 155 | - * @throws Exception |
|
| 156 | - * @param $command |
|
| 157 | - * @return void |
|
| 158 | - */ |
|
| 159 | - protected function select($command) { |
|
| 160 | - |
|
| 161 | - // l'iterateur DATA peut etre appele en passant (data:type) |
|
| 162 | - // le type se retrouve dans la commande 'from' |
|
| 163 | - // dans ce cas la le critere {source}, si present, n'a pas besoin du 1er argument |
|
| 164 | - if (isset($this->command['from'][0])) { |
|
| 165 | - if (isset($this->command['source']) and is_array($this->command['source'])) { |
|
| 166 | - array_unshift($this->command['source'], $this->command['sourcemode']); |
|
| 167 | - } |
|
| 168 | - $this->command['sourcemode'] = $this->command['from'][0]; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // cherchons differents moyens de creer le tableau de donnees |
|
| 172 | - // les commandes connues pour l'iterateur DATA |
|
| 173 | - // sont : {tableau #ARRAY} ; {cle=...} ; {valeur=...} |
|
| 43 | + /** |
|
| 44 | + * tableau de donnees |
|
| 45 | + * @var array |
|
| 46 | + */ |
|
| 47 | + protected $tableau = array(); |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Conditions de filtrage |
|
| 51 | + * ie criteres de selection |
|
| 52 | + * @var array |
|
| 53 | + */ |
|
| 54 | + protected $filtre = array(); |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Cle courante |
|
| 59 | + * @var null |
|
| 60 | + */ |
|
| 61 | + protected $cle = null; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Valeur courante |
|
| 65 | + * @var null |
|
| 66 | + */ |
|
| 67 | + protected $valeur = null; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Erreur presente ? |
|
| 71 | + * |
|
| 72 | + * @var bool |
|
| 73 | + **/ |
|
| 74 | + public $err = false; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Calcul du total des elements |
|
| 78 | + * |
|
| 79 | + * @var int|null |
|
| 80 | + **/ |
|
| 81 | + public $total = null; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Constructeur |
|
| 85 | + * |
|
| 86 | + * @param $command |
|
| 87 | + * @param array $info |
|
| 88 | + */ |
|
| 89 | + public function __construct($command, $info=array()) { |
|
| 90 | + $this->type='DATA'; |
|
| 91 | + $this->command = $command; |
|
| 92 | + $this->info = $info; |
|
| 93 | + |
|
| 94 | + $this->select($command); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Revenir au depart |
|
| 99 | + * @return void |
|
| 100 | + */ |
|
| 101 | + public function rewind() { |
|
| 102 | + reset($this->tableau); |
|
| 103 | + list($this->cle, $this->valeur) = each($this->tableau); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Declarer les criteres exceptions |
|
| 108 | + * @return array |
|
| 109 | + */ |
|
| 110 | + public function exception_des_criteres() { |
|
| 111 | + return array('tableau'); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Recuperer depuis le cache si possible |
|
| 116 | + * @param $cle |
|
| 117 | + * @return |
|
| 118 | + */ |
|
| 119 | + protected function cache_get($cle) { |
|
| 120 | + if (!$cle) return; |
|
| 121 | + # utiliser memoization si dispo |
|
| 122 | + include_spip('inc/memoization'); |
|
| 123 | + if (!function_exists('cache_get')) return; |
|
| 124 | + return cache_get($cle); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Stocker en cache si possible |
|
| 129 | + * @param $cle |
|
| 130 | + * @param $ttl |
|
| 131 | + * @return |
|
| 132 | + */ |
|
| 133 | + protected function cache_set($cle, $ttl, $valeur = null) { |
|
| 134 | + if (!$cle) return; |
|
| 135 | + if (is_null($valeur)) { |
|
| 136 | + $valeur = $this->tableau; |
|
| 137 | + } |
|
| 138 | + # utiliser memoization si dispo |
|
| 139 | + include_spip('inc/memoization'); |
|
| 140 | + if (!function_exists('cache_set')) return; |
|
| 141 | + return cache_set($cle, |
|
| 142 | + array( |
|
| 143 | + 'data' => $valeur, |
|
| 144 | + 'time' => time(), |
|
| 145 | + 'ttl' => $ttl |
|
| 146 | + ), |
|
| 147 | + 3600 + $ttl); |
|
| 148 | + # conserver le cache 1h de plus que la validite demandee, |
|
| 149 | + # pour le cas ou le serveur distant ne reponde plus |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Aller chercher les donnees de la boucle DATA |
|
| 154 | + * |
|
| 155 | + * @throws Exception |
|
| 156 | + * @param $command |
|
| 157 | + * @return void |
|
| 158 | + */ |
|
| 159 | + protected function select($command) { |
|
| 160 | + |
|
| 161 | + // l'iterateur DATA peut etre appele en passant (data:type) |
|
| 162 | + // le type se retrouve dans la commande 'from' |
|
| 163 | + // dans ce cas la le critere {source}, si present, n'a pas besoin du 1er argument |
|
| 164 | + if (isset($this->command['from'][0])) { |
|
| 165 | + if (isset($this->command['source']) and is_array($this->command['source'])) { |
|
| 166 | + array_unshift($this->command['source'], $this->command['sourcemode']); |
|
| 167 | + } |
|
| 168 | + $this->command['sourcemode'] = $this->command['from'][0]; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // cherchons differents moyens de creer le tableau de donnees |
|
| 172 | + // les commandes connues pour l'iterateur DATA |
|
| 173 | + // sont : {tableau #ARRAY} ; {cle=...} ; {valeur=...} |
|
| 174 | 174 | |
| 175 | - // {source format, [URL], [arg2]...} |
|
| 176 | - if (isset($this->command['source']) |
|
| 177 | - AND isset($this->command['sourcemode'])) { |
|
| 178 | - $this->select_source(); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // Critere {liste X1, X2, X3} |
|
| 182 | - if (isset($this->command['liste'])) { |
|
| 183 | - $this->select_liste(); |
|
| 184 | - } |
|
| 185 | - if (isset($this->command['enum'])) { |
|
| 186 | - $this->select_enum(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - // Si a ce stade on n'a pas de table, il y a un bug |
|
| 190 | - if (!is_array($this->tableau)) { |
|
| 191 | - $this->err = true; |
|
| 192 | - spip_log("erreur datasource ".var_export($command,true)); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // {datapath query.results} |
|
| 196 | - // extraire le chemin "query.results" du tableau de donnees |
|
| 197 | - if (!$this->err |
|
| 198 | - AND isset($this->command['datapath']) |
|
| 199 | - AND is_array($this->command['datapath'])) { |
|
| 200 | - $this->select_datapath(); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - // tri {par x} |
|
| 204 | - if ($this->command['orderby']) { |
|
| 205 | - $this->select_orderby(); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - // grouper les resultats {fusion /x/y/z} ; |
|
| 209 | - if ($this->command['groupby']) { |
|
| 210 | - $this->select_groupby(); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $this->rewind(); |
|
| 214 | - #var_dump($this->tableau); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Aller chercher les donnees de la boucle DATA |
|
| 220 | - * depuis une source |
|
| 221 | - * {source format, [URL], [arg2]...} |
|
| 222 | - */ |
|
| 223 | - protected function select_source() { |
|
| 224 | - # un peu crado : avant de charger le cache il faut charger |
|
| 225 | - # les class indispensables, sinon PHP ne saura pas gerer |
|
| 226 | - # l'objet en cache ; cf plugins/icalendar |
|
| 227 | - # perf : pas de fonction table_to_array ! (table est deja un array) |
|
| 228 | - if (isset($this->command['sourcemode']) |
|
| 229 | - AND !in_array($this->command['sourcemode'],array('table', 'array', 'tableau'))) |
|
| 230 | - charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 231 | - |
|
| 232 | - # le premier argument peut etre un array, une URL etc. |
|
| 233 | - $src = $this->command['source'][0]; |
|
| 234 | - |
|
| 235 | - # avons-nous un cache dispo ? |
|
| 236 | - $cle = null; |
|
| 237 | - if (is_string($src)) |
|
| 238 | - $cle = 'datasource_'.md5($this->command['sourcemode'].':'.var_export($this->command['source'],true)); |
|
| 239 | - |
|
| 240 | - $cache = $this->cache_get($cle); |
|
| 241 | - if (isset($this->command['datacache'])) |
|
| 242 | - $ttl = intval($this->command['datacache']); |
|
| 243 | - if ($cache |
|
| 244 | - AND ($cache['time'] + (isset($ttl) ? $ttl : $cache['ttl']) |
|
| 245 | - > time()) |
|
| 246 | - AND !(_request('var_mode') === 'recalcul' |
|
| 247 | - AND include_spip('inc/autoriser') |
|
| 248 | - AND autoriser('recalcul') |
|
| 249 | - )) { |
|
| 250 | - $this->tableau = $cache['data']; |
|
| 251 | - } |
|
| 252 | - else try { |
|
| 253 | - # dommage que ca ne soit pas une option de yql_to_array... |
|
| 254 | - if ($this->command['sourcemode'] == 'yql') |
|
| 255 | - if (!isset($ttl)) $ttl = 3600; |
|
| 256 | - |
|
| 257 | - if (isset($this->command['sourcemode']) |
|
| 258 | - AND in_array($this->command['sourcemode'], |
|
| 259 | - array('table', 'array', 'tableau')) |
|
| 260 | - ) { |
|
| 261 | - if (is_array($a = $src) |
|
| 262 | - OR (is_string($a) |
|
| 263 | - AND $a = str_replace('"', '"', $a) # fragile! |
|
| 264 | - AND is_array($a = @unserialize($a))) |
|
| 265 | - ) |
|
| 266 | - $this->tableau = $a; |
|
| 267 | - } |
|
| 268 | - else { |
|
| 269 | - if (preg_match(',^https?://,', $src)) { |
|
| 270 | - include_spip('inc/distant'); |
|
| 271 | - $u = recuperer_page($src, false, false, _DATA_SOURCE_MAX_SIZE); |
|
| 272 | - if (!$u) |
|
| 273 | - throw new Exception("404"); |
|
| 274 | - if (!isset($ttl)) $ttl = 24*3600; |
|
| 275 | - } else if (@is_dir($src)) { |
|
| 276 | - $u = $src; |
|
| 277 | - if (!isset($ttl)) $ttl = 10; |
|
| 278 | - } else if (@is_readable($src) && @is_file($src)) { |
|
| 279 | - $u = spip_file_get_contents($src); |
|
| 280 | - if (!isset($ttl)) $ttl = 10; |
|
| 281 | - } else { |
|
| 282 | - $u = $src; |
|
| 283 | - if (!isset($ttl)) $ttl = 10; |
|
| 284 | - } |
|
| 285 | - if (!$this->err |
|
| 286 | - AND $g = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true)) { |
|
| 287 | - $args = $this->command['source']; |
|
| 288 | - $args[0] = $u; |
|
| 289 | - if (is_array($a = call_user_func_array($g,$args))) { |
|
| 290 | - $this->tableau = $a; |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - if (!is_array($this->tableau)) |
|
| 296 | - $this->err = true; |
|
| 297 | - |
|
| 298 | - if (!$this->err AND isset($ttl) and $ttl>0) |
|
| 299 | - $this->cache_set($cle, $ttl); |
|
| 300 | - |
|
| 301 | - } |
|
| 302 | - catch (Exception $e) { |
|
| 303 | - $e = $e->getMessage(); |
|
| 304 | - $err = sprintf("[%s, %s] $e", |
|
| 305 | - $src, |
|
| 306 | - $this->command['sourcemode']); |
|
| 307 | - erreur_squelette(array($err, array())); |
|
| 308 | - $this->err = true; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - # en cas d'erreur, utiliser le cache si encore dispo |
|
| 312 | - if ($this->err |
|
| 313 | - AND $cache) { |
|
| 314 | - $this->tableau = $cache['data']; |
|
| 315 | - $this->err = false; |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Retourne un tableau donne depuis un critere liste |
|
| 322 | - * Critere {liste X1, X2, X3} |
|
| 323 | - * |
|
| 324 | - **/ |
|
| 325 | - protected function select_liste() { |
|
| 326 | - # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 327 | - if (!isset($this->command['liste'][1])) { |
|
| 328 | - if (!is_array($this->command['liste'][0])) { |
|
| 329 | - $this->command['liste'] = explode(',', $this->command['liste'][0]); |
|
| 330 | - } else { |
|
| 331 | - $this->command['liste'] = $this->command['liste'][0]; |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - $this->tableau = $this->command['liste']; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Retourne un tableau donne depuis un critere liste |
|
| 339 | - * Critere {enum Xmin, Xmax} |
|
| 340 | - * |
|
| 341 | - **/ |
|
| 342 | - protected function select_enum() { |
|
| 343 | - # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 344 | - if (!isset($this->command['enum'][1])) { |
|
| 345 | - if (!is_array($this->command['enum'][0])) { |
|
| 346 | - $this->command['enum'] = explode(',', $this->command['enum'][0]); |
|
| 347 | - } else { |
|
| 348 | - $this->command['enum'] = $this->command['enum'][0]; |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - if (count($this->command['enum'])>=3) |
|
| 352 | - $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 353 | - else |
|
| 354 | - $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 355 | - $this->tableau = $enum; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * extraire le chemin "query.results" du tableau de donnees |
|
| 361 | - * {datapath query.results} |
|
| 362 | - * |
|
| 363 | - **/ |
|
| 364 | - protected function select_datapath() { |
|
| 365 | - list(,$base) = each($this->command['datapath']); |
|
| 366 | - if (strlen($base = ltrim(trim($base),"/"))) { |
|
| 367 | - $this->tableau = table_valeur($this->tableau, $base); |
|
| 368 | - if (!is_array($this->tableau)) { |
|
| 369 | - $this->tableau = array(); |
|
| 370 | - $this->err = true; |
|
| 371 | - spip_log("datapath '$base' absent"); |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Ordonner les resultats |
|
| 378 | - * {par x} |
|
| 379 | - * |
|
| 380 | - **/ |
|
| 381 | - protected function select_orderby() { |
|
| 382 | - $sortfunc = ''; |
|
| 383 | - $aleas = 0; |
|
| 384 | - foreach($this->command['orderby'] as $tri) { |
|
| 385 | - // virer le / initial pour les criteres de la forme {par /xx} |
|
| 386 | - if (preg_match(',^\.?([/\w]+)( DESC)?$,iS', ltrim($tri, '/'), $r)) { |
|
| 387 | - // tri par cle |
|
| 388 | - if ($r[1] == 'cle'){ |
|
| 389 | - if (isset($r[2]) and $r[2]) |
|
| 390 | - krsort($this->tableau); |
|
| 391 | - else |
|
| 392 | - ksort($this->tableau); |
|
| 393 | - } |
|
| 394 | - # {par hasard} |
|
| 395 | - else if ($r[1] == 'alea') { |
|
| 396 | - $k = array_keys($this->tableau); |
|
| 397 | - shuffle($k); |
|
| 398 | - $v = array(); |
|
| 399 | - foreach($k as $cle) |
|
| 400 | - $v[$cle] = $this->tableau[$cle]; |
|
| 401 | - $this->tableau = $v; |
|
| 402 | - } |
|
| 403 | - else { |
|
| 404 | - # {par valeur} |
|
| 405 | - if ($r[1] == 'valeur') |
|
| 406 | - $tv = '%s'; |
|
| 407 | - # {par valeur/xx/yy} ?? |
|
| 408 | - else |
|
| 409 | - $tv = 'table_valeur(%s, '.var_export($r[1],true).')'; |
|
| 410 | - $sortfunc .= ' |
|
| 175 | + // {source format, [URL], [arg2]...} |
|
| 176 | + if (isset($this->command['source']) |
|
| 177 | + AND isset($this->command['sourcemode'])) { |
|
| 178 | + $this->select_source(); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // Critere {liste X1, X2, X3} |
|
| 182 | + if (isset($this->command['liste'])) { |
|
| 183 | + $this->select_liste(); |
|
| 184 | + } |
|
| 185 | + if (isset($this->command['enum'])) { |
|
| 186 | + $this->select_enum(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + // Si a ce stade on n'a pas de table, il y a un bug |
|
| 190 | + if (!is_array($this->tableau)) { |
|
| 191 | + $this->err = true; |
|
| 192 | + spip_log("erreur datasource ".var_export($command,true)); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // {datapath query.results} |
|
| 196 | + // extraire le chemin "query.results" du tableau de donnees |
|
| 197 | + if (!$this->err |
|
| 198 | + AND isset($this->command['datapath']) |
|
| 199 | + AND is_array($this->command['datapath'])) { |
|
| 200 | + $this->select_datapath(); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + // tri {par x} |
|
| 204 | + if ($this->command['orderby']) { |
|
| 205 | + $this->select_orderby(); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + // grouper les resultats {fusion /x/y/z} ; |
|
| 209 | + if ($this->command['groupby']) { |
|
| 210 | + $this->select_groupby(); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $this->rewind(); |
|
| 214 | + #var_dump($this->tableau); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Aller chercher les donnees de la boucle DATA |
|
| 220 | + * depuis une source |
|
| 221 | + * {source format, [URL], [arg2]...} |
|
| 222 | + */ |
|
| 223 | + protected function select_source() { |
|
| 224 | + # un peu crado : avant de charger le cache il faut charger |
|
| 225 | + # les class indispensables, sinon PHP ne saura pas gerer |
|
| 226 | + # l'objet en cache ; cf plugins/icalendar |
|
| 227 | + # perf : pas de fonction table_to_array ! (table est deja un array) |
|
| 228 | + if (isset($this->command['sourcemode']) |
|
| 229 | + AND !in_array($this->command['sourcemode'],array('table', 'array', 'tableau'))) |
|
| 230 | + charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 231 | + |
|
| 232 | + # le premier argument peut etre un array, une URL etc. |
|
| 233 | + $src = $this->command['source'][0]; |
|
| 234 | + |
|
| 235 | + # avons-nous un cache dispo ? |
|
| 236 | + $cle = null; |
|
| 237 | + if (is_string($src)) |
|
| 238 | + $cle = 'datasource_'.md5($this->command['sourcemode'].':'.var_export($this->command['source'],true)); |
|
| 239 | + |
|
| 240 | + $cache = $this->cache_get($cle); |
|
| 241 | + if (isset($this->command['datacache'])) |
|
| 242 | + $ttl = intval($this->command['datacache']); |
|
| 243 | + if ($cache |
|
| 244 | + AND ($cache['time'] + (isset($ttl) ? $ttl : $cache['ttl']) |
|
| 245 | + > time()) |
|
| 246 | + AND !(_request('var_mode') === 'recalcul' |
|
| 247 | + AND include_spip('inc/autoriser') |
|
| 248 | + AND autoriser('recalcul') |
|
| 249 | + )) { |
|
| 250 | + $this->tableau = $cache['data']; |
|
| 251 | + } |
|
| 252 | + else try { |
|
| 253 | + # dommage que ca ne soit pas une option de yql_to_array... |
|
| 254 | + if ($this->command['sourcemode'] == 'yql') |
|
| 255 | + if (!isset($ttl)) $ttl = 3600; |
|
| 256 | + |
|
| 257 | + if (isset($this->command['sourcemode']) |
|
| 258 | + AND in_array($this->command['sourcemode'], |
|
| 259 | + array('table', 'array', 'tableau')) |
|
| 260 | + ) { |
|
| 261 | + if (is_array($a = $src) |
|
| 262 | + OR (is_string($a) |
|
| 263 | + AND $a = str_replace('"', '"', $a) # fragile! |
|
| 264 | + AND is_array($a = @unserialize($a))) |
|
| 265 | + ) |
|
| 266 | + $this->tableau = $a; |
|
| 267 | + } |
|
| 268 | + else { |
|
| 269 | + if (preg_match(',^https?://,', $src)) { |
|
| 270 | + include_spip('inc/distant'); |
|
| 271 | + $u = recuperer_page($src, false, false, _DATA_SOURCE_MAX_SIZE); |
|
| 272 | + if (!$u) |
|
| 273 | + throw new Exception("404"); |
|
| 274 | + if (!isset($ttl)) $ttl = 24*3600; |
|
| 275 | + } else if (@is_dir($src)) { |
|
| 276 | + $u = $src; |
|
| 277 | + if (!isset($ttl)) $ttl = 10; |
|
| 278 | + } else if (@is_readable($src) && @is_file($src)) { |
|
| 279 | + $u = spip_file_get_contents($src); |
|
| 280 | + if (!isset($ttl)) $ttl = 10; |
|
| 281 | + } else { |
|
| 282 | + $u = $src; |
|
| 283 | + if (!isset($ttl)) $ttl = 10; |
|
| 284 | + } |
|
| 285 | + if (!$this->err |
|
| 286 | + AND $g = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true)) { |
|
| 287 | + $args = $this->command['source']; |
|
| 288 | + $args[0] = $u; |
|
| 289 | + if (is_array($a = call_user_func_array($g,$args))) { |
|
| 290 | + $this->tableau = $a; |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + if (!is_array($this->tableau)) |
|
| 296 | + $this->err = true; |
|
| 297 | + |
|
| 298 | + if (!$this->err AND isset($ttl) and $ttl>0) |
|
| 299 | + $this->cache_set($cle, $ttl); |
|
| 300 | + |
|
| 301 | + } |
|
| 302 | + catch (Exception $e) { |
|
| 303 | + $e = $e->getMessage(); |
|
| 304 | + $err = sprintf("[%s, %s] $e", |
|
| 305 | + $src, |
|
| 306 | + $this->command['sourcemode']); |
|
| 307 | + erreur_squelette(array($err, array())); |
|
| 308 | + $this->err = true; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + # en cas d'erreur, utiliser le cache si encore dispo |
|
| 312 | + if ($this->err |
|
| 313 | + AND $cache) { |
|
| 314 | + $this->tableau = $cache['data']; |
|
| 315 | + $this->err = false; |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Retourne un tableau donne depuis un critere liste |
|
| 322 | + * Critere {liste X1, X2, X3} |
|
| 323 | + * |
|
| 324 | + **/ |
|
| 325 | + protected function select_liste() { |
|
| 326 | + # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 327 | + if (!isset($this->command['liste'][1])) { |
|
| 328 | + if (!is_array($this->command['liste'][0])) { |
|
| 329 | + $this->command['liste'] = explode(',', $this->command['liste'][0]); |
|
| 330 | + } else { |
|
| 331 | + $this->command['liste'] = $this->command['liste'][0]; |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + $this->tableau = $this->command['liste']; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Retourne un tableau donne depuis un critere liste |
|
| 339 | + * Critere {enum Xmin, Xmax} |
|
| 340 | + * |
|
| 341 | + **/ |
|
| 342 | + protected function select_enum() { |
|
| 343 | + # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 344 | + if (!isset($this->command['enum'][1])) { |
|
| 345 | + if (!is_array($this->command['enum'][0])) { |
|
| 346 | + $this->command['enum'] = explode(',', $this->command['enum'][0]); |
|
| 347 | + } else { |
|
| 348 | + $this->command['enum'] = $this->command['enum'][0]; |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + if (count($this->command['enum'])>=3) |
|
| 352 | + $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 353 | + else |
|
| 354 | + $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 355 | + $this->tableau = $enum; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * extraire le chemin "query.results" du tableau de donnees |
|
| 361 | + * {datapath query.results} |
|
| 362 | + * |
|
| 363 | + **/ |
|
| 364 | + protected function select_datapath() { |
|
| 365 | + list(,$base) = each($this->command['datapath']); |
|
| 366 | + if (strlen($base = ltrim(trim($base),"/"))) { |
|
| 367 | + $this->tableau = table_valeur($this->tableau, $base); |
|
| 368 | + if (!is_array($this->tableau)) { |
|
| 369 | + $this->tableau = array(); |
|
| 370 | + $this->err = true; |
|
| 371 | + spip_log("datapath '$base' absent"); |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Ordonner les resultats |
|
| 378 | + * {par x} |
|
| 379 | + * |
|
| 380 | + **/ |
|
| 381 | + protected function select_orderby() { |
|
| 382 | + $sortfunc = ''; |
|
| 383 | + $aleas = 0; |
|
| 384 | + foreach($this->command['orderby'] as $tri) { |
|
| 385 | + // virer le / initial pour les criteres de la forme {par /xx} |
|
| 386 | + if (preg_match(',^\.?([/\w]+)( DESC)?$,iS', ltrim($tri, '/'), $r)) { |
|
| 387 | + // tri par cle |
|
| 388 | + if ($r[1] == 'cle'){ |
|
| 389 | + if (isset($r[2]) and $r[2]) |
|
| 390 | + krsort($this->tableau); |
|
| 391 | + else |
|
| 392 | + ksort($this->tableau); |
|
| 393 | + } |
|
| 394 | + # {par hasard} |
|
| 395 | + else if ($r[1] == 'alea') { |
|
| 396 | + $k = array_keys($this->tableau); |
|
| 397 | + shuffle($k); |
|
| 398 | + $v = array(); |
|
| 399 | + foreach($k as $cle) |
|
| 400 | + $v[$cle] = $this->tableau[$cle]; |
|
| 401 | + $this->tableau = $v; |
|
| 402 | + } |
|
| 403 | + else { |
|
| 404 | + # {par valeur} |
|
| 405 | + if ($r[1] == 'valeur') |
|
| 406 | + $tv = '%s'; |
|
| 407 | + # {par valeur/xx/yy} ?? |
|
| 408 | + else |
|
| 409 | + $tv = 'table_valeur(%s, '.var_export($r[1],true).')'; |
|
| 410 | + $sortfunc .= ' |
|
| 411 | 411 | $a = '.sprintf($tv,'$aa').'; |
| 412 | 412 | $b = '.sprintf($tv,'$bb').'; |
| 413 | 413 | if ($a <> $b) |
| 414 | 414 | return ($a ' . ((isset($r[2]) and $r[2]) ? '>' : '<').' $b) ? -1 : 1;'; |
| 415 | - } |
|
| 416 | - } |
|
| 417 | - } |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | 418 | |
| 419 | - if ($sortfunc) { |
|
| 420 | - uasort($this->tableau, create_function('$aa,$bb', |
|
| 421 | - $sortfunc.' |
|
| 419 | + if ($sortfunc) { |
|
| 420 | + uasort($this->tableau, create_function('$aa,$bb', |
|
| 421 | + $sortfunc.' |
|
| 422 | 422 | return 0;' |
| 423 | - )); |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Grouper les resultats |
|
| 430 | - * {fusion /x/y/z} |
|
| 431 | - * |
|
| 432 | - **/ |
|
| 433 | - protected function select_groupby() { |
|
| 434 | - // virer le / initial pour les criteres de la forme {fusion /xx} |
|
| 435 | - if (strlen($fusion = ltrim($this->command['groupby'][0], '/'))) { |
|
| 436 | - $vu = array(); |
|
| 437 | - foreach($this->tableau as $k => $v) { |
|
| 438 | - $val = table_valeur($v, $fusion); |
|
| 439 | - if (isset($vu[$val])) |
|
| 440 | - unset($this->tableau[$k]); |
|
| 441 | - else |
|
| 442 | - $vu[$val] = true; |
|
| 443 | - } |
|
| 444 | - } |
|
| 445 | - } |
|
| 423 | + )); |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Grouper les resultats |
|
| 430 | + * {fusion /x/y/z} |
|
| 431 | + * |
|
| 432 | + **/ |
|
| 433 | + protected function select_groupby() { |
|
| 434 | + // virer le / initial pour les criteres de la forme {fusion /xx} |
|
| 435 | + if (strlen($fusion = ltrim($this->command['groupby'][0], '/'))) { |
|
| 436 | + $vu = array(); |
|
| 437 | + foreach($this->tableau as $k => $v) { |
|
| 438 | + $val = table_valeur($v, $fusion); |
|
| 439 | + if (isset($vu[$val])) |
|
| 440 | + unset($this->tableau[$k]); |
|
| 441 | + else |
|
| 442 | + $vu[$val] = true; |
|
| 443 | + } |
|
| 444 | + } |
|
| 445 | + } |
|
| 446 | 446 | |
| 447 | 447 | |
| 448 | - /** |
|
| 449 | - * L'iterateur est-il encore valide ? |
|
| 450 | - * @return bool |
|
| 451 | - */ |
|
| 452 | - public function valid(){ |
|
| 453 | - return !is_null($this->cle); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Retourner la valeur |
|
| 458 | - * @return null |
|
| 459 | - */ |
|
| 460 | - public function current() { |
|
| 461 | - return $this->valeur; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Retourner la cle |
|
| 466 | - * @return null |
|
| 467 | - */ |
|
| 468 | - public function key() { |
|
| 469 | - return $this->cle; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Passer a la valeur suivante |
|
| 474 | - * @return void |
|
| 475 | - */ |
|
| 476 | - public function next(){ |
|
| 477 | - if ($this->valid()) |
|
| 478 | - list($this->cle, $this->valeur) = each($this->tableau); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * Compter le nombre total de resultats |
|
| 483 | - * @return int |
|
| 484 | - */ |
|
| 485 | - public function count() { |
|
| 486 | - if (is_null($this->total)) |
|
| 487 | - $this->total = count($this->tableau); |
|
| 488 | - return $this->total; |
|
| 489 | - } |
|
| 448 | + /** |
|
| 449 | + * L'iterateur est-il encore valide ? |
|
| 450 | + * @return bool |
|
| 451 | + */ |
|
| 452 | + public function valid(){ |
|
| 453 | + return !is_null($this->cle); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Retourner la valeur |
|
| 458 | + * @return null |
|
| 459 | + */ |
|
| 460 | + public function current() { |
|
| 461 | + return $this->valeur; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Retourner la cle |
|
| 466 | + * @return null |
|
| 467 | + */ |
|
| 468 | + public function key() { |
|
| 469 | + return $this->cle; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Passer a la valeur suivante |
|
| 474 | + * @return void |
|
| 475 | + */ |
|
| 476 | + public function next(){ |
|
| 477 | + if ($this->valid()) |
|
| 478 | + list($this->cle, $this->valeur) = each($this->tableau); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * Compter le nombre total de resultats |
|
| 483 | + * @return int |
|
| 484 | + */ |
|
| 485 | + public function count() { |
|
| 486 | + if (is_null($this->total)) |
|
| 487 | + $this->total = count($this->tableau); |
|
| 488 | + return $this->total; |
|
| 489 | + } |
|
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | /* |
@@ -500,7 +500,7 @@ discard block |
||
| 500 | 500 | * @return array |
| 501 | 501 | */ |
| 502 | 502 | function inc_file_to_array_dist($u) { |
| 503 | - return preg_split('/\r?\n/', $u); |
|
| 503 | + return preg_split('/\r?\n/', $u); |
|
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 506 | /** |
@@ -508,8 +508,8 @@ discard block |
||
| 508 | 508 | * @return unknown |
| 509 | 509 | */ |
| 510 | 510 | function inc_plugins_to_array_dist() { |
| 511 | - include_spip('inc/plugin'); |
|
| 512 | - return liste_chemin_plugin_actifs(); |
|
| 511 | + include_spip('inc/plugin'); |
|
| 512 | + return liste_chemin_plugin_actifs(); |
|
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | /** |
@@ -518,7 +518,7 @@ discard block |
||
| 518 | 518 | * @return array |
| 519 | 519 | */ |
| 520 | 520 | function inc_xml_to_array_dist($u) { |
| 521 | - return @XMLObjectToArray(new SimpleXmlIterator($u)); |
|
| 521 | + return @XMLObjectToArray(new SimpleXmlIterator($u)); |
|
| 522 | 522 | } |
| 523 | 523 | |
| 524 | 524 | /** |
@@ -546,14 +546,14 @@ discard block |
||
| 546 | 546 | * @return array|bool |
| 547 | 547 | */ |
| 548 | 548 | function inc_yql_to_array_dist($u) { |
| 549 | - define('_YQL_ENDPOINT', 'http://query.yahooapis.com/v1/public/yql?&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q='); |
|
| 550 | - $v = recuperer_page($url = _YQL_ENDPOINT.urlencode($u).'&format=json'); |
|
| 551 | - $w = json_decode($v); |
|
| 552 | - if (!$w) { |
|
| 553 | - throw new Exception('YQL: réponse vide ou mal formée'); |
|
| 554 | - return false; |
|
| 555 | - } |
|
| 556 | - return inc_object_to_array($w); |
|
| 549 | + define('_YQL_ENDPOINT', 'http://query.yahooapis.com/v1/public/yql?&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q='); |
|
| 550 | + $v = recuperer_page($url = _YQL_ENDPOINT.urlencode($u).'&format=json'); |
|
| 551 | + $w = json_decode($v); |
|
| 552 | + if (!$w) { |
|
| 553 | + throw new Exception('YQL: réponse vide ou mal formée'); |
|
| 554 | + return false; |
|
| 555 | + } |
|
| 556 | + return inc_object_to_array($w); |
|
| 557 | 557 | } |
| 558 | 558 | |
| 559 | 559 | /** |
@@ -562,17 +562,17 @@ discard block |
||
| 562 | 562 | * @return array|bool |
| 563 | 563 | */ |
| 564 | 564 | function inc_sql_to_array_dist($u) { |
| 565 | - # sortir le connecteur de $u |
|
| 566 | - preg_match(',^(?:(\w+):)?(.*)$,S', $u, $v); |
|
| 567 | - $serveur = (string) $v[1]; |
|
| 568 | - $req = trim($v[2]); |
|
| 569 | - if ($s = sql_query($req, $serveur)) { |
|
| 570 | - $r = array(); |
|
| 571 | - while ($t = sql_fetch($s)) |
|
| 572 | - $r[] = $t; |
|
| 573 | - return $r; |
|
| 574 | - } |
|
| 575 | - return false; |
|
| 565 | + # sortir le connecteur de $u |
|
| 566 | + preg_match(',^(?:(\w+):)?(.*)$,S', $u, $v); |
|
| 567 | + $serveur = (string) $v[1]; |
|
| 568 | + $req = trim($v[2]); |
|
| 569 | + if ($s = sql_query($req, $serveur)) { |
|
| 570 | + $r = array(); |
|
| 571 | + while ($t = sql_fetch($s)) |
|
| 572 | + $r[] = $t; |
|
| 573 | + return $r; |
|
| 574 | + } |
|
| 575 | + return false; |
|
| 576 | 576 | } |
| 577 | 577 | |
| 578 | 578 | /** |
@@ -581,9 +581,9 @@ discard block |
||
| 581 | 581 | * @return array|bool |
| 582 | 582 | */ |
| 583 | 583 | function inc_json_to_array_dist($u) { |
| 584 | - if (is_array($json = json_decode($u)) |
|
| 585 | - OR is_object($json)) |
|
| 586 | - return (array) $json; |
|
| 584 | + if (is_array($json = json_decode($u)) |
|
| 585 | + OR is_object($json)) |
|
| 586 | + return (array) $json; |
|
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | /** |
@@ -592,17 +592,17 @@ discard block |
||
| 592 | 592 | * @return array|bool |
| 593 | 593 | */ |
| 594 | 594 | function inc_csv_to_array_dist($u) { |
| 595 | - include_spip('inc/csv'); |
|
| 596 | - list($entete,$csv) = analyse_csv($u); |
|
| 597 | - array_unshift($csv,$entete); |
|
| 598 | - |
|
| 599 | - include_spip('inc/charsets'); |
|
| 600 | - foreach ($entete as $k => $v) { |
|
| 601 | - $v = strtolower(preg_replace(',\W+,', '_', translitteration($v))); |
|
| 602 | - foreach ($csv as &$item) |
|
| 603 | - $item[$v] = &$item[$k]; |
|
| 604 | - } |
|
| 605 | - return $csv; |
|
| 595 | + include_spip('inc/csv'); |
|
| 596 | + list($entete,$csv) = analyse_csv($u); |
|
| 597 | + array_unshift($csv,$entete); |
|
| 598 | + |
|
| 599 | + include_spip('inc/charsets'); |
|
| 600 | + foreach ($entete as $k => $v) { |
|
| 601 | + $v = strtolower(preg_replace(',\W+,', '_', translitteration($v))); |
|
| 602 | + foreach ($csv as &$item) |
|
| 603 | + $item[$v] = &$item[$k]; |
|
| 604 | + } |
|
| 605 | + return $csv; |
|
| 606 | 606 | } |
| 607 | 607 | |
| 608 | 608 | /** |
@@ -611,10 +611,10 @@ discard block |
||
| 611 | 611 | * @return array|bool |
| 612 | 612 | */ |
| 613 | 613 | function inc_rss_to_array_dist($u) { |
| 614 | - include_spip('inc/syndic'); |
|
| 615 | - if (is_array($rss = analyser_backend($u))) |
|
| 616 | - $tableau = $rss; |
|
| 617 | - return $tableau; |
|
| 614 | + include_spip('inc/syndic'); |
|
| 615 | + if (is_array($rss = analyser_backend($u))) |
|
| 616 | + $tableau = $rss; |
|
| 617 | + return $tableau; |
|
| 618 | 618 | } |
| 619 | 619 | |
| 620 | 620 | /** |
@@ -623,8 +623,8 @@ discard block |
||
| 623 | 623 | * @return array|bool |
| 624 | 624 | */ |
| 625 | 625 | function inc_atom_to_array_dist($u) { |
| 626 | - $g = charger_fonction('rss_to_array', 'inc'); |
|
| 627 | - return $g($u); |
|
| 626 | + $g = charger_fonction('rss_to_array', 'inc'); |
|
| 627 | + return $g($u); |
|
| 628 | 628 | } |
| 629 | 629 | |
| 630 | 630 | /** |
@@ -634,9 +634,9 @@ discard block |
||
| 634 | 634 | * @return array|bool |
| 635 | 635 | */ |
| 636 | 636 | function inc_glob_to_array_dist($u) { |
| 637 | - return (array) glob($u, |
|
| 638 | - GLOB_MARK | GLOB_NOSORT | GLOB_BRACE |
|
| 639 | - ); |
|
| 637 | + return (array) glob($u, |
|
| 638 | + GLOB_MARK | GLOB_NOSORT | GLOB_BRACE |
|
| 639 | + ); |
|
| 640 | 640 | } |
| 641 | 641 | |
| 642 | 642 | /** |
@@ -646,13 +646,13 @@ discard block |
||
| 646 | 646 | * @throws Exception |
| 647 | 647 | */ |
| 648 | 648 | function inc_yaml_to_array_dist($u){ |
| 649 | - include_spip('inc/yaml-mini'); |
|
| 650 | - if (!function_exists("yaml_decode")){ |
|
| 651 | - throw new Exception('YAML: impossible de trouver la fonction yaml_decode'); |
|
| 652 | - return false; |
|
| 653 | - } |
|
| 649 | + include_spip('inc/yaml-mini'); |
|
| 650 | + if (!function_exists("yaml_decode")){ |
|
| 651 | + throw new Exception('YAML: impossible de trouver la fonction yaml_decode'); |
|
| 652 | + return false; |
|
| 653 | + } |
|
| 654 | 654 | |
| 655 | - return yaml_decode($u); |
|
| 655 | + return yaml_decode($u); |
|
| 656 | 656 | } |
| 657 | 657 | |
| 658 | 658 | |
@@ -666,7 +666,7 @@ discard block |
||
| 666 | 666 | * @return array|bool |
| 667 | 667 | */ |
| 668 | 668 | function inc_pregfiles_to_array_dist($dir, $regexp=-1, $limit=10000) { |
| 669 | - return (array) preg_files($dir, $regexp, $limit); |
|
| 669 | + return (array) preg_files($dir, $regexp, $limit); |
|
| 670 | 670 | } |
| 671 | 671 | |
| 672 | 672 | /** |
@@ -677,19 +677,19 @@ discard block |
||
| 677 | 677 | * @return array|bool |
| 678 | 678 | */ |
| 679 | 679 | function inc_ls_to_array_dist($u) { |
| 680 | - $glob = charger_fonction('glob_to_array', 'inc'); |
|
| 681 | - $a = $glob($u); |
|
| 682 | - foreach ($a as &$v) { |
|
| 683 | - $b = (array) @stat($v); |
|
| 684 | - foreach ($b as $k => $ignore) |
|
| 685 | - if (is_numeric($k)) unset($b[$k]); |
|
| 686 | - $b['file'] = preg_replace('`/$`','',$v) ; |
|
| 687 | - $v = array_merge( |
|
| 688 | - pathinfo($v), |
|
| 689 | - $b |
|
| 690 | - ); |
|
| 691 | - } |
|
| 692 | - return $a; |
|
| 680 | + $glob = charger_fonction('glob_to_array', 'inc'); |
|
| 681 | + $a = $glob($u); |
|
| 682 | + foreach ($a as &$v) { |
|
| 683 | + $b = (array) @stat($v); |
|
| 684 | + foreach ($b as $k => $ignore) |
|
| 685 | + if (is_numeric($k)) unset($b[$k]); |
|
| 686 | + $b['file'] = preg_replace('`/$`','',$v) ; |
|
| 687 | + $v = array_merge( |
|
| 688 | + pathinfo($v), |
|
| 689 | + $b |
|
| 690 | + ); |
|
| 691 | + } |
|
| 692 | + return $a; |
|
| 693 | 693 | } |
| 694 | 694 | |
| 695 | 695 | /** |
@@ -698,23 +698,23 @@ discard block |
||
| 698 | 698 | * @return array|bool |
| 699 | 699 | */ |
| 700 | 700 | function XMLObjectToArray($object){ |
| 701 | - $xml_array = array(); |
|
| 702 | - for( $object->rewind(); $object->valid(); $object->next() ) { |
|
| 703 | - if(array_key_exists($key = $object->key(), $xml_array)){ |
|
| 704 | - $key .= '-'.uniqid(); |
|
| 705 | - } |
|
| 706 | - $vars = get_object_vars($object->current()); |
|
| 707 | - if (isset($vars['@attributes'])) |
|
| 708 | - foreach($vars['@attributes'] as $k => $v) |
|
| 709 | - $xml_array[$key][$k] = $v; |
|
| 710 | - if($object->hasChildren()){ |
|
| 711 | - $xml_array[$key][] = XMLObjectToArray( |
|
| 712 | - $object->current()); |
|
| 713 | - } |
|
| 714 | - else{ |
|
| 715 | - $xml_array[$key][] = strval($object->current()); |
|
| 716 | - } |
|
| 717 | - } |
|
| 718 | - return $xml_array; |
|
| 701 | + $xml_array = array(); |
|
| 702 | + for( $object->rewind(); $object->valid(); $object->next() ) { |
|
| 703 | + if(array_key_exists($key = $object->key(), $xml_array)){ |
|
| 704 | + $key .= '-'.uniqid(); |
|
| 705 | + } |
|
| 706 | + $vars = get_object_vars($object->current()); |
|
| 707 | + if (isset($vars['@attributes'])) |
|
| 708 | + foreach($vars['@attributes'] as $k => $v) |
|
| 709 | + $xml_array[$key][$k] = $v; |
|
| 710 | + if($object->hasChildren()){ |
|
| 711 | + $xml_array[$key][] = XMLObjectToArray( |
|
| 712 | + $object->current()); |
|
| 713 | + } |
|
| 714 | + else{ |
|
| 715 | + $xml_array[$key][] = strval($object->current()); |
|
| 716 | + } |
|
| 717 | + } |
|
| 718 | + return $xml_array; |
|
| 719 | 719 | } |
| 720 | 720 | ?> |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) return; |
| 14 | 14 | |
| 15 | -if (!defined('_DATA_SOURCE_MAX_SIZE')) define('_DATA_SOURCE_MAX_SIZE',2*1048576); |
|
| 15 | +if (!defined('_DATA_SOURCE_MAX_SIZE')) define('_DATA_SOURCE_MAX_SIZE', 2 * 1048576); |
|
| 16 | 16 | |
| 17 | 17 | |
| 18 | 18 | /** |
@@ -86,8 +86,8 @@ discard block |
||
| 86 | 86 | * @param $command |
| 87 | 87 | * @param array $info |
| 88 | 88 | */ |
| 89 | - public function __construct($command, $info=array()) { |
|
| 90 | - $this->type='DATA'; |
|
| 89 | + public function __construct($command, $info = array()) { |
|
| 90 | + $this->type = 'DATA'; |
|
| 91 | 91 | $this->command = $command; |
| 92 | 92 | $this->info = $info; |
| 93 | 93 | |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | // Si a ce stade on n'a pas de table, il y a un bug |
| 190 | 190 | if (!is_array($this->tableau)) { |
| 191 | 191 | $this->err = true; |
| 192 | - spip_log("erreur datasource ".var_export($command,true)); |
|
| 192 | + spip_log("erreur datasource ".var_export($command, true)); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | // {datapath query.results} |
@@ -226,8 +226,8 @@ discard block |
||
| 226 | 226 | # l'objet en cache ; cf plugins/icalendar |
| 227 | 227 | # perf : pas de fonction table_to_array ! (table est deja un array) |
| 228 | 228 | if (isset($this->command['sourcemode']) |
| 229 | - AND !in_array($this->command['sourcemode'],array('table', 'array', 'tableau'))) |
|
| 230 | - charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 229 | + AND !in_array($this->command['sourcemode'], array('table', 'array', 'tableau'))) |
|
| 230 | + charger_fonction($this->command['sourcemode'].'_to_array', 'inc', true); |
|
| 231 | 231 | |
| 232 | 232 | # le premier argument peut etre un array, une URL etc. |
| 233 | 233 | $src = $this->command['source'][0]; |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | # avons-nous un cache dispo ? |
| 236 | 236 | $cle = null; |
| 237 | 237 | if (is_string($src)) |
| 238 | - $cle = 'datasource_'.md5($this->command['sourcemode'].':'.var_export($this->command['source'],true)); |
|
| 238 | + $cle = 'datasource_'.md5($this->command['sourcemode'].':'.var_export($this->command['source'], true)); |
|
| 239 | 239 | |
| 240 | 240 | $cache = $this->cache_get($cle); |
| 241 | 241 | if (isset($this->command['datacache'])) |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | $u = recuperer_page($src, false, false, _DATA_SOURCE_MAX_SIZE); |
| 272 | 272 | if (!$u) |
| 273 | 273 | throw new Exception("404"); |
| 274 | - if (!isset($ttl)) $ttl = 24*3600; |
|
| 274 | + if (!isset($ttl)) $ttl = 24 * 3600; |
|
| 275 | 275 | } else if (@is_dir($src)) { |
| 276 | 276 | $u = $src; |
| 277 | 277 | if (!isset($ttl)) $ttl = 10; |
@@ -283,10 +283,10 @@ discard block |
||
| 283 | 283 | if (!isset($ttl)) $ttl = 10; |
| 284 | 284 | } |
| 285 | 285 | if (!$this->err |
| 286 | - AND $g = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true)) { |
|
| 286 | + AND $g = charger_fonction($this->command['sourcemode'].'_to_array', 'inc', true)) { |
|
| 287 | 287 | $args = $this->command['source']; |
| 288 | 288 | $args[0] = $u; |
| 289 | - if (is_array($a = call_user_func_array($g,$args))) { |
|
| 289 | + if (is_array($a = call_user_func_array($g, $args))) { |
|
| 290 | 290 | $this->tableau = $a; |
| 291 | 291 | } |
| 292 | 292 | } |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | if (!is_array($this->tableau)) |
| 296 | 296 | $this->err = true; |
| 297 | 297 | |
| 298 | - if (!$this->err AND isset($ttl) and $ttl>0) |
|
| 298 | + if (!$this->err AND isset($ttl) and $ttl > 0) |
|
| 299 | 299 | $this->cache_set($cle, $ttl); |
| 300 | 300 | |
| 301 | 301 | } |
@@ -348,10 +348,10 @@ discard block |
||
| 348 | 348 | $this->command['enum'] = $this->command['enum'][0]; |
| 349 | 349 | } |
| 350 | 350 | } |
| 351 | - if (count($this->command['enum'])>=3) |
|
| 352 | - $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 351 | + if (count($this->command['enum']) >= 3) |
|
| 352 | + $enum = range(array_shift($this->command['enum']), array_shift($this->command['enum']), array_shift($this->command['enum'])); |
|
| 353 | 353 | else |
| 354 | - $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 354 | + $enum = range(array_shift($this->command['enum']), array_shift($this->command['enum'])); |
|
| 355 | 355 | $this->tableau = $enum; |
| 356 | 356 | } |
| 357 | 357 | |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | **/ |
| 364 | 364 | protected function select_datapath() { |
| 365 | 365 | list(,$base) = each($this->command['datapath']); |
| 366 | - if (strlen($base = ltrim(trim($base),"/"))) { |
|
| 366 | + if (strlen($base = ltrim(trim($base), "/"))) { |
|
| 367 | 367 | $this->tableau = table_valeur($this->tableau, $base); |
| 368 | 368 | if (!is_array($this->tableau)) { |
| 369 | 369 | $this->tableau = array(); |
@@ -381,11 +381,11 @@ discard block |
||
| 381 | 381 | protected function select_orderby() { |
| 382 | 382 | $sortfunc = ''; |
| 383 | 383 | $aleas = 0; |
| 384 | - foreach($this->command['orderby'] as $tri) { |
|
| 384 | + foreach ($this->command['orderby'] as $tri) { |
|
| 385 | 385 | // virer le / initial pour les criteres de la forme {par /xx} |
| 386 | 386 | if (preg_match(',^\.?([/\w]+)( DESC)?$,iS', ltrim($tri, '/'), $r)) { |
| 387 | 387 | // tri par cle |
| 388 | - if ($r[1] == 'cle'){ |
|
| 388 | + if ($r[1] == 'cle') { |
|
| 389 | 389 | if (isset($r[2]) and $r[2]) |
| 390 | 390 | krsort($this->tableau); |
| 391 | 391 | else |
@@ -396,7 +396,7 @@ discard block |
||
| 396 | 396 | $k = array_keys($this->tableau); |
| 397 | 397 | shuffle($k); |
| 398 | 398 | $v = array(); |
| 399 | - foreach($k as $cle) |
|
| 399 | + foreach ($k as $cle) |
|
| 400 | 400 | $v[$cle] = $this->tableau[$cle]; |
| 401 | 401 | $this->tableau = $v; |
| 402 | 402 | } |
@@ -406,10 +406,10 @@ discard block |
||
| 406 | 406 | $tv = '%s'; |
| 407 | 407 | # {par valeur/xx/yy} ?? |
| 408 | 408 | else |
| 409 | - $tv = 'table_valeur(%s, '.var_export($r[1],true).')'; |
|
| 409 | + $tv = 'table_valeur(%s, '.var_export($r[1], true).')'; |
|
| 410 | 410 | $sortfunc .= ' |
| 411 | - $a = '.sprintf($tv,'$aa').'; |
|
| 412 | - $b = '.sprintf($tv,'$bb').'; |
|
| 411 | + $a = '.sprintf($tv, '$aa').'; |
|
| 412 | + $b = '.sprintf($tv, '$bb').'; |
|
| 413 | 413 | if ($a <> $b) |
| 414 | 414 | return ($a ' . ((isset($r[2]) and $r[2]) ? '>' : '<').' $b) ? -1 : 1;'; |
| 415 | 415 | } |
@@ -434,7 +434,7 @@ discard block |
||
| 434 | 434 | // virer le / initial pour les criteres de la forme {fusion /xx} |
| 435 | 435 | if (strlen($fusion = ltrim($this->command['groupby'][0], '/'))) { |
| 436 | 436 | $vu = array(); |
| 437 | - foreach($this->tableau as $k => $v) { |
|
| 437 | + foreach ($this->tableau as $k => $v) { |
|
| 438 | 438 | $val = table_valeur($v, $fusion); |
| 439 | 439 | if (isset($vu[$val])) |
| 440 | 440 | unset($this->tableau[$k]); |
@@ -449,7 +449,7 @@ discard block |
||
| 449 | 449 | * L'iterateur est-il encore valide ? |
| 450 | 450 | * @return bool |
| 451 | 451 | */ |
| 452 | - public function valid(){ |
|
| 452 | + public function valid() { |
|
| 453 | 453 | return !is_null($this->cle); |
| 454 | 454 | } |
| 455 | 455 | |
@@ -473,7 +473,7 @@ discard block |
||
| 473 | 473 | * Passer a la valeur suivante |
| 474 | 474 | * @return void |
| 475 | 475 | */ |
| 476 | - public function next(){ |
|
| 476 | + public function next() { |
|
| 477 | 477 | if ($this->valid()) |
| 478 | 478 | list($this->cle, $this->valeur) = each($this->tableau); |
| 479 | 479 | } |
@@ -529,14 +529,14 @@ discard block |
||
| 529 | 529 | * @return array |
| 530 | 530 | * |
| 531 | 531 | */ |
| 532 | -function inc_object_to_array( $object ) { |
|
| 533 | - if( !is_object( $object ) && !is_array( $object ) ) { |
|
| 532 | +function inc_object_to_array($object) { |
|
| 533 | + if (!is_object($object) && !is_array($object)) { |
|
| 534 | 534 | return $object; |
| 535 | 535 | } |
| 536 | - if( is_object( $object ) ) { |
|
| 537 | - $object = get_object_vars( $object ); |
|
| 536 | + if (is_object($object)) { |
|
| 537 | + $object = get_object_vars($object); |
|
| 538 | 538 | } |
| 539 | - return array_map( 'inc_object_to_array', $object ); |
|
| 539 | + return array_map('inc_object_to_array', $object); |
|
| 540 | 540 | } |
| 541 | 541 | |
| 542 | 542 | /** |
@@ -593,8 +593,8 @@ discard block |
||
| 593 | 593 | */ |
| 594 | 594 | function inc_csv_to_array_dist($u) { |
| 595 | 595 | include_spip('inc/csv'); |
| 596 | - list($entete,$csv) = analyse_csv($u); |
|
| 597 | - array_unshift($csv,$entete); |
|
| 596 | + list($entete, $csv) = analyse_csv($u); |
|
| 597 | + array_unshift($csv, $entete); |
|
| 598 | 598 | |
| 599 | 599 | include_spip('inc/charsets'); |
| 600 | 600 | foreach ($entete as $k => $v) { |
@@ -645,9 +645,9 @@ discard block |
||
| 645 | 645 | * @return bool|array |
| 646 | 646 | * @throws Exception |
| 647 | 647 | */ |
| 648 | -function inc_yaml_to_array_dist($u){ |
|
| 648 | +function inc_yaml_to_array_dist($u) { |
|
| 649 | 649 | include_spip('inc/yaml-mini'); |
| 650 | - if (!function_exists("yaml_decode")){ |
|
| 650 | + if (!function_exists("yaml_decode")) { |
|
| 651 | 651 | throw new Exception('YAML: impossible de trouver la fonction yaml_decode'); |
| 652 | 652 | return false; |
| 653 | 653 | } |
@@ -665,7 +665,7 @@ discard block |
||
| 665 | 665 | * @param int $limit |
| 666 | 666 | * @return array|bool |
| 667 | 667 | */ |
| 668 | -function inc_pregfiles_to_array_dist($dir, $regexp=-1, $limit=10000) { |
|
| 668 | +function inc_pregfiles_to_array_dist($dir, $regexp = -1, $limit = 10000) { |
|
| 669 | 669 | return (array) preg_files($dir, $regexp, $limit); |
| 670 | 670 | } |
| 671 | 671 | |
@@ -683,7 +683,7 @@ discard block |
||
| 683 | 683 | $b = (array) @stat($v); |
| 684 | 684 | foreach ($b as $k => $ignore) |
| 685 | 685 | if (is_numeric($k)) unset($b[$k]); |
| 686 | - $b['file'] = preg_replace('`/$`','',$v) ; |
|
| 686 | + $b['file'] = preg_replace('`/$`', '', $v); |
|
| 687 | 687 | $v = array_merge( |
| 688 | 688 | pathinfo($v), |
| 689 | 689 | $b |
@@ -697,21 +697,21 @@ discard block |
||
| 697 | 697 | * @param Object $object |
| 698 | 698 | * @return array|bool |
| 699 | 699 | */ |
| 700 | -function XMLObjectToArray($object){ |
|
| 700 | +function XMLObjectToArray($object) { |
|
| 701 | 701 | $xml_array = array(); |
| 702 | - for( $object->rewind(); $object->valid(); $object->next() ) { |
|
| 703 | - if(array_key_exists($key = $object->key(), $xml_array)){ |
|
| 702 | + for ($object->rewind(); $object->valid(); $object->next()) { |
|
| 703 | + if (array_key_exists($key = $object->key(), $xml_array)) { |
|
| 704 | 704 | $key .= '-'.uniqid(); |
| 705 | 705 | } |
| 706 | 706 | $vars = get_object_vars($object->current()); |
| 707 | 707 | if (isset($vars['@attributes'])) |
| 708 | - foreach($vars['@attributes'] as $k => $v) |
|
| 708 | + foreach ($vars['@attributes'] as $k => $v) |
|
| 709 | 709 | $xml_array[$key][$k] = $v; |
| 710 | - if($object->hasChildren()){ |
|
| 710 | + if ($object->hasChildren()) { |
|
| 711 | 711 | $xml_array[$key][] = XMLObjectToArray( |
| 712 | 712 | $object->current()); |
| 713 | 713 | } |
| 714 | - else{ |
|
| 714 | + else { |
|
| 715 | 715 | $xml_array[$key][] = strval($object->current()); |
| 716 | 716 | } |
| 717 | 717 | } |
@@ -10,9 +10,13 @@ discard block |
||
| 10 | 10 | * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * |
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | -if (!defined('_ECRIRE_INC_VERSION')) return; |
|
| 13 | +if (!defined('_ECRIRE_INC_VERSION')) { |
|
| 14 | + return; |
|
| 15 | +} |
|
| 14 | 16 | |
| 15 | -if (!defined('_DATA_SOURCE_MAX_SIZE')) define('_DATA_SOURCE_MAX_SIZE',2*1048576); |
|
| 17 | +if (!defined('_DATA_SOURCE_MAX_SIZE')) { |
|
| 18 | + define('_DATA_SOURCE_MAX_SIZE',2*1048576); |
|
| 19 | +} |
|
| 16 | 20 | |
| 17 | 21 | |
| 18 | 22 | /** |
@@ -117,10 +121,14 @@ discard block |
||
| 117 | 121 | * @return |
| 118 | 122 | */ |
| 119 | 123 | protected function cache_get($cle) { |
| 120 | - if (!$cle) return; |
|
| 124 | + if (!$cle) { |
|
| 125 | + return; |
|
| 126 | + } |
|
| 121 | 127 | # utiliser memoization si dispo |
| 122 | 128 | include_spip('inc/memoization'); |
| 123 | - if (!function_exists('cache_get')) return; |
|
| 129 | + if (!function_exists('cache_get')) { |
|
| 130 | + return; |
|
| 131 | + } |
|
| 124 | 132 | return cache_get($cle); |
| 125 | 133 | } |
| 126 | 134 | |
@@ -131,13 +139,17 @@ discard block |
||
| 131 | 139 | * @return |
| 132 | 140 | */ |
| 133 | 141 | protected function cache_set($cle, $ttl, $valeur = null) { |
| 134 | - if (!$cle) return; |
|
| 142 | + if (!$cle) { |
|
| 143 | + return; |
|
| 144 | + } |
|
| 135 | 145 | if (is_null($valeur)) { |
| 136 | 146 | $valeur = $this->tableau; |
| 137 | 147 | } |
| 138 | 148 | # utiliser memoization si dispo |
| 139 | 149 | include_spip('inc/memoization'); |
| 140 | - if (!function_exists('cache_set')) return; |
|
| 150 | + if (!function_exists('cache_set')) { |
|
| 151 | + return; |
|
| 152 | + } |
|
| 141 | 153 | return cache_set($cle, |
| 142 | 154 | array( |
| 143 | 155 | 'data' => $valeur, |
@@ -226,20 +238,23 @@ discard block |
||
| 226 | 238 | # l'objet en cache ; cf plugins/icalendar |
| 227 | 239 | # perf : pas de fonction table_to_array ! (table est deja un array) |
| 228 | 240 | if (isset($this->command['sourcemode']) |
| 229 | - AND !in_array($this->command['sourcemode'],array('table', 'array', 'tableau'))) |
|
| 230 | - charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 241 | + AND !in_array($this->command['sourcemode'],array('table', 'array', 'tableau'))) { |
|
| 242 | + charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 243 | + } |
|
| 231 | 244 | |
| 232 | 245 | # le premier argument peut etre un array, une URL etc. |
| 233 | 246 | $src = $this->command['source'][0]; |
| 234 | 247 | |
| 235 | 248 | # avons-nous un cache dispo ? |
| 236 | 249 | $cle = null; |
| 237 | - if (is_string($src)) |
|
| 238 | - $cle = 'datasource_'.md5($this->command['sourcemode'].':'.var_export($this->command['source'],true)); |
|
| 250 | + if (is_string($src)) { |
|
| 251 | + $cle = 'datasource_'.md5($this->command['sourcemode'].':'.var_export($this->command['source'],true)); |
|
| 252 | + } |
|
| 239 | 253 | |
| 240 | 254 | $cache = $this->cache_get($cle); |
| 241 | - if (isset($this->command['datacache'])) |
|
| 242 | - $ttl = intval($this->command['datacache']); |
|
| 255 | + if (isset($this->command['datacache'])) { |
|
| 256 | + $ttl = intval($this->command['datacache']); |
|
| 257 | + } |
|
| 243 | 258 | if ($cache |
| 244 | 259 | AND ($cache['time'] + (isset($ttl) ? $ttl : $cache['ttl']) |
| 245 | 260 | > time()) |
@@ -248,11 +263,12 @@ discard block |
||
| 248 | 263 | AND autoriser('recalcul') |
| 249 | 264 | )) { |
| 250 | 265 | $this->tableau = $cache['data']; |
| 251 | - } |
|
| 252 | - else try { |
|
| 266 | + } else { |
|
| 267 | + try { |
|
| 253 | 268 | # dommage que ca ne soit pas une option de yql_to_array... |
| 254 | 269 | if ($this->command['sourcemode'] == 'yql') |
| 255 | 270 | if (!isset($ttl)) $ttl = 3600; |
| 271 | + } |
|
| 256 | 272 | |
| 257 | 273 | if (isset($this->command['sourcemode']) |
| 258 | 274 | AND in_array($this->command['sourcemode'], |
@@ -262,25 +278,34 @@ discard block |
||
| 262 | 278 | OR (is_string($a) |
| 263 | 279 | AND $a = str_replace('"', '"', $a) # fragile! |
| 264 | 280 | AND is_array($a = @unserialize($a))) |
| 265 | - ) |
|
| 266 | - $this->tableau = $a; |
|
| 267 | - } |
|
| 268 | - else { |
|
| 281 | + ) { |
|
| 282 | + $this->tableau = $a; |
|
| 283 | + } |
|
| 284 | + } else { |
|
| 269 | 285 | if (preg_match(',^https?://,', $src)) { |
| 270 | 286 | include_spip('inc/distant'); |
| 271 | 287 | $u = recuperer_page($src, false, false, _DATA_SOURCE_MAX_SIZE); |
| 272 | - if (!$u) |
|
| 273 | - throw new Exception("404"); |
|
| 274 | - if (!isset($ttl)) $ttl = 24*3600; |
|
| 288 | + if (!$u) { |
|
| 289 | + throw new Exception("404"); |
|
| 290 | + } |
|
| 291 | + if (!isset($ttl)) { |
|
| 292 | + $ttl = 24*3600; |
|
| 293 | + } |
|
| 275 | 294 | } else if (@is_dir($src)) { |
| 276 | 295 | $u = $src; |
| 277 | - if (!isset($ttl)) $ttl = 10; |
|
| 296 | + if (!isset($ttl)) { |
|
| 297 | + $ttl = 10; |
|
| 298 | + } |
|
| 278 | 299 | } else if (@is_readable($src) && @is_file($src)) { |
| 279 | 300 | $u = spip_file_get_contents($src); |
| 280 | - if (!isset($ttl)) $ttl = 10; |
|
| 301 | + if (!isset($ttl)) { |
|
| 302 | + $ttl = 10; |
|
| 303 | + } |
|
| 281 | 304 | } else { |
| 282 | 305 | $u = $src; |
| 283 | - if (!isset($ttl)) $ttl = 10; |
|
| 306 | + if (!isset($ttl)) { |
|
| 307 | + $ttl = 10; |
|
| 308 | + } |
|
| 284 | 309 | } |
| 285 | 310 | if (!$this->err |
| 286 | 311 | AND $g = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true)) { |
@@ -292,14 +317,15 @@ discard block |
||
| 292 | 317 | } |
| 293 | 318 | } |
| 294 | 319 | |
| 295 | - if (!is_array($this->tableau)) |
|
| 296 | - $this->err = true; |
|
| 320 | + if (!is_array($this->tableau)) { |
|
| 321 | + $this->err = true; |
|
| 322 | + } |
|
| 297 | 323 | |
| 298 | - if (!$this->err AND isset($ttl) and $ttl>0) |
|
| 299 | - $this->cache_set($cle, $ttl); |
|
| 324 | + if (!$this->err AND isset($ttl) and $ttl>0) { |
|
| 325 | + $this->cache_set($cle, $ttl); |
|
| 326 | + } |
|
| 300 | 327 | |
| 301 | - } |
|
| 302 | - catch (Exception $e) { |
|
| 328 | + } catch (Exception $e) { |
|
| 303 | 329 | $e = $e->getMessage(); |
| 304 | 330 | $err = sprintf("[%s, %s] $e", |
| 305 | 331 | $src, |
@@ -348,10 +374,11 @@ discard block |
||
| 348 | 374 | $this->command['enum'] = $this->command['enum'][0]; |
| 349 | 375 | } |
| 350 | 376 | } |
| 351 | - if (count($this->command['enum'])>=3) |
|
| 352 | - $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 353 | - else |
|
| 354 | - $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 377 | + if (count($this->command['enum'])>=3) { |
|
| 378 | + $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 379 | + } else { |
|
| 380 | + $enum = range(array_shift($this->command['enum']),array_shift($this->command['enum'])); |
|
| 381 | + } |
|
| 355 | 382 | $this->tableau = $enum; |
| 356 | 383 | } |
| 357 | 384 | |
@@ -386,27 +413,30 @@ discard block |
||
| 386 | 413 | if (preg_match(',^\.?([/\w]+)( DESC)?$,iS', ltrim($tri, '/'), $r)) { |
| 387 | 414 | // tri par cle |
| 388 | 415 | if ($r[1] == 'cle'){ |
| 389 | - if (isset($r[2]) and $r[2]) |
|
| 390 | - krsort($this->tableau); |
|
| 391 | - else |
|
| 392 | - ksort($this->tableau); |
|
| 416 | + if (isset($r[2]) and $r[2]) { |
|
| 417 | + krsort($this->tableau); |
|
| 418 | + } else { |
|
| 419 | + ksort($this->tableau); |
|
| 420 | + } |
|
| 393 | 421 | } |
| 394 | 422 | # {par hasard} |
| 395 | 423 | else if ($r[1] == 'alea') { |
| 396 | 424 | $k = array_keys($this->tableau); |
| 397 | 425 | shuffle($k); |
| 398 | 426 | $v = array(); |
| 399 | - foreach($k as $cle) |
|
| 400 | - $v[$cle] = $this->tableau[$cle]; |
|
| 427 | + foreach($k as $cle) { |
|
| 428 | + $v[$cle] = $this->tableau[$cle]; |
|
| 429 | + } |
|
| 401 | 430 | $this->tableau = $v; |
| 402 | - } |
|
| 403 | - else { |
|
| 431 | + } else { |
|
| 404 | 432 | # {par valeur} |
| 405 | - if ($r[1] == 'valeur') |
|
| 406 | - $tv = '%s'; |
|
| 433 | + if ($r[1] == 'valeur') { |
|
| 434 | + $tv = '%s'; |
|
| 435 | + } |
|
| 407 | 436 | # {par valeur/xx/yy} ?? |
| 408 | - else |
|
| 409 | - $tv = 'table_valeur(%s, '.var_export($r[1],true).')'; |
|
| 437 | + else { |
|
| 438 | + $tv = 'table_valeur(%s, '.var_export($r[1],true).')'; |
|
| 439 | + } |
|
| 410 | 440 | $sortfunc .= ' |
| 411 | 441 | $a = '.sprintf($tv,'$aa').'; |
| 412 | 442 | $b = '.sprintf($tv,'$bb').'; |
@@ -436,10 +466,11 @@ discard block |
||
| 436 | 466 | $vu = array(); |
| 437 | 467 | foreach($this->tableau as $k => $v) { |
| 438 | 468 | $val = table_valeur($v, $fusion); |
| 439 | - if (isset($vu[$val])) |
|
| 440 | - unset($this->tableau[$k]); |
|
| 441 | - else |
|
| 442 | - $vu[$val] = true; |
|
| 469 | + if (isset($vu[$val])) { |
|
| 470 | + unset($this->tableau[$k]); |
|
| 471 | + } else { |
|
| 472 | + $vu[$val] = true; |
|
| 473 | + } |
|
| 443 | 474 | } |
| 444 | 475 | } |
| 445 | 476 | } |
@@ -474,8 +505,9 @@ discard block |
||
| 474 | 505 | * @return void |
| 475 | 506 | */ |
| 476 | 507 | public function next(){ |
| 477 | - if ($this->valid()) |
|
| 478 | - list($this->cle, $this->valeur) = each($this->tableau); |
|
| 508 | + if ($this->valid()) { |
|
| 509 | + list($this->cle, $this->valeur) = each($this->tableau); |
|
| 510 | + } |
|
| 479 | 511 | } |
| 480 | 512 | |
| 481 | 513 | /** |
@@ -483,8 +515,9 @@ discard block |
||
| 483 | 515 | * @return int |
| 484 | 516 | */ |
| 485 | 517 | public function count() { |
| 486 | - if (is_null($this->total)) |
|
| 487 | - $this->total = count($this->tableau); |
|
| 518 | + if (is_null($this->total)) { |
|
| 519 | + $this->total = count($this->tableau); |
|
| 520 | + } |
|
| 488 | 521 | return $this->total; |
| 489 | 522 | } |
| 490 | 523 | } |
@@ -568,8 +601,9 @@ discard block |
||
| 568 | 601 | $req = trim($v[2]); |
| 569 | 602 | if ($s = sql_query($req, $serveur)) { |
| 570 | 603 | $r = array(); |
| 571 | - while ($t = sql_fetch($s)) |
|
| 572 | - $r[] = $t; |
|
| 604 | + while ($t = sql_fetch($s)) { |
|
| 605 | + $r[] = $t; |
|
| 606 | + } |
|
| 573 | 607 | return $r; |
| 574 | 608 | } |
| 575 | 609 | return false; |
@@ -582,9 +616,10 @@ discard block |
||
| 582 | 616 | */ |
| 583 | 617 | function inc_json_to_array_dist($u) { |
| 584 | 618 | if (is_array($json = json_decode($u)) |
| 585 | - OR is_object($json)) |
|
| 586 | - return (array) $json; |
|
| 587 | -} |
|
| 619 | + OR is_object($json)) { |
|
| 620 | + return (array) $json; |
|
| 621 | + } |
|
| 622 | + } |
|
| 588 | 623 | |
| 589 | 624 | /** |
| 590 | 625 | * csv -> tableau |
@@ -599,8 +634,9 @@ discard block |
||
| 599 | 634 | include_spip('inc/charsets'); |
| 600 | 635 | foreach ($entete as $k => $v) { |
| 601 | 636 | $v = strtolower(preg_replace(',\W+,', '_', translitteration($v))); |
| 602 | - foreach ($csv as &$item) |
|
| 603 | - $item[$v] = &$item[$k]; |
|
| 637 | + foreach ($csv as &$item) { |
|
| 638 | + $item[$v] = &$item[$k]; |
|
| 639 | + } |
|
| 604 | 640 | } |
| 605 | 641 | return $csv; |
| 606 | 642 | } |
@@ -612,8 +648,9 @@ discard block |
||
| 612 | 648 | */ |
| 613 | 649 | function inc_rss_to_array_dist($u) { |
| 614 | 650 | include_spip('inc/syndic'); |
| 615 | - if (is_array($rss = analyser_backend($u))) |
|
| 616 | - $tableau = $rss; |
|
| 651 | + if (is_array($rss = analyser_backend($u))) { |
|
| 652 | + $tableau = $rss; |
|
| 653 | + } |
|
| 617 | 654 | return $tableau; |
| 618 | 655 | } |
| 619 | 656 | |
@@ -681,8 +718,9 @@ discard block |
||
| 681 | 718 | $a = $glob($u); |
| 682 | 719 | foreach ($a as &$v) { |
| 683 | 720 | $b = (array) @stat($v); |
| 684 | - foreach ($b as $k => $ignore) |
|
| 685 | - if (is_numeric($k)) unset($b[$k]); |
|
| 721 | + foreach ($b as $k => $ignore) { |
|
| 722 | + if (is_numeric($k)) unset($b[$k]); |
|
| 723 | + } |
|
| 686 | 724 | $b['file'] = preg_replace('`/$`','',$v) ; |
| 687 | 725 | $v = array_merge( |
| 688 | 726 | pathinfo($v), |
@@ -704,14 +742,14 @@ discard block |
||
| 704 | 742 | $key .= '-'.uniqid(); |
| 705 | 743 | } |
| 706 | 744 | $vars = get_object_vars($object->current()); |
| 707 | - if (isset($vars['@attributes'])) |
|
| 708 | - foreach($vars['@attributes'] as $k => $v) |
|
| 745 | + if (isset($vars['@attributes'])) { |
|
| 746 | + foreach($vars['@attributes'] as $k => $v) |
|
| 709 | 747 | $xml_array[$key][$k] = $v; |
| 748 | + } |
|
| 710 | 749 | if($object->hasChildren()){ |
| 711 | 750 | $xml_array[$key][] = XMLObjectToArray( |
| 712 | 751 | $object->current()); |
| 713 | - } |
|
| 714 | - else{ |
|
| 752 | + } else{ |
|
| 715 | 753 | $xml_array[$key][] = strval($object->current()); |
| 716 | 754 | } |
| 717 | 755 | } |