1 | <?php |
---|
2 | // +-------------------------------------------------+ |
---|
3 | // © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net) |
---|
4 | // +-------------------------------------------------+ |
---|
5 | // $Id: start_import.php,v 1.22 2015-04-03 11:16:22 jpermanne Exp $ |
---|
6 | |
---|
7 | //Execution de l'import |
---|
8 | $base_path = "../.."; |
---|
9 | $base_auth = "ADMINISTRATION_AUTH|CATALOGAGE_AUTH"; |
---|
10 | $base_title = "\$msg[ie_import_running]"; |
---|
11 | require ($base_path."/includes/init.inc.php"); |
---|
12 | require_once ("$include_path/parser.inc.php"); |
---|
13 | |
---|
14 | //Gestion de l'encodage du fichier d'import |
---|
15 | if(isset($encodage_fic_source)){ |
---|
16 | $_SESSION["encodage_fic_source"]=$encodage_fic_source; |
---|
17 | }elseif($_SESSION["encodage_fic_source"]){ |
---|
18 | $encodage_fic_source=$_SESSION["encodage_fic_source"]; |
---|
19 | } |
---|
20 | |
---|
21 | //Récupération du chemin du fichier de paramétrage de l'import |
---|
22 | function _item_($param) { |
---|
23 | global $import_type; |
---|
24 | global $i; |
---|
25 | global $param_path; |
---|
26 | global $import_type_l; |
---|
27 | |
---|
28 | if (($i == $import_type) || ($import_type == $param['PATH'])) { |
---|
29 | $param_path = $param['PATH']; |
---|
30 | $import_type_l = $param['NAME']; |
---|
31 | } |
---|
32 | $i ++; |
---|
33 | } |
---|
34 | |
---|
35 | //Récupération du nom de l'import |
---|
36 | function _import_name_($param) { |
---|
37 | global $import_name; |
---|
38 | |
---|
39 | $import_name = $param['value']; |
---|
40 | } |
---|
41 | |
---|
42 | //Récupération du nombre de notices à traiter par passe |
---|
43 | function _n_per_pass_($param) { |
---|
44 | global $n_per_pass; |
---|
45 | |
---|
46 | $n_per_pass = $param['value']; |
---|
47 | } |
---|
48 | |
---|
49 | //Récupération du type d'entrée |
---|
50 | function _input_($param) { |
---|
51 | global $input_type; |
---|
52 | global $input_params; |
---|
53 | |
---|
54 | $input_type = $param['TYPE']; |
---|
55 | $input_params = $param; |
---|
56 | } |
---|
57 | |
---|
58 | //Récupération des étapes de conversion |
---|
59 | function _step_($param) { |
---|
60 | global $step; |
---|
61 | |
---|
62 | $step[] = $param; |
---|
63 | } |
---|
64 | |
---|
65 | //Récupération du paramètre d'import |
---|
66 | function _output_($param) { |
---|
67 | global $output; |
---|
68 | global $output_type; |
---|
69 | global $output_params; |
---|
70 | |
---|
71 | $output = $param['IMPORTABLE']; |
---|
72 | $output_type = $param['TYPE']; |
---|
73 | $output_params=$param; |
---|
74 | } |
---|
75 | |
---|
76 | //Lecture des paramètres d'import |
---|
77 | |
---|
78 | //Récupération du répertoire |
---|
79 | $i = 0; |
---|
80 | $param_path = ""; |
---|
81 | |
---|
82 | if (file_exists("imports/catalog_subst.xml")) |
---|
83 | $fic_catal = "imports/catalog_subst.xml"; |
---|
84 | else |
---|
85 | $fic_catal = "imports/catalog.xml"; |
---|
86 | |
---|
87 | _parser_($fic_catal, array("ITEM" => "_item_"), "CATALOG"); |
---|
88 | |
---|
89 | //Lecture des paramètres |
---|
90 | _parser_("imports/".$param_path."/params.xml", array("IMPORTNAME" => "_import_name_", "NPERPASS" => "_n_per_pass_", "INPUT" => "_input_", "STEP" => "_step_", "OUTPUT" => "_output_"), "PARAMS"); |
---|
91 | |
---|
92 | //Inclusion des librairies éventuelles |
---|
93 | for ($i = 0; $i < count($step); $i ++) { |
---|
94 | if ($step[$i]['TYPE'] == "custom") { |
---|
95 | //echo "imports/".$param_path."/".$step[$i][SCRIPT][0][value]; |
---|
96 | require_once ("imports/".$param_path."/".$step[$i]['SCRIPT'][0]['value']); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | require_once ("xmltransform.php"); |
---|
101 | |
---|
102 | //En fonction du type de fichier d'entrée, inclusion du script de gestion des entrées |
---|
103 | switch ($input_type) { |
---|
104 | case "xml" : |
---|
105 | require_once ("imports/input_xml.inc.php"); |
---|
106 | break; |
---|
107 | case "iso_2709" : |
---|
108 | require_once ("imports/input_iso_2709.inc.php"); |
---|
109 | break; |
---|
110 | case "text" : |
---|
111 | require_once("imports/input_text.inc.php"); |
---|
112 | break; |
---|
113 | case "custom" : |
---|
114 | require_once ("imports/$param_path/".$input_params['SCRIPT']); |
---|
115 | break; |
---|
116 | default : |
---|
117 | die($msg["ie_import_entry_not_valid"]); |
---|
118 | } |
---|
119 | |
---|
120 | //En fonction du type de fichier de sortie, inclusion du script de gestion des sorties |
---|
121 | switch ($output_type) { |
---|
122 | case "xml" : |
---|
123 | require_once ("imports/output_xml.inc.php"); |
---|
124 | break; |
---|
125 | case "txt" : |
---|
126 | require_once ("imports/output_txt.inc.php"); |
---|
127 | break; |
---|
128 | case "iso_2709" : |
---|
129 | require_once("imports/output_iso_2709.inc.php"); |
---|
130 | break; |
---|
131 | case "custom" : |
---|
132 | require_once ("imports/$param_path/".$output_params['SCRIPT']); |
---|
133 | break; |
---|
134 | default : |
---|
135 | die($msg["ie_output_type_not_valid"]); |
---|
136 | } |
---|
137 | |
---|
138 | //Si premier accès |
---|
139 | if (!$first) { |
---|
140 | $origine=str_replace(" ","",microtime()); |
---|
141 | $origine=str_replace("0.","",$origine); |
---|
142 | |
---|
143 | //Copie du fichier dans le répertoire temporaire |
---|
144 | if ($_FILES['import_file']['name']) { |
---|
145 | if (!@ copy($_FILES['import_file']['tmp_name'], "$base_path/temp/".$origine.$_FILES['import_file']['name'])) { |
---|
146 | error_message_history($msg["ie_tranfert_error"], $msg["ie_transfert_error_detail"], 1); |
---|
147 | exit; |
---|
148 | } else $file_in = $origine.$_FILES['import_file']['name']; |
---|
149 | } else if ($file_in && file_exists($base_path."/temp/".$file_in)){ |
---|
150 | |
---|
151 | } else { |
---|
152 | if (!$file_in) $file_in = "convert".(defined("LOCATION")?"_".constant("LOCATION"):"").".fic"; |
---|
153 | if (!file_exists($base_path."/temp/convert".(defined("LOCATION")?"_".constant("LOCATION"):"").".fic")) { |
---|
154 | error_message_history($msg["ie_file_not_found"], sprintf($msg["ie_file_not_found_detail"],$file_in), 1); |
---|
155 | exit; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | //Première notice = 0 |
---|
160 | $n_current = 0; |
---|
161 | |
---|
162 | //Nombre d'erreurs = 0; |
---|
163 | $n_errors = 0; |
---|
164 | |
---|
165 | //Création du fichier de sortie |
---|
166 | $f = explode(".", $file_in); |
---|
167 | if (count($f) > 1) { |
---|
168 | unset($f[count($f) - 1]); |
---|
169 | } |
---|
170 | $file_out = implode(".", $f).".".$output_params['SUFFIX']."~"; |
---|
171 | |
---|
172 | $fo = fopen("$base_path/temp/".$file_out, "w+"); |
---|
173 | |
---|
174 | //Ouverture du fichier d'origine |
---|
175 | $fi = fopen("$base_path/temp/".$file_in, "r"); |
---|
176 | |
---|
177 | //Récupération du nombre de notices et enregistrement dans la base de données des notices |
---|
178 | $index = _get_n_notices_($fi, "$base_path/temp/".$file_in, $input_params,$origine); |
---|
179 | if (count($index) == 0) { |
---|
180 | error_message_history($msg["ie_empty_file"], sprintf($msg["ie_empty_file_detail"],$import_type_l), 1); |
---|
181 | exit; |
---|
182 | } |
---|
183 | |
---|
184 | //Entête |
---|
185 | @ fwrite($fo, _get_header_($output_params)); |
---|
186 | fclose($fo); |
---|
187 | |
---|
188 | //Vidage de la table de log |
---|
189 | //pmb_mysql_query("delete from error_log where error_origin='convert.log'"); |
---|
190 | } |
---|
191 | |
---|
192 | function convert_notice($notice,$encoding) { |
---|
193 | global $step; |
---|
194 | global $param_path; |
---|
195 | global $n_errors; |
---|
196 | global $message_convert; |
---|
197 | global $n_current; |
---|
198 | global $z; |
---|
199 | |
---|
200 | for ($i = 0; $i < count($step); $i ++) { |
---|
201 | $s = $step[$i]; |
---|
202 | //si on a un encodage sur la notice, on le rajoute aux parametres |
---|
203 | if($encoding) $s['ENCODING'] = $encoding; |
---|
204 | $islast=($i==count($step)-1); |
---|
205 | $isfirst=($i==0); |
---|
206 | switch ($s[TYPE]) { |
---|
207 | case "xmltransform" : |
---|
208 | $r = perform_xslt($notice, $s, $islast, $isfirst, $param_path); |
---|
209 | break; |
---|
210 | case "toiso" : |
---|
211 | $r = toiso($notice, $s, $islast, $isfirst, $param_path); |
---|
212 | break; |
---|
213 | case "isotoxml" : |
---|
214 | $r = isotoxml($notice, $s, $islast, $isfirst, $param_path); |
---|
215 | break; |
---|
216 | case "texttoxml": |
---|
217 | $r = texttoxml($notice, $s, $islast, $isfirst, $param_path); |
---|
218 | break; |
---|
219 | case "custom" : |
---|
220 | eval("\$r=".$s['CALLBACK'][0][value]."(\$notice, \$s, \$islast, \$isfirst, \$param_path);"); |
---|
221 | break; |
---|
222 | } |
---|
223 | if (!$r['VALID']) { |
---|
224 | $n_errors ++; |
---|
225 | $message_convert.= "<b>Notice ". ($n_current + $z)." : </b>".$r['ERROR']."<br />\n"; |
---|
226 | $notice = ""; |
---|
227 | break; |
---|
228 | } else { |
---|
229 | $notice = $r['DATA']; |
---|
230 | if($r['WARNING']){ |
---|
231 | $n_errors ++; |
---|
232 | $message_convert.= "<b>Notice ". ($n_current + $z)." : </b>".$r['WARNING']."<br />\n"; |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | return $notice; |
---|
237 | } |
---|
238 | |
---|
239 | $requete="select count(1) from import_marc where origine='$origine'"; |
---|
240 | $resultat=pmb_mysql_query($requete); |
---|
241 | $n_notices=pmb_mysql_result($resultat,0,0); |
---|
242 | |
---|
243 | $percent = @ round(($n_current / $n_notices) * 100); |
---|
244 | if ($percent == 0) |
---|
245 | $percent = 1; |
---|
246 | echo "<center><h3>".$msg["conversion_en_cours"]."</h3></center><br />\n"; |
---|
247 | |
---|
248 | echo "<table align=center width=100%><tr><td style=\"border-width:1px;border-style:solid;border-color:#FFFFFF;\" width=100%><img src=\"$base_path/images/jauge.png\" width=\"".$percent."%\" height=\"16\"></td></tr><tr><td ><center>".round($percent)."%</center></td></tr></table>\n"; |
---|
249 | |
---|
250 | echo "<center>".sprintf($msg["ie_processed_notices"],$n_current,$n_notices,($n_notices - $n_current))."</center>"; |
---|
251 | $z = 0; |
---|
252 | |
---|
253 | //Ouverture du fichier final |
---|
254 | $f = explode(".", $file_in); |
---|
255 | if (count($f) > 1) { |
---|
256 | unset($f[count($f) - 1]); |
---|
257 | } |
---|
258 | $file_out = implode(".", $f).".".$output_params['SUFFIX']."~"; |
---|
259 | |
---|
260 | $fo = fopen("$base_path/temp/".$file_out, "r+"); |
---|
261 | |
---|
262 | //Positionnement a la fin du fichier |
---|
263 | fseek($fo, filesize("$base_path/temp/".$file_out)); |
---|
264 | |
---|
265 | for ($i = $n_current; $i < $n_current + $n_per_pass; $i ++) { |
---|
266 | $requete="select notice, encoding from import_marc where no_notice=".($i+1)." and origine='$origine'"; |
---|
267 | $resultat=pmb_mysql_query($requete); |
---|
268 | |
---|
269 | if (pmb_mysql_num_rows($resultat)!=0) { |
---|
270 | //Si la notice existe début de conversion |
---|
271 | $obj=pmb_mysql_fetch_object($resultat); |
---|
272 | $notice_ = convert_notice($obj->notice,$obj->encoding); |
---|
273 | @fwrite($fo, $notice_); |
---|
274 | $z ++; |
---|
275 | } else |
---|
276 | break; |
---|
277 | } |
---|
278 | |
---|
279 | //Y-a-t-il eu des erreurs ? |
---|
280 | if ($message_convert != "") { |
---|
281 | $requete="insert into error_log (error_date,error_origin, error_text) values(now(),'convert.log ".$origine."','".addslashes($message_convert)."')"; |
---|
282 | pmb_mysql_query($requete); |
---|
283 | echo pmb_mysql_error(); |
---|
284 | } |
---|
285 | |
---|
286 | //Fin du fichier de notice ? |
---|
287 | if ($z < $n_per_pass) { |
---|
288 | $n_current = $n_current + $z; |
---|
289 | @ fwrite($fo, _get_footer_($output_params)); |
---|
290 | fclose($fo); |
---|
291 | $requete="delete from import_marc where origine='$origine'"; |
---|
292 | pmb_mysql_query($requete); |
---|
293 | if ($redirect) { |
---|
294 | $redirect .= "&file_in=".rawurlencode($file_in)."&suffix=".$output_params['SUFFIX']."&origine=".$origine."&import_type=$import_type&outputtype=".$output_params["TYPE"]."&converted=1"; |
---|
295 | $location = "parent.document.location='".$redirect."'"; |
---|
296 | } |
---|
297 | else |
---|
298 | $location = "document.location='end_import.php?file_in=".rawurlencode($file_in)."&first=1&n_current=".$n_current."&import_type=".$import_type."&n_total=".count($index)."&n_errors=$n_errors&output=$output&suffix=".$output_params['SUFFIX']."&import_type_l=".rawurlencode($import_type_l)."&noimport=$noimport".$redirect_string."&mimetype=".rawurlencode($output_params['MIMETYPE'])."&origine=".$origine."'"; |
---|
299 | echo "<script>setTimeout(\"$location\",1000);</script>"; |
---|
300 | } else { |
---|
301 | //Si pas fin, recharegement de la page pour les $n_per_pass_suivants |
---|
302 | $n_current = $n_current + $n_per_pass; |
---|
303 | fclose($fo); |
---|
304 | |
---|
305 | if ($redirect) |
---|
306 | $redirect_string = "&redirect=".urlencode($redirect); |
---|
307 | else |
---|
308 | $redirect_string = ""; |
---|
309 | |
---|
310 | echo "<script>setTimeout(\"document.location='start_import.php?file_in=".rawurlencode($file_in)."&first=1&n_current=".$n_current."&import_type=".$import_type."&n_errors=$n_errors&noimport=$noimport".$redirect_string."&origine=".$origine."'\",1000);</script>"; |
---|
311 | } |
---|
312 | ?> |
---|