685824c12ed8c7a733e8bebbe916f687e96e1c55
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2017 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) {
17 // Decompilation de l'arbre de syntaxe abstraite d'un squelette SPIP
19 function decompiler_boucle($struct, $fmt = '', $prof = 0) {
20 $nom = $struct->id_boucle
;
21 $avant = decompiler_($struct->avant
, $fmt, $prof);
22 $apres = decompiler_($struct->apres
, $fmt, $prof);
23 $altern = decompiler_($struct->altern
, $fmt, $prof);
24 $milieu = decompiler_($struct->milieu
, $fmt, $prof);
26 $type = $struct->sql_serveur ?
"$struct->sql_serveur:" : '';
27 $type .= ($struct->type_requete ?
$struct->type_requete
:
28 $struct->table_optionnelle
);
30 if ($struct->jointures_explicites
) {
31 $type .= " " . $struct->jointures_explicites
;
33 if ($struct->table_optionnelle
) {
36 // Revoir le cas de la boucle recursive
38 $crit = $struct->param
;
39 if ($crit and !is_array($crit[0])) {
40 $type = strtolower($type) . array_shift($crit);
42 $crit = decompiler_criteres($struct, $fmt, $prof);
44 $f = 'format_boucle_' . $fmt;
46 return $f($avant, $nom, $type, $crit, $milieu, $apres, $altern, $prof);
49 function decompiler_include($struct, $fmt = '', $prof = 0) {
51 foreach ($struct->param ?
$struct->param
: array() as $couple) {
53 foreach ($couple as $v) {
54 $res[] = decompiler_($v, $fmt, $prof);
57 $file = is_string($struct->texte
) ?
$struct->texte
:
58 decompiler_($struct->texte
, $fmt, $prof);
59 $f = 'format_inclure_' . $fmt;
61 return $f($file, $res, $prof);
64 function decompiler_texte($struct, $fmt = '', $prof = 0) {
65 $f = 'format_texte_' . $fmt;
67 return strlen($struct->texte
) ?
$f($struct->texte
, $prof) : '';
70 function decompiler_polyglotte($struct, $fmt = '', $prof = 0) {
71 $f = 'format_polyglotte_' . $fmt;
73 return $f($struct->traductions
, $prof);
76 function decompiler_idiome($struct, $fmt = '', $prof = 0) {
78 foreach ($struct->arg
as $k => $v) {
79 $args[$k] = public_decompiler($v, $fmt, $prof);
82 $filtres = decompiler_liste($struct->param
, $fmt, $prof);
84 $f = 'format_idiome_' . $fmt;
86 return $f($struct->nom_champ
, $struct->module
, $args, $filtres, $prof);
89 function decompiler_champ($struct, $fmt = '', $prof = 0) {
90 $avant = decompiler_($struct->avant
, $fmt, $prof);
91 $apres = decompiler_($struct->apres
, $fmt, $prof);
92 $args = $filtres = '';
93 if ($p = $struct->param
) {
94 if ($p[0][0] === '') {
95 $args = decompiler_liste(array(array_shift($p)), $fmt, $prof);
97 $filtres = decompiler_liste($p, $fmt, $prof);
99 $f = 'format_champ_' . $fmt;
101 return $f($struct->nom_champ
, $struct->nom_boucle
, $struct->etoile
, $avant, $apres, $args, $filtres, $prof);
104 function decompiler_liste($sources, $fmt = '', $prof = 0) {
105 if (!is_array($sources)) {
108 $f = 'format_liste_' . $fmt;
110 foreach ($sources as $arg) {
111 if (!is_array($arg)) {
112 continue; // ne devrait pas arriver.
114 $r = array_shift($arg);
117 foreach ($arg as $v) {
118 // cas des arguments entoures de ' ou "
120 and $v[0]->type
== 'texte'
121 and (strlen($v[0]->apres
) == 1)
122 and $v[0]->apres
== $v[0]->avant
124 $args[] = $v[0]->avant
. $v[0]->texte
. $v[0]->apres
;
126 $args[] = decompiler_($v, $fmt, 0 - $prof);
129 if (($r !== '') or $args) {
130 $res .= $f($r, $args, $prof);
137 // Decompilation des criteres: on triche et on deroge:
138 // - le phraseur fournit un bout du source en plus de la compil
139 // - le champ apres signale le critere {"separateur"} ou {'separateur'}
140 // - les champs sont implicitement etendus (crochets implicites mais interdits)
141 function decompiler_criteres($boucle, $fmt = '', $prof = 0) {
142 $sources = $boucle->param
;
143 if (!is_array($sources)) {
147 $f = 'format_critere_' . $fmt;
148 foreach ($sources as $crit) {
149 if (!is_array($crit)) {
151 } // boucle recursive
154 foreach ($crit as $i => $v) {
156 and $v[0]->type
== 'texte'
159 $args[] = array(array('texte', ($v[0]->apres
. $v[0]->texte
. $v[0]->apres
)));
162 foreach ($v as $k => $p) {
164 and function_exists($d = 'decompiler_' . $p->type
)
166 $r = $d($p, $fmt, (0 - $prof), @$v[$k +
1]);
167 $res2[] = array($p->type
, $r);
169 spip_log("critere $i / $k mal forme");
182 function decompiler_($liste, $fmt = '', $prof = 0) {
183 if (!is_array($liste)) {
186 $prof2 = ($prof < 0) ?
($prof - 1) : ($prof +
1);
188 foreach ($liste as $k => $p) {
189 if (!isset($p->type
)) {
192 $d = 'decompiler_' . $p->type
;
193 $next = isset($liste[$k +
1]) ?
$liste[$k +
1] : false;
194 // Forcer le champ etendu si son source (pas les reecritures)
195 // contenait des args et s'il est suivi d'espaces,
196 // le champ simple les eliminant est un bug helas perenne.
199 and ($next->type
== 'texte')
200 and $p->type
== 'champ'
205 $n = strlen($next->texte
) - strlen(ltrim($next->texte
));
208 $champ->texte
= substr($next->texte
, 0, $n);
209 $champ->ligne
= $p->ligne
;
210 $p->apres
= array($champ);
211 $next->texte
= substr($next->texte
, $n);
214 $contenu[] = array($d($p, $fmt, $prof2), $p->type
);
217 $f = 'format_suite_' . $fmt;
222 function public_decompiler($liste, $fmt = '', $prof = 0, $quoi = '') {
223 if (!include_spip('public/format_' . $fmt)) {
226 $f = 'decompiler_' . $quoi;
228 return $f($liste, $fmt, $prof);