4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2009, StatusNet, 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
23 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
25 $helptext = <<<ENDOFHELP
26 fixup_utf8.php <maxdate> <maxid> <minid>
28 Fixup records in a database that stored the data incorrectly (pre-0.7.4 for StatusNet).
32 require_once INSTALLDIR.'/scripts/commandline.inc';
33 require_once 'DB.php';
41 function __construct($args)
45 if (!empty($args['max_date'])) {
46 $this->max_date = strftime('%Y-%m-%d %H:%M:%S', strtotime($args['max_date']));
48 $this->max_date = strftime('%Y-%m-%d %H:%M:%S', time());
51 $this->dbl = $this->doConnect('latin1');
53 if (empty($this->dbl)) {
57 $this->dbu = $this->doConnect('utf8');
59 if (empty($this->dbu)) {
64 function doConnect($charset)
66 $db = DB::connect(common_config('db', 'database'),
67 array('persistent' => false));
69 if (PEAR::isError($db)) {
70 echo "ERROR: " . $db->getMessage() . "\n";
74 $conn = $db->connection;
76 $succ = mysqli_set_charset($conn, $charset);
79 echo "ERROR: couldn't set charset\n";
84 $result = $db->autoCommit(true);
86 if (PEAR::isError($result)) {
87 echo "ERROR: " . $result->getMessage() . "\n";
97 $this->fixupNotices($this->args['max_notice'],
98 $this->args['min_notice']);
99 $this->fixupProfiles();
100 $this->fixupGroups();
101 $this->fixupMessages();
104 function fixupNotices($max_id, $min_id) {
106 // Do a separate DB connection
108 $sth = $this->dbu->prepare("UPDATE notice SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
110 if (PEAR::isError($sth)) {
111 echo "ERROR: " . $sth->getMessage() . "\n";
115 $sql = 'SELECT id, content, rendered FROM notice ' .
116 'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
117 'AND modified < "'.$this->max_date.'" ';
119 if (!empty($max_id)) {
120 $sql .= ' AND id <= ' . $max_id;
123 if (!empty($min_id)) {
124 $sql .= ' AND id >= ' . $min_id;
127 $sql .= ' ORDER BY id DESC';
129 $rn = $this->dbl->query($sql);
131 if (PEAR::isError($rn)) {
132 echo "ERROR: " . $rn->getMessage() . "\n";
136 echo "Number of rows: " . $rn->numRows() . "\n";
140 while (DB_OK == $rn->fetchInto($notice)) {
142 $id = ($notice[0])+0;
143 $content = bin2hex($notice[1]);
144 $rendered = bin2hex($notice[2]);
148 $result = $this->dbu->execute($sth, array($content, $rendered, $id));
150 if (PEAR::isError($result)) {
151 echo "ERROR: " . $result->getMessage() . "\n";
155 $cnt = $this->dbu->affectedRows();
158 echo "ERROR: 0 rows affected\n";
162 $notice = Notice::staticGet('id', $id);
170 function fixupProfiles()
172 // Do a separate DB connection
174 $sth = $this->dbu->prepare("UPDATE profile SET ".
175 "fullname = UNHEX(?),".
176 "location = UNHEX(?), ".
180 if (PEAR::isError($sth)) {
181 echo "ERROR: " . $sth->getMessage() . "\n";
185 $sql = 'SELECT id, fullname, location, bio FROM profile ' .
186 'WHERE (LENGTH(fullname) != CHAR_LENGTH(fullname) '.
187 'OR LENGTH(location) != CHAR_LENGTH(location) '.
188 'OR LENGTH(bio) != CHAR_LENGTH(bio)) '.
189 'AND modified < "'.$this->max_date.'" '.
190 ' ORDER BY modified DESC';
192 $rn = $this->dbl->query($sql);
194 if (PEAR::isError($rn)) {
195 echo "ERROR: " . $rn->getMessage() . "\n";
199 echo "Number of rows: " . $rn->numRows() . "\n";
203 while (DB_OK == $rn->fetchInto($profile)) {
205 $id = ($profile[0])+0;
206 $fullname = bin2hex($profile[1]);
207 $location = bin2hex($profile[2]);
208 $bio = bin2hex($profile[3]);
212 $result = $this->dbu->execute($sth, array($fullname, $location, $bio, $id));
214 if (PEAR::isError($result)) {
215 echo "ERROR: " . $result->getMessage() . "\n";
219 $cnt = $this->dbu->affectedRows();
222 echo "ERROR: 0 rows affected\n";
226 $profile = Profile::staticGet('id', $id);
234 function fixupGroups()
236 // Do a separate DB connection
238 $sth = $this->dbu->prepare("UPDATE user_group SET ".
239 "fullname = UNHEX(?),".
240 "location = UNHEX(?), ".
241 "description = UNHEX(?) ".
244 if (PEAR::isError($sth)) {
245 echo "ERROR: " . $sth->getMessage() . "\n";
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';
256 $rn = $this->dbl->query($sql);
258 if (PEAR::isError($rn)) {
259 echo "ERROR: " . $rn->getMessage() . "\n";
263 echo "Number of rows: " . $rn->numRows() . "\n";
265 $user_group = array();
267 while (DB_OK == $rn->fetchInto($user_group)) {
269 $id = ($user_group[0])+0;
270 $fullname = bin2hex($user_group[1]);
271 $location = bin2hex($user_group[2]);
272 $description = bin2hex($user_group[3]);
276 $result = $this->dbu->execute($sth, array($fullname, $location, $description, $id));
278 if (PEAR::isError($result)) {
279 echo "ERROR: " . $result->getMessage() . "\n";
283 $cnt = $this->dbu->affectedRows();
286 echo "ERROR: 0 rows affected\n";
290 $user_group = User_group::staticGet('id', $id);
291 $user_group->decache();
298 function fixupMessages() {
300 // Do a separate DB connection
302 $sth = $this->dbu->prepare("UPDATE message SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
304 if (PEAR::isError($sth)) {
305 echo "ERROR: " . $sth->getMessage() . "\n";
309 $sql = 'SELECT id, content, rendered FROM message ' .
310 'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
311 'AND modified < "'.$this->max_date.'" '.
314 $rn = $this->dbl->query($sql);
316 if (PEAR::isError($rn)) {
317 echo "ERROR: " . $rn->getMessage() . "\n";
321 echo "Number of rows: " . $rn->numRows() . "\n";
325 while (DB_OK == $rn->fetchInto($message)) {
327 $id = ($message[0])+0;
328 $content = bin2hex($message[1]);
329 $rendered = bin2hex($message[2]);
333 $result = $this->dbu->execute($sth, array($content, $rendered, $id));
335 if (PEAR::isError($result)) {
336 echo "ERROR: " . $result->getMessage() . "\n";
340 $cnt = $this->dbu->affectedRows();
343 echo "ERROR: 0 rows affected\n";
347 $message = Message::staticGet('id', $id);
356 $max_date = (count($args) > 0) ? $args[0] : null;
357 $max_id = (count($args) > 1) ? $args[1] : null;
358 $min_id = (count($args) > 2) ? $args[2] : null;
360 $fixer = new UTF8FixerUpper(array('max_date' => $max_date,
361 'max_notice' => $max_id,
362 'min_notice' => $min_id));