1 | <?php |
---|
2 | // +-------------------------------------------------+ |
---|
3 | // © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net) |
---|
4 | // +-------------------------------------------------+ |
---|
5 | // $Id: xmltransform.php,v 1.25 2013-04-17 08:37:34 mbertin Exp $ |
---|
6 | |
---|
7 | if (stristr($_SERVER['REQUEST_URI'], "xmltransform.php")) die("no access"); |
---|
8 | |
---|
9 | if (version_compare(PHP_VERSION,'5','>=') && extension_loaded('xsl')) { |
---|
10 | if (substr(phpversion(), 0, 1) == "5") @ini_set("zend.ze1_compatibility_mode", "0"); |
---|
11 | require_once($include_path.'/xslt-php4-to-php5.inc.php'); |
---|
12 | } |
---|
13 | |
---|
14 | //Bibliothèque des transformations par défaut |
---|
15 | |
---|
16 | require_once ("$base_path/admin/convert/xml_unimarc.class.php"); |
---|
17 | |
---|
18 | //Conversion par une feuille de style XSLT |
---|
19 | function perform_xslt($xml, $s, $islast, $isfirst, $param_path) { |
---|
20 | global $base_path, $charset; |
---|
21 | $transform="$base_path/admin/convert/imports/".$param_path."/".$s['XSLFILE'][0]['value']; |
---|
22 | |
---|
23 | //Si c'est la première transformation, on rajoute les entêtes |
---|
24 | if ($isfirst) { |
---|
25 | if($s['ENCODING']){ |
---|
26 | $xml1 = "<?xml version=\"1.0\" encoding=\"".$s['ENCODING']."\"?>\n<".$s['ROOTELEMENT'][0]["value"]; |
---|
27 | }else{ |
---|
28 | $xml1 = "<?xml version=\"1.0\" encoding=\"$charset\"?>\n<".$s['ROOTELEMENT'][0]["value"]; |
---|
29 | } |
---|
30 | |
---|
31 | if ($s["NAMESPACE"]) { |
---|
32 | $xml1.=" xmlns:".$s["NAMESPACE"][0]["ID"]."='".$s["NAMESPACE"][0]["value"]."' "; |
---|
33 | } |
---|
34 | $xml1.=">\n".$xml."\n</".$s['ROOTELEMENT'][0]['value'].">"; |
---|
35 | $xml=$xml1; |
---|
36 | } |
---|
37 | $f = fopen($transform, "r"); |
---|
38 | $xsl = fread($f, filesize($transform)); |
---|
39 | fclose($f); |
---|
40 | |
---|
41 | //Création du processeur |
---|
42 | $xh = xslt_create(); |
---|
43 | |
---|
44 | //Encodage = $charset |
---|
45 | if (defined("ICONV_IMPL")) { |
---|
46 | xslt_set_encoding($xh, "$charset"); |
---|
47 | } |
---|
48 | |
---|
49 | // Traite le document |
---|
50 | if ($result = @xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, array("/_xml" => $xml, "/_xsl" => $xsl))) { |
---|
51 | $r['VALID']=true; |
---|
52 | $r['DATA']=$result; |
---|
53 | $r['ERROR']=""; |
---|
54 | //Si c'est la dernière transformation, on supprime les entêtes et l'élément root |
---|
55 | if ($islast) { |
---|
56 | $p = preg_match("/<".$s['TNOTICEELEMENT'][0]['value']."(?:\ [^>]*|)>/", $r["DATA"], $m, PREG_OFFSET_CAPTURE); |
---|
57 | if ($p) { |
---|
58 | $r['DATA'] = " ".substr($r['DATA'], $m[0][1]); |
---|
59 | } |
---|
60 | $p1 = 0; |
---|
61 | $p = 0; |
---|
62 | while ($p!==false) { |
---|
63 | $p1 = $p; |
---|
64 | $p = @strpos($r['DATA'], "</".$s['TNOTICEELEMENT'][0]['value'].">",$p1+strlen("</".$s['TNOTICEELEMENT'][0]['value'].">")); |
---|
65 | } |
---|
66 | if (($p1 !== false)&&($p1!=0)) { |
---|
67 | $r['DATA'] = substr($r['DATA'], 0, $p1+strlen($s['TNOTICEELEMENT'][0]['value'])+3)."\n"; |
---|
68 | } |
---|
69 | } |
---|
70 | } else { |
---|
71 | $r['VALID']=false; |
---|
72 | $r['DATA']=""; |
---|
73 | $r['ERROR']="Sorry, notice could not be transformed by $transform the reason is that ".xslt_error($xh)." and the error code is ".xslt_errno($xh); |
---|
74 | } |
---|
75 | |
---|
76 | xslt_free($xh); |
---|
77 | return $r; |
---|
78 | } |
---|
79 | |
---|
80 | //Conversion XML en iso2709 |
---|
81 | function toiso($notice, $s, $islast, $isfirst, $param_path) { |
---|
82 | $x2i = new xml_unimarc(); |
---|
83 | $x2i -> XMLtoiso2709_notice($notice,$s['ENCODING']); |
---|
84 | if($x2i->warning_msg[0]){ |
---|
85 | $r['WARNING']=$x2i->warning_msg[0]; |
---|
86 | } |
---|
87 | if ($x2i->n_valid==0) { |
---|
88 | $r['VALID']=false; |
---|
89 | $r['DATA']=""; |
---|
90 | $r['ERROR']=$x2i->error_msg[0]; |
---|
91 | } else { |
---|
92 | $r['VALID']=true; |
---|
93 | $r['DATA']=$x2i->notices_[0]; |
---|
94 | $r['ERROR']=""; |
---|
95 | } |
---|
96 | return $r; |
---|
97 | } |
---|
98 | |
---|
99 | //Consersion iso2709 en XML |
---|
100 | function isotoxml($notice, $s, $islast, $isfirst, $param_path) { |
---|
101 | global $charset; |
---|
102 | global $output_params; |
---|
103 | $i2x = new xml_unimarc(); |
---|
104 | $i2x->iso2709toXML_notice($notice,$s['FORMAT']); |
---|
105 | if ($i2x->n_valid == 0) { |
---|
106 | $r['VALID']=false; |
---|
107 | $r['DATA']=""; |
---|
108 | $r['ERROR']=$i2x->error_msg[0]; |
---|
109 | } else { |
---|
110 | $r['VALID']=true; |
---|
111 | $r['DATA']=$i2x->notices_xml_[0]; |
---|
112 | $r['ERROR']=""; |
---|
113 | //Si ce n'est pas la dernière transformation, on rajoute des tags root et l'entête |
---|
114 | if (!$islast) { |
---|
115 | $r['DATA'] = "<".$s['TROOTELEMENT'][0]['value'].">\n".$r['DATA']; |
---|
116 | $r['DATA'].= "</".$s['TROOTELEMENT'][0]['value'].">"; |
---|
117 | $r['DATA'] = "<?xml version=\"1.0\" encoding=\"".($i2x->is_utf8?"utf-8":$charset)."\" ?>\n".$r['DATA']; |
---|
118 | } |
---|
119 | } |
---|
120 | return $r; |
---|
121 | } |
---|
122 | |
---|
123 | //Conversion texte en XML |
---|
124 | function texttoxml($notice, $s, $islast, $isfirst, $param_path) { |
---|
125 | global $cols, $charset; |
---|
126 | |
---|
127 | eval("\$spt=\"".$s["SEPARATOR"][0]["value"]."\";"); |
---|
128 | $fields=explode($spt,$notice); |
---|
129 | |
---|
130 | //Recherche du type doc |
---|
131 | if ($s["COLS"][0]["DT"]) { |
---|
132 | if ($s["COLS"][0]["DT"][0]["CORRESP"][0]) { |
---|
133 | $corresp=$s["COLS"][0]["DT"][0]["CORRESP"][0]; |
---|
134 | $f_id=$fields[($corresp["ID"]-1)]; |
---|
135 | if ($s["DELIMITEDBY"][0]["value"]) { |
---|
136 | $f_id=trim($f_id,$s["DELIMITEDBY"][0]["value"]); |
---|
137 | } |
---|
138 | for ($i=0; $i<count($corresp["FOR"]); $i++) { |
---|
139 | if ($corresp["FOR"][$i]["ID"]==$f_id) { |
---|
140 | $dt=$corresp["FOR"][$i]["value"]; |
---|
141 | break; |
---|
142 | } |
---|
143 | } |
---|
144 | } else $dt=$s["COLS"][0]["DT"][0]["value"]; |
---|
145 | } |
---|
146 | |
---|
147 | //Recherche du bl |
---|
148 | if ($s["COLS"][0]["BL"]) { |
---|
149 | if ($s["COLS"][0]["BL"][0]["CORRESP"][0]) { |
---|
150 | $corresp=$s["COLS"][0]["BL"][0]["CORRESP"][0]; |
---|
151 | $f_id=$fields[($corresp["ID"]-1)]; |
---|
152 | if ($s["DELIMITEDBY"][0]["value"]) { |
---|
153 | $f_id=trim($f_id,$s["DELIMITEDBY"][0]["value"]); |
---|
154 | } |
---|
155 | for ($i=0; $i<count($corresp["FOR"]); $i++) { |
---|
156 | if ($corresp["FOR"][$i]["ID"]==$f_id) { |
---|
157 | $bl=$corresp["FOR"][$i]["value"]; |
---|
158 | break; |
---|
159 | } |
---|
160 | } |
---|
161 | } else { |
---|
162 | $bl=$s["COLS"][0]["BL"][0]["value"]; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | //Recherche du type hl |
---|
167 | if ($s["COLS"][0]["HL"]) { |
---|
168 | if ($s["COLS"][0]["HL"][0]["CORRESP"][0]) { |
---|
169 | $corresp=$s["COLS"][0]["HL"][0]["CORRESP"][0]; |
---|
170 | $f_id=$fields[($corresp["ID"]-1)]; |
---|
171 | if ($s["DELIMITEDBY"][0]["value"]) { |
---|
172 | $f_id=trim($f_id,$s["DELIMITEDBY"][0]["value"]); |
---|
173 | } |
---|
174 | for ($i=0; $i<count($corresp["FOR"]); $i++) { |
---|
175 | if ($corresp["FOR"][$i]["ID"]==$f_id) { |
---|
176 | $hl=$corresp["FOR"][$i]["value"]; |
---|
177 | break; |
---|
178 | } |
---|
179 | } |
---|
180 | } else { |
---|
181 | $hl=$s["COLS"][0]["HL"][0]["value"]; |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | if (!$cols) { |
---|
186 | for ($j=0; $j<count($s["COLS"][0]["COL"]); $j++) { |
---|
187 | $cols[$j]=$s["COLS"][0]["COL"][$j]; |
---|
188 | //$cols[$s["COLS"][0]["COL"][$j]["ID"]]=$s["COLS"][0]["COL"][$j]; |
---|
189 | } |
---|
190 | } |
---|
191 | $param=array(); |
---|
192 | $param["rs"][0]["value"]="n"; |
---|
193 | $param["dt"][0]["value"]=($dt?$dt:"a"); |
---|
194 | $param["bl"][0]["value"]=($bl?$bl:"m"); |
---|
195 | $param["hl"][0]["value"]=($hl?$hl:"*"); |
---|
196 | $param["el"][0]["value"]="1"; |
---|
197 | $param["ru"][0]["value"]="i"; |
---|
198 | |
---|
199 | //Pour chaque colonne |
---|
200 | for ($i=0; $i<count($cols); $i++) { |
---|
201 | //Récupération des id |
---|
202 | $ids=explode(",",$cols[$i]["ID"]); |
---|
203 | |
---|
204 | //Correspondances |
---|
205 | for ($j=0; $j<count($cols[$i]["CORRESP"]); $j++) { |
---|
206 | $corresp[$cols[$i]["CORRESP"][$j]["ID"]]=array(); |
---|
207 | $corresp_table=$cols[$i]["CORRESP"][$j]["FOR"]; |
---|
208 | for ($k=0; $k<count($corresp_table); $k++) { |
---|
209 | $corresp[$cols[$i]["CORRESP"][$j]["ID"]][$corresp_table[$k]["ID"]]=$corresp_table[$k]["value"]; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | //print_r($corresp); |
---|
214 | |
---|
215 | //Séparateurs pour répétition |
---|
216 | for ($j=0; $j<count($cols[$i]["REP"]); $j++) { |
---|
217 | if ($cols[$i]["REP"][$j]["FOR"]=="field") |
---|
218 | $rep_field[$cols[$i]["REP"][$j]["ID"]]=$cols[$i]["REP"][$j]["value"]; |
---|
219 | else |
---|
220 | $rep_subfield[$cols[$i]["REP"][$j]["ID"]]=$cols[$i]["REP"][$j]["value"]; |
---|
221 | } |
---|
222 | $max=1; |
---|
223 | for ($j=0; $j<count($ids); $j++) { |
---|
224 | if ($ids[$j][0]=="'") |
---|
225 | $vpte=trim($ids[$j],"'"); |
---|
226 | else { |
---|
227 | if ($s["DELIMITEDBY"][0]["value"]) { |
---|
228 | $fields[$ids[$j]-1]=trim($fields[$ids[$j]-1],$s["DELIMITEDBY"][0]["value"]); |
---|
229 | } |
---|
230 | if ($s["ESCAPED"][0][value]=="yes") { |
---|
231 | $fields[$ids[$j]-1]=stripslashes($fields[$ids[$j]-1]); |
---|
232 | } |
---|
233 | $vpte=$fields[$ids[$j]-1]; |
---|
234 | } |
---|
235 | if ($rep_field[$ids[$j]]) { |
---|
236 | $vput[$ids[$j]]=explode($rep_field[$ids[$j]],$vpte); |
---|
237 | if ($max<count($vput[$ids[$j]])) $max=count($vput[$ids[$j]]); |
---|
238 | } else $vpt[$j]=$vpte; |
---|
239 | } |
---|
240 | for ($j=0; $j<count($ids); $j++) { |
---|
241 | if (!$rep_field[$ids[$j]]) { |
---|
242 | for ($k=0; $k<$max; $k++) { |
---|
243 | $vput[$ids[$j]][$k]=$vpt[$j]; |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | for ($z=0; $z<$max; $z++) { |
---|
248 | $f=array(); |
---|
249 | $f["c"]=$cols[$i]["FIELD"][0]["value"]; |
---|
250 | $f["ind"]=$cols[$i]["IND"][0]["value"]; |
---|
251 | if ($f["ind"]=="") $f["ind"]=" "; |
---|
252 | $subfields=explode(",",$cols[$i]["SUBFIELD"][0]["value"]); |
---|
253 | for ($j=0; $j<count($cols[$i]["SEP"]) ;$j++) { |
---|
254 | $sep[$cols[$i]["SEP"][$j]["ID"]]=$cols[$i]["SEP"][$j]["value"]; |
---|
255 | } |
---|
256 | //$rep_sub=$cols[$i]["SUBFIELD"][0]["REP"]; |
---|
257 | //$rep_field=$cols[$i]["FIELD"][0]["REP"]; |
---|
258 | for ($j=0; $j<count($ids); $j++) { |
---|
259 | $vprsf=array(); |
---|
260 | if ($cols[$i]["SUBFIELD"][0]["value"]) { |
---|
261 | if ($sep[$ids[$j]]) { |
---|
262 | if ($rep_subfield[$ids[$j]]) { |
---|
263 | $vprsf=explode($rep_subfield[$ids[$j]],$vput[$ids[$j]][$z]); |
---|
264 | } else { |
---|
265 | $vprsf[0]=$vput[$ids[$j]][$z]; |
---|
266 | } |
---|
267 | for ($x=0; $x<count($vprsf); $x++) { |
---|
268 | $sfv=explode($sep[$ids[$j]],$vprsf[$x]); |
---|
269 | $sf=explode(";",$subfields[$j]); |
---|
270 | for ($k=0; $k<count($sf); $k++) { |
---|
271 | if ($sf[$k]) { |
---|
272 | $nf=count($f["s"]); |
---|
273 | if ($sfv[$k]) { |
---|
274 | if ($corresp[$ids[$j]]) $sfv[$k]=$corresp[$ids[$j]][trim($sfv[$k])]; |
---|
275 | $f["s"][$nf]["c"]=$sf[$k]; |
---|
276 | $f["s"][$nf]["value"]=htmlspecialchars(trim($sfv[$k]),ENT_QUOTES,$charset); |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | } else { |
---|
282 | if ($rep_subfield[$ids[$j]]) { |
---|
283 | $vprsf=explode($rep_subfield[$ids[$j]],$vput[$ids[$j]][$z]); |
---|
284 | } else { |
---|
285 | $vprsf[0]=$vput[$ids[$j]][$z]; |
---|
286 | } |
---|
287 | for ($x=0; $x<count($vprsf) ;$x++) { |
---|
288 | if ($vprsf[$x]) { |
---|
289 | |
---|
290 | if ($corresp[$ids[$j]]) $vprsf[$x]=$corresp[$ids[$j]][trim($vprsf[$x])]; |
---|
291 | $nf=count($f["s"]); |
---|
292 | $f["s"][$nf]["c"]=$subfields[$j]; |
---|
293 | $f["s"][$nf]["value"]=htmlspecialchars(trim($vprsf[$x]),ENT_QUOTES,$charset); |
---|
294 | } |
---|
295 | } |
---|
296 | } |
---|
297 | } else if ($vput[$ids[$j]][$z]) { |
---|
298 | if ($corresp[$ids[$j]]) $vput[$ids[$j]][$z]=$corresp[$ids[$j]][trim($vput[$ids[$j]][$z])]; |
---|
299 | $f["value"]=htmlspecialchars(trim($vput[$ids[$j]][$z]),ENT_QUOTES,$charset); |
---|
300 | } |
---|
301 | } |
---|
302 | if (($f["s"])||($f["value"])) |
---|
303 | $param["f"][]=$f; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | /*for ($i=0; $i<count($fields); $i++) { |
---|
308 | $f=array(); |
---|
309 | if ($s["DELIMITEDBY"][0]["value"]) { |
---|
310 | $fields[$i]=trim($fields[$i],$s["DELIMITEDBY"][0]["value"]); |
---|
311 | } |
---|
312 | if ($s["ESCAPED"][0][value]=="yes") { |
---|
313 | $fields[$i]=stripslashes($fields[$i]); |
---|
314 | } |
---|
315 | $f["c"]=$cols[$i+1]["FIELD"][0]["value"]; |
---|
316 | $f["ind"]=$cols[$i+1]["IND"][0]["value"]; |
---|
317 | if ($f["ind"]=="") $f["ind"]=" "; |
---|
318 | if ($cols[$i+1]["SUBFIELD"][0]["value"]) { |
---|
319 | $f["s"][0]["c"]=$cols[$i+1]["SUBFIELD"][0]["value"]; |
---|
320 | $f["s"][0]["value"]="<![CDATA[".$fields[$i]."]]>"; |
---|
321 | } else { |
---|
322 | $f["value"]=$fields[$i]; |
---|
323 | } |
---|
324 | $param["f"][]=$f; |
---|
325 | }*/ |
---|
326 | $r['DATA']=@array_to_xml($param,"notice"); |
---|
327 | if ($r['DATA']) { |
---|
328 | //Si ce n'est pas la dernière transformation, on rajoute des tags root et l'entête |
---|
329 | if (!$islast) { |
---|
330 | $r['DATA'] = "<".$s['TROOTELEMENT'][0]['value'].">\n".$r['DATA']; |
---|
331 | $r['DATA'].= "</".$s['TROOTELEMENT'][0]['value'].">"; |
---|
332 | $r['DATA'] = "<?xml version=\"1.0\" encoding=\"$charset\" ?>\n".$r['DATA']; |
---|
333 | } |
---|
334 | $r['VALID']=true; |
---|
335 | $r['ERROR']=""; |
---|
336 | } else { |
---|
337 | $r['VALID']=false; |
---|
338 | $r['ERROR']="Can't convert to XML line ".$notice; |
---|
339 | } |
---|
340 | return $r; |
---|
341 | } |
---|