[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / ecrire / iterateur / data.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) return;
14
15 if (!defined('_DATA_SOURCE_MAX_SIZE')) define('_DATA_SOURCE_MAX_SIZE',2*1048576);
16
17
18 /**
19 * creer une boucle sur un iterateur DATA
20 * annonce au compilo les "champs" disponibles
21 *
22 * @param $b
23 * @return
24 */
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;
36 }
37
38
39 /**
40 * IterateurDATA pour iterer sur des donnees
41 */
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=...}
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('&quot;', '"', $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 $a = '.sprintf($tv,'$aa').';
412 $b = '.sprintf($tv,'$bb').';
413 if ($a <> $b)
414 return ($a ' . ((isset($r[2]) and $r[2]) ? '>' : '<').' $b) ? -1 : 1;';
415 }
416 }
417 }
418
419 if ($sortfunc) {
420 uasort($this->tableau, create_function('$aa,$bb',
421 $sortfunc.'
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 }
446
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 }
490 }
491
492 /*
493 * Fonctions de transformation donnee => tableau
494 */
495
496 /**
497 * file -> tableau
498 *
499 * @param string $u
500 * @return array
501 */
502 function inc_file_to_array_dist($u) {
503 return preg_split('/\r?\n/', $u);
504 }
505
506 /**
507 * plugins -> tableau
508 * @return unknown
509 */
510 function inc_plugins_to_array_dist() {
511 include_spip('inc/plugin');
512 return liste_chemin_plugin_actifs();
513 }
514
515 /**
516 * xml -> tableau
517 * @param string $u
518 * @return array
519 */
520 function inc_xml_to_array_dist($u) {
521 return @XMLObjectToArray(new SimpleXmlIterator($u));
522 }
523
524 /**
525 *
526 * object -> tableau
527 *
528 * @param object $object The object to convert
529 * @return array
530 *
531 */
532 function inc_object_to_array( $object ) {
533 if( !is_object( $object ) && !is_array( $object ) ) {
534 return $object;
535 }
536 if( is_object( $object ) ) {
537 $object = get_object_vars( $object );
538 }
539 return array_map( 'inc_object_to_array', $object );
540 }
541
542 /**
543 * yql -> tableau
544 * @throws Exception
545 * @param string $u
546 * @return array|bool
547 */
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&#233;ponse vide ou mal form&#233;e');
554 return false;
555 }
556 return inc_object_to_array($w);
557 }
558
559 /**
560 * sql -> tableau
561 * @param string $u
562 * @return array|bool
563 */
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;
576 }
577
578 /**
579 * json -> tableau
580 * @param string $u
581 * @return array|bool
582 */
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;
587 }
588
589 /**
590 * csv -> tableau
591 * @param string $u
592 * @return array|bool
593 */
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;
606 }
607
608 /**
609 * RSS -> tableau
610 * @param string $u
611 * @return array|bool
612 */
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;
618 }
619
620 /**
621 * atom, alias de rss -> tableau
622 * @param string $u
623 * @return array|bool
624 */
625 function inc_atom_to_array_dist($u) {
626 $g = charger_fonction('rss_to_array', 'inc');
627 return $g($u);
628 }
629
630 /**
631 * glob -> tableau
632 * lister des fichiers selon un masque, pour la syntaxe cf php.net/glob
633 * @param string $u
634 * @return array|bool
635 */
636 function inc_glob_to_array_dist($u) {
637 return (array) glob($u,
638 GLOB_MARK | GLOB_NOSORT | GLOB_BRACE
639 );
640 }
641
642 /**
643 * YAML -> tableau
644 * @param string $u
645 * @return bool|array
646 * @throws Exception
647 */
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 }
654
655 return yaml_decode($u);
656 }
657
658
659 /**
660 * pregfiles -> tableau
661 * lister des fichiers a partir d'un dossier de base et selon une regexp.
662 * pour la syntaxe cf la fonction spip preg_files
663 * @param string $dir
664 * @param string $regexp
665 * @param int $limit
666 * @return array|bool
667 */
668 function inc_pregfiles_to_array_dist($dir, $regexp=-1, $limit=10000) {
669 return (array) preg_files($dir, $regexp, $limit);
670 }
671
672 /**
673 * ls -> tableau
674 * ls : lister des fichiers selon un masque glob
675 * et renvoyer aussi leurs donnees php.net/stat
676 * @param string $u
677 * @return array|bool
678 */
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'] = basename($v);
687 $v = array_merge(
688 pathinfo($v),
689 $b
690 );
691 }
692 return $a;
693 }
694
695 /**
696 * Object -> tableau
697 * @param Object $object
698 * @return array|bool
699 */
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;
719 }
720 ?>