1 | <?php |
---|
2 | |
---|
3 | /***************************************************************************\ |
---|
4 | * SPIP, Systeme de publication pour l'internet * |
---|
5 | * * |
---|
6 | * Copyright (c) 2001-2015 * |
---|
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 | |
---|
16 | // http://code.spip.net/@base_convert_sql_utf8_dist |
---|
17 | function base_convert_sql_utf8_dist($titre='', $reprise=false) |
---|
18 | { |
---|
19 | if (!$titre) return; // anti-testeur automatique |
---|
20 | ecrire_meta('convert_sql_utf8','oui','non'); |
---|
21 | echo install_debut_html($titre); |
---|
22 | |
---|
23 | echo "<p>", _T('grenier:utf8_convert_timeout'), "</p><hr />\n"; |
---|
24 | |
---|
25 | convert_sql_utf8($titre); |
---|
26 | |
---|
27 | echo "<p><b>"._T('grenier:utf8_convert_termine')."</b></p>"; |
---|
28 | effacer_meta('convert_sql_utf8'); |
---|
29 | |
---|
30 | // bouton "retour au site" + redirige_par_entete |
---|
31 | echo "<p style='text-align: right'>", |
---|
32 | "<a href='", generer_url_ecrire("configurer_langue"), "'> >> ", |
---|
33 | _T('icone_retour'),"</a></p>", |
---|
34 | install_fin_html(); |
---|
35 | } |
---|
36 | |
---|
37 | // http://code.spip.net/@convert_sql_utf8 |
---|
38 | function convert_sql_utf8($titre){ |
---|
39 | |
---|
40 | define('_DEBUG_CONVERT', false); |
---|
41 | $charset_spip = $GLOBALS['meta']['charset']; |
---|
42 | $charset_supporte = false; |
---|
43 | $utf8_supporte = false; |
---|
44 | // verifier que mysql gere le charset courant pour effectuer les conversions |
---|
45 | if ($c = sql_get_charset($charset_spip)){ |
---|
46 | $sql_charset = $c['charset']; |
---|
47 | $sql_collation = $c['collation']; |
---|
48 | $charset_supporte = true; |
---|
49 | } |
---|
50 | if (!$charset_supporte){ |
---|
51 | $res = spip_query("SHOW CHARACTER SET"); |
---|
52 | while ($row = sql_fetch($res)){ |
---|
53 | if ($row['Charset']=='utf8') $utf8_supporte = true; |
---|
54 | } |
---|
55 | echo install_debut_html($titre); |
---|
56 | echo _L("Le charset SPIP actuel $charset_spip n'est pas supporte par votre serveur MySQL<br/>"); # non traduit car complexe & obsolete |
---|
57 | if ($utf8_supporte) |
---|
58 | echo _L("Votre serveur supporte utf-8, vous devriez convertir votre site en utf-8 avant de recommencer cette operation"); |
---|
59 | echo install_fin_html(); |
---|
60 | } else { |
---|
61 | echo _L("Charset Actuel du site SPIP : $charset_spip<br/>"); |
---|
62 | echo _L("Conversion des champs des tables spip de type latin1 vers <b>$sql_charset</b> (collation $sql_collation) <br/>"); |
---|
63 | // lister les collations et leur charset correspondant |
---|
64 | $res = spip_query("SHOW COLLATION"); |
---|
65 | $charset2collations = array(); |
---|
66 | while ($row = sql_fetch($res)){ |
---|
67 | $charset2collations[$row['Collation']] = $row['Charset']; |
---|
68 | } |
---|
69 | |
---|
70 | $count = 0; |
---|
71 | // lister les tables spip |
---|
72 | include_spip('base/serial'); |
---|
73 | include_spip('base/auxiliaires'); |
---|
74 | |
---|
75 | $res = spip_query("SHOW TABLES"); |
---|
76 | while (($row = sql_fetch($res)) /*&& ($count<1)*/){ |
---|
77 | $nom = array_shift($row); |
---|
78 | if (preg_match(',^'.$GLOBALS['table_prefix'].'_(.*)$,',$nom,$regs)){ |
---|
79 | $count++; |
---|
80 | $nom = $regs[1]; |
---|
81 | echo "<hr /><h2>$nom</h2>"; |
---|
82 | // lister les champs de la table |
---|
83 | $res2 = spip_query("SHOW FULL COLUMNS FROM spip_$nom"); |
---|
84 | while ($row2 = sql_fetch($res2)){ |
---|
85 | $collation = $row2['Collation']; |
---|
86 | $champ = $row2['Field']; |
---|
87 | if ($collation!="NULL" |
---|
88 | && isset($charset2collations[$collation]) |
---|
89 | && $charset2collations[$collation]=='latin1'){ |
---|
90 | echo "Conversion de '$champ' depuis $collation (".$charset2collations[$collation]."):"; |
---|
91 | // conversion de latin1 vers le charset reel du contenu |
---|
92 | $type_texte= $row2['Type']; |
---|
93 | $type_blob = "blob"; |
---|
94 | if (strpos($type_texte,"text")!==FALSE) |
---|
95 | $type_blob = str_replace("text","blob",$type_texte); |
---|
96 | |
---|
97 | // sauf si blob expressement demande dans la description ! |
---|
98 | if (( |
---|
99 | $a = $GLOBALS['tables_principales']['spip_'.$nom]['field'][$champ] |
---|
100 | OR $a = $GLOBALS['tables_auxiliaires']['spip_'.$nom]['field'][$champ] |
---|
101 | ) AND preg_match(',blob,i', $a)) { |
---|
102 | echo "On ignore le champ blob $nom.$champ <hr />\n"; |
---|
103 | } else { |
---|
104 | |
---|
105 | $default = $row2['Default']?(" DEFAULT ".sql_quote($row2['Default'])):""; |
---|
106 | $notnull = ($row2['Null']=='YES')?"":" NOT NULL"; |
---|
107 | $q = "ALTER TABLE spip_$nom CHANGE $champ $champ $type_blob $default $notnull"; |
---|
108 | if (!_DEBUG_CONVERT) |
---|
109 | $b = spip_query($q); |
---|
110 | echo "<pre>$q</pre>$b\n"; |
---|
111 | $q = "ALTER TABLE spip_$nom CHANGE $champ $champ $type_texte CHARACTER SET $sql_charset COLLATE $sql_collation $default $notnull"; |
---|
112 | if (!_DEBUG_CONVERT) |
---|
113 | $b = spip_query($q); |
---|
114 | echo "<pre>$q</pre>\n"; |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | // on ne change le charset par defaut de la table que quand tous ses champs sont convertis |
---|
119 | $q = "ALTER TABLE spip_$nom DEFAULT CHARACTER SET $sql_charset COLLATE $sql_collation"; |
---|
120 | if (!_DEBUG_CONVERT) |
---|
121 | $b = spip_query($q); |
---|
122 | echo "<pre>$q</pre>$b\n"; |
---|
123 | } |
---|
124 | } |
---|
125 | ecrire_meta('charset_sql_base',$sql_charset,'non'); |
---|
126 | ecrire_meta('charset_sql_connexion',$sql_charset,'non'); |
---|
127 | } |
---|
128 | } |
---|
129 | ?> |
---|