[SPIP] +installation version 3.0.10
[lhc/web/www.git] / www / ecrire / public / decompiler.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2012 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
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 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) return;
14
15 // Decompilation de l'arbre de syntaxe abstraite d'un squelette SPIP
16
17 function decompiler_boucle($struct, $fmt='', $prof=0)
18 {
19 $nom = $struct->id_boucle;
20 $avant = decompiler_($struct->avant, $fmt, $prof);
21 $apres = decompiler_($struct->apres, $fmt, $prof);
22 $altern = decompiler_($struct->altern, $fmt, $prof);
23 $milieu = decompiler_($struct->milieu, $fmt, $prof);
24
25 $type = $struct->sql_serveur ? "$struct->sql_serveur:" : '';
26 $type .= ($struct->type_requete ? $struct->type_requete :
27 $struct->table_optionnelle);
28
29 if ($struct->jointures_explicites)
30 $type .= " " . $struct->jointures_explicites;
31 if ($struct->table_optionnelle)
32 $type .= "?";
33 // Revoir le cas de la boucle recursive
34
35 $crit = $struct->param;
36 if ($crit AND !is_array($crit[0])) {
37 $type = strtolower($type) . array_shift($crit);
38 }
39 $crit = decompiler_criteres($struct, $fmt, $prof) ;
40
41 $f = 'format_boucle_' . $fmt;
42 return $f($avant, $nom, $type, $crit, $milieu, $apres, $altern, $prof);
43 }
44
45 function decompiler_include($struct, $fmt='', $prof=0)
46 {
47 $res = array();
48 foreach($struct->param ? $struct->param : array() as $couple) {
49 array_shift($couple);
50 foreach($couple as $v) {
51 $res[]= decompiler_($v, $fmt, $prof);
52 }
53 }
54 $file = is_string($struct->texte) ? $struct->texte :
55 decompiler_($struct->texte, $fmt, $prof);
56 $f = 'format_inclure_' . $fmt;
57 return $f($file, $res, $prof);
58 }
59
60 function decompiler_texte($struct, $fmt='', $prof=0)
61 {
62 $f = 'format_texte_' . $fmt;
63 return strlen($struct->texte) ? $f($struct->texte, $prof) : '';
64 }
65
66 function decompiler_polyglotte($struct, $fmt='', $prof=0)
67 {
68 $f = 'format_polyglotte_' . $fmt;
69 return $f($struct->traductions, $prof);
70 }
71
72 function decompiler_idiome($struct, $fmt='', $prof=0)
73 {
74 $module = ($struct->module == MODULES_IDIOMES)? ''
75 : $struct->module;
76
77 $args = array();
78 foreach ($struct->arg as $k => $v) {
79 if ($k) $args[$k]= decompiler_($v, $fmt, $prof);
80 }
81
82 $filtres = decompiler_liste($struct->param, $fmt, $prof);
83
84 $f = 'format_idiome_' . $fmt;
85 return $f($struct->nom_champ, $module, $args, $filtres, $prof);
86 }
87
88 function decompiler_champ($struct, $fmt='', $prof=0)
89 {
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);
96 $filtres = decompiler_liste($p, $fmt, $prof);
97 }
98 $f = 'format_champ_' . $fmt;
99 return $f($struct->nom_champ, $struct->nom_boucle, $struct->etoile, $avant, $apres, $args, $filtres, $prof);
100 }
101
102 function decompiler_liste($sources, $fmt='', $prof=0) {
103 if (!is_array($sources)) return '';
104 $f = 'format_liste_' . $fmt;
105 $res = '';
106 foreach($sources as $arg) {
107 if (!is_array($arg)) {
108 continue; // ne devrait pas arriver.
109 } else {$r = array_shift($arg);}
110 $args = array();
111 foreach($arg as $v) {
112 // cas des arguments entoures de ' ou "
113 if ((count($v) == 1)
114 AND $v[0]->type=='texte'
115 AND (strlen($v[0]->apres) == 1)
116 AND $v[0]->apres == $v[0]->avant)
117 $args[]= $v[0]->avant . $v[0]->texte . $v[0]->apres;
118 else $args[]= decompiler_($v, $fmt, 0-$prof);
119 }
120 if (($r!=='') OR $args) $res .= $f($r, $args, $prof);
121 }
122 return $res;
123 }
124
125 // Decompilation des criteres: on triche et on deroge:
126 // - le phraseur fournit un bout du source en plus de la compil
127 // - le champ apres signale le critere {"separateur"} ou {'separateur'}
128 // - les champs sont implicitement etendus (crochets implicites mais interdits)
129 function decompiler_criteres($boucle, $fmt='', $prof=0) {
130 $sources = $boucle->param;
131 if (!is_array($sources)) return '';
132 $res = '';
133 $f = 'format_critere_' . $fmt;
134 foreach($sources as $crit) {
135 if (!is_array($crit)) continue; // boucle recursive
136 array_shift($crit);
137 $args = array();
138 foreach($crit as $i => $v) {
139 if ((count($v) == 1)
140 AND $v[0]->type=='texte'
141 AND $v[0]->apres)
142 $args[]= array(array('texte', ( $v[0]->apres . $v[0]->texte . $v[0]->apres)));
143 else {
144 $res2 = array();
145 foreach($v as $k => $p) {
146 if (isset($p->type)
147 AND function_exists($d = 'decompiler_' . $p->type)) {
148 $r = $d($p, $fmt, (0-$prof), @$v[$k+1]);
149 $res2[]= array($p->type, $r);
150 } else spip_log("critere $i / $k mal forme");
151 }
152 $args[]= $res2;
153 }
154 }
155 $res .= $f($args);
156 }
157 return $res;
158 }
159
160
161 function decompiler_($liste, $fmt='', $prof=0)
162 {
163 if (!is_array($liste)) return '';
164 $prof2 = ($prof < 0) ? ($prof-1) : ($prof+1);
165 $contenu = array();
166 foreach($liste as $k => $p) {
167 if (!isset($p->type)) continue; #??????
168 $d = 'decompiler_' . $p->type;
169 $next = isset($liste[$k+1]) ? $liste[$k+1] : false;
170 // Forcer le champ etendu si son source (pas les reecritures)
171 // contenait des args et s'il est suivi d'espaces,
172 // le champ simple les eliminant est un bug helas perenne.
173
174 if ($next
175 AND ($next->type == 'texte')
176 AND $p->type == 'champ'
177 AND !$p->apres
178 AND !$p->avant
179 AND $p->fonctions) {
180 $n = strlen($next->texte) - strlen(ltrim($next->texte));
181 if ($n) {
182 $champ = new Texte;
183 $champ->texte = substr($next->texte, 0, $n);
184 $champ->ligne = $p->ligne;
185 $p->apres = array($champ);
186 $next->texte = substr($next->texte, $n);
187 }
188 }
189 $contenu[] = array($d($p, $fmt, $prof2), $p->type);
190
191 }
192 $f = 'format_suite_' . $fmt;
193 return $f($contenu);
194 }
195
196 function public_decompiler($liste, $fmt='', $prof=0, $quoi='')
197 {
198 if (!include_spip('public/format_' . $fmt)) return "'$fmt'?";
199 $f = 'decompiler_' . $quoi;
200 return $f($liste, $fmt, $prof);
201 }
202 ?>