Completed
Push — master ( e150be...262076 )
by cam
11:11
created
ecrire/iterateur/data.php 1 patch
Indentation   +645 added lines, -645 removed lines patch added patch discarded remove patch
@@ -17,11 +17,11 @@  discard block
 block discarded – undo
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
 block discarded – undo
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,520 +62,520 @@  discard block
 block discarded – undo
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
-				# dommage que ca ne soit pas une option de yql_to_array...
302
-				if ($this->command['sourcemode'] == 'yql') {
303
-					if (!isset($ttl)) {
304
-						$ttl = 3600;
305
-					}
306
-				}
307
-
308
-				if (isset($this->command['sourcemode'])
309
-					and in_array($this->command['sourcemode'],
310
-						array('table', 'array', 'tableau'))
311
-				) {
312
-					if (is_array($a = $src)
313
-						or (is_string($a)
314
-							and $a = str_replace('"', '"', $a) # fragile!
315
-							and is_array($a = @unserialize($a)))
316
-					) {
317
-						$this->tableau = $a;
318
-					}
319
-				} else {
320
-					if (tester_url_absolue($src)) {
321
-						include_spip('inc/distant');
322
-						$u = recuperer_page($src, false, false, _DATA_SOURCE_MAX_SIZE);
323
-						if (!$u) {
324
-							throw new Exception("404");
325
-						}
326
-						if (!isset($ttl)) {
327
-							$ttl = 24 * 3600;
328
-						}
329
-					} else {
330
-						if (@is_dir($src)) {
331
-							$u = $src;
332
-							if (!isset($ttl)) {
333
-								$ttl = 10;
334
-							}
335
-						} else {
336
-							if (@is_readable($src) && @is_file($src)) {
337
-								$u = spip_file_get_contents($src);
338
-								if (!isset($ttl)) {
339
-									$ttl = 10;
340
-								}
341
-							} else {
342
-								$u = $src;
343
-								if (!isset($ttl)) {
344
-									$ttl = 10;
345
-								}
346
-							}
347
-						}
348
-					}
349
-					if (!$this->err
350
-						and $g = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true)
351
-					) {
352
-						$args = $this->command['source'];
353
-						$args[0] = $u;
354
-						if (is_array($a = call_user_func_array($g, $args))) {
355
-							$this->tableau = $a;
356
-						}
357
-					}
358
-				}
359
-
360
-				if (!is_array($this->tableau)) {
361
-					$this->err = true;
362
-				}
363
-
364
-				if (!$this->err and isset($ttl) and $ttl > 0) {
365
-					$this->cache_set($cle, $ttl);
366
-				}
367
-
368
-			} catch (Exception $e) {
369
-				$e = $e->getMessage();
370
-				$err = sprintf("[%s, %s] $e",
371
-					$src,
372
-					$this->command['sourcemode']);
373
-				erreur_squelette(array($err, array()));
374
-				$this->err = true;
375
-			}
376
-		}
377
-
378
-		# en cas d'erreur, utiliser le cache si encore dispo
379
-		if ($this->err
380
-			and $cache
381
-		) {
382
-			$this->tableau = $cache['data'];
383
-			$this->err = false;
384
-		}
385
-	}
386
-
387
-
388
-	/**
389
-	 * Retourne un tableau donne depuis un critère liste
390
-	 *
391
-	 * Critère `{liste X1, X2, X3}`
392
-	 *
393
-	 * @see critere_DATA_liste_dist()
394
-	 *
395
-	 **/
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]);
401
-			} else {
402
-				$this->command['liste'] = $this->command['liste'][0];
403
-			}
404
-		}
405
-		$this->tableau = $this->command['liste'];
406
-	}
407
-
408
-	/**
409
-	 * Retourne un tableau donne depuis un critere liste
410
-	 * Critere {enum Xmin, Xmax}
411
-	 *
412
-	 **/
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]);
418
-			} else {
419
-				$this->command['enum'] = $this->command['enum'][0];
420
-			}
421
-		}
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']));
425
-		} else {
426
-			$enum = range(array_shift($this->command['enum']), array_shift($this->command['enum']));
427
-		}
428
-		$this->tableau = $enum;
429
-	}
430
-
431
-
432
-	/**
433
-	 * extraire le chemin "query.results" du tableau de donnees
434
-	 * {datapath query.results}
435
-	 *
436
-	 **/
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();
443
-				$this->err = true;
444
-				spip_log("datapath '$base' absent");
445
-			}
446
-		}
447
-	}
448
-
449
-	/**
450
-	 * Ordonner les resultats
451
-	 * {par x}
452
-	 *
453
-	 **/
454
-	protected function select_orderby() {
455
-		$sortfunc = '';
456
-		$aleas = 0;
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);
461
-
462
-				// tri par cle
463
-				if ($r[1] == 'cle') {
464
-					if (isset($r[2]) and $r[2]) {
465
-						krsort($this->tableau);
466
-					} else {
467
-						ksort($this->tableau);
468
-					}
469
-				} # {par hasard}
470
-				else {
471
-					if ($r[1] == 'hasard') {
472
-						$k = array_keys($this->tableau);
473
-						shuffle($k);
474
-						$v = array();
475
-						foreach ($k as $cle) {
476
-							$v[$cle] = $this->tableau[$cle];
477
-						}
478
-						$this->tableau = $v;
479
-					} else {
480
-						# {par valeur}
481
-						if ($r[1] == 'valeur') {
482
-							$tv = '%s';
483
-						} # {par valeur/xx/yy} ??
484
-						else {
485
-							$tv = 'table_valeur(%s, ' . var_export($r[1], true) . ')';
486
-						}
487
-						$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
+                # dommage que ca ne soit pas une option de yql_to_array...
302
+                if ($this->command['sourcemode'] == 'yql') {
303
+                    if (!isset($ttl)) {
304
+                        $ttl = 3600;
305
+                    }
306
+                }
307
+
308
+                if (isset($this->command['sourcemode'])
309
+                    and in_array($this->command['sourcemode'],
310
+                        array('table', 'array', 'tableau'))
311
+                ) {
312
+                    if (is_array($a = $src)
313
+                        or (is_string($a)
314
+                            and $a = str_replace('"', '"', $a) # fragile!
315
+                            and is_array($a = @unserialize($a)))
316
+                    ) {
317
+                        $this->tableau = $a;
318
+                    }
319
+                } else {
320
+                    if (tester_url_absolue($src)) {
321
+                        include_spip('inc/distant');
322
+                        $u = recuperer_page($src, false, false, _DATA_SOURCE_MAX_SIZE);
323
+                        if (!$u) {
324
+                            throw new Exception("404");
325
+                        }
326
+                        if (!isset($ttl)) {
327
+                            $ttl = 24 * 3600;
328
+                        }
329
+                    } else {
330
+                        if (@is_dir($src)) {
331
+                            $u = $src;
332
+                            if (!isset($ttl)) {
333
+                                $ttl = 10;
334
+                            }
335
+                        } else {
336
+                            if (@is_readable($src) && @is_file($src)) {
337
+                                $u = spip_file_get_contents($src);
338
+                                if (!isset($ttl)) {
339
+                                    $ttl = 10;
340
+                                }
341
+                            } else {
342
+                                $u = $src;
343
+                                if (!isset($ttl)) {
344
+                                    $ttl = 10;
345
+                                }
346
+                            }
347
+                        }
348
+                    }
349
+                    if (!$this->err
350
+                        and $g = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true)
351
+                    ) {
352
+                        $args = $this->command['source'];
353
+                        $args[0] = $u;
354
+                        if (is_array($a = call_user_func_array($g, $args))) {
355
+                            $this->tableau = $a;
356
+                        }
357
+                    }
358
+                }
359
+
360
+                if (!is_array($this->tableau)) {
361
+                    $this->err = true;
362
+                }
363
+
364
+                if (!$this->err and isset($ttl) and $ttl > 0) {
365
+                    $this->cache_set($cle, $ttl);
366
+                }
367
+
368
+            } catch (Exception $e) {
369
+                $e = $e->getMessage();
370
+                $err = sprintf("[%s, %s] $e",
371
+                    $src,
372
+                    $this->command['sourcemode']);
373
+                erreur_squelette(array($err, array()));
374
+                $this->err = true;
375
+            }
376
+        }
377
+
378
+        # en cas d'erreur, utiliser le cache si encore dispo
379
+        if ($this->err
380
+            and $cache
381
+        ) {
382
+            $this->tableau = $cache['data'];
383
+            $this->err = false;
384
+        }
385
+    }
386
+
387
+
388
+    /**
389
+     * Retourne un tableau donne depuis un critère liste
390
+     *
391
+     * Critère `{liste X1, X2, X3}`
392
+     *
393
+     * @see critere_DATA_liste_dist()
394
+     *
395
+     **/
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]);
401
+            } else {
402
+                $this->command['liste'] = $this->command['liste'][0];
403
+            }
404
+        }
405
+        $this->tableau = $this->command['liste'];
406
+    }
407
+
408
+    /**
409
+     * Retourne un tableau donne depuis un critere liste
410
+     * Critere {enum Xmin, Xmax}
411
+     *
412
+     **/
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]);
418
+            } else {
419
+                $this->command['enum'] = $this->command['enum'][0];
420
+            }
421
+        }
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']));
425
+        } else {
426
+            $enum = range(array_shift($this->command['enum']), array_shift($this->command['enum']));
427
+        }
428
+        $this->tableau = $enum;
429
+    }
430
+
431
+
432
+    /**
433
+     * extraire le chemin "query.results" du tableau de donnees
434
+     * {datapath query.results}
435
+     *
436
+     **/
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();
443
+                $this->err = true;
444
+                spip_log("datapath '$base' absent");
445
+            }
446
+        }
447
+    }
448
+
449
+    /**
450
+     * Ordonner les resultats
451
+     * {par x}
452
+     *
453
+     **/
454
+    protected function select_orderby() {
455
+        $sortfunc = '';
456
+        $aleas = 0;
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);
461
+
462
+                // tri par cle
463
+                if ($r[1] == 'cle') {
464
+                    if (isset($r[2]) and $r[2]) {
465
+                        krsort($this->tableau);
466
+                    } else {
467
+                        ksort($this->tableau);
468
+                    }
469
+                } # {par hasard}
470
+                else {
471
+                    if ($r[1] == 'hasard') {
472
+                        $k = array_keys($this->tableau);
473
+                        shuffle($k);
474
+                        $v = array();
475
+                        foreach ($k as $cle) {
476
+                            $v[$cle] = $this->tableau[$cle];
477
+                        }
478
+                        $this->tableau = $v;
479
+                    } else {
480
+                        # {par valeur}
481
+                        if ($r[1] == 'valeur') {
482
+                            $tv = '%s';
483
+                        } # {par valeur/xx/yy} ??
484
+                        else {
485
+                            $tv = 'table_valeur(%s, ' . var_export($r[1], true) . ')';
486
+                        }
487
+                        $sortfunc .= '
488 488
 					$a = ' . sprintf($tv, '$aa') . ';
489 489
 					$b = ' . sprintf($tv, '$bb') . ';
490 490
 					if ($a <> $b)
491 491
 						return ($a ' . (!empty($r[2]) ? '>' : '<') . ' $b) ? -1 : 1;';
492
-					}
493
-				}
494
-			}
495
-		}
496
-
497
-		if ($sortfunc) {
498
-			$sortfunc .= "\n return 0;";
499
-			uasort($this->tableau, function($aa, $bb) use ($sortfunc) {
500
-				return eval($sortfunc);
501
-			});
502
-		}
503
-	}
504
-
505
-
506
-	/**
507
-	 * Grouper les resultats
508
-	 * {fusion /x/y/z}
509
-	 *
510
-	 **/
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], '/'))) {
514
-			$vu = array();
515
-			foreach ($this->tableau as $k => $v) {
516
-				$val = table_valeur($v, $fusion);
517
-				if (isset($vu[$val])) {
518
-					unset($this->tableau[$k]);
519
-				} else {
520
-					$vu[$val] = true;
521
-				}
522
-			}
523
-		}
524
-	}
525
-
526
-
527
-	/**
528
-	 * L'iterateur est-il encore valide ?
529
-	 *
530
-	 * @return bool
531
-	 */
532
-	public function valid() {
533
-		return !is_null($this->cle);
534
-	}
535
-
536
-	/**
537
-	 * Retourner la valeur
538
-	 *
539
-	 * @return null
540
-	 */
541
-	public function current() {
542
-		return $this->valeur;
543
-	}
544
-
545
-	/**
546
-	 * Retourner la cle
547
-	 *
548
-	 * @return null
549
-	 */
550
-	public function key() {
551
-		return $this->cle;
552
-	}
553
-
554
-	/**
555
-	 * Passer a la valeur suivante
556
-	 *
557
-	 * @return void
558
-	 */
559
-	public function next() {
560
-		if ($this->valid()) {
561
-			$this->cle = key($this->tableau);
562
-			$this->valeur = current($this->tableau);
563
-			next($this->tableau);
564
-		}
565
-	}
566
-
567
-	/**
568
-	 * Compter le nombre total de resultats
569
-	 *
570
-	 * @return int
571
-	 */
572
-	public function count() {
573
-		if (is_null($this->total)) {
574
-			$this->total = count($this->tableau);
575
-		}
576
-
577
-		return $this->total;
578
-	}
492
+                    }
493
+                }
494
+            }
495
+        }
496
+
497
+        if ($sortfunc) {
498
+            $sortfunc .= "\n return 0;";
499
+            uasort($this->tableau, function($aa, $bb) use ($sortfunc) {
500
+                return eval($sortfunc);
501
+            });
502
+        }
503
+    }
504
+
505
+
506
+    /**
507
+     * Grouper les resultats
508
+     * {fusion /x/y/z}
509
+     *
510
+     **/
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], '/'))) {
514
+            $vu = array();
515
+            foreach ($this->tableau as $k => $v) {
516
+                $val = table_valeur($v, $fusion);
517
+                if (isset($vu[$val])) {
518
+                    unset($this->tableau[$k]);
519
+                } else {
520
+                    $vu[$val] = true;
521
+                }
522
+            }
523
+        }
524
+    }
525
+
526
+
527
+    /**
528
+     * L'iterateur est-il encore valide ?
529
+     *
530
+     * @return bool
531
+     */
532
+    public function valid() {
533
+        return !is_null($this->cle);
534
+    }
535
+
536
+    /**
537
+     * Retourner la valeur
538
+     *
539
+     * @return null
540
+     */
541
+    public function current() {
542
+        return $this->valeur;
543
+    }
544
+
545
+    /**
546
+     * Retourner la cle
547
+     *
548
+     * @return null
549
+     */
550
+    public function key() {
551
+        return $this->cle;
552
+    }
553
+
554
+    /**
555
+     * Passer a la valeur suivante
556
+     *
557
+     * @return void
558
+     */
559
+    public function next() {
560
+        if ($this->valid()) {
561
+            $this->cle = key($this->tableau);
562
+            $this->valeur = current($this->tableau);
563
+            next($this->tableau);
564
+        }
565
+    }
566
+
567
+    /**
568
+     * Compter le nombre total de resultats
569
+     *
570
+     * @return int
571
+     */
572
+    public function count() {
573
+        if (is_null($this->total)) {
574
+            $this->total = count($this->tableau);
575
+        }
576
+
577
+        return $this->total;
578
+    }
579 579
 }
580 580
 
581 581
 /*
@@ -589,7 +589,7 @@  discard block
 block discarded – undo
589 589
  * @return array
590 590
  */
591 591
 function inc_file_to_array_dist($u) {
592
-	return preg_split('/\r?\n/', $u);
592
+    return preg_split('/\r?\n/', $u);
593 593
 }
594 594
 
595 595
 /**
@@ -598,9 +598,9 @@  discard block
 block discarded – undo
598 598
  * @return unknown
599 599
  */
600 600
 function inc_plugins_to_array_dist() {
601
-	include_spip('inc/plugin');
601
+    include_spip('inc/plugin');
602 602
 
603
-	return liste_chemin_plugin_actifs();
603
+    return liste_chemin_plugin_actifs();
604 604
 }
605 605
 
606 606
 /**
@@ -610,7 +610,7 @@  discard block
 block discarded – undo
610 610
  * @return array
611 611
  */
612 612
 function inc_xml_to_array_dist($u) {
613
-	return @XMLObjectToArray(new SimpleXmlIterator($u));
613
+    return @XMLObjectToArray(new SimpleXmlIterator($u));
614 614
 }
615 615
 
616 616
 /**
@@ -622,14 +622,14 @@  discard block
 block discarded – undo
622 622
  *
623 623
  */
624 624
 function inc_object_to_array($object) {
625
-	if (!is_object($object) && !is_array($object)) {
626
-		return $object;
627
-	}
628
-	if (is_object($object)) {
629
-		$object = get_object_vars($object);
630
-	}
631
-
632
-	return array_map('inc_object_to_array', $object);
625
+    if (!is_object($object) && !is_array($object)) {
626
+        return $object;
627
+    }
628
+    if (is_object($object)) {
629
+        $object = get_object_vars($object);
630
+    }
631
+
632
+    return array_map('inc_object_to_array', $object);
633 633
 }
634 634
 
635 635
 /**
@@ -640,18 +640,18 @@  discard block
 block discarded – undo
640 640
  * @return array|bool
641 641
  */
642 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');
645
-	if (!$v['page']
646
-		or !$w = json_decode($v['page'], true)
647
-	) {
648
-		throw new Exception('YQL: r&#233;ponse vide ou mal form&#233;e');
649
-	}
650
-	if (isset($w['error'])) {
651
-		throw new Exception($w['error']['description']);
652
-	}
653
-
654
-	return inc_object_to_array($w);
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');
645
+    if (!$v['page']
646
+        or !$w = json_decode($v['page'], true)
647
+    ) {
648
+        throw new Exception('YQL: r&#233;ponse vide ou mal form&#233;e');
649
+    }
650
+    if (isset($w['error'])) {
651
+        throw new Exception($w['error']['description']);
652
+    }
653
+
654
+    return inc_object_to_array($w);
655 655
 }
656 656
 
657 657
 /**
@@ -661,20 +661,20 @@  discard block
 block discarded – undo
661 661
  * @return array|bool
662 662
  */
663 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];
667
-	$req = trim($v[2]);
668
-	if ($s = sql_query($req, $serveur)) {
669
-		$r = array();
670
-		while ($t = sql_fetch($s)) {
671
-			$r[] = $t;
672
-		}
673
-
674
-		return $r;
675
-	}
676
-
677
-	return false;
664
+    # sortir le connecteur de $u
665
+    preg_match(',^(?:(\w+):)?(.*)$,S', $u, $v);
666
+    $serveur = (string)$v[1];
667
+    $req = trim($v[2]);
668
+    if ($s = sql_query($req, $serveur)) {
669
+        $r = array();
670
+        while ($t = sql_fetch($s)) {
671
+            $r[] = $t;
672
+        }
673
+
674
+        return $r;
675
+    }
676
+
677
+    return false;
678 678
 }
679 679
 
680 680
 /**
@@ -684,11 +684,11 @@  discard block
 block discarded – undo
684 684
  * @return array|bool
685 685
  */
686 686
 function inc_json_to_array_dist($u) {
687
-	if (is_array($json = json_decode($u))
688
-		or is_object($json)
689
-	) {
690
-		return (array)$json;
691
-	}
687
+    if (is_array($json = json_decode($u))
688
+        or is_object($json)
689
+    ) {
690
+        return (array)$json;
691
+    }
692 692
 }
693 693
 
694 694
 /**
@@ -698,30 +698,30 @@  discard block
 block discarded – undo
698 698
  * @return array|bool
699 699
  */
700 700
 function inc_csv_to_array_dist($u) {
701
-	include_spip('inc/csv');
702
-	list($entete, $csv) = analyse_csv($u);
703
-	array_unshift($csv, $entete);
704
-
705
-	include_spip('inc/charsets');
706
-	$i = 1;
707
-	foreach ($entete as $k => $v) {
708
-		if (trim($v) == "") {
709
-			$v = "col" . $i;
710
-		} // reperer des eventuelles cases vides
711
-		if (is_numeric($v) and $v < 0) {
712
-			$v = "__" . $v;
713
-		} // ne pas risquer d'ecraser une cle numerique
714
-		if (is_numeric($v)) {
715
-			$v = "_" . $v;
716
-		} // ne pas risquer d'ecraser une cle numerique
717
-		$v = strtolower(preg_replace(',\W+,', '_', translitteration($v)));
718
-		foreach ($csv as &$item) {
719
-			$item[$v] = &$item[$k];
720
-		}
721
-		$i++;
722
-	}
723
-
724
-	return $csv;
701
+    include_spip('inc/csv');
702
+    list($entete, $csv) = analyse_csv($u);
703
+    array_unshift($csv, $entete);
704
+
705
+    include_spip('inc/charsets');
706
+    $i = 1;
707
+    foreach ($entete as $k => $v) {
708
+        if (trim($v) == "") {
709
+            $v = "col" . $i;
710
+        } // reperer des eventuelles cases vides
711
+        if (is_numeric($v) and $v < 0) {
712
+            $v = "__" . $v;
713
+        } // ne pas risquer d'ecraser une cle numerique
714
+        if (is_numeric($v)) {
715
+            $v = "_" . $v;
716
+        } // ne pas risquer d'ecraser une cle numerique
717
+        $v = strtolower(preg_replace(',\W+,', '_', translitteration($v)));
718
+        foreach ($csv as &$item) {
719
+            $item[$v] = &$item[$k];
720
+        }
721
+        $i++;
722
+    }
723
+
724
+    return $csv;
725 725
 }
726 726
 
727 727
 /**
@@ -731,12 +731,12 @@  discard block
 block discarded – undo
731 731
  * @return array|bool
732 732
  */
733 733
 function inc_rss_to_array_dist($u) {
734
-	include_spip('inc/syndic');
735
-	if (is_array($rss = analyser_backend($u))) {
736
-		$tableau = $rss;
737
-	}
734
+    include_spip('inc/syndic');
735
+    if (is_array($rss = analyser_backend($u))) {
736
+        $tableau = $rss;
737
+    }
738 738
 
739
-	return $tableau;
739
+    return $tableau;
740 740
 }
741 741
 
742 742
 /**
@@ -746,9 +746,9 @@  discard block
 block discarded – undo
746 746
  * @return array|bool
747 747
  */
748 748
 function inc_atom_to_array_dist($u) {
749
-	$g = charger_fonction('rss_to_array', 'inc');
749
+    $g = charger_fonction('rss_to_array', 'inc');
750 750
 
751
-	return $g($u);
751
+    return $g($u);
752 752
 }
753 753
 
754 754
 /**
@@ -759,11 +759,11 @@  discard block
 block discarded – undo
759 759
  * @return array|bool
760 760
  */
761 761
 function inc_glob_to_array_dist($u) {
762
-	$a = glob($u,
763
-		GLOB_MARK | GLOB_NOSORT | GLOB_BRACE
764
-	);
762
+    $a = glob($u,
763
+        GLOB_MARK | GLOB_NOSORT | GLOB_BRACE
764
+    );
765 765
 
766
-	return $a ? $a : array();
766
+    return $a ? $a : array();
767 767
 }
768 768
 
769 769
 /**
@@ -774,14 +774,14 @@  discard block
 block discarded – undo
774 774
  * @throws Exception
775 775
  */
776 776
 function inc_yaml_to_array_dist($u) {
777
-	include_spip('inc/yaml-mini');
778
-	if (!function_exists("yaml_decode")) {
779
-		throw new Exception('YAML: impossible de trouver la fonction yaml_decode');
777
+    include_spip('inc/yaml-mini');
778
+    if (!function_exists("yaml_decode")) {
779
+        throw new Exception('YAML: impossible de trouver la fonction yaml_decode');
780 780
 
781
-		return false;
782
-	}
781
+        return false;
782
+    }
783 783
 
784
-	return yaml_decode($u);
784
+    return yaml_decode($u);
785 785
 }
786 786
 
787 787
 
@@ -796,7 +796,7 @@  discard block
 block discarded – undo
796 796
  * @return array|bool
797 797
  */
798 798
 function inc_pregfiles_to_array_dist($dir, $regexp = -1, $limit = 10000) {
799
-	return (array)preg_files($dir, $regexp, $limit);
799
+    return (array)preg_files($dir, $regexp, $limit);
800 800
 }
801 801
 
802 802
 /**
@@ -808,23 +808,23 @@  discard block
 block discarded – undo
808 808
  * @return array|bool
809 809
  */
810 810
 function inc_ls_to_array_dist($u) {
811
-	$glob = charger_fonction('glob_to_array', 'inc');
812
-	$a = $glob($u);
813
-	foreach ($a as &$v) {
814
-		$b = (array)@stat($v);
815
-		foreach ($b as $k => $ignore) {
816
-			if (is_numeric($k)) {
817
-				unset($b[$k]);
818
-			}
819
-		}
820
-		$b['file'] = preg_replace('`/$`','',$v) ;
821
-		$v = array_merge(
822
-			pathinfo($v),
823
-			$b
824
-		);
825
-	}
826
-
827
-	return $a;
811
+    $glob = charger_fonction('glob_to_array', 'inc');
812
+    $a = $glob($u);
813
+    foreach ($a as &$v) {
814
+        $b = (array)@stat($v);
815
+        foreach ($b as $k => $ignore) {
816
+            if (is_numeric($k)) {
817
+                unset($b[$k]);
818
+            }
819
+        }
820
+        $b['file'] = preg_replace('`/$`','',$v) ;
821
+        $v = array_merge(
822
+            pathinfo($v),
823
+            $b
824
+        );
825
+    }
826
+
827
+    return $a;
828 828
 }
829 829
 
830 830
 /**
@@ -834,24 +834,24 @@  discard block
 block discarded – undo
834 834
  * @return array|bool
835 835
  */
836 836
 function XMLObjectToArray($object) {
837
-	$xml_array = array();
838
-	for ($object->rewind(); $object->valid(); $object->next()) {
839
-		if (array_key_exists($key = $object->key(), $xml_array)) {
840
-			$key .= '-' . uniqid();
841
-		}
842
-		$vars = get_object_vars($object->current());
843
-		if (isset($vars['@attributes'])) {
844
-			foreach ($vars['@attributes'] as $k => $v) {
845
-				$xml_array[$key][$k] = $v;
846
-			}
847
-		}
848
-		if ($object->hasChildren()) {
849
-			$xml_array[$key][] = XMLObjectToArray(
850
-				$object->current());
851
-		} else {
852
-			$xml_array[$key][] = strval($object->current());
853
-		}
854
-	}
855
-
856
-	return $xml_array;
837
+    $xml_array = array();
838
+    for ($object->rewind(); $object->valid(); $object->next()) {
839
+        if (array_key_exists($key = $object->key(), $xml_array)) {
840
+            $key .= '-' . uniqid();
841
+        }
842
+        $vars = get_object_vars($object->current());
843
+        if (isset($vars['@attributes'])) {
844
+            foreach ($vars['@attributes'] as $k => $v) {
845
+                $xml_array[$key][$k] = $v;
846
+            }
847
+        }
848
+        if ($object->hasChildren()) {
849
+            $xml_array[$key][] = XMLObjectToArray(
850
+                $object->current());
851
+        } else {
852
+            $xml_array[$key][] = strval($object->current());
853
+        }
854
+    }
855
+
856
+    return $xml_array;
857 857
 }
Please login to merge, or discard this patch.