4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2008-2011 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 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
23 $shortoptions = 'x::';
24 $longoptions = array('extensions=');
26 $helptext = <<<END_OF_UPGRADE_HELP
27 php upgrade.php [options]
28 Upgrade database schema and data to latest software
32 require_once INSTALLDIR.'/scripts/commandline.inc';
36 if (Event::handle('StartUpgrade')) {
38 updateSchemaPlugins();
40 // These replace old "fixup_*" scripts
42 fixupNoticeRendered();
43 fixupNoticeConversation();
52 initSubscriptionURI();
55 Event::handle('EndUpgrade');
62 require INSTALLDIR.'/db/core.php';
66 function updateSchemaCore()
68 printfnq("Upgrading core schema...");
70 $schema = Schema::get();
71 $schemaUpdater = new SchemaUpdater($schema);
72 foreach (tableDefs() as $table => $def) {
73 $schemaUpdater->register($table, $def);
75 $schemaUpdater->checkSchema();
80 function updateSchemaPlugins()
82 printfnq("Upgrading plugin schema...");
84 Event::handle('CheckSchema');
89 function fixupNoticeRendered()
91 printfnq("Ensuring all notices have rendered HTML...");
93 $notice = new Notice();
95 $notice->whereAdd('rendered IS NULL');
98 while ($notice->fetch()) {
99 $original = clone($notice);
100 $notice->rendered = common_render_content($notice->content, $notice);
101 $notice->update($original);
107 function fixupNoticeConversation()
109 printfnq("Ensuring all notices have a conversation ID...");
111 $notice = new Notice();
112 $notice->whereAdd('conversation is null');
113 $notice->orderBy('id'); // try to get originals before replies
116 while ($notice->fetch()) {
120 $orig = clone($notice);
122 if (empty($notice->reply_to)) {
123 $notice->conversation = $notice->id;
125 $reply = Notice::staticGet('id', $notice->reply_to);
128 $notice->conversation = $notice->id;
129 } else if (empty($reply->conversation)) {
130 $notice->conversation = $notice->id;
132 $notice->conversation = $reply->conversation;
139 $result = $notice->update($orig);
143 } catch (Exception $e) {
144 printv("Error setting conversation: " . $e->getMessage());
151 function fixupGroupURI()
153 printfnq("Ensuring all groups have an URI...");
155 $group = new User_group();
156 $group->whereAdd('uri IS NULL');
158 if ($group->find()) {
159 while ($group->fetch()) {
160 $orig = User_group::staticGet('id', $group->id);
161 $group->uri = $group->getUri();
162 $group->update($orig);
169 function initConversation()
171 printfnq("Ensuring all conversations have a row in conversation table...");
173 $notice = new Notice();
174 $notice->query('select distinct notice.conversation from notice '.
175 'where notice.conversation is not null '.
176 'and not exists (select conversation.id from conversation where id = notice.conversation)');
178 while ($notice->fetch()) {
180 $id = $notice->conversation;
182 $uri = common_local_url('conversation', array('id' => $id));
184 // @fixme db_dataobject won't save our value for an autoincrement
185 // so we're bypassing the insert wrappers
186 $conv = new Conversation();
187 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
191 $conv->escape(common_sql_now()));
200 printfnq("Ensuring all users have an inbox...");
203 $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
204 $user->orderBy('id');
208 while ($user->fetch()) {
211 $notice = new Notice();
213 $notice->selectAdd();
214 $notice->selectAdd('id');
215 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
216 $notice->whereAdd('subscription.subscriber = ' . $user->id);
217 $notice->whereAdd('notice.created >= subscription.created');
221 if ($notice->find()) {
222 while ($notice->fetch()) {
223 $ids[] = $notice->id;
229 $inbox = new Inbox();
230 $inbox->user_id = $user->id;
233 } catch (Exception $e) {
234 printv("Error initializing inbox: " . $e->getMessage());
242 function initLocalGroup()
244 printfnq("Ensuring all local user groups have a local_group...");
246 $group = new User_group();
247 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
250 while ($group->fetch()) {
252 // Hack to check for local groups
253 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
254 $lg = new Local_group();
256 $lg->group_id = $group->id;
257 $lg->nickname = $group->nickname;
258 $lg->created = $group->created; // XXX: common_sql_now() ?
259 $lg->modified = $group->modified;
263 } catch (Exception $e) {
264 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
271 function initNoticeReshare()
273 printfnq("Ensuring all reshares have the correct verb and object-type...");
275 $notice = new Notice();
276 $notice->whereAdd('repeat_of is not null');
277 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
279 if ($notice->find()) {
280 while ($notice->fetch()) {
282 $orig = Notice::staticGet('id', $notice->id);
283 $notice->verb = ActivityVerb::SHARE;
284 $notice->object_type = ActivityObject::ACTIVITY;
285 $notice->update($orig);
286 } catch (Exception $e) {
287 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
295 function initFaveURI()
297 printfnq("Ensuring all faves have a URI...");
300 $fave->whereAdd('uri IS NULL');
303 while ($fave->fetch()) {
306 $fave->query(sprintf('update fave '.
309 'where user_id = %d '.
310 'and notice_id = %d',
311 Fave::newURI($fave->user_id, $fave->notice_id, $fave->modified),
312 common_sql_date(strtotime($fave->modified)),
315 } catch (Exception $e) {
316 common_log(LOG_ERR, "Error updated fave URI: " . $e->getMessage());
324 function initSubscriptionURI()
326 printfnq("Ensuring all subscriptions have a URI...");
328 $sub = new Subscription();
329 $sub->whereAdd('uri IS NULL');
332 while ($sub->fetch()) {
335 $sub->query(sprintf('update subscription '.
337 'where subscriber = %d '.
338 'and subscribed = %d',
339 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
342 } catch (Exception $e) {
343 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
351 function initGroupMemberURI()
353 printfnq("Ensuring all group memberships have a URI...");
355 $mem = new Group_member();
356 $mem->whereAdd('uri IS NULL');
359 while ($mem->fetch()) {
362 $mem->query(sprintf('update group_member set uri = "%s" '.
363 'where profile_id = %d ' .
364 'and group_id = %d ',
365 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
368 } catch (Exception $e) {
369 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());