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