4 /***************************************************************************\
5 * SPIP, Systeme de publication pour l'internet *
7 * Copyright (c) 2001-2019 *
8 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
10 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
11 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
12 \***************************************************************************/
14 if (!defined('_ECRIRE_INC_VERSION')) {
19 * Fabrique d'iterateur
20 * permet de charger n'importe quel iterateur IterateurXXX
21 * fourni dans le fichier iterateurs/xxx.php
25 public static function create($iterateur, $command, $info = null) {
27 // cas des SI {si expression} analises tres tot
28 // pour eviter le chargement de tout iterateur
29 if (isset($command['si'])) {
30 foreach ($command['si'] as $si) {
32 // $command pour boucle SQL peut generer des erreurs de compilation
33 // s'il est transmis alors qu'on est dans un iterateur vide
34 return new IterDecorator(new EmptyIterator(), array(), $info);
39 // chercher un iterateur PHP existant (par exemple dans SPL)
40 // (il faudrait passer l'argument ->sql_serveur
41 // pour etre certain qu'on est sur un "php:")
42 if (class_exists($iterateur)) {
43 $a = isset($command['args']) ?
$command['args'] : array();
45 // permettre de passer un Iterateur directement {args #ITERATEUR} :
46 // si on recoit deja un iterateur en argument, on l'utilise
47 if (count($a) == 1 and is_object($a[0]) and is_subclass_of($a[0], 'Iterator')) {
50 // sinon, on cree un iterateur du type donne
52 // arguments de creation de l'iterateur...
57 $iter = new $iterateur();
60 $iter = new $iterateur($a[0]);
63 $iter = new $iterateur($a[0], $a[1]);
66 $iter = new $iterateur($a[0], $a[1], $a[2]);
69 $iter = new $iterateur($a[0], $a[1], $a[2], $a[3]);
72 } catch (Exception
$e) {
73 spip_log("Erreur de chargement de l'iterateur $iterateur");
74 spip_log($e->getMessage());
75 $iter = new EmptyIterator();
79 // chercher la classe d'iterateur
81 // definie dans le fichier iterateurs/xxx.php
82 $class = "Iterateur" . $iterateur;
83 if (!class_exists($class)) {
84 if (!include_spip("iterateur/" . strtolower($iterateur))
85 or !class_exists($class)
87 die("Iterateur $iterateur non trouvé");
88 // si l'iterateur n'existe pas, on se rabat sur le generique
89 # $iter = new EmptyIterator();
92 $iter = new $class($command, $info);
95 return new IterDecorator($iter, $command, $info);
100 class IterDecorator
extends FilterIterator
{
104 * Conditions de filtrage
105 * ie criteres de selection
109 protected $filtre = array();
112 * Fonction de filtrage compilee a partir des criteres de filtre
116 protected $func_filtre = null;
119 * Critere {offset, limit}
124 protected $offset = null;
125 protected $limit = null;
128 * nombre d'elements recuperes depuis la position 0,
129 * en tenant compte des filtres
133 protected $fetched = 0;
136 * Y a t'il une erreur ?
140 protected $err = false;
143 * Drapeau a activer en cas d'echec
144 * (select SQL errone, non chargement des DATA, etc)
146 public function err() {
147 if (method_exists($this->iter
, 'err')) {
148 return $this->iter
->err();
150 if (property_exists($this->iter
, 'err')) {
151 return $this->iter
->err
;
157 public function __construct(Iterator
$iter, $command, $info) {
158 parent
::__construct($iter);
159 parent
::rewind(); // remettre a la premiere position (bug? connu de FilterIterator)
161 // recuperer l'iterateur transmis
162 $this->iter
= $this->getInnerIterator();
163 $this->command
= $command;
168 // chercher la liste des champs a retourner par
169 // fetch si l'objet ne les calcule pas tout seul
170 if (!method_exists($this->iter
, 'fetch')) {
171 $this->calculer_select();
172 $this->calculer_filtres();
175 // emptyIterator critere {si} faux n'a pas d'erreur !
176 if (isset($this->iter
->err
)) {
177 $this->err
= $this->iter
->err
;
180 // pas d'init a priori, le calcul ne sera fait qu'en cas de besoin (provoque une double requete souvent inutile en sqlite)
181 //$this->total = $this->count();
185 // calcule les elements a retournes par fetch()
186 // enleve les elements inutiles du select()
188 private function calculer_select() {
189 if ($select = &$this->command
['select']) {
190 foreach ($select as $s) {
195 $this->select
[] = $s;
200 // recuperer la valeur d'une balise #X
201 // en fonction des methodes
202 // et proprietes disponibles
203 public function get_select($nom) {
204 if (is_object($this->iter
)
205 and method_exists($this->iter
, $nom)
208 return $this->iter
->$nom();
209 } catch (Exception
$e) {
210 // #GETCHILDREN sur un fichier de DirectoryIterator ...
211 spip_log("Methode $nom en echec sur " . get_class($this->iter
));
212 spip_log("Cela peut être normal : retour d'une ligne de resultat ne pouvant pas calculer cette methode");
218 if (property_exists($this->iter, $nom)) {
219 return $this->iter->$nom;
221 // cle et valeur par defaut
222 // ICI PLANTAGE SI ON NE CONTROLE PAS $nom
223 if (in_array($nom, array('cle', 'valeur'))
224 and method_exists($this, $nom)
226 return $this->$nom();
229 // Par defaut chercher en xpath dans la valeur()
230 return table_valeur($this->valeur(), $nom, null);
234 private function calculer_filtres() {
236 // Issu de calculer_select() de public/composer L.519
237 // TODO: externaliser...
239 // retirer les criteres vides:
240 // {X ?} avec X absent de l'URL
241 // {par #ENV{X}} avec X absent de l'URL
242 // IN sur collection vide (ce dernier devrait pouvoir etre fait a la compil)
243 if ($where = &$this->command
['where']) {
244 foreach ($where as $k => $v) {
246 if ((count($v) >= 2) && ($v[0] == 'REGEXP') && ($v[2] == "'.*'")) {
248 } elseif ((count($v) >= 2) && ($v[0] == 'LIKE') && ($v[2] == "'%'")) {
251 $op = $v[0] ?
$v[0] : $v;
256 if ((!$op) or ($op == 1) or ($op == '0=0')) {
259 // traiter {cle IN a,b} ou {valeur !IN a,b}
260 // prendre en compte le cas particulier de sous-requetes
261 // produites par sql_in quand plus de 255 valeurs passees a IN
262 if (preg_match_all(',\s+IN\s+(\(.*\)),', $op, $s_req)) {
264 foreach ($s_req[1] as $key => $val) {
265 $req .= trim($val, '(,)') . ',';
267 $req = '(' . rtrim($req, ',') . ')';
269 if (preg_match(',^\(\(([\w/]+)(\s+NOT)?\s+IN\s+(\(.*\))\)(?:\s+(AND|OR)\s+\(([\w/]+)(\s+NOT)?\s+IN\s+(\(.*\))\))*\)$,',
271 $this->ajouter_filtre($regs[1], 'IN', strlen($req) ?
$req : $regs[3], $regs[2]);
272 unset($op, $where[$k]);
275 foreach ($where as $k => $v) {
276 // 3 possibilites : count($v) =
277 // * 1 : {x y} ; on recoit $v[0] = y
278 // * 2 : {x !op y} ; on recoit $v[0] = 'NOT', $v[1] = array() // array du type {x op y}
279 // * 3 : {x op y} ; on recoit $v[0] = 'op', $v[1] = x, $v[2] = y
281 // 1 : forcement traite par un critere, on passe
282 if (!$v or count($v) == 1) {
285 if (count($v) == 2 and is_array($v[1])) {
286 $this->ajouter_filtre($v[1][1], $v[1][0], $v[1][2], 'NOT');
288 if (count($v) == 3) {
289 $this->ajouter_filtre($v[1], $v[0], $v[2]);
295 if (isset($this->command
['limit']) and $this->command
['limit']) {
296 $limit = explode(',', $this->command
['limit']);
297 $this->offset
= $limit[0];
298 $this->limit
= $limit[1];
301 // Creer la fonction de filtrage sur $this
303 $this->func_filtre
= create_function('$me', $b = 'return (' . join(') AND (', $this->filtre
) . ');');
308 protected function ajouter_filtre($cle, $op, $valeur, $not = false) {
309 if (method_exists($this->iter
, 'exception_des_criteres')) {
310 if (in_array($cle, $this->iter
->exception_des_criteres())) {
314 // TODO: analyser le filtre pour refuser ce qu'on ne sait pas traiter ?
315 # mais c'est normalement deja opere par calculer_critere_infixe()
316 # qui regarde la description 'desc' (en casse reelle d'ailleurs : {isDir=1}
317 # ne sera pas vu si l'on a defini desc['field']['isdir'] pour que #ISDIR soit present.
318 # il faudrait peut etre definir les 2 champs isDir et isdir... a reflechir...
320 # if (!in_array($cle, array('cle', 'valeur')))
323 $a = '$me->get_select(\'' . $cle . '\')';
327 if ($op == 'REGEXP') {
328 $filtre = 'match(' . $a . ', ' . str_replace('\"', '"', $valeur) . ')';
332 $valeur = str_replace(array('\"', '_', '%'), array('"', '.', '.*'), preg_quote($valeur));
333 $filtre = 'match(' . $a . ', ' . $valeur . ')';
340 $filtre = 'in_array(' . $a . ', array' . $valeur . ')';
343 if (!in_array($op, array('<', '<=', '>', '>='))) {
344 spip_log('operateur non reconnu ' . $op); // [todo] mettre une erreur de squelette
353 $filtre = $a . $op . str_replace('\"', '"', $valeur);
357 $filtre = "!($filtre)";
361 $this->filtre
[] = $filtre;
366 public function next() {
376 public function rewind() {
383 # Extension SPIP des iterateurs PHP
385 * type de l'iterateur
392 * parametres de l'iterateur
399 * infos de compilateur
406 * position courante de l'iterateur
410 protected $pos = null;
413 * nombre total resultats dans l'iterateur
417 protected $total = null;
420 * nombre maximal de recherche pour $total
421 * si l'iterateur n'implemente pas de fonction specifique
423 protected $max = 100000;
427 * Liste des champs a inserer dans les $row
428 * retournes par ->fetch()
430 protected $select = array();
434 * aller a la position absolue n,
435 * comptee depuis le debut
439 * @param string $continue
442 * success or fail if not implemented
444 public function seek($n = 0, $continue = null) {
445 if ($this->func_filtre
or !method_exists($this->iter
, 'seek') or !$this->iter
->seek($n)) {
446 $this->seek_loop($n);
455 * aller a la position $n en parcourant
456 * un par un tous les elements
458 private function seek_loop($n) {
459 if ($this->pos
> $n) {
463 while ($this->pos
< $n and $this->valid()) {
471 * Avancer de $saut pas
477 public function skip($saut, $max = null) {
478 // pas de saut en arriere autorise pour cette fonction
479 if (($saut = intval($saut)) <= 0) {
482 $seek = $this->pos +
$saut;
483 // si le saut fait depasser le maxi, on libere la resource
486 $max = $this->count();
489 if ($seek >= $max or $seek >= $this->count()) {
490 // sortie plus rapide que de faire next() jusqu'a la fin !
502 * Renvoyer un tableau des donnees correspondantes
503 * a la position courante de l'iterateur
504 * en controlant si on respecte le filtre
505 * Appliquer aussi le critere {offset,limit}
509 public function fetch() {
510 if (method_exists($this->iter
, 'fetch')) {
511 return $this->iter
->fetch();
514 while ($this->valid()
517 or (isset($this->offset
) and $this->fetched++
< $this->offset
)
522 if (!$this->valid()) {
526 if (isset($this->limit
)
527 and $this->fetched
> $this->offset +
$this->limit
533 foreach ($this->select
as $nom) {
534 $r[$nom] = $this->get_select($nom);
542 // retourner la cle pour #CLE
543 public function cle() {
547 // retourner la valeur pour #VALEUR
548 public function valeur() {
549 # attention PHP est mechant avec les objets, parfois il ne les
550 # clone pas proprement (directoryiterator sous php 5.2.2)
551 # on se rabat sur la version __toString()
552 if (is_object($v = $this->current())) {
553 if (method_exists($v, '__toString')) {
554 $v = $v->__toString();
564 * Accepte-t-on l'entree courante lue ?
565 * On execute les filtres pour le savoir.
567 public function accept() {
568 if ($f = $this->func_filtre
) {
576 * liberer la ressource
580 public function free() {
581 if (method_exists($this->iter
, 'free')) {
584 $this->pos
= $this->total
= 0;
590 * Compter le nombre total de resultats
595 public function count() {
596 if (is_null($this->total
)) {
597 if (method_exists($this->iter
, 'count')
598 and !$this->func_filtre
600 return $this->total
= $this->iter
->count();
602 // compter les lignes et rembobiner
604 $pos = $this->pos
; // sauver la position
606 while ($this->fetch() and $total < $this->max
) {
610 $this->total
= $total;