]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/fixup_utf8.php
Merge branch '0.8.x' of git@gitorious.org:+laconica-developers/laconica/dev into...
[quix0rs-gnu-social.git] / scripts / fixup_utf8.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * Laconica - a distributed open-source microblogging tool
5  * Copyright (C) 2009, Control Yourself, Inc.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 # Abort if called from a web server
22 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
23     print "This script must be run from the command line\n";
24     exit(1);
25 }
26
27 ini_set("max_execution_time", "0");
28 ini_set("max_input_time", "0");
29 set_time_limit(0);
30 mb_internal_encoding('UTF-8');
31
32 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
33 define('LACONICA', true);
34
35 require_once(INSTALLDIR . '/lib/common.php');
36 require_once('DB.php');
37
38 class UTF8FixerUpper
39 {
40     var $dbl = null;
41     var $dbu = null;
42     var $args = array();
43
44     function __construct($args)
45     {
46         $this->args = $args;
47
48         if (array_key_exists('max_date', $args)) {
49             $this->max_date = strftime('%Y-%m-%d %H:%M:%S', strtotime($args['max_date']));
50         } else {
51             $this->max_date = strftime('%Y-%m-%d %H:%M:%S', time());
52         }
53
54         $this->dbl = $this->doConnect('latin1');
55
56         if (empty($this->dbl)) {
57             return;
58         }
59
60         $this->dbu = $this->doConnect('utf8');
61
62         if (empty($this->dbu)) {
63             return;
64         }
65     }
66
67     function doConnect($charset)
68     {
69         $db = DB::connect(common_config('db', 'database'),
70                           array('persistent' => false));
71
72         if (PEAR::isError($db)) {
73             echo "ERROR: " . $db->getMessage() . "\n";
74             return NULL;
75         }
76
77         $conn = $db->connection;
78
79         $succ = mysqli_set_charset($conn, $charset);
80
81         if (!$succ) {
82             echo "ERROR: couldn't set charset\n";
83             $db->disconnect();
84             return NULL;
85         }
86
87         $result = $db->autoCommit(true);
88
89         if (PEAR::isError($result)) {
90             echo "ERROR: " . $result->getMessage() . "\n";
91             $db->disconnect();
92             return NULL;
93         }
94
95         return $db;
96     }
97
98     function fixup()
99     {
100         $this->fixupNotices($this->args['max_notice'],
101                             $this->args['min_notice']);
102         $this->fixupProfiles();
103         $this->fixupGroups();
104         $this->fixupMessages();
105     }
106
107     function fixupNotices($max_id, $min_id) {
108
109         // Do a separate DB connection
110
111         $sth = $this->dbu->prepare("UPDATE notice SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
112
113         if (PEAR::isError($sth)) {
114             echo "ERROR: " . $sth->getMessage() . "\n";
115             return;
116         }
117
118         $sql = 'SELECT id, content, rendered FROM notice ' .
119           'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
120           'AND modified < "'.$this->max_date.'" ';
121
122         if (!empty($max_id)) {
123             $sql .= ' AND id <= ' . $max_id;
124         }
125
126         if (!empty($min_id)) {
127             $sql .= ' AND id >= ' . $min_id;
128         }
129
130         $sql .= ' ORDER BY id DESC';
131
132         $rn = $this->dbl->query($sql);
133
134         if (PEAR::isError($rn)) {
135             echo "ERROR: " . $rn->getMessage() . "\n";
136             return;
137         }
138
139         echo "Number of rows: " . $rn->numRows() . "\n";
140
141         $notice = array();
142
143         while (DB_OK == $rn->fetchInto($notice)) {
144
145             $id = ($notice[0])+0;
146             $content = bin2hex($notice[1]);
147             $rendered = bin2hex($notice[2]);
148
149             echo "$id...";
150
151             $result =& $this->dbu->execute($sth, array($content, $rendered, $id));
152
153             if (PEAR::isError($result)) {
154                 echo "ERROR: " . $result->getMessage() . "\n";
155                 continue;
156             }
157
158             $cnt = $this->dbu->affectedRows();
159
160             if ($cnt != 1) {
161                 echo "ERROR: 0 rows affected\n";
162                 continue;
163             }
164
165             $notice = Notice::staticGet('id', $id);
166             $notice->decache();
167             $notice->free();
168
169             echo "OK\n";
170         }
171     }
172
173     function fixupProfiles()
174     {
175         // Do a separate DB connection
176
177         $sth = $this->dbu->prepare("UPDATE profile SET ".
178                                    "fullname = UNHEX(?),".
179                                    "location = UNHEX(?), ".
180                                    "bio = UNHEX(?) ".
181                                    "WHERE id = ?");
182
183         if (PEAR::isError($sth)) {
184             echo "ERROR: " . $sth->getMessage() . "\n";
185             return;
186         }
187
188         $sql = 'SELECT id, fullname, location, bio FROM profile ' .
189           'WHERE (LENGTH(fullname) != CHAR_LENGTH(fullname) '.
190           'OR LENGTH(location) != CHAR_LENGTH(location) '.
191           'OR LENGTH(bio) != CHAR_LENGTH(bio)) '.
192           'AND modified < "'.$this->max_date.'" '.
193           ' ORDER BY modified DESC';
194
195         $rn = $this->dbl->query($sql);
196
197         if (PEAR::isError($rn)) {
198             echo "ERROR: " . $rn->getMessage() . "\n";
199             return;
200         }
201
202         echo "Number of rows: " . $rn->numRows() . "\n";
203
204         $profile = array();
205
206         while (DB_OK == $rn->fetchInto($profile)) {
207
208             $id = ($profile[0])+0;
209             $fullname = bin2hex($profile[1]);
210             $location = bin2hex($profile[2]);
211             $bio = bin2hex($profile[3]);
212
213             echo "$id...";
214
215             $result =& $this->dbu->execute($sth, array($fullname, $location, $bio, $id));
216
217             if (PEAR::isError($result)) {
218                 echo "ERROR: " . $result->getMessage() . "\n";
219                 continue;
220             }
221
222             $cnt = $this->dbu->affectedRows();
223
224             if ($cnt != 1) {
225                 echo "ERROR: 0 rows affected\n";
226                 continue;
227             }
228
229             $profile = Profile::staticGet('id', $id);
230             $profile->decache();
231             $profile->free();
232
233             echo "OK\n";
234         }
235     }
236
237     function fixupGroups()
238     {
239         // Do a separate DB connection
240
241         $sth = $this->dbu->prepare("UPDATE user_group SET ".
242                                    "fullname = UNHEX(?),".
243                                    "location = UNHEX(?), ".
244                                    "description = UNHEX(?) ".
245                                    "WHERE id = ?");
246
247         if (PEAR::isError($sth)) {
248             echo "ERROR: " . $sth->getMessage() . "\n";
249             return;
250         }
251
252         $sql = 'SELECT id, fullname, location, description FROM user_group ' .
253           'WHERE LENGTH(fullname) != CHAR_LENGTH(fullname) '.
254           'OR LENGTH(location) != CHAR_LENGTH(location) '.
255           'OR LENGTH(description) != CHAR_LENGTH(description) ';
256           'AND modified < "'.$this->max_date.'" '.
257           'ORDER BY modified DESC';
258
259         $rn = $this->dbl->query($sql);
260
261         if (PEAR::isError($rn)) {
262             echo "ERROR: " . $rn->getMessage() . "\n";
263             return;
264         }
265
266         echo "Number of rows: " . $rn->numRows() . "\n";
267
268         $user_group = array();
269
270         while (DB_OK == $rn->fetchInto($user_group)) {
271
272             $id = ($user_group[0])+0;
273             $fullname = bin2hex($user_group[1]);
274             $location = bin2hex($user_group[2]);
275             $description = bin2hex($user_group[3]);
276
277             echo "$id...";
278
279             $result =& $this->dbu->execute($sth, array($fullname, $location, $description, $id));
280
281             if (PEAR::isError($result)) {
282                 echo "ERROR: " . $result->getMessage() . "\n";
283                 continue;
284             }
285
286             $cnt = $this->dbu->affectedRows();
287
288             if ($cnt != 1) {
289                 echo "ERROR: 0 rows affected\n";
290                 continue;
291             }
292
293             $user_group = User_group::staticGet('id', $id);
294             $user_group->decache();
295             $user_group->free();
296
297             echo "OK\n";
298         }
299     }
300
301     function fixupMessages() {
302
303         // Do a separate DB connection
304
305         $sth = $this->dbu->prepare("UPDATE message SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
306
307         if (PEAR::isError($sth)) {
308             echo "ERROR: " . $sth->getMessage() . "\n";
309             return;
310         }
311
312         $sql = 'SELECT id, content, rendered FROM message ' .
313           'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
314           'AND modified < "'.$this->max_date.'" '.
315           'ORDER BY id DESC';
316
317         $rn = $this->dbl->query($sql);
318
319         if (PEAR::isError($rn)) {
320             echo "ERROR: " . $rn->getMessage() . "\n";
321             return;
322         }
323
324         echo "Number of rows: " . $rn->numRows() . "\n";
325
326         $message = array();
327
328         while (DB_OK == $rn->fetchInto($message)) {
329
330             $id = ($message[0])+0;
331             $content = bin2hex($message[1]);
332             $rendered = bin2hex($message[2]);
333
334             echo "$id...";
335
336             $result =& $this->dbu->execute($sth, array($content, $rendered, $id));
337
338             if (PEAR::isError($result)) {
339                 echo "ERROR: " . $result->getMessage() . "\n";
340                 continue;
341             }
342
343             $cnt = $this->dbu->affectedRows();
344
345             if ($cnt != 1) {
346                 echo "ERROR: 0 rows affected\n";
347                 continue;
348             }
349
350             $message = Message::staticGet('id', $id);
351             $message->decache();
352             $message->free();
353
354             echo "OK\n";
355         }
356     }
357 }
358
359 $max_date = ($argc > 1) ? $argv[1] : null;
360 $max_id = ($argc > 2) ? $argv[2] : null;
361 $min_id = ($argc > 3) ? $argv[3] : null;
362
363 $fixer = new UTF8FixerUpper(array('max_date' => $max_date,
364                                   'max_notice' => $max_id,
365                                   'min_notice' => $min_id));
366
367 $fixer->fixup();
368