@@ -21,109 +21,109 @@ |
||
| 21 | 21 | * Ne pas mettre de span@lang=fr si on est déjà en fr. |
| 22 | 22 | */ |
| 23 | 23 | class Idiomes extends AbstractCollecteur { |
| 24 | - protected static string $markPrefix = 'IDIOME'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * La preg pour découper et collecter les modèles |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected string $preg_idiome; |
|
| 31 | - |
|
| 32 | - public function __construct(?string $preg = null) { |
|
| 33 | - |
|
| 34 | - $this->preg_idiome = ($preg ?: '@<:(?:([a-z0-9_]+):)?([a-z0-9_]+):>@isS'); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Sanitizer une collection d'occurences d'idiomes : on ne fait rien |
|
| 39 | - * |
|
| 40 | - * @param array $collection |
|
| 41 | - * @param string $sanitize_callback |
|
| 42 | - * @return array |
|
| 43 | - */ |
|
| 44 | - protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 45 | - |
|
| 46 | - return $collection; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param string $texte |
|
| 52 | - * @param array $options |
|
| 53 | - * bool $collecter_liens |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - public function collecter(string $texte, array $options = []): array { |
|
| 57 | - if (!$texte) { |
|
| 58 | - return []; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - // collecter les matchs de la preg |
|
| 62 | - $idiomes = static::collecteur($texte, '', '<:', $this->preg_idiome, empty($options['detecter_presence']) ? 0 : 1); |
|
| 63 | - |
|
| 64 | - // si on veut seulement detecter la présence, on peut retourner tel quel |
|
| 65 | - if (empty($options['detecter_presence'])) { |
|
| 66 | - $pos_prev = 0; |
|
| 67 | - foreach ($idiomes as $k => &$idiome) { |
|
| 68 | - $idiome['module'] = $idiome['match'][1]; |
|
| 69 | - $idiome['chaine'] = $idiome['match'][2]; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - return $idiomes; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Traiter les idiomes d'un texte |
|
| 78 | - * |
|
| 79 | - * @uses inc_traduire_dist() |
|
| 80 | - * @uses code_echappement() |
|
| 81 | - * @uses echappe_retour() |
|
| 82 | - * |
|
| 83 | - * @param string $texte |
|
| 84 | - * @param array $options |
|
| 85 | - * ?string $lang |
|
| 86 | - * ?bool echappe_span |
|
| 87 | - * @return string |
|
| 88 | - */ |
|
| 89 | - public function traiter(string $texte, array $options) { |
|
| 90 | - static $traduire; |
|
| 91 | - if ($texte) { |
|
| 92 | - $idiomes = $this->collecter($texte); |
|
| 93 | - if ($idiomes !== []) { |
|
| 94 | - $lang = $options['lang'] ?? $GLOBALS['spip_lang']; |
|
| 95 | - $echappe_span = $options['echappe_span'] ?? false; |
|
| 96 | - |
|
| 97 | - if (is_null($traduire)) { |
|
| 98 | - $traduire = charger_fonction('traduire', 'inc'); |
|
| 99 | - include_spip('inc/lang'); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $offset_pos = 0; |
|
| 103 | - foreach ($idiomes as $idiome) { |
|
| 104 | - $cle = ($idiome['module'] ? $idiome['module'] . ':' : '') . $idiome['chaine']; |
|
| 105 | - $desc = $traduire($cle, $lang, true); |
|
| 106 | - $l = $desc->langue; |
|
| 107 | - |
|
| 108 | - // si pas de traduction, on laissera l'écriture de l'idiome entier dans le texte. |
|
| 109 | - if (strlen($desc->texte ?? '')) { |
|
| 110 | - $trad = code_echappement($desc->texte, 'idiome', false); |
|
| 111 | - if ($l !== $lang) { |
|
| 112 | - $trad = str_replace("'", '"', (string) inserer_attribut($trad, 'lang', $l)); |
|
| 113 | - } |
|
| 114 | - if (lang_dir($l) !== lang_dir($lang)) { |
|
| 115 | - $trad = str_replace("'", '"', (string) inserer_attribut($trad, 'dir', lang_dir($l))); |
|
| 116 | - } |
|
| 117 | - if (!$echappe_span) { |
|
| 118 | - $trad = echappe_retour($trad, 'idiome'); |
|
| 119 | - } |
|
| 120 | - $texte = substr_replace($texte, (string) $trad, $idiome['pos'] + $offset_pos, $idiome['length']); |
|
| 121 | - $offset_pos += strlen((string) $trad) - $idiome['length']; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - return $texte; |
|
| 128 | - } |
|
| 24 | + protected static string $markPrefix = 'IDIOME'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * La preg pour découper et collecter les modèles |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected string $preg_idiome; |
|
| 31 | + |
|
| 32 | + public function __construct(?string $preg = null) { |
|
| 33 | + |
|
| 34 | + $this->preg_idiome = ($preg ?: '@<:(?:([a-z0-9_]+):)?([a-z0-9_]+):>@isS'); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Sanitizer une collection d'occurences d'idiomes : on ne fait rien |
|
| 39 | + * |
|
| 40 | + * @param array $collection |
|
| 41 | + * @param string $sanitize_callback |
|
| 42 | + * @return array |
|
| 43 | + */ |
|
| 44 | + protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 45 | + |
|
| 46 | + return $collection; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param string $texte |
|
| 52 | + * @param array $options |
|
| 53 | + * bool $collecter_liens |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + public function collecter(string $texte, array $options = []): array { |
|
| 57 | + if (!$texte) { |
|
| 58 | + return []; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + // collecter les matchs de la preg |
|
| 62 | + $idiomes = static::collecteur($texte, '', '<:', $this->preg_idiome, empty($options['detecter_presence']) ? 0 : 1); |
|
| 63 | + |
|
| 64 | + // si on veut seulement detecter la présence, on peut retourner tel quel |
|
| 65 | + if (empty($options['detecter_presence'])) { |
|
| 66 | + $pos_prev = 0; |
|
| 67 | + foreach ($idiomes as $k => &$idiome) { |
|
| 68 | + $idiome['module'] = $idiome['match'][1]; |
|
| 69 | + $idiome['chaine'] = $idiome['match'][2]; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + return $idiomes; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Traiter les idiomes d'un texte |
|
| 78 | + * |
|
| 79 | + * @uses inc_traduire_dist() |
|
| 80 | + * @uses code_echappement() |
|
| 81 | + * @uses echappe_retour() |
|
| 82 | + * |
|
| 83 | + * @param string $texte |
|
| 84 | + * @param array $options |
|
| 85 | + * ?string $lang |
|
| 86 | + * ?bool echappe_span |
|
| 87 | + * @return string |
|
| 88 | + */ |
|
| 89 | + public function traiter(string $texte, array $options) { |
|
| 90 | + static $traduire; |
|
| 91 | + if ($texte) { |
|
| 92 | + $idiomes = $this->collecter($texte); |
|
| 93 | + if ($idiomes !== []) { |
|
| 94 | + $lang = $options['lang'] ?? $GLOBALS['spip_lang']; |
|
| 95 | + $echappe_span = $options['echappe_span'] ?? false; |
|
| 96 | + |
|
| 97 | + if (is_null($traduire)) { |
|
| 98 | + $traduire = charger_fonction('traduire', 'inc'); |
|
| 99 | + include_spip('inc/lang'); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $offset_pos = 0; |
|
| 103 | + foreach ($idiomes as $idiome) { |
|
| 104 | + $cle = ($idiome['module'] ? $idiome['module'] . ':' : '') . $idiome['chaine']; |
|
| 105 | + $desc = $traduire($cle, $lang, true); |
|
| 106 | + $l = $desc->langue; |
|
| 107 | + |
|
| 108 | + // si pas de traduction, on laissera l'écriture de l'idiome entier dans le texte. |
|
| 109 | + if (strlen($desc->texte ?? '')) { |
|
| 110 | + $trad = code_echappement($desc->texte, 'idiome', false); |
|
| 111 | + if ($l !== $lang) { |
|
| 112 | + $trad = str_replace("'", '"', (string) inserer_attribut($trad, 'lang', $l)); |
|
| 113 | + } |
|
| 114 | + if (lang_dir($l) !== lang_dir($lang)) { |
|
| 115 | + $trad = str_replace("'", '"', (string) inserer_attribut($trad, 'dir', lang_dir($l))); |
|
| 116 | + } |
|
| 117 | + if (!$echappe_span) { |
|
| 118 | + $trad = echappe_retour($trad, 'idiome'); |
|
| 119 | + } |
|
| 120 | + $texte = substr_replace($texte, (string) $trad, $idiome['pos'] + $offset_pos, $idiome['length']); |
|
| 121 | + $offset_pos += strlen((string) $trad) - $idiome['length']; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + return $texte; |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -15,265 +15,265 @@ |
||
| 15 | 15 | * Présentation des pages simplifiées |
| 16 | 16 | **/ |
| 17 | 17 | abstract class AbstractPage { |
| 18 | - public const TYPE = ''; |
|
| 18 | + public const TYPE = ''; |
|
| 19 | 19 | |
| 20 | - public function __construct() { |
|
| 21 | - include_fichiers_fonctions(); |
|
| 22 | - include_spip('inc/headers'); |
|
| 23 | - include_spip('inc/texte'); //inclue inc/lang et inc/filtres |
|
| 24 | - include_spip('inc/filtres_images_mini'); |
|
| 25 | - } |
|
| 20 | + public function __construct() { |
|
| 21 | + include_fichiers_fonctions(); |
|
| 22 | + include_spip('inc/headers'); |
|
| 23 | + include_spip('inc/texte'); //inclue inc/lang et inc/filtres |
|
| 24 | + include_spip('inc/filtres_images_mini'); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Retourne le début d'une page HTML minimale |
|
| 29 | - * |
|
| 30 | - * Le contenu de CSS minimales (reset.css, clear.css, minipage.css) est inséré |
|
| 31 | - * dans une balise script inline (compactée si possible) |
|
| 32 | - * |
|
| 33 | - * @param array $options |
|
| 34 | - * string $lang : forcer la langue utilisateur |
|
| 35 | - * string $page_title : titre éventuel de la page (nom du site par défaut) |
|
| 36 | - * string $couleur_fond : pour la couleur dominante de la page (par défaut on reprend le réglage de la page de login) |
|
| 37 | - * bool $all_inline : inliner les CSS pour envoyer toute la page en 1 hit |
|
| 38 | - * string $doctype |
|
| 39 | - * string $charset |
|
| 40 | - * string $onload |
|
| 41 | - * array $css_files : ajouter des fichiers css |
|
| 42 | - * string $css : ajouter du CSS inline |
|
| 43 | - * string $head : contenu à ajouter à la fin <head> (pour inclusion de JS ou JS inline...) |
|
| 44 | - * @return string |
|
| 45 | - * Code HTML |
|
| 46 | - * |
|
| 47 | - * @uses html_lang_attributes() |
|
| 48 | - * @uses minifier() si le plugin compresseur est présent |
|
| 49 | - * @uses url_absolue_css() |
|
| 50 | - * |
|
| 51 | - * @uses utiliser_langue_visiteur() |
|
| 52 | - * @uses http_no_cache() |
|
| 53 | - */ |
|
| 54 | - protected function ouvreBody($options = []) { |
|
| 55 | - $h = null; |
|
| 56 | - $s = null; |
|
| 57 | - $l = null; |
|
| 58 | - if (empty($options['lang'])) { |
|
| 59 | - // on se limite sur une langue de $GLOBALS['meta']['langues_multilingue'] car on est dans le public |
|
| 60 | - utiliser_langue_visiteur($GLOBALS['meta']['langues_multilingue'] ?? null); |
|
| 61 | - } else { |
|
| 62 | - changer_langue($options['lang']); |
|
| 63 | - } |
|
| 64 | - http_no_cache(); |
|
| 27 | + /** |
|
| 28 | + * Retourne le début d'une page HTML minimale |
|
| 29 | + * |
|
| 30 | + * Le contenu de CSS minimales (reset.css, clear.css, minipage.css) est inséré |
|
| 31 | + * dans une balise script inline (compactée si possible) |
|
| 32 | + * |
|
| 33 | + * @param array $options |
|
| 34 | + * string $lang : forcer la langue utilisateur |
|
| 35 | + * string $page_title : titre éventuel de la page (nom du site par défaut) |
|
| 36 | + * string $couleur_fond : pour la couleur dominante de la page (par défaut on reprend le réglage de la page de login) |
|
| 37 | + * bool $all_inline : inliner les CSS pour envoyer toute la page en 1 hit |
|
| 38 | + * string $doctype |
|
| 39 | + * string $charset |
|
| 40 | + * string $onload |
|
| 41 | + * array $css_files : ajouter des fichiers css |
|
| 42 | + * string $css : ajouter du CSS inline |
|
| 43 | + * string $head : contenu à ajouter à la fin <head> (pour inclusion de JS ou JS inline...) |
|
| 44 | + * @return string |
|
| 45 | + * Code HTML |
|
| 46 | + * |
|
| 47 | + * @uses html_lang_attributes() |
|
| 48 | + * @uses minifier() si le plugin compresseur est présent |
|
| 49 | + * @uses url_absolue_css() |
|
| 50 | + * |
|
| 51 | + * @uses utiliser_langue_visiteur() |
|
| 52 | + * @uses http_no_cache() |
|
| 53 | + */ |
|
| 54 | + protected function ouvreBody($options = []) { |
|
| 55 | + $h = null; |
|
| 56 | + $s = null; |
|
| 57 | + $l = null; |
|
| 58 | + if (empty($options['lang'])) { |
|
| 59 | + // on se limite sur une langue de $GLOBALS['meta']['langues_multilingue'] car on est dans le public |
|
| 60 | + utiliser_langue_visiteur($GLOBALS['meta']['langues_multilingue'] ?? null); |
|
| 61 | + } else { |
|
| 62 | + changer_langue($options['lang']); |
|
| 63 | + } |
|
| 64 | + http_no_cache(); |
|
| 65 | 65 | |
| 66 | - $page_title = ($options['page_title'] ?? $GLOBALS['meta']['nom_site']); |
|
| 67 | - $doctype = ($options['doctype'] ?? '<!DOCTYPE html>'); |
|
| 68 | - $doctype = trim((string) $doctype) . "\n"; |
|
| 69 | - $charset = ($options['charset'] ?? 'utf-8'); |
|
| 70 | - $all_inline = ($options['all_inline'] ?? true); |
|
| 71 | - $onLoad = ($options['onLoad'] ?? ''); |
|
| 72 | - if ($onLoad) { |
|
| 73 | - $onLoad = ' onload="' . attribut_html($onLoad) . '"'; |
|
| 74 | - } |
|
| 66 | + $page_title = ($options['page_title'] ?? $GLOBALS['meta']['nom_site']); |
|
| 67 | + $doctype = ($options['doctype'] ?? '<!DOCTYPE html>'); |
|
| 68 | + $doctype = trim((string) $doctype) . "\n"; |
|
| 69 | + $charset = ($options['charset'] ?? 'utf-8'); |
|
| 70 | + $all_inline = ($options['all_inline'] ?? true); |
|
| 71 | + $onLoad = ($options['onLoad'] ?? ''); |
|
| 72 | + if ($onLoad) { |
|
| 73 | + $onLoad = ' onload="' . attribut_html($onLoad) . '"'; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - # envoyer le charset |
|
| 77 | - if (!headers_sent()) { |
|
| 78 | - header('Content-Type: text/html; charset=' . $charset); |
|
| 79 | - } |
|
| 76 | + # envoyer le charset |
|
| 77 | + if (!headers_sent()) { |
|
| 78 | + header('Content-Type: text/html; charset=' . $charset); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - $css = ''; |
|
| 81 | + $css = ''; |
|
| 82 | 82 | |
| 83 | - if (function_exists('couleur_hex_to_hsl')) { |
|
| 84 | - $couleur_fond = empty($options['couleur_fond']) |
|
| 85 | - ? lire_config('couleur_login', '#db1762') |
|
| 86 | - : $options['couleur_fond']; |
|
| 87 | - $h = couleur_hex_to_hsl($couleur_fond, 'h'); |
|
| 88 | - $s = couleur_hex_to_hsl($couleur_fond, 's'); |
|
| 89 | - $l = couleur_hex_to_hsl($couleur_fond, 'l'); |
|
| 90 | - } |
|
| 83 | + if (function_exists('couleur_hex_to_hsl')) { |
|
| 84 | + $couleur_fond = empty($options['couleur_fond']) |
|
| 85 | + ? lire_config('couleur_login', '#db1762') |
|
| 86 | + : $options['couleur_fond']; |
|
| 87 | + $h = couleur_hex_to_hsl($couleur_fond, 'h'); |
|
| 88 | + $s = couleur_hex_to_hsl($couleur_fond, 's'); |
|
| 89 | + $l = couleur_hex_to_hsl($couleur_fond, 'l'); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - $inline = ':root {' |
|
| 93 | - . "--minipage-color-theme--h: $h;" |
|
| 94 | - . "--minipage-color-theme--s: $s;" |
|
| 95 | - . "--minipage-color-theme--l: $l;}"; |
|
| 96 | - $vars = file_get_contents(find_in_theme('minipage.vars.css')); |
|
| 97 | - $inline .= "\n" . trim($vars); |
|
| 98 | - if (function_exists('minifier')) { |
|
| 99 | - $inline = minifier($inline, 'css'); |
|
| 100 | - } |
|
| 101 | - $files = [ |
|
| 102 | - find_in_theme('reset.css'), |
|
| 103 | - find_in_theme('clear.css'), |
|
| 104 | - find_in_theme('minipage.css'), |
|
| 105 | - ]; |
|
| 106 | - if (!empty($options['css_files'])) { |
|
| 107 | - foreach ($options['css_files'] as $css_file) { |
|
| 108 | - $files[] = $css_file; |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - if ($all_inline) { |
|
| 112 | - // inliner les CSS (optimisation de la page minipage qui passe en un seul hit a la demande) |
|
| 113 | - foreach ($files as $name) { |
|
| 114 | - $file = direction_css($name); |
|
| 115 | - if (function_exists('minifier')) { |
|
| 116 | - $file = minifier($file); |
|
| 117 | - } else { |
|
| 118 | - $file = url_absolue_css($file); // precaution |
|
| 119 | - } |
|
| 120 | - $css .= file_get_contents($file); |
|
| 121 | - } |
|
| 122 | - $css = "$inline\n$css"; |
|
| 123 | - if (!empty($options['css'])) { |
|
| 124 | - $css .= "\n" . $options['css']; |
|
| 125 | - } |
|
| 126 | - $css = "<style type='text/css'>$css</style>"; |
|
| 127 | - } else { |
|
| 128 | - $css = "<style type='text/css'>$inline</style>"; |
|
| 129 | - foreach ($files as $name) { |
|
| 130 | - $file = timestamp(direction_css($name)); |
|
| 131 | - $css .= "<link rel='stylesheet' href='" . attribut_html($file) . "' type='text/css' />\n"; |
|
| 132 | - } |
|
| 133 | - if (!empty($options['css'])) { |
|
| 134 | - $css .= "<style type='text/css'>" . $options['css'] . '</style>'; |
|
| 135 | - } |
|
| 136 | - } |
|
| 92 | + $inline = ':root {' |
|
| 93 | + . "--minipage-color-theme--h: $h;" |
|
| 94 | + . "--minipage-color-theme--s: $s;" |
|
| 95 | + . "--minipage-color-theme--l: $l;}"; |
|
| 96 | + $vars = file_get_contents(find_in_theme('minipage.vars.css')); |
|
| 97 | + $inline .= "\n" . trim($vars); |
|
| 98 | + if (function_exists('minifier')) { |
|
| 99 | + $inline = minifier($inline, 'css'); |
|
| 100 | + } |
|
| 101 | + $files = [ |
|
| 102 | + find_in_theme('reset.css'), |
|
| 103 | + find_in_theme('clear.css'), |
|
| 104 | + find_in_theme('minipage.css'), |
|
| 105 | + ]; |
|
| 106 | + if (!empty($options['css_files'])) { |
|
| 107 | + foreach ($options['css_files'] as $css_file) { |
|
| 108 | + $files[] = $css_file; |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + if ($all_inline) { |
|
| 112 | + // inliner les CSS (optimisation de la page minipage qui passe en un seul hit a la demande) |
|
| 113 | + foreach ($files as $name) { |
|
| 114 | + $file = direction_css($name); |
|
| 115 | + if (function_exists('minifier')) { |
|
| 116 | + $file = minifier($file); |
|
| 117 | + } else { |
|
| 118 | + $file = url_absolue_css($file); // precaution |
|
| 119 | + } |
|
| 120 | + $css .= file_get_contents($file); |
|
| 121 | + } |
|
| 122 | + $css = "$inline\n$css"; |
|
| 123 | + if (!empty($options['css'])) { |
|
| 124 | + $css .= "\n" . $options['css']; |
|
| 125 | + } |
|
| 126 | + $css = "<style type='text/css'>$css</style>"; |
|
| 127 | + } else { |
|
| 128 | + $css = "<style type='text/css'>$inline</style>"; |
|
| 129 | + foreach ($files as $name) { |
|
| 130 | + $file = timestamp(direction_css($name)); |
|
| 131 | + $css .= "<link rel='stylesheet' href='" . attribut_html($file) . "' type='text/css' />\n"; |
|
| 132 | + } |
|
| 133 | + if (!empty($options['css'])) { |
|
| 134 | + $css .= "<style type='text/css'>" . $options['css'] . '</style>'; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - return $doctype . |
|
| 139 | - html_lang_attributes() . |
|
| 140 | - "<head>\n" . |
|
| 141 | - '<title>' . |
|
| 142 | - textebrut($page_title) . |
|
| 143 | - "</title>\n" . |
|
| 144 | - "<meta name=\"viewport\" content=\"width=device-width\" />\n" . |
|
| 145 | - $css . |
|
| 146 | - (empty($options['head']) ? '' : $options['head']) . |
|
| 147 | - "</head>\n" . |
|
| 148 | - "<body{$onLoad} class=\"minipage" . ($this::TYPE ? ' minipage--' . $this::TYPE : '') . "\">\n" . |
|
| 149 | - "\t<div class=\"minipage-bloc\">\n"; |
|
| 150 | - } |
|
| 138 | + return $doctype . |
|
| 139 | + html_lang_attributes() . |
|
| 140 | + "<head>\n" . |
|
| 141 | + '<title>' . |
|
| 142 | + textebrut($page_title) . |
|
| 143 | + "</title>\n" . |
|
| 144 | + "<meta name=\"viewport\" content=\"width=device-width\" />\n" . |
|
| 145 | + $css . |
|
| 146 | + (empty($options['head']) ? '' : $options['head']) . |
|
| 147 | + "</head>\n" . |
|
| 148 | + "<body{$onLoad} class=\"minipage" . ($this::TYPE ? ' minipage--' . $this::TYPE : '') . "\">\n" . |
|
| 149 | + "\t<div class=\"minipage-bloc\">\n"; |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Ouvre le corps : affiche le header avec un éventuel titre + ouvre le div corps |
|
| 154 | - * @param $options |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - protected function ouvreCorps($options = []) { |
|
| 158 | - $url_site = url_de_base(); |
|
| 159 | - $header = "<header>\n" . |
|
| 160 | - '<h1><a href="' . attribut_html($url_site) . '">' . interdire_scripts($GLOBALS['meta']['nom_site'] ?? '') . "</a></h1>\n"; |
|
| 152 | + /** |
|
| 153 | + * Ouvre le corps : affiche le header avec un éventuel titre + ouvre le div corps |
|
| 154 | + * @param $options |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + protected function ouvreCorps($options = []) { |
|
| 158 | + $url_site = url_de_base(); |
|
| 159 | + $header = "<header>\n" . |
|
| 160 | + '<h1><a href="' . attribut_html($url_site) . '">' . interdire_scripts($GLOBALS['meta']['nom_site'] ?? '') . "</a></h1>\n"; |
|
| 161 | 161 | |
| 162 | - $titre = ($options['titre'] ?? ''); |
|
| 163 | - if ($titre) { |
|
| 164 | - $header .= '<h2>' . interdire_scripts($titre) . '</h2>'; |
|
| 165 | - } |
|
| 166 | - $header .= '</header>'; |
|
| 162 | + $titre = ($options['titre'] ?? ''); |
|
| 163 | + if ($titre) { |
|
| 164 | + $header .= '<h2>' . interdire_scripts($titre) . '</h2>'; |
|
| 165 | + } |
|
| 166 | + $header .= '</header>'; |
|
| 167 | 167 | |
| 168 | - return $header . "<div class='corps'>\n"; |
|
| 169 | - } |
|
| 168 | + return $header . "<div class='corps'>\n"; |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * Ferme le corps : affiche le footer par défaut ou custom et ferme le div corps |
|
| 173 | - * @param $options |
|
| 174 | - * @return string |
|
| 175 | - */ |
|
| 176 | - protected function fermeCorps($options = []) { |
|
| 177 | - $url_site = url_de_base(); |
|
| 171 | + /** |
|
| 172 | + * Ferme le corps : affiche le footer par défaut ou custom et ferme le div corps |
|
| 173 | + * @param $options |
|
| 174 | + * @return string |
|
| 175 | + */ |
|
| 176 | + protected function fermeCorps($options = []) { |
|
| 177 | + $url_site = url_de_base(); |
|
| 178 | 178 | |
| 179 | - if (isset($options['footer'])) { |
|
| 180 | - $footer = $options['footer']; |
|
| 181 | - } else { |
|
| 182 | - $footer = '<a href="' . attribut_html($url_site) . '">' . _T('retour') . "</a>\n"; |
|
| 183 | - } |
|
| 184 | - if (!empty($footer)) { |
|
| 185 | - $footer = "<footer>\n{$footer}</footer>"; |
|
| 186 | - } |
|
| 179 | + if (isset($options['footer'])) { |
|
| 180 | + $footer = $options['footer']; |
|
| 181 | + } else { |
|
| 182 | + $footer = '<a href="' . attribut_html($url_site) . '">' . _T('retour') . "</a>\n"; |
|
| 183 | + } |
|
| 184 | + if (!empty($footer)) { |
|
| 185 | + $footer = "<footer>\n{$footer}</footer>"; |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - return "</div>\n" . $footer; |
|
| 189 | - } |
|
| 188 | + return "</div>\n" . $footer; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * Retourne la fin d'une page HTML minimale |
|
| 194 | - * |
|
| 195 | - * @return string Code HTML |
|
| 196 | - */ |
|
| 197 | - protected function fermeBody() { |
|
| 198 | - return "\n\t</div>\n</body>\n</html>"; |
|
| 199 | - } |
|
| 192 | + /** |
|
| 193 | + * Retourne la fin d'une page HTML minimale |
|
| 194 | + * |
|
| 195 | + * @return string Code HTML |
|
| 196 | + */ |
|
| 197 | + protected function fermeBody() { |
|
| 198 | + return "\n\t</div>\n</body>\n</html>"; |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * Retourne une page HTML contenant, dans une présentation minimale, |
|
| 204 | - * le contenu transmis dans `$corps`. |
|
| 205 | - * |
|
| 206 | - * Appelée pour afficher un message ou une demande de confirmation simple et rapide |
|
| 207 | - * |
|
| 208 | - * @param string $corps |
|
| 209 | - * Corps de la page |
|
| 210 | - * @param array $options |
|
| 211 | - * @return string |
|
| 212 | - * HTML de la page |
|
| 213 | - * @see ouvreBody() |
|
| 214 | - * @see ouvreCorps() |
|
| 215 | - * string $titre : Titre à l'affichage (différent de $page_title) |
|
| 216 | - * int $status : status de la page |
|
| 217 | - * string $footer : pied de la box en remplacement du bouton retour par défaut |
|
| 218 | - * @uses ouvreBody() |
|
| 219 | - * @uses ouvreCorps() |
|
| 220 | - * @uses fermeCorps() |
|
| 221 | - * @uses fermeBody() |
|
| 222 | - * |
|
| 223 | - */ |
|
| 224 | - public function page($corps, $options = []) { |
|
| 202 | + /** |
|
| 203 | + * Retourne une page HTML contenant, dans une présentation minimale, |
|
| 204 | + * le contenu transmis dans `$corps`. |
|
| 205 | + * |
|
| 206 | + * Appelée pour afficher un message ou une demande de confirmation simple et rapide |
|
| 207 | + * |
|
| 208 | + * @param string $corps |
|
| 209 | + * Corps de la page |
|
| 210 | + * @param array $options |
|
| 211 | + * @return string |
|
| 212 | + * HTML de la page |
|
| 213 | + * @see ouvreBody() |
|
| 214 | + * @see ouvreCorps() |
|
| 215 | + * string $titre : Titre à l'affichage (différent de $page_title) |
|
| 216 | + * int $status : status de la page |
|
| 217 | + * string $footer : pied de la box en remplacement du bouton retour par défaut |
|
| 218 | + * @uses ouvreBody() |
|
| 219 | + * @uses ouvreCorps() |
|
| 220 | + * @uses fermeCorps() |
|
| 221 | + * @uses fermeBody() |
|
| 222 | + * |
|
| 223 | + */ |
|
| 224 | + public function page($corps, $options = []) { |
|
| 225 | 225 | |
| 226 | - // par securite |
|
| 227 | - if (!defined('_AJAX')) { |
|
| 228 | - define('_AJAX', false); |
|
| 229 | - } |
|
| 226 | + // par securite |
|
| 227 | + if (!defined('_AJAX')) { |
|
| 228 | + define('_AJAX', false); |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - $status = ((int) ($options['status'] ?? 200)) ?: 200; |
|
| 231 | + $status = ((int) ($options['status'] ?? 200)) ?: 200; |
|
| 232 | 232 | |
| 233 | - http_response_code($status); |
|
| 233 | + http_response_code($status); |
|
| 234 | 234 | |
| 235 | - $html = $this->ouvreBody($options) |
|
| 236 | - . $this->ouvreCorps($options) |
|
| 237 | - . $corps |
|
| 238 | - . $this->fermeCorps($options) |
|
| 239 | - . $this->fermeBody(); |
|
| 235 | + $html = $this->ouvreBody($options) |
|
| 236 | + . $this->ouvreCorps($options) |
|
| 237 | + . $corps |
|
| 238 | + . $this->fermeCorps($options) |
|
| 239 | + . $this->fermeBody(); |
|
| 240 | 240 | |
| 241 | - if ( |
|
| 242 | - $GLOBALS['profondeur_url'] >= (_DIR_RESTREINT ? 1 : 2) |
|
| 243 | - && empty($options['all_inline']) |
|
| 244 | - ) { |
|
| 245 | - define('_SET_HTML_BASE', true); |
|
| 246 | - include_spip('public/assembler'); |
|
| 247 | - $GLOBALS['html'] = true; |
|
| 248 | - page_base_href($html); |
|
| 249 | - } |
|
| 250 | - return $html; |
|
| 251 | - } |
|
| 241 | + if ( |
|
| 242 | + $GLOBALS['profondeur_url'] >= (_DIR_RESTREINT ? 1 : 2) |
|
| 243 | + && empty($options['all_inline']) |
|
| 244 | + ) { |
|
| 245 | + define('_SET_HTML_BASE', true); |
|
| 246 | + include_spip('public/assembler'); |
|
| 247 | + $GLOBALS['html'] = true; |
|
| 248 | + page_base_href($html); |
|
| 249 | + } |
|
| 250 | + return $html; |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 254 | - * Fonction helper pour les erreurs |
|
| 255 | - * @param ?string $message_erreur |
|
| 256 | - * @param array $options |
|
| 257 | - * @see page() |
|
| 258 | - * @return string |
|
| 259 | - * |
|
| 260 | - */ |
|
| 261 | - public function pageErreur($message_erreur = null, $options = []) { |
|
| 253 | + /** |
|
| 254 | + * Fonction helper pour les erreurs |
|
| 255 | + * @param ?string $message_erreur |
|
| 256 | + * @param array $options |
|
| 257 | + * @see page() |
|
| 258 | + * @return string |
|
| 259 | + * |
|
| 260 | + */ |
|
| 261 | + public function pageErreur($message_erreur = null, $options = []) { |
|
| 262 | 262 | |
| 263 | - if (empty($message_erreur)) { |
|
| 264 | - if (empty($options['lang'])) { |
|
| 265 | - utiliser_langue_visiteur(); |
|
| 266 | - } else { |
|
| 267 | - changer_langue($options['lang']); |
|
| 268 | - } |
|
| 269 | - $message_erreur = _T('info_acces_interdit'); |
|
| 270 | - } |
|
| 271 | - $corps = "<div class='msg-alert error'>" |
|
| 272 | - . $message_erreur |
|
| 273 | - . '</div>'; |
|
| 274 | - if (empty($options['status'])) { |
|
| 275 | - $options['status'] = 403; |
|
| 276 | - } |
|
| 277 | - return $this->page($corps, $options); |
|
| 278 | - } |
|
| 263 | + if (empty($message_erreur)) { |
|
| 264 | + if (empty($options['lang'])) { |
|
| 265 | + utiliser_langue_visiteur(); |
|
| 266 | + } else { |
|
| 267 | + changer_langue($options['lang']); |
|
| 268 | + } |
|
| 269 | + $message_erreur = _T('info_acces_interdit'); |
|
| 270 | + } |
|
| 271 | + $corps = "<div class='msg-alert error'>" |
|
| 272 | + . $message_erreur |
|
| 273 | + . '</div>'; |
|
| 274 | + if (empty($options['status'])) { |
|
| 275 | + $options['status'] = 403; |
|
| 276 | + } |
|
| 277 | + return $this->page($corps, $options); |
|
| 278 | + } |
|
| 279 | 279 | } |
@@ -65,17 +65,17 @@ discard block |
||
| 65 | 65 | |
| 66 | 66 | $page_title = ($options['page_title'] ?? $GLOBALS['meta']['nom_site']); |
| 67 | 67 | $doctype = ($options['doctype'] ?? '<!DOCTYPE html>'); |
| 68 | - $doctype = trim((string) $doctype) . "\n"; |
|
| 68 | + $doctype = trim((string) $doctype)."\n"; |
|
| 69 | 69 | $charset = ($options['charset'] ?? 'utf-8'); |
| 70 | 70 | $all_inline = ($options['all_inline'] ?? true); |
| 71 | 71 | $onLoad = ($options['onLoad'] ?? ''); |
| 72 | 72 | if ($onLoad) { |
| 73 | - $onLoad = ' onload="' . attribut_html($onLoad) . '"'; |
|
| 73 | + $onLoad = ' onload="'.attribut_html($onLoad).'"'; |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | # envoyer le charset |
| 77 | 77 | if (!headers_sent()) { |
| 78 | - header('Content-Type: text/html; charset=' . $charset); |
|
| 78 | + header('Content-Type: text/html; charset='.$charset); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | $css = ''; |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | . "--minipage-color-theme--s: $s;" |
| 95 | 95 | . "--minipage-color-theme--l: $l;}"; |
| 96 | 96 | $vars = file_get_contents(find_in_theme('minipage.vars.css')); |
| 97 | - $inline .= "\n" . trim($vars); |
|
| 97 | + $inline .= "\n".trim($vars); |
|
| 98 | 98 | if (function_exists('minifier')) { |
| 99 | 99 | $inline = minifier($inline, 'css'); |
| 100 | 100 | } |
@@ -121,31 +121,31 @@ discard block |
||
| 121 | 121 | } |
| 122 | 122 | $css = "$inline\n$css"; |
| 123 | 123 | if (!empty($options['css'])) { |
| 124 | - $css .= "\n" . $options['css']; |
|
| 124 | + $css .= "\n".$options['css']; |
|
| 125 | 125 | } |
| 126 | 126 | $css = "<style type='text/css'>$css</style>"; |
| 127 | 127 | } else { |
| 128 | 128 | $css = "<style type='text/css'>$inline</style>"; |
| 129 | 129 | foreach ($files as $name) { |
| 130 | 130 | $file = timestamp(direction_css($name)); |
| 131 | - $css .= "<link rel='stylesheet' href='" . attribut_html($file) . "' type='text/css' />\n"; |
|
| 131 | + $css .= "<link rel='stylesheet' href='".attribut_html($file)."' type='text/css' />\n"; |
|
| 132 | 132 | } |
| 133 | 133 | if (!empty($options['css'])) { |
| 134 | - $css .= "<style type='text/css'>" . $options['css'] . '</style>'; |
|
| 134 | + $css .= "<style type='text/css'>".$options['css'].'</style>'; |
|
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - return $doctype . |
|
| 139 | - html_lang_attributes() . |
|
| 140 | - "<head>\n" . |
|
| 141 | - '<title>' . |
|
| 142 | - textebrut($page_title) . |
|
| 143 | - "</title>\n" . |
|
| 144 | - "<meta name=\"viewport\" content=\"width=device-width\" />\n" . |
|
| 145 | - $css . |
|
| 146 | - (empty($options['head']) ? '' : $options['head']) . |
|
| 147 | - "</head>\n" . |
|
| 148 | - "<body{$onLoad} class=\"minipage" . ($this::TYPE ? ' minipage--' . $this::TYPE : '') . "\">\n" . |
|
| 138 | + return $doctype. |
|
| 139 | + html_lang_attributes(). |
|
| 140 | + "<head>\n". |
|
| 141 | + '<title>'. |
|
| 142 | + textebrut($page_title). |
|
| 143 | + "</title>\n". |
|
| 144 | + "<meta name=\"viewport\" content=\"width=device-width\" />\n". |
|
| 145 | + $css. |
|
| 146 | + (empty($options['head']) ? '' : $options['head']). |
|
| 147 | + "</head>\n". |
|
| 148 | + "<body{$onLoad} class=\"minipage".($this::TYPE ? ' minipage--'.$this::TYPE : '')."\">\n". |
|
| 149 | 149 | "\t<div class=\"minipage-bloc\">\n"; |
| 150 | 150 | } |
| 151 | 151 | |
@@ -156,16 +156,16 @@ discard block |
||
| 156 | 156 | */ |
| 157 | 157 | protected function ouvreCorps($options = []) { |
| 158 | 158 | $url_site = url_de_base(); |
| 159 | - $header = "<header>\n" . |
|
| 160 | - '<h1><a href="' . attribut_html($url_site) . '">' . interdire_scripts($GLOBALS['meta']['nom_site'] ?? '') . "</a></h1>\n"; |
|
| 159 | + $header = "<header>\n". |
|
| 160 | + '<h1><a href="'.attribut_html($url_site).'">'.interdire_scripts($GLOBALS['meta']['nom_site'] ?? '')."</a></h1>\n"; |
|
| 161 | 161 | |
| 162 | 162 | $titre = ($options['titre'] ?? ''); |
| 163 | 163 | if ($titre) { |
| 164 | - $header .= '<h2>' . interdire_scripts($titre) . '</h2>'; |
|
| 164 | + $header .= '<h2>'.interdire_scripts($titre).'</h2>'; |
|
| 165 | 165 | } |
| 166 | 166 | $header .= '</header>'; |
| 167 | 167 | |
| 168 | - return $header . "<div class='corps'>\n"; |
|
| 168 | + return $header."<div class='corps'>\n"; |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /** |
@@ -179,13 +179,13 @@ discard block |
||
| 179 | 179 | if (isset($options['footer'])) { |
| 180 | 180 | $footer = $options['footer']; |
| 181 | 181 | } else { |
| 182 | - $footer = '<a href="' . attribut_html($url_site) . '">' . _T('retour') . "</a>\n"; |
|
| 182 | + $footer = '<a href="'.attribut_html($url_site).'">'._T('retour')."</a>\n"; |
|
| 183 | 183 | } |
| 184 | 184 | if (!empty($footer)) { |
| 185 | 185 | $footer = "<footer>\n{$footer}</footer>"; |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - return "</div>\n" . $footer; |
|
| 188 | + return "</div>\n".$footer; |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | |
@@ -8,566 +8,566 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Decorator extends FilterIterator |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Conditions de filtrage |
|
| 13 | - * ie criteres de selection. |
|
| 14 | - * |
|
| 15 | - * @var array |
|
| 16 | - */ |
|
| 17 | - protected $filtre = []; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Fonction de filtrage compilee a partir des criteres de filtre. |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected $func_filtre; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Critere {offset, limit}. |
|
| 28 | - * |
|
| 29 | - * @var int |
|
| 30 | - * @var int |
|
| 31 | - */ |
|
| 32 | - protected $offset; |
|
| 33 | - protected $limit; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * nombre d'elements recuperes depuis la position 0, |
|
| 37 | - * en tenant compte des filtres. |
|
| 38 | - * |
|
| 39 | - * @var int |
|
| 40 | - */ |
|
| 41 | - protected $fetched = 0; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Y a t'il une erreur ? |
|
| 45 | - * |
|
| 46 | - * @var bool |
|
| 47 | - */ |
|
| 48 | - protected $err = false; |
|
| 49 | - |
|
| 50 | - // Extension SPIP des iterateurs PHP |
|
| 51 | - /** |
|
| 52 | - * type de l'iterateur. |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - protected $type; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * position courante de l'iterateur. |
|
| 60 | - * |
|
| 61 | - * @var int |
|
| 62 | - */ |
|
| 63 | - protected $pos = 0; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * nombre total resultats dans l'iterateur. |
|
| 67 | - * |
|
| 68 | - * @var int |
|
| 69 | - */ |
|
| 70 | - protected $total; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * nombre maximal de recherche pour $total |
|
| 74 | - * si l'iterateur n'implemente pas de fonction specifique. |
|
| 75 | - */ |
|
| 76 | - protected $max = 100000; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Liste des champs a inserer dans les $row |
|
| 80 | - * retournes par ->fetch(). |
|
| 81 | - */ |
|
| 82 | - protected $select = []; |
|
| 83 | - private Iterator $iter; |
|
| 84 | - |
|
| 85 | - public function __construct( |
|
| 86 | - Iterator $iter, |
|
| 87 | - /** Parametres de l'iterateur */ |
|
| 88 | - protected array $command, |
|
| 89 | - /** Infos du compilateur */ |
|
| 90 | - protected array $info |
|
| 91 | - ) { |
|
| 92 | - parent::__construct($iter); |
|
| 93 | - parent::rewind(); // remettre a la premiere position (bug? connu de FilterIterator) |
|
| 94 | - |
|
| 95 | - // recuperer l'iterateur transmis |
|
| 96 | - $this->iter = $this->getInnerIterator(); |
|
| 97 | - |
|
| 98 | - // chercher la liste des champs a retourner par |
|
| 99 | - // fetch si l'objet ne les calcule pas tout seul |
|
| 100 | - if (!method_exists($this->iter, 'fetch')) { |
|
| 101 | - $this->calculer_select(); |
|
| 102 | - $this->calculer_filtres(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // emptyIterator critere {si} faux n'a pas d'erreur ! |
|
| 106 | - if (property_exists($this->iter, 'err') && $this->iter->err !== null) { |
|
| 107 | - $this->err = $this->iter->err; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // pas d'init a priori, le calcul ne sera fait qu'en cas de besoin (provoque une double requete souvent inutile en sqlite) |
|
| 111 | - //$this->total = $this->count(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Drapeau a activer en cas d'echec |
|
| 116 | - * (select SQL errone, non chargement des DATA, etc). |
|
| 117 | - */ |
|
| 118 | - public function err() { |
|
| 119 | - if (method_exists($this->iter, 'err')) { |
|
| 120 | - return $this->iter->err(); |
|
| 121 | - } |
|
| 122 | - if (property_exists($this->iter, 'err')) { |
|
| 123 | - return $this->iter->err; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return false; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // recuperer la valeur d'une balise #X |
|
| 130 | - // en fonction des methodes |
|
| 131 | - // et proprietes disponibles |
|
| 132 | - public function get_select($nom) { |
|
| 133 | - if (is_object($this->iter) && method_exists($this->iter, $nom)) { |
|
| 134 | - try { |
|
| 135 | - return $this->iter->{$nom}(); |
|
| 136 | - } catch (Exception) { |
|
| 137 | - // #GETCHILDREN sur un fichier de DirectoryIterator ... |
|
| 138 | - spip_log("Methode {$nom} en echec sur " . $this->iter::class); |
|
| 139 | - spip_log("Cela peut être normal : retour d'une ligne de resultat ne pouvant pas calculer cette methode"); |
|
| 140 | - |
|
| 141 | - return ''; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - /* |
|
| 11 | + /** |
|
| 12 | + * Conditions de filtrage |
|
| 13 | + * ie criteres de selection. |
|
| 14 | + * |
|
| 15 | + * @var array |
|
| 16 | + */ |
|
| 17 | + protected $filtre = []; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Fonction de filtrage compilee a partir des criteres de filtre. |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected $func_filtre; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Critere {offset, limit}. |
|
| 28 | + * |
|
| 29 | + * @var int |
|
| 30 | + * @var int |
|
| 31 | + */ |
|
| 32 | + protected $offset; |
|
| 33 | + protected $limit; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * nombre d'elements recuperes depuis la position 0, |
|
| 37 | + * en tenant compte des filtres. |
|
| 38 | + * |
|
| 39 | + * @var int |
|
| 40 | + */ |
|
| 41 | + protected $fetched = 0; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Y a t'il une erreur ? |
|
| 45 | + * |
|
| 46 | + * @var bool |
|
| 47 | + */ |
|
| 48 | + protected $err = false; |
|
| 49 | + |
|
| 50 | + // Extension SPIP des iterateurs PHP |
|
| 51 | + /** |
|
| 52 | + * type de l'iterateur. |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + protected $type; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * position courante de l'iterateur. |
|
| 60 | + * |
|
| 61 | + * @var int |
|
| 62 | + */ |
|
| 63 | + protected $pos = 0; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * nombre total resultats dans l'iterateur. |
|
| 67 | + * |
|
| 68 | + * @var int |
|
| 69 | + */ |
|
| 70 | + protected $total; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * nombre maximal de recherche pour $total |
|
| 74 | + * si l'iterateur n'implemente pas de fonction specifique. |
|
| 75 | + */ |
|
| 76 | + protected $max = 100000; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Liste des champs a inserer dans les $row |
|
| 80 | + * retournes par ->fetch(). |
|
| 81 | + */ |
|
| 82 | + protected $select = []; |
|
| 83 | + private Iterator $iter; |
|
| 84 | + |
|
| 85 | + public function __construct( |
|
| 86 | + Iterator $iter, |
|
| 87 | + /** Parametres de l'iterateur */ |
|
| 88 | + protected array $command, |
|
| 89 | + /** Infos du compilateur */ |
|
| 90 | + protected array $info |
|
| 91 | + ) { |
|
| 92 | + parent::__construct($iter); |
|
| 93 | + parent::rewind(); // remettre a la premiere position (bug? connu de FilterIterator) |
|
| 94 | + |
|
| 95 | + // recuperer l'iterateur transmis |
|
| 96 | + $this->iter = $this->getInnerIterator(); |
|
| 97 | + |
|
| 98 | + // chercher la liste des champs a retourner par |
|
| 99 | + // fetch si l'objet ne les calcule pas tout seul |
|
| 100 | + if (!method_exists($this->iter, 'fetch')) { |
|
| 101 | + $this->calculer_select(); |
|
| 102 | + $this->calculer_filtres(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // emptyIterator critere {si} faux n'a pas d'erreur ! |
|
| 106 | + if (property_exists($this->iter, 'err') && $this->iter->err !== null) { |
|
| 107 | + $this->err = $this->iter->err; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // pas d'init a priori, le calcul ne sera fait qu'en cas de besoin (provoque une double requete souvent inutile en sqlite) |
|
| 111 | + //$this->total = $this->count(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Drapeau a activer en cas d'echec |
|
| 116 | + * (select SQL errone, non chargement des DATA, etc). |
|
| 117 | + */ |
|
| 118 | + public function err() { |
|
| 119 | + if (method_exists($this->iter, 'err')) { |
|
| 120 | + return $this->iter->err(); |
|
| 121 | + } |
|
| 122 | + if (property_exists($this->iter, 'err')) { |
|
| 123 | + return $this->iter->err; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return false; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // recuperer la valeur d'une balise #X |
|
| 130 | + // en fonction des methodes |
|
| 131 | + // et proprietes disponibles |
|
| 132 | + public function get_select($nom) { |
|
| 133 | + if (is_object($this->iter) && method_exists($this->iter, $nom)) { |
|
| 134 | + try { |
|
| 135 | + return $this->iter->{$nom}(); |
|
| 136 | + } catch (Exception) { |
|
| 137 | + // #GETCHILDREN sur un fichier de DirectoryIterator ... |
|
| 138 | + spip_log("Methode {$nom} en echec sur " . $this->iter::class); |
|
| 139 | + spip_log("Cela peut être normal : retour d'une ligne de resultat ne pouvant pas calculer cette methode"); |
|
| 140 | + |
|
| 141 | + return ''; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + /* |
|
| 145 | 145 | if (property_exists($this->iter, $nom)) { |
| 146 | 146 | return $this->iter->$nom; |
| 147 | 147 | }*/ |
| 148 | - // cle et valeur par defaut |
|
| 149 | - // ICI PLANTAGE SI ON NE CONTROLE PAS $nom |
|
| 150 | - if ( |
|
| 151 | - in_array($nom, ['cle', 'valeur']) |
|
| 152 | - && method_exists($this, $nom) |
|
| 153 | - ) { |
|
| 154 | - return $this->{$nom}(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // Par defaut chercher en xpath dans la valeur() |
|
| 158 | - return table_valeur($this->valeur(), $nom, null); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - public function next(): void { |
|
| 162 | - ++$this->pos; |
|
| 163 | - parent::next(); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * revient au depart. |
|
| 168 | - */ |
|
| 169 | - public function rewind(): void { |
|
| 170 | - $this->pos = 0; |
|
| 171 | - $this->fetched = 0; |
|
| 172 | - parent::rewind(); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * aller a la position absolue n, |
|
| 177 | - * comptee depuis le debut. |
|
| 178 | - * |
|
| 179 | - * @param int $n |
|
| 180 | - * absolute pos |
|
| 181 | - * @param string $continue |
|
| 182 | - * param for sql_ api |
|
| 183 | - * |
|
| 184 | - * @return bool |
|
| 185 | - * success or fail if not implemented |
|
| 186 | - */ |
|
| 187 | - public function seek($n = 0, $continue = null) { |
|
| 188 | - if ($this->func_filtre || !method_exists($this->iter, 'seek') || !$this->iter->seek($n)) { |
|
| 189 | - $this->seek_loop($n); |
|
| 190 | - } |
|
| 191 | - $this->pos = $n; |
|
| 192 | - $this->fetched = $n; |
|
| 193 | - |
|
| 194 | - return true; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Avancer de $saut pas. |
|
| 199 | - * |
|
| 200 | - * @param $saut |
|
| 201 | - * @param $max |
|
| 202 | - * |
|
| 203 | - * @return int |
|
| 204 | - */ |
|
| 205 | - public function skip($saut, $max = null) { |
|
| 206 | - // pas de saut en arriere autorise pour cette fonction |
|
| 207 | - if (($saut = (int) $saut) <= 0) { |
|
| 208 | - return $this->pos; |
|
| 209 | - } |
|
| 210 | - $seek = $this->pos + $saut; |
|
| 211 | - // si le saut fait depasser le maxi, on libere la resource |
|
| 212 | - // et on sort |
|
| 213 | - if (is_null($max)) { |
|
| 214 | - $max = $this->count(); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if ($seek >= $max || $seek >= $this->count()) { |
|
| 218 | - // sortie plus rapide que de faire next() jusqu'a la fin ! |
|
| 219 | - $this->free(); |
|
| 220 | - |
|
| 221 | - return $max; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $this->seek($seek); |
|
| 225 | - |
|
| 226 | - return $this->pos; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Renvoyer un tableau des donnees correspondantes |
|
| 231 | - * a la position courante de l'iterateur |
|
| 232 | - * en controlant si on respecte le filtre |
|
| 233 | - * Appliquer aussi le critere {offset,limit}. |
|
| 234 | - * |
|
| 235 | - * @return array|bool |
|
| 236 | - */ |
|
| 237 | - public function fetch() { |
|
| 238 | - if (method_exists($this->iter, 'fetch')) { |
|
| 239 | - return $this->iter->fetch(); |
|
| 240 | - } |
|
| 241 | - while ( |
|
| 242 | - $this->valid() |
|
| 243 | - && (!$this->accept() || $this->offset !== null && $this->fetched++ < $this->offset) |
|
| 244 | - ) { |
|
| 245 | - $this->next(); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - if (!$this->valid()) { |
|
| 249 | - return false; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - if ( |
|
| 253 | - $this->limit !== null |
|
| 254 | - && $this->fetched > $this->offset + $this->limit |
|
| 255 | - ) { |
|
| 256 | - return false; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - $r = []; |
|
| 260 | - foreach ($this->select as $nom) { |
|
| 261 | - $r[$nom] = $this->get_select($nom); |
|
| 262 | - } |
|
| 263 | - $this->next(); |
|
| 264 | - |
|
| 265 | - return $r; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - // retourner la cle pour #CLE |
|
| 269 | - public function cle() { |
|
| 270 | - return $this->key(); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - // retourner la valeur pour #VALEUR |
|
| 274 | - public function valeur() { |
|
| 275 | - return $this->current(); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Accepte-t-on l'entree courante lue ? |
|
| 280 | - * On execute les filtres pour le savoir. |
|
| 281 | - */ |
|
| 282 | - public function accept(): bool { |
|
| 283 | - if ($f = $this->func_filtre) { |
|
| 284 | - return $f(); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return true; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * liberer la ressource. |
|
| 292 | - * |
|
| 293 | - * @return bool |
|
| 294 | - */ |
|
| 295 | - public function free() { |
|
| 296 | - if (method_exists($this->iter, 'free')) { |
|
| 297 | - $this->iter->free(); |
|
| 298 | - } |
|
| 299 | - $this->pos = $this->total = 0; |
|
| 300 | - |
|
| 301 | - return true; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Compter le nombre total de resultats |
|
| 306 | - * pour #TOTAL_BOUCLE. |
|
| 307 | - * |
|
| 308 | - * @return int |
|
| 309 | - */ |
|
| 310 | - public function count() { |
|
| 311 | - if (is_null($this->total)) { |
|
| 312 | - if ( |
|
| 313 | - method_exists($this->iter, 'count') |
|
| 314 | - && !$this->func_filtre |
|
| 315 | - ) { |
|
| 316 | - return $this->total = $this->iter->count(); |
|
| 317 | - } |
|
| 318 | - // compter les lignes et rembobiner |
|
| 319 | - $total = 0; |
|
| 320 | - $pos = $this->pos; // sauver la position |
|
| 321 | - $this->rewind(); |
|
| 322 | - while ($this->fetch() && $total < $this->max) { |
|
| 323 | - ++$total; |
|
| 324 | - } |
|
| 325 | - $this->seek($pos); |
|
| 326 | - $this->total = $total; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - return $this->total; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Assembler le tableau de filtres traduits depuis les conditions SQL |
|
| 334 | - * les filtres vides ou null sont ignores. |
|
| 335 | - * |
|
| 336 | - * @param $filtres |
|
| 337 | - * @param string $operateur |
|
| 338 | - * |
|
| 339 | - * @return null|string |
|
| 340 | - */ |
|
| 341 | - protected function assembler_filtres($filtres, $operateur = 'AND') { |
|
| 342 | - $filtres_string = []; |
|
| 343 | - foreach ($filtres as $k => $v) { |
|
| 344 | - // si c'est un tableau de OR/AND + 2 sous-filtres, on recurse pour transformer en chaine |
|
| 345 | - if (is_array($v) && in_array(reset($v), ['OR', 'AND'])) { |
|
| 346 | - $op = array_shift($v); |
|
| 347 | - $v = $this->assembler_filtres($v, $op); |
|
| 348 | - } |
|
| 349 | - if (is_null($v) || !is_string($v) || empty($v)) { |
|
| 350 | - continue; |
|
| 351 | - } |
|
| 352 | - $filtres_string[] = $v; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - if ($filtres_string === []) { |
|
| 356 | - return null; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - return '(' . implode(") {$operateur} (", $filtres_string) . ')'; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Traduire un element du tableau where SQL en un filtre. |
|
| 364 | - * |
|
| 365 | - * @param $v |
|
| 366 | - * |
|
| 367 | - * @return null|array|string |
|
| 368 | - */ |
|
| 369 | - protected function traduire_condition_sql_en_filtre($v) { |
|
| 370 | - if (is_array($v)) { |
|
| 371 | - if ((count($v) >= 2) && ('REGEXP' == $v[0]) && ("'.*'" == $v[2])) { |
|
| 372 | - return 'true'; |
|
| 373 | - } |
|
| 374 | - if ((count($v) >= 2) && ('LIKE' == $v[0]) && ("'%'" == $v[2])) { |
|
| 375 | - return 'true'; |
|
| 376 | - } |
|
| 377 | - $op = $v[0] ?: $v; |
|
| 378 | - } else { |
|
| 379 | - $op = $v; |
|
| 380 | - } |
|
| 381 | - if (!$op || 1 == $op || '0=0' == $op) { |
|
| 382 | - return 'true'; |
|
| 383 | - } |
|
| 384 | - if ('0=1' === $op) { |
|
| 385 | - return 'false'; |
|
| 386 | - } |
|
| 387 | - // traiter {cle IN a,b} ou {valeur !IN a,b} |
|
| 388 | - if (preg_match(',^\(([\w/]+)(\s+NOT)?\s+IN\s+(\(.*\))\)$,', (string) $op, $regs)) { |
|
| 389 | - return $this->composer_filtre($regs[1], 'IN', $regs[3], $regs[2]); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - // 3 possibilites : count($v) = |
|
| 393 | - // * 1 : {x y} ; on recoit $v[0] = y |
|
| 394 | - // * 2 : {x !op y} ; on recoit $v[0] = 'NOT', $v[1] = array() // array du type {x op y} |
|
| 395 | - // * 3 : {x op y} ; on recoit $v[0] = 'op', $v[1] = x, $v[2] = y |
|
| 396 | - |
|
| 397 | - // 1 : forcement traite par un critere, on passe |
|
| 398 | - if (!$v || !is_array($v) || 1 == count($v)) { |
|
| 399 | - return null; // sera ignore |
|
| 400 | - } |
|
| 401 | - if (2 == count($v) && is_array($v[1])) { |
|
| 402 | - return $this->composer_filtre($v[1][1], $v[1][0], $v[1][2], 'NOT'); |
|
| 403 | - } |
|
| 404 | - if (3 == count($v)) { |
|
| 405 | - // traiter le OR/AND suivi de 2 valeurs |
|
| 406 | - if (in_array($op, ['OR', 'AND'])) { |
|
| 407 | - array_shift($v); |
|
| 408 | - foreach (array_keys($v) as $k) { |
|
| 409 | - $v[$k] = $this->traduire_condition_sql_en_filtre($v[$k]); |
|
| 410 | - if (null === $v[$k]) { |
|
| 411 | - unset($v[$k]); |
|
| 412 | - } elseif ('true' === $v[$k]) { |
|
| 413 | - if ('OR' === $op) { |
|
| 414 | - return 'true'; |
|
| 415 | - } |
|
| 416 | - if ('AND' === $op) { |
|
| 417 | - unset($v[$k]); |
|
| 418 | - } |
|
| 419 | - } elseif ('false' === $v[$k]) { |
|
| 420 | - if ('OR' === $op) { |
|
| 421 | - unset($v[$k]); |
|
| 422 | - } |
|
| 423 | - if ('AND' === $op) { |
|
| 424 | - return 'false'; |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - if ($v === []) { |
|
| 429 | - return null; |
|
| 430 | - } |
|
| 431 | - if (1 === count($v)) { |
|
| 432 | - return reset($v); |
|
| 433 | - } |
|
| 434 | - array_unshift($v, $op); |
|
| 435 | - |
|
| 436 | - return $v; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return $this->composer_filtre($v[1], $v[0], $v[2]); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - return null; // sera ignore |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Calculer un filtre sur un champ du tableau. |
|
| 447 | - * |
|
| 448 | - * @param $cle |
|
| 449 | - * @param $op |
|
| 450 | - * @param $valeur |
|
| 451 | - * @param false $not |
|
| 452 | - * |
|
| 453 | - * @return null|string |
|
| 454 | - */ |
|
| 455 | - protected function composer_filtre($cle, $op, $valeur, $not = false) { |
|
| 456 | - if ( |
|
| 457 | - method_exists($this->iter, 'exception_des_criteres') |
|
| 458 | - && in_array($cle, $this->iter->exception_des_criteres()) |
|
| 459 | - ) { |
|
| 460 | - return null; |
|
| 461 | - } |
|
| 462 | - // TODO: analyser le filtre pour refuser ce qu'on ne sait pas traiter ? |
|
| 463 | - // mais c'est normalement deja opere par calculer_critere_infixe() |
|
| 464 | - // qui regarde la description 'desc' (en casse reelle d'ailleurs : {isDir=1} |
|
| 465 | - // ne sera pas vu si l'on a defini desc['field']['isdir'] pour que #ISDIR soit present. |
|
| 466 | - // il faudrait peut etre definir les 2 champs isDir et isdir... a reflechir... |
|
| 467 | - |
|
| 468 | - // if (!in_array($cle, array('cle', 'valeur'))) |
|
| 469 | - // return; |
|
| 470 | - |
|
| 471 | - $a = '$this->get_select(\'' . $cle . '\')'; |
|
| 472 | - |
|
| 473 | - $filtre = ''; |
|
| 474 | - |
|
| 475 | - if ('REGEXP' == $op) { |
|
| 476 | - $filtre = 'filtrer("match", ' . $a . ', ' . str_replace('\"', '"', (string) $valeur) . ')'; |
|
| 477 | - $op = ''; |
|
| 478 | - } else { |
|
| 479 | - if ('LIKE' == $op) { |
|
| 480 | - $valeur = str_replace(['\"', '_', '%'], ['"', '.', '.*'], preg_quote((string) $valeur)); |
|
| 481 | - $filtre = 'filtrer("match", ' . $a . ', ' . $valeur . ')'; |
|
| 482 | - $op = ''; |
|
| 483 | - } else { |
|
| 484 | - if ('=' == $op) { |
|
| 485 | - $op = '=='; |
|
| 486 | - } else { |
|
| 487 | - if ('IN' == $op) { |
|
| 488 | - $filtre = 'in_array(' . $a . ', array' . $valeur . ')'; |
|
| 489 | - $op = ''; |
|
| 490 | - } else { |
|
| 491 | - if (!in_array($op, ['<', '<=', '>', '>='])) { |
|
| 492 | - spip_log('operateur non reconnu ' . $op); // [todo] mettre une erreur de squelette |
|
| 493 | - $op = ''; |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - } |
|
| 497 | - } |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - if ($op) { |
|
| 501 | - $filtre = $a . $op . str_replace('\"', '"', (string) $valeur); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - if ($not) { |
|
| 505 | - $filtre = "!({$filtre})"; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - return $filtre; |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - // calcule les elements a retournes par fetch() |
|
| 512 | - // enleve les elements inutiles du select() |
|
| 513 | - // |
|
| 514 | - private function calculer_select() { |
|
| 515 | - if ($select = &$this->command['select']) { |
|
| 516 | - foreach ($select as $s) { |
|
| 517 | - // /!\ $s = '.nom' |
|
| 518 | - if ('.' == $s[0]) { |
|
| 519 | - $s = substr((string) $s, 1); |
|
| 520 | - } |
|
| 521 | - $this->select[] = $s; |
|
| 522 | - } |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - private function calculer_filtres() { |
|
| 527 | - // Issu de calculer_select() de public/composer L.519 |
|
| 528 | - // TODO: externaliser... |
|
| 529 | - // |
|
| 530 | - // retirer les criteres vides: |
|
| 531 | - // {X ?} avec X absent de l'URL |
|
| 532 | - // {par #ENV{X}} avec X absent de l'URL |
|
| 533 | - // IN sur collection vide (ce dernier devrait pouvoir etre fait a la compil) |
|
| 534 | - if ($where = &$this->command['where']) { |
|
| 535 | - foreach ($where as $k => $v) { |
|
| 536 | - $this->filtre[] = $this->traduire_condition_sql_en_filtre($v); |
|
| 537 | - } |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - // critere {2,7} |
|
| 541 | - if (isset($this->command['limit']) && $this->command['limit']) { |
|
| 542 | - $limit = explode(',', (string) $this->command['limit']); |
|
| 543 | - $this->offset = $limit[0]; |
|
| 544 | - $this->limit = $limit[1]; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - // Creer la fonction de filtrage sur $this |
|
| 548 | - if ($this->filtre) { |
|
| 549 | - if ($filtres = $this->assembler_filtres($this->filtre)) { |
|
| 550 | - $filtres = 'return ' . $filtres . ';'; |
|
| 551 | - $this->func_filtre = fn () => eval($filtres); |
|
| 552 | - } else { |
|
| 553 | - $this->func_filtre = null; |
|
| 554 | - } |
|
| 555 | - } |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - /* |
|
| 148 | + // cle et valeur par defaut |
|
| 149 | + // ICI PLANTAGE SI ON NE CONTROLE PAS $nom |
|
| 150 | + if ( |
|
| 151 | + in_array($nom, ['cle', 'valeur']) |
|
| 152 | + && method_exists($this, $nom) |
|
| 153 | + ) { |
|
| 154 | + return $this->{$nom}(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // Par defaut chercher en xpath dans la valeur() |
|
| 158 | + return table_valeur($this->valeur(), $nom, null); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + public function next(): void { |
|
| 162 | + ++$this->pos; |
|
| 163 | + parent::next(); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * revient au depart. |
|
| 168 | + */ |
|
| 169 | + public function rewind(): void { |
|
| 170 | + $this->pos = 0; |
|
| 171 | + $this->fetched = 0; |
|
| 172 | + parent::rewind(); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * aller a la position absolue n, |
|
| 177 | + * comptee depuis le debut. |
|
| 178 | + * |
|
| 179 | + * @param int $n |
|
| 180 | + * absolute pos |
|
| 181 | + * @param string $continue |
|
| 182 | + * param for sql_ api |
|
| 183 | + * |
|
| 184 | + * @return bool |
|
| 185 | + * success or fail if not implemented |
|
| 186 | + */ |
|
| 187 | + public function seek($n = 0, $continue = null) { |
|
| 188 | + if ($this->func_filtre || !method_exists($this->iter, 'seek') || !$this->iter->seek($n)) { |
|
| 189 | + $this->seek_loop($n); |
|
| 190 | + } |
|
| 191 | + $this->pos = $n; |
|
| 192 | + $this->fetched = $n; |
|
| 193 | + |
|
| 194 | + return true; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Avancer de $saut pas. |
|
| 199 | + * |
|
| 200 | + * @param $saut |
|
| 201 | + * @param $max |
|
| 202 | + * |
|
| 203 | + * @return int |
|
| 204 | + */ |
|
| 205 | + public function skip($saut, $max = null) { |
|
| 206 | + // pas de saut en arriere autorise pour cette fonction |
|
| 207 | + if (($saut = (int) $saut) <= 0) { |
|
| 208 | + return $this->pos; |
|
| 209 | + } |
|
| 210 | + $seek = $this->pos + $saut; |
|
| 211 | + // si le saut fait depasser le maxi, on libere la resource |
|
| 212 | + // et on sort |
|
| 213 | + if (is_null($max)) { |
|
| 214 | + $max = $this->count(); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if ($seek >= $max || $seek >= $this->count()) { |
|
| 218 | + // sortie plus rapide que de faire next() jusqu'a la fin ! |
|
| 219 | + $this->free(); |
|
| 220 | + |
|
| 221 | + return $max; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $this->seek($seek); |
|
| 225 | + |
|
| 226 | + return $this->pos; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Renvoyer un tableau des donnees correspondantes |
|
| 231 | + * a la position courante de l'iterateur |
|
| 232 | + * en controlant si on respecte le filtre |
|
| 233 | + * Appliquer aussi le critere {offset,limit}. |
|
| 234 | + * |
|
| 235 | + * @return array|bool |
|
| 236 | + */ |
|
| 237 | + public function fetch() { |
|
| 238 | + if (method_exists($this->iter, 'fetch')) { |
|
| 239 | + return $this->iter->fetch(); |
|
| 240 | + } |
|
| 241 | + while ( |
|
| 242 | + $this->valid() |
|
| 243 | + && (!$this->accept() || $this->offset !== null && $this->fetched++ < $this->offset) |
|
| 244 | + ) { |
|
| 245 | + $this->next(); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + if (!$this->valid()) { |
|
| 249 | + return false; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + if ( |
|
| 253 | + $this->limit !== null |
|
| 254 | + && $this->fetched > $this->offset + $this->limit |
|
| 255 | + ) { |
|
| 256 | + return false; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + $r = []; |
|
| 260 | + foreach ($this->select as $nom) { |
|
| 261 | + $r[$nom] = $this->get_select($nom); |
|
| 262 | + } |
|
| 263 | + $this->next(); |
|
| 264 | + |
|
| 265 | + return $r; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + // retourner la cle pour #CLE |
|
| 269 | + public function cle() { |
|
| 270 | + return $this->key(); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + // retourner la valeur pour #VALEUR |
|
| 274 | + public function valeur() { |
|
| 275 | + return $this->current(); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Accepte-t-on l'entree courante lue ? |
|
| 280 | + * On execute les filtres pour le savoir. |
|
| 281 | + */ |
|
| 282 | + public function accept(): bool { |
|
| 283 | + if ($f = $this->func_filtre) { |
|
| 284 | + return $f(); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + return true; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * liberer la ressource. |
|
| 292 | + * |
|
| 293 | + * @return bool |
|
| 294 | + */ |
|
| 295 | + public function free() { |
|
| 296 | + if (method_exists($this->iter, 'free')) { |
|
| 297 | + $this->iter->free(); |
|
| 298 | + } |
|
| 299 | + $this->pos = $this->total = 0; |
|
| 300 | + |
|
| 301 | + return true; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Compter le nombre total de resultats |
|
| 306 | + * pour #TOTAL_BOUCLE. |
|
| 307 | + * |
|
| 308 | + * @return int |
|
| 309 | + */ |
|
| 310 | + public function count() { |
|
| 311 | + if (is_null($this->total)) { |
|
| 312 | + if ( |
|
| 313 | + method_exists($this->iter, 'count') |
|
| 314 | + && !$this->func_filtre |
|
| 315 | + ) { |
|
| 316 | + return $this->total = $this->iter->count(); |
|
| 317 | + } |
|
| 318 | + // compter les lignes et rembobiner |
|
| 319 | + $total = 0; |
|
| 320 | + $pos = $this->pos; // sauver la position |
|
| 321 | + $this->rewind(); |
|
| 322 | + while ($this->fetch() && $total < $this->max) { |
|
| 323 | + ++$total; |
|
| 324 | + } |
|
| 325 | + $this->seek($pos); |
|
| 326 | + $this->total = $total; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + return $this->total; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Assembler le tableau de filtres traduits depuis les conditions SQL |
|
| 334 | + * les filtres vides ou null sont ignores. |
|
| 335 | + * |
|
| 336 | + * @param $filtres |
|
| 337 | + * @param string $operateur |
|
| 338 | + * |
|
| 339 | + * @return null|string |
|
| 340 | + */ |
|
| 341 | + protected function assembler_filtres($filtres, $operateur = 'AND') { |
|
| 342 | + $filtres_string = []; |
|
| 343 | + foreach ($filtres as $k => $v) { |
|
| 344 | + // si c'est un tableau de OR/AND + 2 sous-filtres, on recurse pour transformer en chaine |
|
| 345 | + if (is_array($v) && in_array(reset($v), ['OR', 'AND'])) { |
|
| 346 | + $op = array_shift($v); |
|
| 347 | + $v = $this->assembler_filtres($v, $op); |
|
| 348 | + } |
|
| 349 | + if (is_null($v) || !is_string($v) || empty($v)) { |
|
| 350 | + continue; |
|
| 351 | + } |
|
| 352 | + $filtres_string[] = $v; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + if ($filtres_string === []) { |
|
| 356 | + return null; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + return '(' . implode(") {$operateur} (", $filtres_string) . ')'; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Traduire un element du tableau where SQL en un filtre. |
|
| 364 | + * |
|
| 365 | + * @param $v |
|
| 366 | + * |
|
| 367 | + * @return null|array|string |
|
| 368 | + */ |
|
| 369 | + protected function traduire_condition_sql_en_filtre($v) { |
|
| 370 | + if (is_array($v)) { |
|
| 371 | + if ((count($v) >= 2) && ('REGEXP' == $v[0]) && ("'.*'" == $v[2])) { |
|
| 372 | + return 'true'; |
|
| 373 | + } |
|
| 374 | + if ((count($v) >= 2) && ('LIKE' == $v[0]) && ("'%'" == $v[2])) { |
|
| 375 | + return 'true'; |
|
| 376 | + } |
|
| 377 | + $op = $v[0] ?: $v; |
|
| 378 | + } else { |
|
| 379 | + $op = $v; |
|
| 380 | + } |
|
| 381 | + if (!$op || 1 == $op || '0=0' == $op) { |
|
| 382 | + return 'true'; |
|
| 383 | + } |
|
| 384 | + if ('0=1' === $op) { |
|
| 385 | + return 'false'; |
|
| 386 | + } |
|
| 387 | + // traiter {cle IN a,b} ou {valeur !IN a,b} |
|
| 388 | + if (preg_match(',^\(([\w/]+)(\s+NOT)?\s+IN\s+(\(.*\))\)$,', (string) $op, $regs)) { |
|
| 389 | + return $this->composer_filtre($regs[1], 'IN', $regs[3], $regs[2]); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + // 3 possibilites : count($v) = |
|
| 393 | + // * 1 : {x y} ; on recoit $v[0] = y |
|
| 394 | + // * 2 : {x !op y} ; on recoit $v[0] = 'NOT', $v[1] = array() // array du type {x op y} |
|
| 395 | + // * 3 : {x op y} ; on recoit $v[0] = 'op', $v[1] = x, $v[2] = y |
|
| 396 | + |
|
| 397 | + // 1 : forcement traite par un critere, on passe |
|
| 398 | + if (!$v || !is_array($v) || 1 == count($v)) { |
|
| 399 | + return null; // sera ignore |
|
| 400 | + } |
|
| 401 | + if (2 == count($v) && is_array($v[1])) { |
|
| 402 | + return $this->composer_filtre($v[1][1], $v[1][0], $v[1][2], 'NOT'); |
|
| 403 | + } |
|
| 404 | + if (3 == count($v)) { |
|
| 405 | + // traiter le OR/AND suivi de 2 valeurs |
|
| 406 | + if (in_array($op, ['OR', 'AND'])) { |
|
| 407 | + array_shift($v); |
|
| 408 | + foreach (array_keys($v) as $k) { |
|
| 409 | + $v[$k] = $this->traduire_condition_sql_en_filtre($v[$k]); |
|
| 410 | + if (null === $v[$k]) { |
|
| 411 | + unset($v[$k]); |
|
| 412 | + } elseif ('true' === $v[$k]) { |
|
| 413 | + if ('OR' === $op) { |
|
| 414 | + return 'true'; |
|
| 415 | + } |
|
| 416 | + if ('AND' === $op) { |
|
| 417 | + unset($v[$k]); |
|
| 418 | + } |
|
| 419 | + } elseif ('false' === $v[$k]) { |
|
| 420 | + if ('OR' === $op) { |
|
| 421 | + unset($v[$k]); |
|
| 422 | + } |
|
| 423 | + if ('AND' === $op) { |
|
| 424 | + return 'false'; |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + if ($v === []) { |
|
| 429 | + return null; |
|
| 430 | + } |
|
| 431 | + if (1 === count($v)) { |
|
| 432 | + return reset($v); |
|
| 433 | + } |
|
| 434 | + array_unshift($v, $op); |
|
| 435 | + |
|
| 436 | + return $v; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return $this->composer_filtre($v[1], $v[0], $v[2]); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + return null; // sera ignore |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Calculer un filtre sur un champ du tableau. |
|
| 447 | + * |
|
| 448 | + * @param $cle |
|
| 449 | + * @param $op |
|
| 450 | + * @param $valeur |
|
| 451 | + * @param false $not |
|
| 452 | + * |
|
| 453 | + * @return null|string |
|
| 454 | + */ |
|
| 455 | + protected function composer_filtre($cle, $op, $valeur, $not = false) { |
|
| 456 | + if ( |
|
| 457 | + method_exists($this->iter, 'exception_des_criteres') |
|
| 458 | + && in_array($cle, $this->iter->exception_des_criteres()) |
|
| 459 | + ) { |
|
| 460 | + return null; |
|
| 461 | + } |
|
| 462 | + // TODO: analyser le filtre pour refuser ce qu'on ne sait pas traiter ? |
|
| 463 | + // mais c'est normalement deja opere par calculer_critere_infixe() |
|
| 464 | + // qui regarde la description 'desc' (en casse reelle d'ailleurs : {isDir=1} |
|
| 465 | + // ne sera pas vu si l'on a defini desc['field']['isdir'] pour que #ISDIR soit present. |
|
| 466 | + // il faudrait peut etre definir les 2 champs isDir et isdir... a reflechir... |
|
| 467 | + |
|
| 468 | + // if (!in_array($cle, array('cle', 'valeur'))) |
|
| 469 | + // return; |
|
| 470 | + |
|
| 471 | + $a = '$this->get_select(\'' . $cle . '\')'; |
|
| 472 | + |
|
| 473 | + $filtre = ''; |
|
| 474 | + |
|
| 475 | + if ('REGEXP' == $op) { |
|
| 476 | + $filtre = 'filtrer("match", ' . $a . ', ' . str_replace('\"', '"', (string) $valeur) . ')'; |
|
| 477 | + $op = ''; |
|
| 478 | + } else { |
|
| 479 | + if ('LIKE' == $op) { |
|
| 480 | + $valeur = str_replace(['\"', '_', '%'], ['"', '.', '.*'], preg_quote((string) $valeur)); |
|
| 481 | + $filtre = 'filtrer("match", ' . $a . ', ' . $valeur . ')'; |
|
| 482 | + $op = ''; |
|
| 483 | + } else { |
|
| 484 | + if ('=' == $op) { |
|
| 485 | + $op = '=='; |
|
| 486 | + } else { |
|
| 487 | + if ('IN' == $op) { |
|
| 488 | + $filtre = 'in_array(' . $a . ', array' . $valeur . ')'; |
|
| 489 | + $op = ''; |
|
| 490 | + } else { |
|
| 491 | + if (!in_array($op, ['<', '<=', '>', '>='])) { |
|
| 492 | + spip_log('operateur non reconnu ' . $op); // [todo] mettre une erreur de squelette |
|
| 493 | + $op = ''; |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + } |
|
| 497 | + } |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + if ($op) { |
|
| 501 | + $filtre = $a . $op . str_replace('\"', '"', (string) $valeur); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + if ($not) { |
|
| 505 | + $filtre = "!({$filtre})"; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + return $filtre; |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + // calcule les elements a retournes par fetch() |
|
| 512 | + // enleve les elements inutiles du select() |
|
| 513 | + // |
|
| 514 | + private function calculer_select() { |
|
| 515 | + if ($select = &$this->command['select']) { |
|
| 516 | + foreach ($select as $s) { |
|
| 517 | + // /!\ $s = '.nom' |
|
| 518 | + if ('.' == $s[0]) { |
|
| 519 | + $s = substr((string) $s, 1); |
|
| 520 | + } |
|
| 521 | + $this->select[] = $s; |
|
| 522 | + } |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + private function calculer_filtres() { |
|
| 527 | + // Issu de calculer_select() de public/composer L.519 |
|
| 528 | + // TODO: externaliser... |
|
| 529 | + // |
|
| 530 | + // retirer les criteres vides: |
|
| 531 | + // {X ?} avec X absent de l'URL |
|
| 532 | + // {par #ENV{X}} avec X absent de l'URL |
|
| 533 | + // IN sur collection vide (ce dernier devrait pouvoir etre fait a la compil) |
|
| 534 | + if ($where = &$this->command['where']) { |
|
| 535 | + foreach ($where as $k => $v) { |
|
| 536 | + $this->filtre[] = $this->traduire_condition_sql_en_filtre($v); |
|
| 537 | + } |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + // critere {2,7} |
|
| 541 | + if (isset($this->command['limit']) && $this->command['limit']) { |
|
| 542 | + $limit = explode(',', (string) $this->command['limit']); |
|
| 543 | + $this->offset = $limit[0]; |
|
| 544 | + $this->limit = $limit[1]; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + // Creer la fonction de filtrage sur $this |
|
| 548 | + if ($this->filtre) { |
|
| 549 | + if ($filtres = $this->assembler_filtres($this->filtre)) { |
|
| 550 | + $filtres = 'return ' . $filtres . ';'; |
|
| 551 | + $this->func_filtre = fn () => eval($filtres); |
|
| 552 | + } else { |
|
| 553 | + $this->func_filtre = null; |
|
| 554 | + } |
|
| 555 | + } |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + /* |
|
| 559 | 559 | * aller a la position $n en parcourant |
| 560 | 560 | * un par un tous les elements |
| 561 | 561 | */ |
| 562 | - private function seek_loop($n) { |
|
| 563 | - if ($this->pos > $n) { |
|
| 564 | - $this->rewind(); |
|
| 565 | - } |
|
| 562 | + private function seek_loop($n) { |
|
| 563 | + if ($this->pos > $n) { |
|
| 564 | + $this->rewind(); |
|
| 565 | + } |
|
| 566 | 566 | |
| 567 | - while ($this->pos < $n && $this->valid()) { |
|
| 568 | - $this->next(); |
|
| 569 | - } |
|
| 567 | + while ($this->pos < $n && $this->valid()) { |
|
| 568 | + $this->next(); |
|
| 569 | + } |
|
| 570 | 570 | |
| 571 | - return true; |
|
| 572 | - } |
|
| 571 | + return true; |
|
| 572 | + } |
|
| 573 | 573 | } |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | return $this->iter->{$nom}(); |
| 136 | 136 | } catch (Exception) { |
| 137 | 137 | // #GETCHILDREN sur un fichier de DirectoryIterator ... |
| 138 | - spip_log("Methode {$nom} en echec sur " . $this->iter::class); |
|
| 138 | + spip_log("Methode {$nom} en echec sur ".$this->iter::class); |
|
| 139 | 139 | spip_log("Cela peut être normal : retour d'une ligne de resultat ne pouvant pas calculer cette methode"); |
| 140 | 140 | |
| 141 | 141 | return ''; |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | return null; |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - return '(' . implode(") {$operateur} (", $filtres_string) . ')'; |
|
| 359 | + return '('.implode(") {$operateur} (", $filtres_string).')'; |
|
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | /** |
@@ -439,7 +439,7 @@ discard block |
||
| 439 | 439 | return $this->composer_filtre($v[1], $v[0], $v[2]); |
| 440 | 440 | } |
| 441 | 441 | |
| 442 | - return null; // sera ignore |
|
| 442 | + return null; // sera ignore |
|
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | /** |
@@ -468,28 +468,28 @@ discard block |
||
| 468 | 468 | // if (!in_array($cle, array('cle', 'valeur'))) |
| 469 | 469 | // return; |
| 470 | 470 | |
| 471 | - $a = '$this->get_select(\'' . $cle . '\')'; |
|
| 471 | + $a = '$this->get_select(\''.$cle.'\')'; |
|
| 472 | 472 | |
| 473 | 473 | $filtre = ''; |
| 474 | 474 | |
| 475 | 475 | if ('REGEXP' == $op) { |
| 476 | - $filtre = 'filtrer("match", ' . $a . ', ' . str_replace('\"', '"', (string) $valeur) . ')'; |
|
| 476 | + $filtre = 'filtrer("match", '.$a.', '.str_replace('\"', '"', (string) $valeur).')'; |
|
| 477 | 477 | $op = ''; |
| 478 | 478 | } else { |
| 479 | 479 | if ('LIKE' == $op) { |
| 480 | 480 | $valeur = str_replace(['\"', '_', '%'], ['"', '.', '.*'], preg_quote((string) $valeur)); |
| 481 | - $filtre = 'filtrer("match", ' . $a . ', ' . $valeur . ')'; |
|
| 481 | + $filtre = 'filtrer("match", '.$a.', '.$valeur.')'; |
|
| 482 | 482 | $op = ''; |
| 483 | 483 | } else { |
| 484 | 484 | if ('=' == $op) { |
| 485 | 485 | $op = '=='; |
| 486 | 486 | } else { |
| 487 | 487 | if ('IN' == $op) { |
| 488 | - $filtre = 'in_array(' . $a . ', array' . $valeur . ')'; |
|
| 488 | + $filtre = 'in_array('.$a.', array'.$valeur.')'; |
|
| 489 | 489 | $op = ''; |
| 490 | 490 | } else { |
| 491 | 491 | if (!in_array($op, ['<', '<=', '>', '>='])) { |
| 492 | - spip_log('operateur non reconnu ' . $op); // [todo] mettre une erreur de squelette |
|
| 492 | + spip_log('operateur non reconnu '.$op); // [todo] mettre une erreur de squelette |
|
| 493 | 493 | $op = ''; |
| 494 | 494 | } |
| 495 | 495 | } |
@@ -498,7 +498,7 @@ discard block |
||
| 498 | 498 | } |
| 499 | 499 | |
| 500 | 500 | if ($op) { |
| 501 | - $filtre = $a . $op . str_replace('\"', '"', (string) $valeur); |
|
| 501 | + $filtre = $a.$op.str_replace('\"', '"', (string) $valeur); |
|
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | if ($not) { |
@@ -547,7 +547,7 @@ discard block |
||
| 547 | 547 | // Creer la fonction de filtrage sur $this |
| 548 | 548 | if ($this->filtre) { |
| 549 | 549 | if ($filtres = $this->assembler_filtres($this->filtre)) { |
| 550 | - $filtres = 'return ' . $filtres . ';'; |
|
| 550 | + $filtres = 'return '.$filtres.';'; |
|
| 551 | 551 | $this->func_filtre = fn () => eval($filtres); |
| 552 | 552 | } else { |
| 553 | 553 | $this->func_filtre = null; |
@@ -12,60 +12,60 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Factory |
| 14 | 14 | { |
| 15 | - public static function create($iterateur, $command, $info = null) { |
|
| 16 | - $iter = null; |
|
| 17 | - // cas des SI {si expression} analises tres tot |
|
| 18 | - // pour eviter le chargement de tout iterateur |
|
| 19 | - if (isset($command['si'])) { |
|
| 20 | - foreach ($command['si'] as $si) { |
|
| 21 | - if (!$si) { |
|
| 22 | - // $command pour boucle SQL peut generer des erreurs de compilation |
|
| 23 | - // s'il est transmis alors qu'on est dans un iterateur vide |
|
| 24 | - return new Decorator(new EmptyIterator(), [], $info); |
|
| 25 | - } |
|
| 26 | - } |
|
| 27 | - } |
|
| 15 | + public static function create($iterateur, $command, $info = null) { |
|
| 16 | + $iter = null; |
|
| 17 | + // cas des SI {si expression} analises tres tot |
|
| 18 | + // pour eviter le chargement de tout iterateur |
|
| 19 | + if (isset($command['si'])) { |
|
| 20 | + foreach ($command['si'] as $si) { |
|
| 21 | + if (!$si) { |
|
| 22 | + // $command pour boucle SQL peut generer des erreurs de compilation |
|
| 23 | + // s'il est transmis alors qu'on est dans un iterateur vide |
|
| 24 | + return new Decorator(new EmptyIterator(), [], $info); |
|
| 25 | + } |
|
| 26 | + } |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - // chercher un iterateur PHP existant (par exemple dans SPL) |
|
| 30 | - // (il faudrait passer l'argument ->sql_serveur |
|
| 31 | - // pour etre certain qu'on est sur un "php:") |
|
| 32 | - if (class_exists($iterateur)) { |
|
| 33 | - $a = $command['args'] ?? []; |
|
| 29 | + // chercher un iterateur PHP existant (par exemple dans SPL) |
|
| 30 | + // (il faudrait passer l'argument ->sql_serveur |
|
| 31 | + // pour etre certain qu'on est sur un "php:") |
|
| 32 | + if (class_exists($iterateur)) { |
|
| 33 | + $a = $command['args'] ?? []; |
|
| 34 | 34 | |
| 35 | - // permettre de passer un Iterateur directement {args #ITERATEUR} : |
|
| 36 | - // si on recoit deja un iterateur en argument, on l'utilise |
|
| 37 | - if ((is_countable($a) ? count($a) : 0) == 1 && is_object($a[0]) && is_subclass_of($a[0], \Iterator::class)) { |
|
| 38 | - $iter = $a[0]; |
|
| 39 | - } else { |
|
| 40 | - // sinon, on cree un iterateur du type donne |
|
| 41 | - // arguments de creation de l'iterateur... |
|
| 42 | - try { |
|
| 43 | - $iter = new $iterateur(...$a); |
|
| 44 | - } catch (Exception $e) { |
|
| 45 | - spip_log("Erreur de chargement de l'iterateur {$iterateur}"); |
|
| 46 | - spip_log($e->getMessage()); |
|
| 47 | - $iter = new EmptyIterator(); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - } else { |
|
| 51 | - // chercher la classe d'iterateur Iterateur/XXX |
|
| 52 | - // definie dans le fichier src/Compilateur/Iterateur/xxx.php |
|
| 53 | - // FIXME: déclarer quelque part les iterateurs supplémentaires |
|
| 54 | - $class = __NAMESPACE__ . '\\' . ucfirst(strtolower((string) $iterateur)); |
|
| 55 | - if (!class_exists($class)) { |
|
| 56 | - // historique |
|
| 57 | - // Chercher IterateurXXX |
|
| 58 | - include_spip('iterateur/' . $iterateur); |
|
| 59 | - $class = 'Iterateur' . $iterateur; |
|
| 60 | - if (!class_exists($class)) { |
|
| 61 | - exit("Iterateur {$iterateur} non trouvé"); |
|
| 62 | - // si l'iterateur n'existe pas, on se rabat sur le generique |
|
| 63 | - // $iter = new EmptyIterator(); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - $iter = new $class($command, $info); |
|
| 67 | - } |
|
| 35 | + // permettre de passer un Iterateur directement {args #ITERATEUR} : |
|
| 36 | + // si on recoit deja un iterateur en argument, on l'utilise |
|
| 37 | + if ((is_countable($a) ? count($a) : 0) == 1 && is_object($a[0]) && is_subclass_of($a[0], \Iterator::class)) { |
|
| 38 | + $iter = $a[0]; |
|
| 39 | + } else { |
|
| 40 | + // sinon, on cree un iterateur du type donne |
|
| 41 | + // arguments de creation de l'iterateur... |
|
| 42 | + try { |
|
| 43 | + $iter = new $iterateur(...$a); |
|
| 44 | + } catch (Exception $e) { |
|
| 45 | + spip_log("Erreur de chargement de l'iterateur {$iterateur}"); |
|
| 46 | + spip_log($e->getMessage()); |
|
| 47 | + $iter = new EmptyIterator(); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + } else { |
|
| 51 | + // chercher la classe d'iterateur Iterateur/XXX |
|
| 52 | + // definie dans le fichier src/Compilateur/Iterateur/xxx.php |
|
| 53 | + // FIXME: déclarer quelque part les iterateurs supplémentaires |
|
| 54 | + $class = __NAMESPACE__ . '\\' . ucfirst(strtolower((string) $iterateur)); |
|
| 55 | + if (!class_exists($class)) { |
|
| 56 | + // historique |
|
| 57 | + // Chercher IterateurXXX |
|
| 58 | + include_spip('iterateur/' . $iterateur); |
|
| 59 | + $class = 'Iterateur' . $iterateur; |
|
| 60 | + if (!class_exists($class)) { |
|
| 61 | + exit("Iterateur {$iterateur} non trouvé"); |
|
| 62 | + // si l'iterateur n'existe pas, on se rabat sur le generique |
|
| 63 | + // $iter = new EmptyIterator(); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + $iter = new $class($command, $info); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - return new Decorator($iter, $command, $info); |
|
| 70 | - } |
|
| 69 | + return new Decorator($iter, $command, $info); |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -51,12 +51,12 @@ |
||
| 51 | 51 | // chercher la classe d'iterateur Iterateur/XXX |
| 52 | 52 | // definie dans le fichier src/Compilateur/Iterateur/xxx.php |
| 53 | 53 | // FIXME: déclarer quelque part les iterateurs supplémentaires |
| 54 | - $class = __NAMESPACE__ . '\\' . ucfirst(strtolower((string) $iterateur)); |
|
| 54 | + $class = __NAMESPACE__.'\\'.ucfirst(strtolower((string) $iterateur)); |
|
| 55 | 55 | if (!class_exists($class)) { |
| 56 | 56 | // historique |
| 57 | 57 | // Chercher IterateurXXX |
| 58 | - include_spip('iterateur/' . $iterateur); |
|
| 59 | - $class = 'Iterateur' . $iterateur; |
|
| 58 | + include_spip('iterateur/'.$iterateur); |
|
| 59 | + $class = 'Iterateur'.$iterateur; |
|
| 60 | 60 | if (!class_exists($class)) { |
| 61 | 61 | exit("Iterateur {$iterateur} non trouvé"); |
| 62 | 62 | // si l'iterateur n'existe pas, on se rabat sur le generique |
@@ -12,486 +12,486 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Data extends AbstractIterateur implements Iterator |
| 14 | 14 | { |
| 15 | - /** Tableau de données */ |
|
| 16 | - protected array $tableau = []; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Conditions de filtrage |
|
| 20 | - * ie criteres de selection |
|
| 21 | - */ |
|
| 22 | - protected array $filtre = []; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Cle courante |
|
| 26 | - * |
|
| 27 | - * @var scalar |
|
| 28 | - */ |
|
| 29 | - protected $cle = null; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Valeur courante |
|
| 33 | - * |
|
| 34 | - * @var mixed |
|
| 35 | - */ |
|
| 36 | - protected $valeur = null; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Constructeur |
|
| 40 | - * |
|
| 41 | - * @param $command |
|
| 42 | - * @param array $info |
|
| 43 | - */ |
|
| 44 | - public function __construct(array $command, array $info = []) { |
|
| 45 | - include_spip('iterateur/data'); |
|
| 46 | - $this->type = 'DATA'; |
|
| 47 | - $this->command = $command; |
|
| 48 | - $this->info = $info; |
|
| 49 | - $this->select($command); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Revenir au depart |
|
| 54 | - * |
|
| 55 | - * @return void |
|
| 56 | - */ |
|
| 57 | - public function rewind(): void { |
|
| 58 | - reset($this->tableau); |
|
| 59 | - $this->cle = array_key_first($this->tableau); |
|
| 60 | - $this->valeur = current($this->tableau); |
|
| 61 | - next($this->tableau); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Déclarer les critères exceptions |
|
| 66 | - * |
|
| 67 | - * @return array |
|
| 68 | - */ |
|
| 69 | - public function exception_des_criteres() { |
|
| 70 | - return ['tableau']; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Récupérer depuis le cache si possible |
|
| 75 | - * |
|
| 76 | - * @param string $cle |
|
| 77 | - * @return mixed |
|
| 78 | - */ |
|
| 79 | - protected function cache_get($cle) { |
|
| 80 | - if (!$cle) { |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - # utiliser memoization si dispo |
|
| 84 | - if (!function_exists('cache_get')) { |
|
| 85 | - return; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - return cache_get($cle); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Stocker en cache si possible |
|
| 93 | - * |
|
| 94 | - * @param string $cle |
|
| 95 | - * @param int $ttl |
|
| 96 | - * @param null|mixed $valeur |
|
| 97 | - * @return bool |
|
| 98 | - */ |
|
| 99 | - protected function cache_set($cle, $ttl, $valeur = null) { |
|
| 100 | - if (!$cle) { |
|
| 101 | - return; |
|
| 102 | - } |
|
| 103 | - if (is_null($valeur)) { |
|
| 104 | - $valeur = $this->tableau; |
|
| 105 | - } |
|
| 106 | - # utiliser memoization si dispo |
|
| 107 | - if (!function_exists('cache_set')) { |
|
| 108 | - return; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return cache_set( |
|
| 112 | - $cle, |
|
| 113 | - [ |
|
| 114 | - 'data' => $valeur, |
|
| 115 | - 'time' => time(), |
|
| 116 | - 'ttl' => $ttl |
|
| 117 | - ], |
|
| 118 | - 3600 + $ttl |
|
| 119 | - ); |
|
| 120 | - # conserver le cache 1h de plus que la validite demandee, |
|
| 121 | - # pour le cas ou le serveur distant ne reponde plus |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Aller chercher les données de la boucle DATA |
|
| 126 | - * |
|
| 127 | - * @throws Exception |
|
| 128 | - * @param array $command |
|
| 129 | - * @return void |
|
| 130 | - */ |
|
| 131 | - protected function select($command) { |
|
| 132 | - |
|
| 133 | - // l'iterateur DATA peut etre appele en passant (data:type) |
|
| 134 | - // le type se retrouve dans la commande 'from' |
|
| 135 | - // dans ce cas la le critere {source}, si present, n'a pas besoin du 1er argument |
|
| 136 | - if (isset($this->command['from'][0])) { |
|
| 137 | - if (isset($this->command['source']) && is_array($this->command['source'])) { |
|
| 138 | - array_unshift($this->command['source'], $this->command['sourcemode']); |
|
| 139 | - } |
|
| 140 | - $this->command['sourcemode'] = $this->command['from'][0]; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // cherchons differents moyens de creer le tableau de donnees |
|
| 144 | - // les commandes connues pour l'iterateur DATA |
|
| 145 | - // sont : {tableau #ARRAY} ; {cle=...} ; {valeur=...} |
|
| 146 | - |
|
| 147 | - // {source format, [URL], [arg2]...} |
|
| 148 | - if ( |
|
| 149 | - isset($this->command['source']) |
|
| 150 | - && isset($this->command['sourcemode']) |
|
| 151 | - ) { |
|
| 152 | - $this->select_source(); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // Critere {liste X1, X2, X3} |
|
| 156 | - if (isset($this->command['liste'])) { |
|
| 157 | - $this->select_liste(); |
|
| 158 | - } |
|
| 159 | - if (isset($this->command['enum'])) { |
|
| 160 | - $this->select_enum(); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Si a ce stade on n'a pas de table, il y a un bug |
|
| 164 | - if (!is_array($this->tableau)) { |
|
| 165 | - $this->err = true; |
|
| 166 | - spip_log('erreur datasource ' . var_export($command, true)); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - // {datapath query.results} |
|
| 170 | - // extraire le chemin "query.results" du tableau de donnees |
|
| 171 | - if ( |
|
| 172 | - !$this->err |
|
| 173 | - && isset($this->command['datapath']) |
|
| 174 | - && is_array($this->command['datapath']) |
|
| 175 | - ) { |
|
| 176 | - $this->select_datapath(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // tri {par x} |
|
| 180 | - if ($this->command['orderby']) { |
|
| 181 | - $this->select_orderby(); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // grouper les resultats {fusion /x/y/z} ; |
|
| 185 | - if ($this->command['groupby']) { |
|
| 186 | - $this->select_groupby(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $this->rewind(); |
|
| 190 | - #var_dump($this->tableau); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Aller chercher les donnees de la boucle DATA |
|
| 196 | - * depuis une source |
|
| 197 | - * {source format, [URL], [arg2]...} |
|
| 198 | - */ |
|
| 199 | - protected function select_source() { |
|
| 200 | - # un peu crado : avant de charger le cache il faut charger |
|
| 201 | - # les class indispensables, sinon PHP ne saura pas gerer |
|
| 202 | - # l'objet en cache ; cf plugins/icalendar |
|
| 203 | - # perf : pas de fonction table_to_array ! (table est deja un array) |
|
| 204 | - if ( |
|
| 205 | - isset($this->command['sourcemode']) |
|
| 206 | - && !in_array($this->command['sourcemode'], ['table', 'array', 'tableau']) |
|
| 207 | - ) { |
|
| 208 | - charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - # le premier argument peut etre un array, une URL etc. |
|
| 212 | - $src = $this->command['source'][0] ?? null; |
|
| 213 | - |
|
| 214 | - # avons-nous un cache dispo ? |
|
| 215 | - $cle = null; |
|
| 216 | - if (is_string($src)) { |
|
| 217 | - $cle = 'datasource_' . md5($this->command['sourcemode'] . ':' . var_export($this->command['source'], true)); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - $cache = $this->cache_get($cle); |
|
| 221 | - if (isset($this->command['datacache'])) { |
|
| 222 | - $ttl = (int) $this->command['datacache']; |
|
| 223 | - } |
|
| 224 | - if ( |
|
| 225 | - $cache |
|
| 226 | - && $cache['time'] + ($ttl ?? $cache['ttl']) > time() |
|
| 227 | - && !(_request('var_mode') === 'recalcul' && include_spip('inc/autoriser') && autoriser('recalcul')) |
|
| 228 | - ) { |
|
| 229 | - $this->tableau = $cache['data']; |
|
| 230 | - } else { |
|
| 231 | - try { |
|
| 232 | - if ( |
|
| 233 | - isset($this->command['sourcemode']) |
|
| 234 | - && in_array( |
|
| 235 | - $this->command['sourcemode'], |
|
| 236 | - ['table', 'array', 'tableau'] |
|
| 237 | - ) |
|
| 238 | - ) { |
|
| 239 | - if ( |
|
| 240 | - is_array($a = $src) |
|
| 241 | - || is_string($a) && ($a = str_replace('"', '"', $a)) && is_array($a = @unserialize($a)) |
|
| 242 | - ) { |
|
| 243 | - $this->tableau = $a; |
|
| 244 | - } |
|
| 245 | - } else { |
|
| 246 | - $data = $src; |
|
| 247 | - if (is_string($src)) { |
|
| 248 | - if (tester_url_absolue($src)) { |
|
| 249 | - include_spip('inc/distant'); |
|
| 250 | - $data = recuperer_url($src, ['taille_max' => _DATA_SOURCE_MAX_SIZE]); |
|
| 251 | - $data = $data['page'] ?? ''; |
|
| 252 | - if (!$data) { |
|
| 253 | - throw new Exception('404'); |
|
| 254 | - } |
|
| 255 | - if (!isset($ttl)) { |
|
| 256 | - $ttl = 24 * 3600; |
|
| 257 | - } |
|
| 258 | - } elseif (@is_dir($src)) { |
|
| 259 | - $data = $src; |
|
| 260 | - } elseif (@is_readable($src) && @is_file($src)) { |
|
| 261 | - $data = spip_file_get_contents($src); |
|
| 262 | - } |
|
| 263 | - if (!isset($ttl)) { |
|
| 264 | - $ttl = 10; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - if ( |
|
| 268 | - !$this->err |
|
| 269 | - && ($data_to_array = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true)) |
|
| 270 | - ) { |
|
| 271 | - $args = $this->command['source']; |
|
| 272 | - $args[0] = $data; |
|
| 273 | - if (is_array($a = $data_to_array(...$args))) { |
|
| 274 | - $this->tableau = $a; |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - if (!is_array($this->tableau)) { |
|
| 280 | - $this->err = true; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - if (!$this->err && isset($ttl) && $ttl > 0) { |
|
| 284 | - $this->cache_set($cle, $ttl); |
|
| 285 | - } |
|
| 286 | - } catch (Exception $e) { |
|
| 287 | - $e = $e->getMessage(); |
|
| 288 | - $err = sprintf( |
|
| 289 | - "[%s, %s] $e", |
|
| 290 | - $src, |
|
| 291 | - $this->command['sourcemode'] |
|
| 292 | - ); |
|
| 293 | - erreur_squelette([$err, []]); |
|
| 294 | - $this->err = true; |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - # en cas d'erreur, utiliser le cache si encore dispo |
|
| 299 | - if ($this->err && $cache) { |
|
| 300 | - $this->tableau = $cache['data']; |
|
| 301 | - $this->err = false; |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Retourne un tableau donne depuis un critère liste |
|
| 308 | - * |
|
| 309 | - * Critère `{liste X1, X2, X3}` |
|
| 310 | - * |
|
| 311 | - * @see critere_DATA_liste_dist() |
|
| 312 | - * |
|
| 313 | - **/ |
|
| 314 | - protected function select_liste() { |
|
| 315 | - # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 316 | - if (!isset($this->command['liste'][1])) { |
|
| 317 | - if (!is_array($this->command['liste'][0])) { |
|
| 318 | - $this->command['liste'] = explode(',', (string) $this->command['liste'][0]); |
|
| 319 | - } else { |
|
| 320 | - $this->command['liste'] = $this->command['liste'][0]; |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - $this->tableau = $this->command['liste']; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Retourne un tableau donne depuis un critere liste |
|
| 328 | - * Critere {enum Xmin, Xmax} |
|
| 329 | - * |
|
| 330 | - **/ |
|
| 331 | - protected function select_enum() { |
|
| 332 | - # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 333 | - if (!isset($this->command['enum'][1])) { |
|
| 334 | - if (!is_array($this->command['enum'][0])) { |
|
| 335 | - $this->command['enum'] = explode(',', (string) $this->command['enum'][0]); |
|
| 336 | - } else { |
|
| 337 | - $this->command['enum'] = $this->command['enum'][0]; |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - if ((is_countable($this->command['enum']) ? count($this->command['enum']) : 0) >= 3) { |
|
| 341 | - $enum = range( |
|
| 342 | - array_shift($this->command['enum']), |
|
| 343 | - array_shift($this->command['enum']), |
|
| 344 | - array_shift($this->command['enum']) |
|
| 345 | - ); |
|
| 346 | - } else { |
|
| 347 | - $enum = range(array_shift($this->command['enum']), array_shift($this->command['enum'])); |
|
| 348 | - } |
|
| 349 | - $this->tableau = $enum; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * extraire le chemin "query.results" du tableau de donnees |
|
| 355 | - * {datapath query.results} |
|
| 356 | - * |
|
| 357 | - **/ |
|
| 358 | - protected function select_datapath() { |
|
| 359 | - $base = reset($this->command['datapath']); |
|
| 360 | - if (strlen($base = ltrim(trim((string) $base), '/'))) { |
|
| 361 | - $results = table_valeur($this->tableau, $base); |
|
| 362 | - if (is_array($results)) { |
|
| 363 | - $this->tableau = $results; |
|
| 364 | - } else { |
|
| 365 | - $this->tableau = []; |
|
| 366 | - $this->err = true; |
|
| 367 | - spip_log("datapath '$base' absent"); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Ordonner les resultats |
|
| 374 | - * {par x} |
|
| 375 | - * |
|
| 376 | - **/ |
|
| 377 | - protected function select_orderby() { |
|
| 378 | - $sortfunc = ''; |
|
| 379 | - $aleas = 0; |
|
| 380 | - foreach ($this->command['orderby'] as $tri) { |
|
| 381 | - // virer le / initial pour les criteres de la forme {par /xx} |
|
| 382 | - if (preg_match(',^\.?([/\w:_-]+)( DESC)?$,iS', ltrim((string) $tri, '/'), $r)) { |
|
| 383 | - $r = array_pad($r, 3, null); |
|
| 384 | - |
|
| 385 | - // tri par cle |
|
| 386 | - if ($r[1] == 'cle') { |
|
| 387 | - if (isset($r[2]) && $r[2]) { |
|
| 388 | - krsort($this->tableau); |
|
| 389 | - } else { |
|
| 390 | - ksort($this->tableau); |
|
| 391 | - } |
|
| 392 | - } # {par hasard} |
|
| 393 | - else { |
|
| 394 | - if ($r[1] == 'hasard') { |
|
| 395 | - $k = array_keys($this->tableau); |
|
| 396 | - shuffle($k); |
|
| 397 | - $v = []; |
|
| 398 | - foreach ($k as $cle) { |
|
| 399 | - $v[$cle] = $this->tableau[$cle]; |
|
| 400 | - } |
|
| 401 | - $this->tableau = $v; |
|
| 402 | - } else { |
|
| 403 | - # {par valeur} ou {par valeur/xx/yy} |
|
| 404 | - $tv = $r[1] == 'valeur' ? '%s' : 'table_valeur(%s, ' . var_export($r[1], true) . ')'; |
|
| 405 | - $sortfunc .= ' |
|
| 15 | + /** Tableau de données */ |
|
| 16 | + protected array $tableau = []; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Conditions de filtrage |
|
| 20 | + * ie criteres de selection |
|
| 21 | + */ |
|
| 22 | + protected array $filtre = []; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Cle courante |
|
| 26 | + * |
|
| 27 | + * @var scalar |
|
| 28 | + */ |
|
| 29 | + protected $cle = null; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Valeur courante |
|
| 33 | + * |
|
| 34 | + * @var mixed |
|
| 35 | + */ |
|
| 36 | + protected $valeur = null; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Constructeur |
|
| 40 | + * |
|
| 41 | + * @param $command |
|
| 42 | + * @param array $info |
|
| 43 | + */ |
|
| 44 | + public function __construct(array $command, array $info = []) { |
|
| 45 | + include_spip('iterateur/data'); |
|
| 46 | + $this->type = 'DATA'; |
|
| 47 | + $this->command = $command; |
|
| 48 | + $this->info = $info; |
|
| 49 | + $this->select($command); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Revenir au depart |
|
| 54 | + * |
|
| 55 | + * @return void |
|
| 56 | + */ |
|
| 57 | + public function rewind(): void { |
|
| 58 | + reset($this->tableau); |
|
| 59 | + $this->cle = array_key_first($this->tableau); |
|
| 60 | + $this->valeur = current($this->tableau); |
|
| 61 | + next($this->tableau); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Déclarer les critères exceptions |
|
| 66 | + * |
|
| 67 | + * @return array |
|
| 68 | + */ |
|
| 69 | + public function exception_des_criteres() { |
|
| 70 | + return ['tableau']; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Récupérer depuis le cache si possible |
|
| 75 | + * |
|
| 76 | + * @param string $cle |
|
| 77 | + * @return mixed |
|
| 78 | + */ |
|
| 79 | + protected function cache_get($cle) { |
|
| 80 | + if (!$cle) { |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + # utiliser memoization si dispo |
|
| 84 | + if (!function_exists('cache_get')) { |
|
| 85 | + return; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + return cache_get($cle); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Stocker en cache si possible |
|
| 93 | + * |
|
| 94 | + * @param string $cle |
|
| 95 | + * @param int $ttl |
|
| 96 | + * @param null|mixed $valeur |
|
| 97 | + * @return bool |
|
| 98 | + */ |
|
| 99 | + protected function cache_set($cle, $ttl, $valeur = null) { |
|
| 100 | + if (!$cle) { |
|
| 101 | + return; |
|
| 102 | + } |
|
| 103 | + if (is_null($valeur)) { |
|
| 104 | + $valeur = $this->tableau; |
|
| 105 | + } |
|
| 106 | + # utiliser memoization si dispo |
|
| 107 | + if (!function_exists('cache_set')) { |
|
| 108 | + return; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return cache_set( |
|
| 112 | + $cle, |
|
| 113 | + [ |
|
| 114 | + 'data' => $valeur, |
|
| 115 | + 'time' => time(), |
|
| 116 | + 'ttl' => $ttl |
|
| 117 | + ], |
|
| 118 | + 3600 + $ttl |
|
| 119 | + ); |
|
| 120 | + # conserver le cache 1h de plus que la validite demandee, |
|
| 121 | + # pour le cas ou le serveur distant ne reponde plus |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Aller chercher les données de la boucle DATA |
|
| 126 | + * |
|
| 127 | + * @throws Exception |
|
| 128 | + * @param array $command |
|
| 129 | + * @return void |
|
| 130 | + */ |
|
| 131 | + protected function select($command) { |
|
| 132 | + |
|
| 133 | + // l'iterateur DATA peut etre appele en passant (data:type) |
|
| 134 | + // le type se retrouve dans la commande 'from' |
|
| 135 | + // dans ce cas la le critere {source}, si present, n'a pas besoin du 1er argument |
|
| 136 | + if (isset($this->command['from'][0])) { |
|
| 137 | + if (isset($this->command['source']) && is_array($this->command['source'])) { |
|
| 138 | + array_unshift($this->command['source'], $this->command['sourcemode']); |
|
| 139 | + } |
|
| 140 | + $this->command['sourcemode'] = $this->command['from'][0]; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // cherchons differents moyens de creer le tableau de donnees |
|
| 144 | + // les commandes connues pour l'iterateur DATA |
|
| 145 | + // sont : {tableau #ARRAY} ; {cle=...} ; {valeur=...} |
|
| 146 | + |
|
| 147 | + // {source format, [URL], [arg2]...} |
|
| 148 | + if ( |
|
| 149 | + isset($this->command['source']) |
|
| 150 | + && isset($this->command['sourcemode']) |
|
| 151 | + ) { |
|
| 152 | + $this->select_source(); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // Critere {liste X1, X2, X3} |
|
| 156 | + if (isset($this->command['liste'])) { |
|
| 157 | + $this->select_liste(); |
|
| 158 | + } |
|
| 159 | + if (isset($this->command['enum'])) { |
|
| 160 | + $this->select_enum(); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Si a ce stade on n'a pas de table, il y a un bug |
|
| 164 | + if (!is_array($this->tableau)) { |
|
| 165 | + $this->err = true; |
|
| 166 | + spip_log('erreur datasource ' . var_export($command, true)); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + // {datapath query.results} |
|
| 170 | + // extraire le chemin "query.results" du tableau de donnees |
|
| 171 | + if ( |
|
| 172 | + !$this->err |
|
| 173 | + && isset($this->command['datapath']) |
|
| 174 | + && is_array($this->command['datapath']) |
|
| 175 | + ) { |
|
| 176 | + $this->select_datapath(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // tri {par x} |
|
| 180 | + if ($this->command['orderby']) { |
|
| 181 | + $this->select_orderby(); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // grouper les resultats {fusion /x/y/z} ; |
|
| 185 | + if ($this->command['groupby']) { |
|
| 186 | + $this->select_groupby(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $this->rewind(); |
|
| 190 | + #var_dump($this->tableau); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Aller chercher les donnees de la boucle DATA |
|
| 196 | + * depuis une source |
|
| 197 | + * {source format, [URL], [arg2]...} |
|
| 198 | + */ |
|
| 199 | + protected function select_source() { |
|
| 200 | + # un peu crado : avant de charger le cache il faut charger |
|
| 201 | + # les class indispensables, sinon PHP ne saura pas gerer |
|
| 202 | + # l'objet en cache ; cf plugins/icalendar |
|
| 203 | + # perf : pas de fonction table_to_array ! (table est deja un array) |
|
| 204 | + if ( |
|
| 205 | + isset($this->command['sourcemode']) |
|
| 206 | + && !in_array($this->command['sourcemode'], ['table', 'array', 'tableau']) |
|
| 207 | + ) { |
|
| 208 | + charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + # le premier argument peut etre un array, une URL etc. |
|
| 212 | + $src = $this->command['source'][0] ?? null; |
|
| 213 | + |
|
| 214 | + # avons-nous un cache dispo ? |
|
| 215 | + $cle = null; |
|
| 216 | + if (is_string($src)) { |
|
| 217 | + $cle = 'datasource_' . md5($this->command['sourcemode'] . ':' . var_export($this->command['source'], true)); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + $cache = $this->cache_get($cle); |
|
| 221 | + if (isset($this->command['datacache'])) { |
|
| 222 | + $ttl = (int) $this->command['datacache']; |
|
| 223 | + } |
|
| 224 | + if ( |
|
| 225 | + $cache |
|
| 226 | + && $cache['time'] + ($ttl ?? $cache['ttl']) > time() |
|
| 227 | + && !(_request('var_mode') === 'recalcul' && include_spip('inc/autoriser') && autoriser('recalcul')) |
|
| 228 | + ) { |
|
| 229 | + $this->tableau = $cache['data']; |
|
| 230 | + } else { |
|
| 231 | + try { |
|
| 232 | + if ( |
|
| 233 | + isset($this->command['sourcemode']) |
|
| 234 | + && in_array( |
|
| 235 | + $this->command['sourcemode'], |
|
| 236 | + ['table', 'array', 'tableau'] |
|
| 237 | + ) |
|
| 238 | + ) { |
|
| 239 | + if ( |
|
| 240 | + is_array($a = $src) |
|
| 241 | + || is_string($a) && ($a = str_replace('"', '"', $a)) && is_array($a = @unserialize($a)) |
|
| 242 | + ) { |
|
| 243 | + $this->tableau = $a; |
|
| 244 | + } |
|
| 245 | + } else { |
|
| 246 | + $data = $src; |
|
| 247 | + if (is_string($src)) { |
|
| 248 | + if (tester_url_absolue($src)) { |
|
| 249 | + include_spip('inc/distant'); |
|
| 250 | + $data = recuperer_url($src, ['taille_max' => _DATA_SOURCE_MAX_SIZE]); |
|
| 251 | + $data = $data['page'] ?? ''; |
|
| 252 | + if (!$data) { |
|
| 253 | + throw new Exception('404'); |
|
| 254 | + } |
|
| 255 | + if (!isset($ttl)) { |
|
| 256 | + $ttl = 24 * 3600; |
|
| 257 | + } |
|
| 258 | + } elseif (@is_dir($src)) { |
|
| 259 | + $data = $src; |
|
| 260 | + } elseif (@is_readable($src) && @is_file($src)) { |
|
| 261 | + $data = spip_file_get_contents($src); |
|
| 262 | + } |
|
| 263 | + if (!isset($ttl)) { |
|
| 264 | + $ttl = 10; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + if ( |
|
| 268 | + !$this->err |
|
| 269 | + && ($data_to_array = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true)) |
|
| 270 | + ) { |
|
| 271 | + $args = $this->command['source']; |
|
| 272 | + $args[0] = $data; |
|
| 273 | + if (is_array($a = $data_to_array(...$args))) { |
|
| 274 | + $this->tableau = $a; |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + if (!is_array($this->tableau)) { |
|
| 280 | + $this->err = true; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + if (!$this->err && isset($ttl) && $ttl > 0) { |
|
| 284 | + $this->cache_set($cle, $ttl); |
|
| 285 | + } |
|
| 286 | + } catch (Exception $e) { |
|
| 287 | + $e = $e->getMessage(); |
|
| 288 | + $err = sprintf( |
|
| 289 | + "[%s, %s] $e", |
|
| 290 | + $src, |
|
| 291 | + $this->command['sourcemode'] |
|
| 292 | + ); |
|
| 293 | + erreur_squelette([$err, []]); |
|
| 294 | + $this->err = true; |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + # en cas d'erreur, utiliser le cache si encore dispo |
|
| 299 | + if ($this->err && $cache) { |
|
| 300 | + $this->tableau = $cache['data']; |
|
| 301 | + $this->err = false; |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Retourne un tableau donne depuis un critère liste |
|
| 308 | + * |
|
| 309 | + * Critère `{liste X1, X2, X3}` |
|
| 310 | + * |
|
| 311 | + * @see critere_DATA_liste_dist() |
|
| 312 | + * |
|
| 313 | + **/ |
|
| 314 | + protected function select_liste() { |
|
| 315 | + # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 316 | + if (!isset($this->command['liste'][1])) { |
|
| 317 | + if (!is_array($this->command['liste'][0])) { |
|
| 318 | + $this->command['liste'] = explode(',', (string) $this->command['liste'][0]); |
|
| 319 | + } else { |
|
| 320 | + $this->command['liste'] = $this->command['liste'][0]; |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + $this->tableau = $this->command['liste']; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Retourne un tableau donne depuis un critere liste |
|
| 328 | + * Critere {enum Xmin, Xmax} |
|
| 329 | + * |
|
| 330 | + **/ |
|
| 331 | + protected function select_enum() { |
|
| 332 | + # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 333 | + if (!isset($this->command['enum'][1])) { |
|
| 334 | + if (!is_array($this->command['enum'][0])) { |
|
| 335 | + $this->command['enum'] = explode(',', (string) $this->command['enum'][0]); |
|
| 336 | + } else { |
|
| 337 | + $this->command['enum'] = $this->command['enum'][0]; |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + if ((is_countable($this->command['enum']) ? count($this->command['enum']) : 0) >= 3) { |
|
| 341 | + $enum = range( |
|
| 342 | + array_shift($this->command['enum']), |
|
| 343 | + array_shift($this->command['enum']), |
|
| 344 | + array_shift($this->command['enum']) |
|
| 345 | + ); |
|
| 346 | + } else { |
|
| 347 | + $enum = range(array_shift($this->command['enum']), array_shift($this->command['enum'])); |
|
| 348 | + } |
|
| 349 | + $this->tableau = $enum; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * extraire le chemin "query.results" du tableau de donnees |
|
| 355 | + * {datapath query.results} |
|
| 356 | + * |
|
| 357 | + **/ |
|
| 358 | + protected function select_datapath() { |
|
| 359 | + $base = reset($this->command['datapath']); |
|
| 360 | + if (strlen($base = ltrim(trim((string) $base), '/'))) { |
|
| 361 | + $results = table_valeur($this->tableau, $base); |
|
| 362 | + if (is_array($results)) { |
|
| 363 | + $this->tableau = $results; |
|
| 364 | + } else { |
|
| 365 | + $this->tableau = []; |
|
| 366 | + $this->err = true; |
|
| 367 | + spip_log("datapath '$base' absent"); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Ordonner les resultats |
|
| 374 | + * {par x} |
|
| 375 | + * |
|
| 376 | + **/ |
|
| 377 | + protected function select_orderby() { |
|
| 378 | + $sortfunc = ''; |
|
| 379 | + $aleas = 0; |
|
| 380 | + foreach ($this->command['orderby'] as $tri) { |
|
| 381 | + // virer le / initial pour les criteres de la forme {par /xx} |
|
| 382 | + if (preg_match(',^\.?([/\w:_-]+)( DESC)?$,iS', ltrim((string) $tri, '/'), $r)) { |
|
| 383 | + $r = array_pad($r, 3, null); |
|
| 384 | + |
|
| 385 | + // tri par cle |
|
| 386 | + if ($r[1] == 'cle') { |
|
| 387 | + if (isset($r[2]) && $r[2]) { |
|
| 388 | + krsort($this->tableau); |
|
| 389 | + } else { |
|
| 390 | + ksort($this->tableau); |
|
| 391 | + } |
|
| 392 | + } # {par hasard} |
|
| 393 | + else { |
|
| 394 | + if ($r[1] == 'hasard') { |
|
| 395 | + $k = array_keys($this->tableau); |
|
| 396 | + shuffle($k); |
|
| 397 | + $v = []; |
|
| 398 | + foreach ($k as $cle) { |
|
| 399 | + $v[$cle] = $this->tableau[$cle]; |
|
| 400 | + } |
|
| 401 | + $this->tableau = $v; |
|
| 402 | + } else { |
|
| 403 | + # {par valeur} ou {par valeur/xx/yy} |
|
| 404 | + $tv = $r[1] == 'valeur' ? '%s' : 'table_valeur(%s, ' . var_export($r[1], true) . ')'; |
|
| 405 | + $sortfunc .= ' |
|
| 406 | 406 | $a = ' . sprintf($tv, '$aa') . '; |
| 407 | 407 | $b = ' . sprintf($tv, '$bb') . '; |
| 408 | 408 | if ($a <> $b) |
| 409 | 409 | return ($a ' . (empty($r[2]) ? '<' : '>') . ' $b) ? -1 : 1;'; |
| 410 | - } |
|
| 411 | - } |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - if ($sortfunc) { |
|
| 416 | - $sortfunc .= "\n return 0;"; |
|
| 417 | - uasort($this->tableau, fn($aa, $bb) => eval($sortfunc)); |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Grouper les resultats |
|
| 424 | - * {fusion /x/y/z} |
|
| 425 | - * |
|
| 426 | - **/ |
|
| 427 | - protected function select_groupby() { |
|
| 428 | - // virer le / initial pour les criteres de la forme {fusion /xx} |
|
| 429 | - if (strlen($fusion = ltrim((string) $this->command['groupby'][0], '/'))) { |
|
| 430 | - $vu = []; |
|
| 431 | - foreach ($this->tableau as $k => $v) { |
|
| 432 | - $val = table_valeur($v, $fusion); |
|
| 433 | - if (isset($vu[$val])) { |
|
| 434 | - unset($this->tableau[$k]); |
|
| 435 | - } else { |
|
| 436 | - $vu[$val] = true; |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * L'iterateur est-il encore valide ? |
|
| 445 | - * |
|
| 446 | - * @return bool |
|
| 447 | - */ |
|
| 448 | - public function valid(): bool { |
|
| 449 | - return !is_null($this->cle); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Retourner la valeur |
|
| 454 | - * |
|
| 455 | - * @return mixed |
|
| 456 | - */ |
|
| 457 | - #[\ReturnTypeWillChange] |
|
| 458 | - public function current() { |
|
| 459 | - return $this->valeur; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Retourner la cle |
|
| 464 | - * |
|
| 465 | - * @return mixed |
|
| 466 | - */ |
|
| 467 | - #[\ReturnTypeWillChange] |
|
| 468 | - public function key() { |
|
| 469 | - return $this->cle; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Passer a la valeur suivante |
|
| 474 | - * |
|
| 475 | - * @return void |
|
| 476 | - */ |
|
| 477 | - public function next(): void { |
|
| 478 | - if ($this->valid()) { |
|
| 479 | - $this->cle = key($this->tableau); |
|
| 480 | - $this->valeur = current($this->tableau); |
|
| 481 | - next($this->tableau); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Compter le nombre total de resultats |
|
| 487 | - * |
|
| 488 | - * @return int |
|
| 489 | - */ |
|
| 490 | - public function count() { |
|
| 491 | - if (is_null($this->total)) { |
|
| 492 | - $this->total = count($this->tableau); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - return $this->total; |
|
| 496 | - } |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + if ($sortfunc) { |
|
| 416 | + $sortfunc .= "\n return 0;"; |
|
| 417 | + uasort($this->tableau, fn($aa, $bb) => eval($sortfunc)); |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Grouper les resultats |
|
| 424 | + * {fusion /x/y/z} |
|
| 425 | + * |
|
| 426 | + **/ |
|
| 427 | + protected function select_groupby() { |
|
| 428 | + // virer le / initial pour les criteres de la forme {fusion /xx} |
|
| 429 | + if (strlen($fusion = ltrim((string) $this->command['groupby'][0], '/'))) { |
|
| 430 | + $vu = []; |
|
| 431 | + foreach ($this->tableau as $k => $v) { |
|
| 432 | + $val = table_valeur($v, $fusion); |
|
| 433 | + if (isset($vu[$val])) { |
|
| 434 | + unset($this->tableau[$k]); |
|
| 435 | + } else { |
|
| 436 | + $vu[$val] = true; |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * L'iterateur est-il encore valide ? |
|
| 445 | + * |
|
| 446 | + * @return bool |
|
| 447 | + */ |
|
| 448 | + public function valid(): bool { |
|
| 449 | + return !is_null($this->cle); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Retourner la valeur |
|
| 454 | + * |
|
| 455 | + * @return mixed |
|
| 456 | + */ |
|
| 457 | + #[\ReturnTypeWillChange] |
|
| 458 | + public function current() { |
|
| 459 | + return $this->valeur; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Retourner la cle |
|
| 464 | + * |
|
| 465 | + * @return mixed |
|
| 466 | + */ |
|
| 467 | + #[\ReturnTypeWillChange] |
|
| 468 | + public function key() { |
|
| 469 | + return $this->cle; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Passer a la valeur suivante |
|
| 474 | + * |
|
| 475 | + * @return void |
|
| 476 | + */ |
|
| 477 | + public function next(): void { |
|
| 478 | + if ($this->valid()) { |
|
| 479 | + $this->cle = key($this->tableau); |
|
| 480 | + $this->valeur = current($this->tableau); |
|
| 481 | + next($this->tableau); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Compter le nombre total de resultats |
|
| 487 | + * |
|
| 488 | + * @return int |
|
| 489 | + */ |
|
| 490 | + public function count() { |
|
| 491 | + if (is_null($this->total)) { |
|
| 492 | + $this->total = count($this->tableau); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + return $this->total; |
|
| 496 | + } |
|
| 497 | 497 | } |