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();
106 function fixupNotices($max_id, $min_id) {
108 // Do a separate DB connection
110 $sth = $this->dbu->prepare("UPDATE notice SET content = UNHEX(?), rendered = UNHEX(?) WHERE id = ?");
112 if (PEAR::isError($sth)) {
113 echo "ERROR: " . $sth->getMessage() . "\n";
117 $sql = 'SELECT id, content, rendered FROM notice ' .
118 'WHERE LENGTH(content) != CHAR_LENGTH(content) '.
119 'AND modified < "'.$this->max_date.'" ';
121 if (!empty($max_id)) {
122 $sql .= ' AND id <= ' . $max_id;
125 if (!empty($min_id)) {
126 $sql .= ' AND id >= ' . $min_id;
129 $sql .= ' ORDER BY id DESC';
131 $rn = $this->dbl->query($sql);
133 if (PEAR::isError($rn)) {
134 echo "ERROR: " . $rn->getMessage() . "\n";
138 echo "Number of rows: " . $rn->numRows() . "\n";
142 while (DB_OK == $rn->fetchInto($notice)) {
144 $id = ($notice[0])+0;
145 $content = bin2hex($notice[1]);
146 $rendered = bin2hex($notice[2]);
150 $result =& $this->dbu->execute($sth, array($content, $rendered, $id));
152 if (PEAR::isError($result)) {
153 echo "ERROR: " . $result->getMessage() . "\n";
157 $cnt = $this->dbu->affectedRows();
160 echo "ERROR: 0 rows affected\n";
164 $notice = Notice::staticGet('id', $id);
172 function fixupProfiles()
174 // Do a separate DB connection
176 $sth = $this->dbu->prepare("UPDATE profile SET ".
177 "fullname = UNHEX(?),".
178 "location = UNHEX(?), ".
182 if (PEAR::isError($sth)) {
183 echo "ERROR: " . $sth->getMessage() . "\n";
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';
194 $rn = $this->dbl->query($sql);
196 if (PEAR::isError($rn)) {
197 echo "ERROR: " . $rn->getMessage() . "\n";
201 echo "Number of rows: " . $rn->numRows() . "\n";
205 while (DB_OK == $rn->fetchInto($profile)) {
207 $id = ($profile[0])+0;
208 $fullname = bin2hex($profile[1]);
209 $location = bin2hex($profile[2]);
210 $bio = bin2hex($profile[3]);
214 $result =& $this->dbu->execute($sth, array($fullname, $location, $bio, $id));
216 if (PEAR::isError($result)) {
217 echo "ERROR: " . $result->getMessage() . "\n";
221 $cnt = $this->dbu->affectedRows();
224 echo "ERROR: 0 rows affected\n";
228 $profile = Profile::staticGet('id', $id);
236 function fixupGroups()
238 // Do a separate DB connection
240 $sth = $this->dbu->prepare("UPDATE user_group SET ".
241 "fullname = UNHEX(?),".
242 "location = UNHEX(?), ".
243 "description = UNHEX(?) ".
246 if (PEAR::isError($sth)) {
247 echo "ERROR: " . $sth->getMessage() . "\n";
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';
258 $rn = $this->dbl->query($sql);
260 if (PEAR::isError($rn)) {
261 echo "ERROR: " . $rn->getMessage() . "\n";
265 echo "Number of rows: " . $rn->numRows() . "\n";
267 $user_group = array();
269 while (DB_OK == $rn->fetchInto($user_group)) {
271 $id = ($user_group[0])+0;
272 $fullname = bin2hex($user_group[1]);
273 $location = bin2hex($user_group[2]);
274 $description = bin2hex($user_group[3]);
278 $result =& $this->dbu->execute($sth, array($fullname, $location, $description, $id));
280 if (PEAR::isError($result)) {
281 echo "ERROR: " . $result->getMessage() . "\n";
285 $cnt = $this->dbu->affectedRows();
288 echo "ERROR: 0 rows affected\n";
292 $user_group = User_group::staticGet('id', $id);
293 $user_group->decache();
301 $max_date = ($argc > 1) ? $argv[1] : null;
302 $max_id = ($argc > 2) ? $argv[2] : null;
303 $min_id = ($argc > 3) ? $argv[3] : null;
305 $fixer = new UTF8FixerUpper(array('max_date' => $max_date,
306 'max_notice' => $max_id,
307 'min_notice' => $min_id));