4 * Laconica - a distributed open-source microblogging tool
5 * Copyright (C) 2009, Control Yourself, Inc.
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.
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.
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/>.
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";
27 ini_set("max_execution_time", "0");
28 ini_set("max_input_time", "0");
30 mb_internal_encoding('UTF-8');
32 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
33 define('LACONICA', true);
35 require_once(INSTALLDIR . '/lib/common.php');
36 require_once('DB.php');
44 function __construct($args)
48 if (array_key_exists('max_date', $args)) {
49 $this->max_date = strftime('%Y-%m-%d %H:%M:%S', strtotime($args['max_date']));
51 $this->max_date = strftime('%Y-%m-%d %H:%M:%S', time());
54 $this->dbl = $this->doConnect('latin1');
56 if (empty($this->dbl)) {
60 $this->dbu = $this->doConnect('utf8');
62 if (empty($this->dbu)) {
67 function doConnect($charset)
69 $db = DB::connect(common_config('db', 'database'),
70 array('persistent' => false));
72 if (PEAR::isError($db)) {
73 echo "ERROR: " . $db->getMessage() . "\n";
77 $conn = $db->connection;
79 $succ = mysqli_set_charset($conn, $charset);
82 echo "ERROR: couldn't set charset\n";
87 $result = $db->autoCommit(true);
89 if (PEAR::isError($result)) {
90 echo "ERROR: " . $result->getMessage() . "\n";
100 $this->fixupNotices($this->args['max_notice'],
101 $this->args['min_notice']);
102 $this->fixupProfiles();
103 $this->fixupGroups();
104 $this->fixupMessages();
107 function fixupNotices($max_id, $min_id) {
109 // Do a separate DB connection
111 $sth = $this->dbu->prepare("UPDATE notice SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
113 if (PEAR::isError($sth)) {
114 echo "ERROR: " . $sth->getMessage() . "\n";
118 $sql = 'SELECT id, content, rendered FROM notice ' .
119 'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
120 'AND modified < "'.$this->max_date.'" ';
122 if (!empty($max_id)) {
123 $sql .= ' AND id <= ' . $max_id;
126 if (!empty($min_id)) {
127 $sql .= ' AND id >= ' . $min_id;
130 $sql .= ' ORDER BY id DESC';
132 $rn = $this->dbl->query($sql);
134 if (PEAR::isError($rn)) {
135 echo "ERROR: " . $rn->getMessage() . "\n";
139 echo "Number of rows: " . $rn->numRows() . "\n";
143 while (DB_OK == $rn->fetchInto($notice)) {
145 $id = ($notice[0])+0;
146 $content = bin2hex($notice[1]);
147 $rendered = bin2hex($notice[2]);
151 $result =& $this->dbu->execute($sth, array($content, $rendered, $id));
153 if (PEAR::isError($result)) {
154 echo "ERROR: " . $result->getMessage() . "\n";
158 $cnt = $this->dbu->affectedRows();
161 echo "ERROR: 0 rows affected\n";
165 $notice = Notice::staticGet('id', $id);
173 function fixupProfiles()
175 // Do a separate DB connection
177 $sth = $this->dbu->prepare("UPDATE profile SET ".
178 "fullname = UNHEX(?),".
179 "location = UNHEX(?), ".
183 if (PEAR::isError($sth)) {
184 echo "ERROR: " . $sth->getMessage() . "\n";
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';
195 $rn = $this->dbl->query($sql);
197 if (PEAR::isError($rn)) {
198 echo "ERROR: " . $rn->getMessage() . "\n";
202 echo "Number of rows: " . $rn->numRows() . "\n";
206 while (DB_OK == $rn->fetchInto($profile)) {
208 $id = ($profile[0])+0;
209 $fullname = bin2hex($profile[1]);
210 $location = bin2hex($profile[2]);
211 $bio = bin2hex($profile[3]);
215 $result =& $this->dbu->execute($sth, array($fullname, $location, $bio, $id));
217 if (PEAR::isError($result)) {
218 echo "ERROR: " . $result->getMessage() . "\n";
222 $cnt = $this->dbu->affectedRows();
225 echo "ERROR: 0 rows affected\n";
229 $profile = Profile::staticGet('id', $id);
237 function fixupGroups()
239 // Do a separate DB connection
241 $sth = $this->dbu->prepare("UPDATE user_group SET ".
242 "fullname = UNHEX(?),".
243 "location = UNHEX(?), ".
244 "description = UNHEX(?) ".
247 if (PEAR::isError($sth)) {
248 echo "ERROR: " . $sth->getMessage() . "\n";
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';
259 $rn = $this->dbl->query($sql);
261 if (PEAR::isError($rn)) {
262 echo "ERROR: " . $rn->getMessage() . "\n";
266 echo "Number of rows: " . $rn->numRows() . "\n";
268 $user_group = array();
270 while (DB_OK == $rn->fetchInto($user_group)) {
272 $id = ($user_group[0])+0;
273 $fullname = bin2hex($user_group[1]);
274 $location = bin2hex($user_group[2]);
275 $description = bin2hex($user_group[3]);
279 $result =& $this->dbu->execute($sth, array($fullname, $location, $description, $id));
281 if (PEAR::isError($result)) {
282 echo "ERROR: " . $result->getMessage() . "\n";
286 $cnt = $this->dbu->affectedRows();
289 echo "ERROR: 0 rows affected\n";
293 $user_group = User_group::staticGet('id', $id);
294 $user_group->decache();
301 function fixupMessages() {
303 // Do a separate DB connection
305 $sth = $this->dbu->prepare("UPDATE message SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
307 if (PEAR::isError($sth)) {
308 echo "ERROR: " . $sth->getMessage() . "\n";
312 $sql = 'SELECT id, content, rendered FROM message ' .
313 'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
314 'AND modified < "'.$this->max_date.'" '.
317 $rn = $this->dbl->query($sql);
319 if (PEAR::isError($rn)) {
320 echo "ERROR: " . $rn->getMessage() . "\n";
324 echo "Number of rows: " . $rn->numRows() . "\n";
328 while (DB_OK == $rn->fetchInto($message)) {
330 $id = ($message[0])+0;
331 $content = bin2hex($message[1]);
332 $rendered = bin2hex($message[2]);
336 $result =& $this->dbu->execute($sth, array($content, $rendered, $id));
338 if (PEAR::isError($result)) {
339 echo "ERROR: " . $result->getMessage() . "\n";
343 $cnt = $this->dbu->affectedRows();
346 echo "ERROR: 0 rows affected\n";
350 $message = Message::staticGet('id', $id);
359 $max_date = ($argc > 1) ? $argv[1] : null;
360 $max_id = ($argc > 2) ? $argv[2] : null;
361 $min_id = ($argc > 3) ? $argv[3] : null;
363 $fixer = new UTF8FixerUpper(array('max_date' => $max_date,
364 'max_notice' => $max_id,
365 'min_notice' => $min_id));