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