1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | This file is part of Salvatore, the translation robot of Trad-lang (SPIP) |
---|
5 | |
---|
6 | Salvatore is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | Trad-Lang is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with Trad-Lang; if not, write to the Free Software |
---|
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | |
---|
20 | Copyright 2003-2020 |
---|
21 | Florent Jugla <florent.jugla@eledo.com>, |
---|
22 | Philippe Riviere <fil@rezo.net>, |
---|
23 | Chryjs <chryjs!@!free!.!fr>, |
---|
24 | kent1 <kent1@arscenic.info> |
---|
25 | Cerdic <cedric@yterium.com> |
---|
26 | */ |
---|
27 | |
---|
28 | |
---|
29 | include_spip('base/abstract_sql'); |
---|
30 | include_spip('inc/charsets'); |
---|
31 | include_spip('inc/filtres'); |
---|
32 | include_spip('inc/texte'); |
---|
33 | include_spip('inc/xml'); |
---|
34 | include_spip('inc/lang_liste'); |
---|
35 | include_spip('inc/session'); |
---|
36 | |
---|
37 | /** |
---|
38 | * @param array $liste_sources |
---|
39 | * @param bool $force_reload |
---|
40 | * @param string $dir_modules |
---|
41 | * @throws Exception |
---|
42 | */ |
---|
43 | function salvatore_lire($liste_sources, $force_reload = false, $dir_modules = null){ |
---|
44 | include_spip('inc/salvatore'); |
---|
45 | salvatore_init(); |
---|
46 | |
---|
47 | // on va modifier a la base, il faut qu'elle soit a jour |
---|
48 | salvatore_verifier_base_upgradee(); |
---|
49 | |
---|
50 | if (is_null($dir_modules)){ |
---|
51 | $dir_modules = _DIR_SALVATORE_MODULES; |
---|
52 | } |
---|
53 | salvatore_check_dir($dir_modules); |
---|
54 | $refresh_time = time()-_SALVATORE_LECTEUR_REFRESH_DELAY; |
---|
55 | |
---|
56 | $tradlang_verifier_langue_base = charger_fonction('tradlang_verifier_langue_base', 'inc', true); |
---|
57 | $tradlang_verifier_bilans = charger_fonction('tradlang_verifier_bilans', 'inc', true); |
---|
58 | $invalider = false; |
---|
59 | |
---|
60 | foreach ($liste_sources as $source){ |
---|
61 | salvatore_log("\n<info>--- Module " . $source['module'] . " | " . $source['dir_module'] . " | " . $source['url'] . "</info>"); |
---|
62 | |
---|
63 | $module = $source['module']; |
---|
64 | $dir_module = $dir_modules . $source['dir_module']; |
---|
65 | |
---|
66 | if ($autre_gestionnaire = salvatore_verifier_gestionnaire_traduction($dir_module, $module)){ |
---|
67 | salvatore_fail("[Lecteur] Erreur sur $module", "Erreur : import impossible, le fichier est traduit autre part : $autre_gestionnaire\n"); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * on doit absolument charger la langue principale en premier (a cause des MD5) |
---|
72 | */ |
---|
73 | $fichier_lang_principal = $dir_module . '/' . $module . '_' . $source['lang'] . '.php'; |
---|
74 | $liste_fichiers_lang = glob($dir_module . '/' . $module . '_*.php'); |
---|
75 | if (!in_array($fichier_lang_principal, $liste_fichiers_lang)){ |
---|
76 | salvatore_fail("[Lecteur] Erreur sur $module", "|-- Pas de fichier lang principal $fichier_lang_principal : import impossible pour ce module"); |
---|
77 | } |
---|
78 | |
---|
79 | // pour la suite, on enleve la langue principale de la liste des fichiers |
---|
80 | $liste_fichiers_lang = array_diff($liste_fichiers_lang, [$fichier_lang_principal]); |
---|
81 | |
---|
82 | /** |
---|
83 | * On regarde quelle est la date de dernière modification du fichier de langue principale |
---|
84 | */ |
---|
85 | $last_update = filemtime($fichier_lang_principal); |
---|
86 | |
---|
87 | if ($row_module = salvatore_retrouver_tradlang_module($dir_module, $module)) { |
---|
88 | $id_tradlang_module = intval($row_module['id_tradlang_module']); |
---|
89 | salvatore_log("Module en base #$id_tradlang_module"); |
---|
90 | /** |
---|
91 | * Si la langue mere a changée, on la modifie |
---|
92 | */ |
---|
93 | if ($row_module['lang_mere']!==$source['lang']){ |
---|
94 | sql_updateq('spip_tradlang_modules', array('lang_mere' => $source['lang']), 'id_tradlang_module=' . intval($id_tradlang_module)); |
---|
95 | salvatore_log("lang_mere mise a jour : " . $row_module['lang_mere'] . " => " . $source['lang']); |
---|
96 | $row_module['lang_mere'] = $source['lang']; |
---|
97 | $last_update = time(); |
---|
98 | } |
---|
99 | /** |
---|
100 | * Si le dir_module a change, on le met a jour |
---|
101 | */ |
---|
102 | if ($row_module['dir_module']!==$source['dir_module']){ |
---|
103 | sql_updateq('spip_tradlang_modules', array('dir_module' => $source['dir_module']), 'id_tradlang_module=' . intval($id_tradlang_module)); |
---|
104 | salvatore_log("dir_module mis a jour : " . $row_module['dir_module'] . " => " . $source['dir_module']); |
---|
105 | $last_update = time(); |
---|
106 | $row_module['dir_module'] = $source['dir_module']; |
---|
107 | } |
---|
108 | /** |
---|
109 | * On met le titre a jour si jamais il a change (ou si on a change de facon de le calculer) |
---|
110 | */ |
---|
111 | if (($t = calculer_nom_module($source['module'], $source['dir_module'])) !== $row_module['nom_mod']) { |
---|
112 | sql_updateq('spip_tradlang_modules', array('nom_mod' => $t), 'id_tradlang_module=' . intval($id_tradlang_module)); |
---|
113 | salvatore_log("nom_mod mis a jour : " . $row_module['nom_mod'] . " => " . $t); |
---|
114 | $last_update = time(); |
---|
115 | $row_module['nom_mod'] = $t; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | $langues_a_jour = array(); |
---|
120 | |
---|
121 | if (!$row_module |
---|
122 | or $force_reload |
---|
123 | or $last_update>$refresh_time){ |
---|
124 | $priorite = ''; |
---|
125 | $modifs = 0; |
---|
126 | if (defined('_TRAD_PRIORITE_DEFAUT')){ |
---|
127 | $priorite = _TRAD_PRIORITE_DEFAUT; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * Si le module n'existe pas... on le crée |
---|
132 | */ |
---|
133 | if (!$row_module or !$id_tradlang_module = intval($row_module['id_tradlang_module'])){ |
---|
134 | $insert = [ |
---|
135 | 'module' => $source['module'], |
---|
136 | 'dir_module' => $source['dir_module'], |
---|
137 | 'nom_mod' => calculer_nom_module($source['module'],$source['dir_module']), |
---|
138 | 'lang_prefix' => $source['module'], |
---|
139 | 'lang_mere' => $source['lang'], |
---|
140 | 'priorite' => $priorite, |
---|
141 | ]; |
---|
142 | $id_tradlang_module = sql_insertq('spip_tradlang_modules', $insert); |
---|
143 | /** |
---|
144 | * Si insertion echoue on fail |
---|
145 | */ |
---|
146 | if (!intval($id_tradlang_module)){ |
---|
147 | salvatore_fail("[Lecteur] Erreur sur $module", "Echec insertion dans spip_tradlang_modules " . json_encode($insert)); |
---|
148 | } |
---|
149 | else { |
---|
150 | salvatore_log("Insertion en base #$id_tradlang_module"); |
---|
151 | } |
---|
152 | } |
---|
153 | $force_reload = true; |
---|
154 | } |
---|
155 | // Pas de mise a jour recente du fichier maitre deja en base |
---|
156 | else { |
---|
157 | salvatore_log("On ne modifie rien : fichier original $fichier_lang_principal inchangé depuis " . date("Y-m-d H:i:s", $last_update)); |
---|
158 | $id_tradlang_module=intval($row_module['id_tradlang_module']); |
---|
159 | |
---|
160 | /** |
---|
161 | * Le fichier d'origine n'a pas été modifié |
---|
162 | * Mais on a peut être de nouvelles langues |
---|
163 | */ |
---|
164 | $langues_en_base = sql_allfetsel('DISTINCT lang', 'spip_tradlangs', 'id_tradlang_module=' . intval($id_tradlang_module)); |
---|
165 | $langues_en_base = array_column($langues_en_base, 'lang'); |
---|
166 | |
---|
167 | $langues_a_ajouter = array(); |
---|
168 | foreach ($liste_fichiers_lang as $fichier_lang){ |
---|
169 | $lang = salvatore_get_lang_from($module, $fichier_lang); |
---|
170 | if (!in_array($lang, $langues_en_base)){ |
---|
171 | $langues_a_ajouter[] = array('lang' => $lang, 'fichier' => $fichier_lang); |
---|
172 | } |
---|
173 | else { |
---|
174 | // inutile de regarder ce fichier |
---|
175 | $langues_a_jour[] = $lang; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | $liste_fichiers_lang = array(); |
---|
180 | if ($langues_a_ajouter){ |
---|
181 | salvatore_log('On a ' . count($langues_a_ajouter) . " nouvelle(s) langue(s) à insérer (".count($langues_en_base). " langue(s) an base)"); |
---|
182 | $liste_fichiers_lang = array_column($langues_a_ajouter, 'fichier'); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | // traiter les fichiers lang |
---|
187 | if (count($liste_fichiers_lang) or $force_reload) { |
---|
188 | |
---|
189 | // on commence par la langue mere |
---|
190 | $liste_md5_master = array(); |
---|
191 | $modifs_master = salvatore_importer_module_langue($id_tradlang_module, $source, $fichier_lang_principal, true, $liste_md5_master); |
---|
192 | |
---|
193 | // et on fait les autres langues |
---|
194 | foreach ($liste_fichiers_lang as $fichier_lang){ |
---|
195 | salvatore_importer_module_langue($id_tradlang_module, $source, $fichier_lang, false, $liste_md5_master); |
---|
196 | |
---|
197 | $lang = salvatore_get_lang_from($module, $fichier_lang); |
---|
198 | if ($modifs_master>0) { |
---|
199 | if ($tradlang_verifier_langue_base) { |
---|
200 | $tradlang_verifier_langue_base($id_tradlang_module, $lang); |
---|
201 | salvatore_log('|-- Synchro de la langue ' . $lang . ' pour le module ' . $source['module']); |
---|
202 | } |
---|
203 | else { |
---|
204 | salvatore_log("<error>|-- Pas de Fonction de synchro inexistante pour synchroniser lang $lang</error>"); |
---|
205 | } |
---|
206 | } |
---|
207 | $langues_a_jour[] = $lang; |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * On s'occupe des langues en base sans fichier |
---|
212 | * s'il y a eu au moins une modif et que l'on peut faire la synchro |
---|
213 | */ |
---|
214 | if ($modifs_master>0 and $tradlang_verifier_langue_base){ |
---|
215 | $langues_en_base = sql_allfetsel('DISTINCT lang', 'spip_tradlangs', 'id_tradlang_module=' . intval($id_tradlang_module)); |
---|
216 | $langues_en_base = array_column($langues_en_base, 'lang'); |
---|
217 | |
---|
218 | if ($langues_pas_a_jour = array_diff($langues_en_base, $langues_a_jour)) { |
---|
219 | foreach ($langues_pas_a_jour as $langue_todo){ |
---|
220 | $tradlang_verifier_langue_base($id_tradlang_module, $langue_todo); |
---|
221 | salvatore_log("|-- Synchro de la langue non exportée en fichier $langue_todo pour le module $module"); |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | $invalider = true; |
---|
227 | salvatore_log("|"); |
---|
228 | unset($langues_a_jour, $langues_pas_a_jour); |
---|
229 | } |
---|
230 | |
---|
231 | // Mise à jour des bilans |
---|
232 | if ($tradlang_verifier_bilans){ |
---|
233 | salvatore_log("Création ou MAJ des bilans du module #$id_tradlang_module $module"); |
---|
234 | $tradlang_verifier_bilans($id_tradlang_module, $source['lang'], false); |
---|
235 | salvatore_log("-"); |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | if ($invalider){ |
---|
240 | include_spip('inc/invalideur'); |
---|
241 | suivre_invalideur('1'); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * Import d'un fichier de langue dans la base |
---|
247 | * |
---|
248 | * @param int $id_tradlang_module |
---|
249 | * @param array $source |
---|
250 | * tableau decrivant le module extrait du fichier traductions |
---|
251 | * @param string $fichier_lang |
---|
252 | * chemin vers le fichier de langue |
---|
253 | * @param bool $is_master |
---|
254 | * true signifie que c'est la langue originale |
---|
255 | * @param array $liste_md5_master |
---|
256 | * @return string |
---|
257 | */ |
---|
258 | function salvatore_importer_module_langue($id_tradlang_module, $source, $fichier_lang, $is_master, &$liste_md5_master){ |
---|
259 | salvatore_log("!\n+ Import de $fichier_lang\n"); |
---|
260 | $idx = $GLOBALS['idx_lang'] = 'i18n_' . crc32($fichier_lang) . '_tmp'; |
---|
261 | |
---|
262 | $lang = salvatore_get_lang_from($source['module'], $fichier_lang); |
---|
263 | $module = $source['module']; |
---|
264 | |
---|
265 | // charger le fichier et ses commentaires |
---|
266 | $GLOBALS[$idx] = null; |
---|
267 | $commentaires = salvatore_charger_commentaires_fichier_langue($fichier_lang); |
---|
268 | |
---|
269 | include $fichier_lang; |
---|
270 | $chaines = $GLOBALS[$idx]; // on a vu certains fichiers faire des betises et modifier idx_lang |
---|
271 | |
---|
272 | if (is_null($chaines)){ |
---|
273 | $erreur = "Erreur, fichier $fichier_lang mal forme"; |
---|
274 | salvatore_log("<error>$erreur</error>"); |
---|
275 | salvatore_envoyer_mail("[Lecteur] Erreur sur $module", $erreur); |
---|
276 | return false; |
---|
277 | } |
---|
278 | |
---|
279 | /** |
---|
280 | * Nettoyer le contenu de ses <MODIF>,<NEW> et <PLUS_UTILISE> |
---|
281 | * Ces chaines sont utilisées comme statut |
---|
282 | */ |
---|
283 | $status = array(); |
---|
284 | |
---|
285 | foreach ($chaines as $id => $chaine){ |
---|
286 | if ($is_master){ |
---|
287 | $status[$id] = 'OK'; |
---|
288 | } else { |
---|
289 | if (preg_match(',^<(MODIF|NEW|PLUS_UTILISE)>,US', $chaine, $r)){ |
---|
290 | $chaines[$id] = preg_replace(',^(<(MODIF|NEW|PLUS_UTILISE)>)+,US', '', $chaine); |
---|
291 | $status[$id] = $r[1]; |
---|
292 | } else { |
---|
293 | $status[$id] = 'OK'; |
---|
294 | } |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | $ajoutees = $inchangees = $supprimees = $modifiees = $ignorees = $recuperees = 0; |
---|
299 | |
---|
300 | if (array_key_exists($lang, $GLOBALS['codes_langues'])) { |
---|
301 | $statut_exclus = 'attic'; |
---|
302 | $res = sql_select("id, str, md5", "spip_tradlangs", "id_tradlang_module=" . intval($id_tradlang_module) . " AND lang=" . sql_quote($lang) . " AND statut!=" . sql_quote($statut_exclus)); |
---|
303 | $nb = sql_count($res); |
---|
304 | if ($nb>0){ |
---|
305 | salvatore_log("!-- Fichier de langue $lang du module $module deja inclus dans la base\n"); |
---|
306 | } |
---|
307 | |
---|
308 | /** |
---|
309 | * Si la langue est deja dans la base, on ne l'ecrase que s'il s'agit |
---|
310 | * de la langue source |
---|
311 | */ |
---|
312 | if (!$nb or $is_master){ |
---|
313 | // La liste de ce qui existe deja |
---|
314 | $existant = $str_existant = array(); |
---|
315 | while ($row = sql_fetch($res)){ |
---|
316 | $existant[$row['id']] = $row['md5']; |
---|
317 | $str_existant[$row['id']] = $row['str']; |
---|
318 | } |
---|
319 | |
---|
320 | $bigwhere = "id_tradlang_module=" . intval($id_tradlang_module) . ' AND lang=' . sql_quote($lang); |
---|
321 | |
---|
322 | include_spip('action/editer_tradlang'); |
---|
323 | // Dans ce qui arrive, il y a 4 cas : |
---|
324 | $ids = array_unique(array_merge(array_keys($existant), array_keys($chaines))); |
---|
325 | foreach ($ids as $id){ |
---|
326 | $comm = (isset($commentaires[$id])) ? $commentaires[$id] : ''; |
---|
327 | |
---|
328 | /** |
---|
329 | * 1. chaine neuve |
---|
330 | */ |
---|
331 | if (isset($chaines[$id]) and !isset($existant[$id])){ |
---|
332 | $md5 = null; |
---|
333 | if ($is_master){ |
---|
334 | $md5 = md5($chaines[$id]); |
---|
335 | } else { |
---|
336 | if (!isset($liste_md5_master[$id])){ |
---|
337 | salvatore_log("<info>!-- Chaine $id inconnue dans la langue principale</info>"); |
---|
338 | $ignorees++; |
---|
339 | } else { |
---|
340 | $md5 = $liste_md5_master[$id]; |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | if ($md5){ |
---|
345 | $chaines[$id] = salvatore_nettoyer_chaine_langue($chaines[$id], $lang); |
---|
346 | |
---|
347 | /** |
---|
348 | * Calcul du nouveau md5 |
---|
349 | */ |
---|
350 | $md5 = md5($chaines[$id]); |
---|
351 | |
---|
352 | /** |
---|
353 | * Si le commentaire est un statut et que l'on ne traite pas le fichier de langue mère |
---|
354 | * On vire le commentaire et met son contenu comme statut |
---|
355 | */ |
---|
356 | if (in_array($comm, array('NEW', 'OK', 'MODIF', 'MODI')) and !$is_master){ |
---|
357 | if ($comm=='MODI'){ |
---|
358 | $comm = 'MODIF'; |
---|
359 | } |
---|
360 | $status[$id] = $comm; |
---|
361 | $comm = ''; |
---|
362 | } else { |
---|
363 | if ((strlen($comm)>1) && preg_match('/(.*?)(NEW|OK|MODIF)(.*?)/', $comm, $matches)){ |
---|
364 | if (!$is_master){ |
---|
365 | $status[$id] = $matches[2]; |
---|
366 | } |
---|
367 | $comm = preg_replace('/(NEW|OK|MODIF)/', '', $comm); |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | /** |
---|
372 | * On génère un titre |
---|
373 | */ |
---|
374 | $titre = $id . ' : ' . $source['module'] . ' - ' . $lang; |
---|
375 | |
---|
376 | $set = array( |
---|
377 | 'id_tradlang_module' => $id_tradlang_module, |
---|
378 | 'titre' => $titre, |
---|
379 | 'module' => $source['module'], |
---|
380 | 'lang' => $lang, |
---|
381 | 'id' => $id, |
---|
382 | 'str' => $chaines[$id], |
---|
383 | 'comm' => $comm, |
---|
384 | 'md5' => $md5, |
---|
385 | 'statut' => $status[$id] |
---|
386 | ); |
---|
387 | $id_tradlang = sql_insertq('spip_tradlangs', $set); |
---|
388 | |
---|
389 | /** |
---|
390 | * L'identifiant de la chaîne de langue a peut être déjà été utilisé puis mis au grenier |
---|
391 | * On le récupère donc |
---|
392 | */ |
---|
393 | if (!$id_tradlang){ |
---|
394 | // mais il serait bien de pouvoir piquer une chaine attic du meme module meme si pas id_tradlang_module identique |
---|
395 | $tradlang = sql_fetsel('*', 'spip_tradlangs', 'id=' . sql_quote($id) . ' AND id_tradlang_module=' . intval($id_tradlang_module) . ' AND lang=' . sql_quote($lang) . ' AND statut=' . sql_quote('attic')); |
---|
396 | if ($tradlang and $id_tradlang = intval($tradlang['id_tradlang'])){ |
---|
397 | salvatore_log("<info>Recuperation chaine ".$source['module'].":{$id}[{$lang}] de statut ATTIC</info>"); |
---|
398 | sql_updateq('spip_tradlangs', $set, 'id_tradlang=' . intval($id_tradlang)); |
---|
399 | |
---|
400 | $trads = sql_allfetsel('id_tradlang', 'spip_tradlangs', 'id=' . sql_quote($id) . ' AND id_tradlang_module=' . intval($id_tradlang_module) . 'AND lang!=' . sql_quote($lang) . ' AND statut=' . sql_quote('attic')); |
---|
401 | $maj = array('statut' => 'MODIF'); |
---|
402 | foreach ($trads as $trad){ |
---|
403 | salvatore_log("Changement de la trad #" . $trad['id_tradlang'] . " ATTIC => MODIF"); |
---|
404 | sql_updateq('spip_tradlangs', $maj, 'id_tradlang=' . intval($trad['id_tradlang'])); |
---|
405 | } |
---|
406 | $recuperees++; |
---|
407 | } |
---|
408 | else { |
---|
409 | salvatore_fail("[Lecteur] Echec insertion", "Echec insertion en base : " . json_encode($set)); |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | /** |
---|
414 | * Rechercher si on serait pas en train de copier une chaine existante |
---|
415 | * (C'est un renommage) |
---|
416 | * |
---|
417 | * Si oui, on sélectionne toutes les occurences existantes dans les autres langues et on les duplique |
---|
418 | */ |
---|
419 | |
---|
420 | // si on a un contenu identique a une autre chaine du meme module |
---|
421 | // (renommage de l'id dans un meme fichier de langue) |
---|
422 | $where_identique = [ |
---|
423 | 'id_tradlang_module=' . intval($id_tradlang_module), |
---|
424 | 'lang=' . sql_quote($lang), |
---|
425 | 'id!=' . sql_quote($id), |
---|
426 | 'str=' . sql_quote($chaines[$id]), |
---|
427 | ]; |
---|
428 | $identique = sql_fetsel('id_tradlang_module, module, id', 'spip_tradlangs', $where_identique); |
---|
429 | |
---|
430 | // si on est sur la langue master on cherche si ce n'est pas un renommage/decoupe de module : |
---|
431 | // chercher une chaine avec meme id et meme contenu dans un autre module |
---|
432 | if (!$identique and $is_master) { |
---|
433 | $where_identique = [ |
---|
434 | 'id_tradlang_module!=' . intval($id_tradlang_module), |
---|
435 | 'lang=' . sql_quote($lang), |
---|
436 | 'id=' . sql_quote($id), |
---|
437 | 'str=' . sql_quote($chaines[$id]), |
---|
438 | ]; |
---|
439 | $identique = sql_fetsel('id_tradlang_module, module, id', 'spip_tradlangs', $where_identique); |
---|
440 | } |
---|
441 | |
---|
442 | |
---|
443 | if ($identique){ |
---|
444 | salvatore_log("La nouvelle chaine $id est une chaine dupliquée de " . $identique['module'].':'.$identique['id']); |
---|
445 | |
---|
446 | $deja_lang = sql_allfetsel('lang', 'spip_tradlangs', 'id=' . sql_quote($id) . ' AND id_tradlang_module=' . intval($id_tradlang_module)); |
---|
447 | $deja_lang = array_column($deja_lang, 'lang'); |
---|
448 | $chaines_a_dupliquer = sql_allfetsel('*', 'spip_tradlangs', 'id=' . sql_quote($identique['id']) . ' AND id_tradlang_module=' . intval($identique['id_tradlang_module']) . ' AND ' . sql_in('lang', $deja_lang, 'NOT')); |
---|
449 | foreach ($chaines_a_dupliquer as $chaine){ |
---|
450 | unset($chaine['id_tradlang']); |
---|
451 | unset($chaine['maj']); |
---|
452 | $chaine['id'] = $id; |
---|
453 | $chaine['id_tradlang_module'] = $id_tradlang_module; |
---|
454 | $chaine['module'] = $source['module']; |
---|
455 | $chaine['titre'] = $id . ' : ' . $source['module'] . ' - ' . $chaine['lang']; |
---|
456 | $chaine['md5'] = md5($chaine['str']); |
---|
457 | $chaine['date_modif'] = date('Y-m-d H:i:s'); |
---|
458 | if ($chaine['statut']=='attic'){ |
---|
459 | $chaine['statut'] = 'NEW'; |
---|
460 | } |
---|
461 | $id_tradlang_new = sql_insertq('spip_tradlangs', $chaine); |
---|
462 | if (!$id_tradlang_new) { |
---|
463 | salvatore_fail("[Lecteur] Echec insertion", "Echec insertion en base : " . json_encode($chaine)); |
---|
464 | } |
---|
465 | salvatore_log('Ajout de la version ' . $chaine['lang'] . ' - #' . $id_tradlang_new); |
---|
466 | } |
---|
467 | } |
---|
468 | $ajoutees++; |
---|
469 | } |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | /** |
---|
474 | * 2. chaine existante |
---|
475 | * |
---|
476 | */ |
---|
477 | elseif (isset($chaines[$id]) and isset($existant[$id])) { |
---|
478 | // * chaine existante |
---|
479 | // * identique ? => NOOP |
---|
480 | $chaines[$id] = salvatore_nettoyer_chaine_langue($chaines[$id], $lang); |
---|
481 | |
---|
482 | /** |
---|
483 | * Calcul du nouveau md5 |
---|
484 | */ |
---|
485 | $md5 = md5($chaines[$id]); |
---|
486 | if ($md5 === $existant[$id]){ |
---|
487 | $inchangees++; |
---|
488 | } |
---|
489 | else { |
---|
490 | // * modifiee ? => UPDATE |
---|
491 | salvatore_log("Chaine $id modifiee $md5 != ".$existant[$id]); |
---|
492 | |
---|
493 | // modifier la chaine |
---|
494 | $modifs = array( |
---|
495 | 'str' => $chaines[$id], |
---|
496 | 'md5' => ($is_master ? $md5 : $existant[$id]), |
---|
497 | 'statut' => ($is_master ? 'OK' : ''), |
---|
498 | 'comm' => $comm, |
---|
499 | ); |
---|
500 | $id_tradlang = sql_getfetsel('id_tradlang', 'spip_tradlangs', "$bigwhere AND id = " . sql_quote($id)); |
---|
501 | $test = tradlang_set($id_tradlang, $modifs); |
---|
502 | |
---|
503 | /** |
---|
504 | * signaler le statut MODIF de ses traductions OK |
---|
505 | * update des str de ses traductions NEW |
---|
506 | */ |
---|
507 | if ($is_master){ |
---|
508 | sql_updateq('spip_tradlangs', array('statut'=> 'MODIF'), |
---|
509 | 'id_tradlang_module=' . intval($id_tradlang_module) |
---|
510 | . ' AND id=' . sql_quote($id) |
---|
511 | . ' AND md5!=' . sql_quote($md5) |
---|
512 | . ' AND lang!=' . sql_quote($lang) |
---|
513 | . ' AND statut!=' . sql_quote('NEW')); |
---|
514 | } |
---|
515 | sql_updateq('spip_tradlangs', array('str'=> $chaines[$id]), |
---|
516 | 'id_tradlang_module=' . intval($id_tradlang_module) |
---|
517 | . ' AND id=' . sql_quote($id) |
---|
518 | . ' AND md5!=' . sql_quote($md5) |
---|
519 | . ' AND lang!=' . sql_quote($lang) |
---|
520 | . ' AND statut=' . sql_quote('NEW')); |
---|
521 | $modifiees++; |
---|
522 | } |
---|
523 | } |
---|
524 | |
---|
525 | /** |
---|
526 | * 3. chaine supprimee |
---|
527 | * |
---|
528 | */ |
---|
529 | elseif (!isset($chaines[$id]) and isset($existant[$id])) { |
---|
530 | // * chaine supprimee |
---|
531 | // mettre au grenier |
---|
532 | sql_updateq('spip_tradlangs', array('statut' => 'attic'), 'id_tradlang_module=' . intval($id_tradlang_module) . ' AND id=' . sql_quote($id)); |
---|
533 | $supprimees++; |
---|
534 | } |
---|
535 | |
---|
536 | if ($is_master and isset($chaines[$id])){ |
---|
537 | $liste_md5_master[$id] = md5($chaines[$id]); |
---|
538 | } |
---|
539 | } |
---|
540 | salvatore_log('!-- module ' . $source['module'] . ", $lang : $modifiees modifiees, $ajoutees ajoutees, $supprimees supprimees, $recuperees recuperees, $ignorees ignorees, $inchangees inchangees"); |
---|
541 | } |
---|
542 | } |
---|
543 | else { |
---|
544 | salvatore_log("<error>!-- Attention : La langue $lang n'existe pas dans les langues possibles - $module</error>"); |
---|
545 | } |
---|
546 | |
---|
547 | // TODO : BUG ? |
---|
548 | // unset $liste_md5_master alors qu'elle est repassee d'un appel a l'autre ? |
---|
549 | unset($liste_md5_master, $chaines, $GLOBALS[$GLOBALS['idx_lang']]); |
---|
550 | |
---|
551 | return $ajoutees + $supprimees + $modifiees; |
---|
552 | } |
---|
553 | |
---|
554 | |
---|
555 | /** |
---|
556 | * Chargement des commentaires de fichier de langue |
---|
557 | * Le fichier est chargé en mode texte pour récupérer les commentaires dans lesquels sont situés les statuts |
---|
558 | * |
---|
559 | * @param string $fichier_lang Le chemin du fichier de langue |
---|
560 | * @return array $liste_trad Un tableau id/chaine |
---|
561 | */ |
---|
562 | function salvatore_charger_commentaires_fichier_langue($fichier_lang){ |
---|
563 | |
---|
564 | $contenu = file_get_contents($fichier_lang); |
---|
565 | $tokens = token_get_all($contenu); |
---|
566 | $comments = array(); |
---|
567 | |
---|
568 | // allons jusqu'au debut du tableau |
---|
569 | while (count($tokens)) { |
---|
570 | $token = array_shift($tokens); |
---|
571 | if ($token[0] === T_ARRAY) { |
---|
572 | break; |
---|
573 | } |
---|
574 | } |
---|
575 | |
---|
576 | $last_tring = ''; |
---|
577 | $index = ''; |
---|
578 | while (count($tokens)){ |
---|
579 | $token = array_shift($tokens); |
---|
580 | switch ($token[0]) { |
---|
581 | case T_CONSTANT_ENCAPSED_STRING: |
---|
582 | $last_tring = $token[1]; |
---|
583 | break; |
---|
584 | case T_DOUBLE_ARROW: |
---|
585 | $index = trim($last_tring,"'\""); |
---|
586 | break; |
---|
587 | case T_WHITESPACE: |
---|
588 | // si c'est une nouvelle ligne, on est plus interesse par le commentaire |
---|
589 | if (strpos($token[1], "\n") !== false or strpos($token[1], "\r") !== false) { |
---|
590 | $index = ''; |
---|
591 | } |
---|
592 | break; |
---|
593 | case T_COMMENT: |
---|
594 | if ($index and strpos($token[1], '#') === 0) { |
---|
595 | $comments[$index] = trim(substr($token[1],1)); |
---|
596 | } |
---|
597 | break; |
---|
598 | } |
---|
599 | } |
---|
600 | return $comments; |
---|
601 | } |
---|
602 | |
---|