]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/fixup_utf8.php
83e4850217b7ccf912e80ae1cb58b7a5d075f83c
[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     }
105
106     function fixupNotices($max_id, $min_id) {
107
108         // Do a separate DB connection
109
110         $sth = $this->dbu->prepare("UPDATE notice SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
111
112         if (PEAR::isError($sth)) {
113             echo "ERROR: " . $sth->getMessage() . "\n";
114             return;
115         }
116
117         $sql = 'SELECT id, content, rendered FROM notice ' .
118           'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
119           'AND modified < "'.$this->max_date.'" ';
120
121         if (!empty($max_id)) {
122             $sql .= ' AND id <= ' . $max_id;
123         }
124
125         if (!empty($min_id)) {
126             $sql .= ' AND id >= ' . $min_id;
127         }
128
129         $sql .= ' ORDER BY id DESC';
130
131         $rn = $this->dbl->query($sql);
132
133         if (PEAR::isError($rn)) {
134             echo "ERROR: " . $rn->getMessage() . "\n";
135             return;
136         }
137
138         echo "Number of rows: " . $rn->numRows() . "\n";
139
140         $notice = array();
141
142         while (DB_OK == $rn->fetchInto($notice)) {
143
144             $id = ($notice[0])+0;
145             $content = bin2hex($notice[1]);
146             $rendered = bin2hex($notice[2]);
147
148             echo "$id...";
149
150             $result =& $this->dbu->execute($sth, array($content, $rendered, $id));
151
152             if (PEAR::isError($result)) {
153                 echo "ERROR: " . $result->getMessage() . "\n";
154                 continue;
155             }
156
157             $cnt = $this->dbu->affectedRows();
158
159             if ($cnt != 1) {
160                 echo "ERROR: 0 rows affected\n";
161                 continue;
162             }
163
164             $notice = Notice::staticGet('id', $id);
165             $notice->decache();
166             $notice->free();
167
168             echo "OK\n";
169         }
170     }
171
172     function fixupProfiles()
173     {
174         // Do a separate DB connection
175
176         $sth = $this->dbu->prepare("UPDATE profile SET ".
177                                    "fullname = UNHEX(?),".
178                                    "location = UNHEX(?), ".
179                                    "bio = UNHEX(?) ".
180                                    "WHERE id = ?");
181
182         if (PEAR::isError($sth)) {
183             echo "ERROR: " . $sth->getMessage() . "\n";
184             return;
185         }
186
187         $sql = 'SELECT id, fullname, location, bio FROM profile ' .
188           'WHERE (LENGTH(fullname) != CHAR_LENGTH(fullname) '.
189           'OR LENGTH(location) != CHAR_LENGTH(location) '.
190           'OR LENGTH(bio) != CHAR_LENGTH(bio)) '.
191           'AND modified < "'.$this->max_date.'" '.
192           ' ORDER BY modified DESC';
193
194         $rn = $this->dbl->query($sql);
195
196         if (PEAR::isError($rn)) {
197             echo "ERROR: " . $rn->getMessage() . "\n";
198             return;
199         }
200
201         echo "Number of rows: " . $rn->numRows() . "\n";
202
203         $profile = array();
204
205         while (DB_OK == $rn->fetchInto($profile)) {
206
207             $id = ($profile[0])+0;
208             $fullname = bin2hex($profile[1]);
209             $location = bin2hex($profile[2]);
210             $bio = bin2hex($profile[3]);
211
212             echo "$id...";
213
214             $result =& $this->dbu->execute($sth, array($fullname, $location, $bio, $id));
215
216             if (PEAR::isError($result)) {
217                 echo "ERROR: " . $result->getMessage() . "\n";
218                 continue;
219             }
220
221             $cnt = $this->dbu->affectedRows();
222
223             if ($cnt != 1) {
224                 echo "ERROR: 0 rows affected\n";
225                 continue;
226             }
227
228             $profile = Profile::staticGet('id', $id);
229             $profile->decache();
230             $profile->free();
231
232             echo "OK\n";
233         }
234     }
235
236     function fixupGroups()
237     {
238         // Do a separate DB connection
239
240         $sth = $this->dbu->prepare("UPDATE user_group SET ".
241                                    "fullname = UNHEX(?),".
242                                    "location = UNHEX(?), ".
243                                    "description = UNHEX(?) ".
244                                    "WHERE id = ?");
245
246         if (PEAR::isError($sth)) {
247             echo "ERROR: " . $sth->getMessage() . "\n";
248             return;
249         }
250
251         $sql = 'SELECT id, fullname, location, description FROM user_group ' .
252           'WHERE LENGTH(fullname) != CHAR_LENGTH(fullname) '.
253           'OR LENGTH(location) != CHAR_LENGTH(location) '.
254           'OR LENGTH(description) != CHAR_LENGTH(description) ';
255           'AND modified < "'.$this->max_date.'" '.
256           'ORDER BY modified DESC';
257
258         $rn = $this->dbl->query($sql);
259
260         if (PEAR::isError($rn)) {
261             echo "ERROR: " . $rn->getMessage() . "\n";
262             return;
263         }
264
265         echo "Number of rows: " . $rn->numRows() . "\n";
266
267         $user_group = array();
268
269         while (DB_OK == $rn->fetchInto($user_group)) {
270
271             $id = ($user_group[0])+0;
272             $fullname = bin2hex($user_group[1]);
273             $location = bin2hex($user_group[2]);
274             $description = bin2hex($user_group[3]);
275
276             echo "$id...";
277
278             $result =& $this->dbu->execute($sth, array($fullname, $location, $description, $id));
279
280             if (PEAR::isError($result)) {
281                 echo "ERROR: " . $result->getMessage() . "\n";
282                 continue;
283             }
284
285             $cnt = $this->dbu->affectedRows();
286
287             if ($cnt != 1) {
288                 echo "ERROR: 0 rows affected\n";
289                 continue;
290             }
291
292             $user_group = User_group::staticGet('id', $id);
293             $user_group->decache();
294             $user_group->free();
295
296             echo "OK\n";
297         }
298     }
299
300     function fixupMessages() {
301
302         // Do a separate DB connection
303
304         $sth = $this->dbu->prepare("UPDATE message SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
305
306         if (PEAR::isError($sth)) {
307             echo "ERROR: " . $sth->getMessage() . "\n";
308             return;
309         }
310
311         $sql = 'SELECT id, content, rendered FROM message ' .
312           'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
313           'AND modified < "'.$this->max_date.'" '.
314           'ORDER BY id DESC';
315
316         $rn = $this->dbl->query($sql);
317
318         if (PEAR::isError($rn)) {
319             echo "ERROR: " . $rn->getMessage() . "\n";
320             return;
321         }
322
323         echo "Number of rows: " . $rn->numRows() . "\n";
324
325         $message = array();
326
327         while (DB_OK == $rn->fetchInto($message)) {
328
329             $id = ($message[0])+0;
330             $content = bin2hex($message[1]);
331             $rendered = bin2hex($message[2]);
332
333             echo "$id...";
334
335             $result =& $this->dbu->execute($sth, array($content, $rendered, $id));
336
337             if (PEAR::isError($result)) {
338                 echo "ERROR: " . $result->getMessage() . "\n";
339                 continue;
340             }
341
342             $cnt = $this->dbu->affectedRows();
343
344             if ($cnt != 1) {
345                 echo "ERROR: 0 rows affected\n";
346                 continue;
347             }
348
349             $message = Message::staticGet('id', $id);
350             $message->decache();
351             $message->free();
352
353             echo "OK\n";
354         }
355     }
356 }
357
358 $max_date = ($argc > 1) ? $argv[1] : null;
359 $max_id = ($argc > 2) ? $argv[2] : null;
360 $min_id = ($argc > 3) ? $argv[3] : null;
361
362 $fixer = new UTF8FixerUpper(array('max_date' => $max_date,
363                                   'max_notice' => $max_id,
364                                   'min_notice' => $min_id));
365
366 $fixer->fixup();
367