[SPIP] +2.1.12
[velocampus/web/www.git] / www / ecrire / base / convert_utf8.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2011 *
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 // http://doc.spip.org/@convert_utf8_init
16 function convert_utf8_init($tables_a_convertir)
17 {
18 // noter dans les meta qu'on veut convertir, et quoi
19 $charset_source = $GLOBALS['meta']['charset'];
20 ecrire_meta('charset', 'utf-8');
21 foreach ($tables_a_convertir as $table => $champ) {
22 spip_log("demande update charset table $table ($champ)");
23 spip_query("UPDATE $table SET $champ = CONCAT('<CONVERT ".$charset_source.">', $champ) WHERE $champ NOT LIKE '<CONVERT %'");
24 }
25
26 spip_unlink(_DIR_TMP.'convert_utf8_backup.sql');
27
28 // convertir spip_meta
29
30 foreach ($GLOBALS['meta'] as $c => $v) {
31 $v2 = unicode_to_utf_8(charset2unicode($v, $charset_source));
32 if ($v2 != $v) ecrire_meta($c, $v2);
33 }
34 }
35
36 // http://doc.spip.org/@base_convert_utf8_dist
37 function base_convert_utf8_dist($titre='', $reprise=false)
38 {
39 if (!$titre) return; // anti-testeur automatique
40 // une liste des tables a convertir, avec le champ dans lequel on
41 // indique '<CONVERT charset>' ; on commence par les rubriques sinon
42 // ca fait desordre dans l'interface privee
43 $tables_a_convertir = array(
44 'spip_rubriques' => 'titre',
45 'spip_auteurs' => 'nom',
46 'spip_articles' => 'titre',
47 'spip_breves' => 'titre',
48 'spip_documents' => 'titre',
49 'spip_forum' => 'titre',
50 'spip_mots' => 'titre',
51 'spip_groupes_mots' => 'titre',
52 'spip_petitions' => 'texte',
53 'spip_signatures' => 'nom_email',
54 'spip_syndic' => 'nom_site',
55 'spip_syndic_articles' => 'titre',
56 'spip_messages' => 'titre'
57 );
58
59 if (!$reprise) convert_utf8_init($tables_a_convertir);
60
61 echo install_debut_html($titre);
62
63 echo "<p>", _T('utf8_convert_timeout'), "</p><hr />\n";
64
65 // preparer un fichier de sauvegarde au cas ou
66 // on met 'a' car ca peut demander plusieurs rechargements
67 $f = @fopen(_DIR_TMP.'convert_utf8_backup.sql', 'a');
68
69 foreach ($tables_a_convertir as $table => $champ) {
70 convert_table_utf8($f, $table, $champ);
71 }
72
73 if ($f) fclose($f);
74
75 echo "<p><b>"._T('utf8_convert_termine')."</b></p>";
76 echo "<p>,"._T('utf8_convert_verifier', array('rep' => joli_repertoire(_DIR_TMP))), '</p>';
77
78 // bouton "retour au site" + redirige_par_entete
79 echo "<p style='text-align: right'>",
80 "<a href='", generer_url_ecrire("config_lang"), "'> &gt;&gt; ",
81 _T('icone_retour'),"</a></p>",
82 install_fin_html();
83 }
84
85
86 // http://doc.spip.org/@convert_table_utf8
87 function convert_table_utf8($f, $table, $champ)
88 {
89 echo "<br /><b>$table</b> &nbsp; ";
90 $s = spip_query("SELECT * FROM $table WHERE $champ LIKE '<CONVERT %'");
91
92 // recuperer 'id_article' (encore un truc a faire dans table_objet)
93 preg_match(',^spip_(.*?)s?$,', $table, $r);
94 $id_champ = 'id_'.$r[1];
95 if ($table == 'spip_petitions') $id_champ = 'id_article';
96 if ($table == 'spip_groupes_mots') $id_champ = 'id_groupe';
97
98 // lire les donnees dans un array
99 while ($t = sql_fetch($s)) {
100 $query = array();
101 $query_no_convert = '';
102 $query_extra = '';
103 $charset_source='AUTO';
104 foreach ($t as $c => $v) {
105 if ($c == $champ) {
106 preg_match(',^<CONVERT (.*?)>,', $v, $reg);
107 $v = substr($v, strlen($reg[0]));
108 $charset_source = $reg[1];
109 $query[] = "$c=" . sql_quote($v);
110 } else {
111 if (!is_numeric($v)
112 AND !is_ascii($v)) {
113 // traitement special car donnees serializees
114 if ($c == 'extra') {
115 $query_no_convert .= ", $c=".sql_quote($v);
116 $query_extra = convert_extra($v, $charset_source);
117 } else
118 $query[] = "$c=" . sql_quote($v);
119 } else
120 # pour le backup
121 $query_no_convert .= ", $c=".sql_quote($v);
122 }
123 }
124
125 $set = join(', ', $query);
126 $where = "$id_champ = ".$t[$id_champ];
127
128 // On l'enregistre telle quelle sur le fichier de sauvegarde
129 if ($f) fwrite($f,
130 "UPDATE $table SET $set$query_no_convert"
131 ." WHERE $where;\n"
132 );
133
134 // Mais on la transcode
135 // en evitant une double conversion
136 if ($charset_source != 'utf-8') {
137 $query = "UPDATE $table SET "
138 . unicode_to_utf_8(charset2unicode($set, $charset_source))
139 . $query_extra
140 . " WHERE $where AND $champ LIKE '<CONVERT %'";
141 #echo $query;
142 spip_query($query);
143 echo '. '; flush();
144 }
145 }
146 sql_free($s);
147 }
148
149 // stocker le nouvel extra
150 // http://doc.spip.org/@convert_extra
151 function convert_extra($v, $charset_source) {
152 if ($extra = @unserialize($v)) {
153 foreach ($extra as $key=>$val)
154 $extra[$key] = unicode_to_utf_8(
155 charset2unicode($val, $charset_source));
156 return ", extra=".sql_quote(serialize($extra));
157 }
158 }
159 ?>