[ Index ]
 

Code source de SPIP 1.9.2c

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/ecrire/exec/ -> convert_utf8.php (source)

   1  <?php
   2  
   3  /***************************************************************************\
   4   *  SPIP, Systeme de publication pour l'internet                           *
   5   *                                                                         *
   6   *  Copyright (c) 2001-2007                                                *
   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  include_spip('inc/admin');
  17  
  18  
  19  // http://doc.spip.org/@demander_conversion
  20  function demander_conversion($tables_a_convertir, $action) {
  21      global $spip_lang_right;
  22  
  23      $charset_orig = $GLOBALS['meta']['charset'];
  24  
  25      // tester si le charset d'origine est connu de spip
  26      if (!load_charset($charset_orig))
  27          $message = _T('utf8_convert_erreur_orig', array('charset' => "<b>".$charset_orig."</b>"));
  28  
  29      // ne pas convertir si deja utf8
  30      else if ($charset_orig == 'utf-8')
  31          $message = _T('utf8_convert_erreur_deja',
  32              array('charset' => $charset_orig)
  33          );
  34  
  35      else {
  36          $commentaire = _T('utf8_convert_avertissement',
  37              array('orig' => $charset_orig,
  38                  'charset' => 'utf-8')
  39          );
  40          $commentaire .=  "<p><small>"
  41          . http_img_pack('warning.gif', _T('info_avertissement'), "style='width: 48px; height: 48px; float: right;margin: 10px;'");
  42          $commentaire .= _T('utf8_convert_backup', array('charset' => 'utf-8'))
  43          ."</small>";
  44          $commentaire .= '<p>'._T('utf8_convert_timeout');
  45          $commentaire .= "<hr />\n";
  46  
  47          debut_admin("convert_utf8", $action, $commentaire);
  48  
  49          // noter dans les meta qu'on veut convertir
  50          ecrire_meta('conversion_charset', $charset_orig);
  51          ecrire_meta('charset', 'utf-8');
  52          ecrire_metas();
  53          foreach ($tables_a_convertir as $table => $champ) {
  54              spip_log("demande update charset table $table ($champ)");
  55              spip_query("UPDATE $table SET $champ = CONCAT('<CONVERT ".$charset_orig.">', $champ)    WHERE $champ NOT LIKE '<CONVERT %'");
  56          }
  57          return;
  58      }
  59  
  60      // Ici en cas d'erreur, une page admin normale avec bouton de retour
  61      echo minipres($action, ('<p>'.$message. "</p><p align='right'> <a href='" . generer_url_ecrire("config_lang"). "'> &gt;&gt; "._T('icone_retour')."</a>"));
  62      exit;
  63  }
  64  
  65  // stocker le nouvel extra
  66  // http://doc.spip.org/@convert_extra
  67  function convert_extra($v) {
  68      if ($extra = @unserialize($v)) {
  69          foreach ($extra as $key=>$val)
  70              $extra[$key] = unicode_to_utf_8(
  71              charset2unicode($val, $charset_source));
  72              return ", extra="._q(serialize($extra));
  73      }
  74  }
  75  
  76  
  77  // http://doc.spip.org/@exec_convert_utf8_dist
  78  function exec_convert_utf8_dist() {
  79      include_spip('inc/meta');
  80      include_spip('inc/charsets');
  81      lire_metas();
  82  
  83      // une liste des tables a convertir, avec le champ dans lequel on
  84      // indique '<CONVERT charset>' ; on commence par les rubriques sinon
  85      // ca fait desordre dans l'interface privee
  86      $tables_a_convertir = array(
  87          'spip_rubriques' => 'titre',
  88          'spip_auteurs' => 'nom',
  89          'spip_articles' => 'titre',
  90          'spip_breves' => 'titre',
  91          'spip_documents' => 'titre',
  92          'spip_forum' => 'titre',
  93          'spip_mots' => 'titre',
  94          'spip_groupes_mots' => 'titre',
  95          'spip_petitions' => 'texte',
  96          'spip_signatures' => 'nom_email',
  97          'spip_syndic' => 'nom_site',
  98          'spip_syndic_articles' => 'titre',
  99          'spip_messages' => 'titre'
 100      );
 101  
 102      // Definir le titre de la page (et le nom du fichier admin)
 103      $action = _T('utf8_convertir_votre_site');
 104  
 105      // si l'appel est explicite, passer par l'authentification ftp
 106      if (!$GLOBALS['meta']['conversion_charset']) {
 107          demander_conversion($tables_a_convertir, $action);
 108  
 109          // si on est la c'est que l'autorisation ftp vient d'etre donnee
 110          @unlink(_DIR_TMP.'convert_utf8_backup.sql');
 111  
 112          // convertir spip_meta
 113          $charset_source = $GLOBALS['meta']['conversion_charset'];
 114          foreach ($GLOBALS['meta'] as $c => $v) {
 115              $v2 = unicode_to_utf_8(charset2unicode($v, $charset_source));
 116              if ($v2 != $v)
 117                  ecrire_meta($c, $v2);
 118          }
 119          ecrire_metas();
 120      }
 121  
 122      // commencer (ou continuer apres un timeout et reload)
 123  
 124      echo install_debut_html($action);
 125      
 126      echo "<p>" . _T('utf8_convert_timeout') . "<hr />\n";
 127  
 128      if (!spip_get_lock('conversion_charset')) {
 129          echo minipres(_T('utf8_convert_attendez'));
 130          exit;
 131      }
 132  
 133      // preparer un fichier de sauvegarde au cas ou
 134      // on met 'a' car ca peut demander plusieurs rechargements
 135      $f = @fopen(_DIR_TMP.'convert_utf8_backup.sql', 'a');
 136  
 137  
 138      foreach ($tables_a_convertir as $table => $champ) {
 139          echo "<br /><b>$table</b> &nbsp; ";
 140          $s = spip_query("SELECT * FROM $table WHERE $champ LIKE '<CONVERT %'");
 141  
 142          // recuperer 'id_article' (encore un truc a faire dans table_objet)
 143          preg_match(',^spip_(.*?)s?$,', $table, $r);
 144          $id_champ = 'id_'.$r[1];
 145          if ($table == 'spip_petitions') $id_champ = 'id_article';
 146          if ($table == 'spip_groupes_mots') $id_champ = 'id_groupe';
 147  
 148          // lire les donnees dans un array
 149          while ($t = spip_fetch_array($s, SPIP_ASSOC)) {
 150              $query = array();
 151              $query_no_convert = '';
 152              $query_extra = '';
 153              foreach ($t as $c => $v) {
 154                  if ($c == $champ) {
 155                      preg_match(',^<CONVERT (.*?)>,', $v, $reg);
 156                      $v = substr($v, strlen($reg[0]));
 157                      $charset_source = $reg[1];
 158                      $query[] = "$c=" . _q($v);
 159                  } else {
 160                      if (!is_numeric($v)
 161                      AND !is_ascii($v)) {
 162                          // traitement special car donnees serializees
 163                          if ($c == 'extra') {
 164                              $query_no_convert .= ", $c="._q($v);
 165                              $query_extra = convert_extra($v);
 166                          } else
 167                              $query[] = "$c=" . _q($v);
 168                      } else
 169                          # pour le backup
 170                          $query_no_convert .= ", $c="._q($v);
 171                  }
 172              }
 173  
 174              $set = join(', ', $query);
 175              $where = "$id_champ = ".$t[$id_champ];
 176  
 177              // On l'enregistre telle quelle sur le fichier de sauvegarde
 178              if ($f) fwrite($f,
 179                  "UPDATE $table SET $set$query_no_convert"
 180                  ." WHERE $where;\n"
 181              );
 182  
 183              // Mais on la transcode
 184              // en evitant une double conversion
 185              if ($charset_source != 'utf-8') {
 186                  $query = "UPDATE $table SET "
 187                  . unicode_to_utf_8(charset2unicode($set, $charset_source))
 188                  . $query_extra
 189                  . " WHERE $where AND $champ LIKE '<CONVERT %'";
 190                  #echo $query;
 191                  spip_query($query);
 192                  echo '.           '; flush();
 193  
 194              }
 195          }
 196          spip_free_result($s);
 197      }
 198  
 199      if ($f) fclose($f);
 200  
 201      echo "<p><b>"._T('utf8_convert_termine')."</b>";
 202      echo "<p> "._T('utf8_convert_verifier', array('rep' => joli_repertoire(_DIR_TMP)));
 203      effacer_meta('conversion_charset');
 204      ecrire_metas();
 205  
 206      // C'est fini, supprimer le fichier autorisant les modifs
 207      fin_admin($action);
 208      
 209      include_spip('inc/headers');
 210      // bouton "retour au site" + redirige_par_entete
 211      echo "<p align='right'> <a href='" . generer_url_ecrire("config_lang")
 212      . "'> &gt;&gt; "._T('icone_retour')."</a>";
 213  
 214      echo install_fin_html();
 215  }
 216  
 217  
 218  ?>


Généré le : Wed Nov 21 10:20:27 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics