didn't want to commit this
[platal.git] / upgrade / 0.9.14 / geoloc.utf8.php
CommitLineData
3a824ce3 1#!/usr/bin/php5
2<?php
3
0380bf85 4global $globals;
3a824ce3 5require_once 'connect.db.inc.php';
0380bf85 6$globals->dbcharset = 'latin1';
3a824ce3 7
8function is_utf8($s)
9{
10 return @iconv('utf-8', 'utf-8', $s) == $s;
11}
12
13$tables = array ('city', 'city_in_maps', 'maps', 'pays', 'region');
3a824ce3 14foreach ($tables as $table) {
15 $res = XDB::query("SELECT * FROM geoloc_$table");
16 if (!$res) {
17 echo "$table\n";
18 continue;
19 }
20 $all = $res->fetchAllAssoc();
21 foreach ($all as &$array) {
22 $from = array();
23 $to = array();
24 foreach ($array as $key=>$value) {
f62bd784 25 $from[] = $key . '="' . XDB::escape($value) . '"';
3a824ce3 26 $valued = utf8_decode($value);
27 if (is_utf8($value) && $valued != $value) {
f62bd784 28 $to[] = $key . '="' . XDB::escape($valued) .'"';
3a824ce3 29 }
30 }
31 if (!empty($to)) {
32 $to = implode(', ', $to);
33 $from = implode(' AND ', $from);
34 $sql = "UPDATE geoloc_$table SET $to WHERE $from";
35 if (!XDB::execute($sql)) {
36 echo "Echec : $sql\n";
37 }
38 }
39 }
40}
41
42?>