3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
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 \***************************************************************************/
14 * Gestion de l'itérateur DATA
16 * @package SPIP\Core\Iterateur\DATA
19 if (!defined('_ECRIRE_INC_VERSION')) {
23 if (!defined('_DATA_SOURCE_MAX_SIZE')) {
24 define('_DATA_SOURCE_MAX_SIZE', 2 * 1048576);
29 * Créer une boucle sur un itérateur DATA
31 * Annonce au compilateur les "champs" disponibles, c'est à dire
32 * 'cle', 'valeur' et '*' (tout nom possible).
34 * On ne peut effectivement pas connaître à la compilation la structure
35 * des données qui seront obtenues. On indique donc au compilateur que
36 * toute balise utilisée dans la boucle est possiblement un champ
40 * Description de la boucle
42 * Description de la boucle complétée des champs
44 function iterateur_DATA_dist($b) {
45 $b->iterateur
= 'DATA'; # designe la classe d'iterateur
50 '*' => 'ALL' // Champ joker *
53 $b->select
[] = '.valeur';
62 * Pour itérer sur des données quelconques (transformables en tableau)
64 class IterateurDATA
implements Iterator
{
70 protected $tableau = array();
73 * Conditions de filtrage
74 * ie criteres de selection
78 protected $filtre = array();
86 protected $cle = null;
93 protected $valeur = null;
103 * Calcul du total des elements
107 public $total = null;
115 public function __construct($command, $info = array()) {
116 $this->type
= 'DATA';
117 $this->command
= $command;
120 $this->select($command);
128 public function rewind() {
129 reset($this->tableau
);
130 $this->cle
= key($this->tableau
);
131 $this->valeur
= current($this->tableau
);
132 next($this->tableau
);
136 * Déclarer les critères exceptions
140 public function exception_des_criteres() {
141 return array('tableau');
145 * Récupérer depuis le cache si possible
150 protected function cache_get($cle) {
154 # utiliser memoization si dispo
155 if (!function_exists('cache_get')) {
159 return cache_get($cle);
163 * Stocker en cache si possible
167 * @param null|mixed $valeur
170 protected function cache_set($cle, $ttl, $valeur = null) {
174 if (is_null($valeur)) {
175 $valeur = $this->tableau
;
177 # utiliser memoization si dispo
178 if (!function_exists('cache_set')) {
182 return cache_set($cle,
189 # conserver le cache 1h de plus que la validite demandee,
190 # pour le cas ou le serveur distant ne reponde plus
194 * Aller chercher les données de la boucle DATA
197 * @param array $command
200 protected function select($command) {
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']);
209 $this->command
['sourcemode'] = $this->command
['from'][0];
212 // cherchons differents moyens de creer le tableau de donnees
213 // les commandes connues pour l'iterateur DATA
214 // sont : {tableau #ARRAY} ; {cle=...} ; {valeur=...}
216 // {source format, [URL], [arg2]...}
217 if (isset($this->command
['source'])
218 and isset($this->command
['sourcemode'])
220 $this->select_source();
223 // Critere {liste X1, X2, X3}
224 if (isset($this->command
['liste'])) {
225 $this->select_liste();
227 if (isset($this->command
['enum'])) {
228 $this->select_enum();
231 // Si a ce stade on n'a pas de table, il y a un bug
232 if (!is_array($this->tableau
)) {
234 spip_log("erreur datasource " . var_export($command, true));
237 // {datapath query.results}
238 // extraire le chemin "query.results" du tableau de donnees
240 and isset($this->command
['datapath'])
241 and is_array($this->command
['datapath'])
243 $this->select_datapath();
247 if ($this->command
['orderby']) {
248 $this->select_orderby();
251 // grouper les resultats {fusion /x/y/z} ;
252 if ($this->command
['groupby']) {
253 $this->select_groupby();
257 #var_dump($this->tableau);
262 * Aller chercher les donnees de la boucle DATA
264 * {source format, [URL], [arg2]...}
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'))
274 charger_fonction($this->command
['sourcemode'] . '_to_array', 'inc', true);
277 # le premier argument peut etre un array, une URL etc.
278 $src = $this->command
['source'][0];
280 # avons-nous un cache dispo ?
282 if (is_string($src)) {
283 $cle = 'datasource_' . md5($this->command
['sourcemode'] . ':' . var_export($this->command
['source'], true));
286 $cache = $this->cache_get($cle);
287 if (isset($this->command
['datacache'])) {
288 $ttl = intval($this->command
['datacache']);
291 and ($cache['time'] +
(isset($ttl) ?
$ttl : $cache['ttl'])
293 and !(_request('var_mode') === 'recalcul'
294 and include_spip('inc/autoriser')
295 and autoriser('recalcul')
298 $this->tableau
= $cache['data'];
301 # dommage que ca ne soit pas une option de yql_to_array...
302 if ($this->command
['sourcemode'] == 'yql') {
308 if (isset($this->command
['sourcemode'])
309 and in_array($this->command
['sourcemode'],
310 array('table', 'array', 'tableau'))
312 if (is_array($a = $src)
314 and $a = str_replace('"', '"', $a) # fragile!
315 and is_array($a = @unserialize
($a)))
320 if (tester_url_absolue($src)) {
321 include_spip('inc/distant');
322 $u = recuperer_page($src, false, false, _DATA_SOURCE_MAX_SIZE
);
324 throw new Exception("404");
336 if (@is_readable
($src) && @is_file
($src)) {
337 $u = spip_file_get_contents($src);
350 and $g = charger_fonction($this->command
['sourcemode'] . '_to_array', 'inc', true)
352 $args = $this->command
['source'];
354 if (is_array($a = call_user_func_array($g, $args))) {
360 if (!is_array($this->tableau
)) {
364 if (!$this->err
and isset($ttl) and $ttl > 0) {
365 $this->cache_set($cle, $ttl);
368 } catch (Exception
$e) {
369 $e = $e->getMessage();
370 $err = sprintf("[%s, %s] $e",
372 $this->command
['sourcemode']);
373 erreur_squelette(array($err, array()));
378 # en cas d'erreur, utiliser le cache si encore dispo
382 $this->tableau
= $cache['data'];
389 * Retourne un tableau donne depuis un critère liste
391 * Critère `{liste X1, X2, X3}`
393 * @see critere_DATA_liste_dist()
396 protected function select_liste() {
397 # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE
398 if (!isset($this->command
['liste'][1])) {
399 if (!is_array($this->command
['liste'][0])) {
400 $this->command
['liste'] = explode(',', $this->command
['liste'][0]);
402 $this->command
['liste'] = $this->command
['liste'][0];
405 $this->tableau
= $this->command
['liste'];
409 * Retourne un tableau donne depuis un critere liste
410 * Critere {enum Xmin, Xmax}
413 protected function select_enum() {
414 # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE
415 if (!isset($this->command
['enum'][1])) {
416 if (!is_array($this->command
['enum'][0])) {
417 $this->command
['enum'] = explode(',', $this->command
['enum'][0]);
419 $this->command
['enum'] = $this->command
['enum'][0];
422 if (count($this->command
['enum']) >= 3) {
423 $enum = range(array_shift($this->command
['enum']), array_shift($this->command
['enum']),
424 array_shift($this->command
['enum']));
426 $enum = range(array_shift($this->command
['enum']), array_shift($this->command
['enum']));
428 $this->tableau
= $enum;
433 * extraire le chemin "query.results" du tableau de donnees
434 * {datapath query.results}
437 protected function select_datapath() {
438 $base = reset($this->command
['datapath']);
439 if (strlen($base = ltrim(trim($base), "/"))) {
440 $this->tableau
= table_valeur($this->tableau
, $base);
441 if (!is_array($this->tableau
)) {
442 $this->tableau
= array();
444 spip_log("datapath '$base' absent");
450 * Ordonner les resultats
454 protected function select_orderby() {
457 foreach ($this->command
['orderby'] as $tri) {
458 // virer le / initial pour les criteres de la forme {par /xx}
459 if (preg_match(',^\.?([/\w]+)( DESC)?$,iS', ltrim($tri, '/'), $r)) {
460 $r = array_pad($r, 3, null);
463 if ($r[1] == 'cle') {
464 if (isset($r[2]) and $r[2]) {
465 krsort($this->tableau
);
467 ksort($this->tableau
);
471 if ($r[1] == 'hasard') {
472 $k = array_keys($this->tableau
);
475 foreach ($k as $cle) {
476 $v[$cle] = $this->tableau
[$cle];
481 if ($r[1] == 'valeur') {
483 } # {par valeur/xx/yy} ??
485 $tv = 'table_valeur(%s, ' . var_export($r[1], true) . ')';
488 $a = ' . sprintf($tv, '$aa') . ';
489 $b = ' . sprintf($tv, '$bb') . ';
491 return ($a ' . (!empty($r[2]) ?
'>' : '<') . ' $b) ? -1 : 1;';
498 $sortfunc .= "\n return 0;";
499 uasort($this->tableau
, function($aa, $bb) use ($sortfunc) {
500 return eval($sortfunc);
507 * Grouper les resultats
511 protected function select_groupby() {
512 // virer le / initial pour les criteres de la forme {fusion /xx}
513 if (strlen($fusion = ltrim($this->command
['groupby'][0], '/'))) {
515 foreach ($this->tableau
as $k => $v) {
516 $val = table_valeur($v, $fusion);
517 if (isset($vu[$val])) {
518 unset($this->tableau
[$k]);
528 * L'iterateur est-il encore valide ?
532 public function valid() {
533 return !is_null($this->cle
);
537 * Retourner la valeur
541 public function current() {
542 return $this->valeur
;
550 public function key() {
555 * Passer a la valeur suivante
559 public function next() {
560 if ($this->valid()) {
561 $this->cle
= key($this->tableau
);
562 $this->valeur
= current($this->tableau
);
563 next($this->tableau
);
568 * Compter le nombre total de resultats
572 public function count() {
573 if (is_null($this->total
)) {
574 $this->total
= count($this->tableau
);
582 * Fonctions de transformation donnee => tableau
591 function inc_file_to_array_dist($u) {
592 return preg_split('/\r?\n/', $u);
600 function inc_plugins_to_array_dist() {
601 include_spip('inc/plugin');
603 return liste_chemin_plugin_actifs();
612 function inc_xml_to_array_dist($u) {
613 return @XMLObjectToArray
(new SimpleXmlIterator($u));
620 * @param object $object The object to convert
624 function inc_object_to_array($object) {
625 if (!is_object($object) && !is_array($object)) {
628 if (is_object($object)) {
629 $object = get_object_vars($object);
632 return array_map('inc_object_to_array', $object);
642 function inc_yql_to_array_dist($u) {
643 define('_YQL_ENDPOINT', 'https://query.yahooapis.com/v1/public/yql?&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q=');
644 $v = recuperer_url($url = _YQL_ENDPOINT
. urlencode($u) . '&format=json');
646 or !$w = json_decode($v['page'], true)
648 throw new Exception('YQL: réponse vide ou mal formée');
650 if (isset($w['error'])) {
651 throw new Exception($w['error']['description']);
654 return inc_object_to_array($w);
663 function inc_sql_to_array_dist($u) {
664 # sortir le connecteur de $u
665 preg_match(',^(?:(\w+):)?(.*)$,S', $u, $v);
666 $serveur = (string)$v[1];
668 if ($s = sql_query($req, $serveur)) {
670 while ($t = sql_fetch($s)) {
686 function inc_json_to_array_dist($u) {
687 if (is_array($json = json_decode($u, true))) {
698 function inc_csv_to_array_dist($u) {
699 include_spip('inc/csv');
700 list($entete, $csv) = analyse_csv($u);
701 array_unshift($csv, $entete);
703 include_spip('inc/charsets');
705 foreach ($entete as $k => $v) {
706 if (trim($v) == "") {
708 } // reperer des eventuelles cases vides
709 if (is_numeric($v) and $v < 0) {
711 } // ne pas risquer d'ecraser une cle numerique
712 if (is_numeric($v)) {
714 } // ne pas risquer d'ecraser une cle numerique
715 $v = strtolower(preg_replace(',\W+,', '_', translitteration($v)));
716 foreach ($csv as &$item) {
717 $item[$v] = &$item[$k];
731 function inc_rss_to_array_dist($u) {
732 include_spip('inc/syndic');
733 if (is_array($rss = analyser_backend($u))) {
741 * atom, alias de rss -> tableau
746 function inc_atom_to_array_dist($u) {
747 $g = charger_fonction('rss_to_array', 'inc');
754 * lister des fichiers selon un masque, pour la syntaxe cf php.net/glob
759 function inc_glob_to_array_dist($u) {
761 GLOB_MARK | GLOB_NOSORT | GLOB_BRACE
764 return $a ?
$a : array();
774 function inc_yaml_to_array_dist($u) {
775 include_spip('inc/yaml-mini');
776 if (!function_exists("yaml_decode")) {
777 throw new Exception('YAML: impossible de trouver la fonction yaml_decode');
782 return yaml_decode($u);
787 * pregfiles -> tableau
788 * lister des fichiers a partir d'un dossier de base et selon une regexp.
789 * pour la syntaxe cf la fonction spip preg_files
792 * @param string $regexp
796 function inc_pregfiles_to_array_dist($dir, $regexp = -1, $limit = 10000) {
797 return (array)preg_files($dir, $regexp, $limit);
802 * ls : lister des fichiers selon un masque glob
803 * et renvoyer aussi leurs donnees php.net/stat
808 function inc_ls_to_array_dist($u) {
809 $glob = charger_fonction('glob_to_array', 'inc');
811 foreach ($a as &$v) {
812 $b = (array)@stat
($v);
813 foreach ($b as $k => $ignore) {
814 if (is_numeric($k)) {
818 $b['file'] = preg_replace('`/$`','',$v) ;
831 * @param Object $object
834 function XMLObjectToArray($object) {
835 $xml_array = array();
836 for ($object->rewind(); $object->valid(); $object->next()) {
837 if (array_key_exists($key = $object->key(), $xml_array)) {
838 $key .= '-' . uniqid();
840 $vars = get_object_vars($object->current());
841 if (isset($vars['@attributes'])) {
842 foreach ($vars['@attributes'] as $k => $v) {
843 $xml_array[$key][$k] = $v;
846 if ($object->hasChildren()) {
847 $xml_array[$key][] = XMLObjectToArray(
850 $xml_array[$key][] = strval($object->current());