]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/fixup_utf8.php
2046d2b770a6ea34fc61c02ea174d29bfd61fe87
[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
167             echo "OK\n";
168         }
169     }
170
171     function fixupProfiles()
172     {
173         // Do a separate DB connection
174
175         $sth = $this->dbu->prepare("UPDATE profile SET ".
176                                    "fullname = UNHEX(?),".
177                                    "location = UNHEX(?), ".
178                                    "bio = UNHEX(?) ".
179                                    "WHERE id = ?");
180
181         if (PEAR::isError($sth)) {
182             echo "ERROR: " . $sth->getMessage() . "\n";
183             return;
184         }
185
186         $sql = 'SELECT id, fullname, location, bio FROM profile ' .
187           'WHERE (LENGTH(fullname) != CHAR_LENGTH(fullname) '.
188           'OR LENGTH(location) != CHAR_LENGTH(location) '.
189           'OR LENGTH(bio) != CHAR_LENGTH(bio)) '.
190           'AND modified < "'.$this->max_date.'" '.
191           ' ORDER BY modified DESC';
192
193         $rn = $this->dbl->query($sql);
194
195         if (PEAR::isError($rn)) {
196             echo "ERROR: " . $rn->getMessage() . "\n";
197             return;
198         }
199
200         echo "Number of rows: " . $rn->numRows() . "\n";
201
202         $profile = array();
203
204         while (DB_OK == $rn->fetchInto($profile)) {
205
206             $id = ($profile[0])+0;
207             $fullname = bin2hex($profile[1]);
208             $location = bin2hex($profile[2]);
209             $bio = bin2hex($profile[3]);
210
211             echo "$id...";
212
213             $result =& $this->dbu->execute($sth, array($fullname, $location, $bio, $id));
214
215             if (PEAR::isError($result)) {
216                 echo "ERROR: " . $result->getMessage() . "\n";
217                 continue;
218             }
219
220             $cnt = $this->dbu->affectedRows();
221
222             if ($cnt != 1) {
223                 echo "ERROR: 0 rows affected\n";
224                 continue;
225             }
226
227             $profile = Profile::staticGet('id', $id);
228             $profile->decache();
229
230             echo "OK\n";
231         }
232     }
233
234     function fixupGroups()
235     {
236         // Do a separate DB connection
237
238         $sth = $this->dbu->prepare("UPDATE user_group SET ".
239                                    "fullname = UNHEX(?),".
240                                    "location = UNHEX(?), ".
241                                    "description = UNHEX(?) ".
242                                    "WHERE id = ?");
243
244         if (PEAR::isError($sth)) {
245             echo "ERROR: " . $sth->getMessage() . "\n";
246             return;
247         }
248
249         $sql = 'SELECT id, fullname, location, description FROM user_group ' .
250           'WHERE LENGTH(fullname) != CHAR_LENGTH(fullname) '.
251           'OR LENGTH(location) != CHAR_LENGTH(location) '.
252           'OR LENGTH(description) != CHAR_LENGTH(description) ';
253           'AND modified < "'.$this->max_date.'" '.
254           'ORDER BY modified DESC';
255
256         $rn = $this->dbl->query($sql);
257
258         if (PEAR::isError($rn)) {
259             echo "ERROR: " . $rn->getMessage() . "\n";
260             return;
261         }
262
263         echo "Number of rows: " . $rn->numRows() . "\n";
264
265         $user_group = array();
266
267         while (DB_OK == $rn->fetchInto($user_group)) {
268
269             $id = ($user_group[0])+0;
270             $fullname = bin2hex($user_group[1]);
271             $location = bin2hex($user_group[2]);
272             $description = bin2hex($user_group[3]);
273
274             echo "$id...";
275
276             $result =& $this->dbu->execute($sth, array($fullname, $location, $description, $id));
277
278             if (PEAR::isError($result)) {
279                 echo "ERROR: " . $result->getMessage() . "\n";
280                 continue;
281             }
282
283             $cnt = $this->dbu->affectedRows();
284
285             if ($cnt != 1) {
286                 echo "ERROR: 0 rows affected\n";
287                 continue;
288             }
289
290             $user_group = User_group::staticGet('id', $id);
291             $user_group->decache();
292
293             echo "OK\n";
294         }
295     }
296 }
297
298 $max_date = ($argc > 1) ? $argv[1] : null;
299 $max_id = ($argc > 2) ? $argv[2] : null;
300 $min_id = ($argc > 3) ? $argv[3] : null;
301
302 $fixer = new UTF8FixerUpper(array('max_date' => $max_date,
303                                   'max_notice' => $max_id,
304                                   'min_notice' => $min_id));
305
306 $fixer->fixup();
307