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';
37 updateSchemaPlugins();
39 // These replace old "fixup_*" scripts
41 fixupNoticeRendered();
42 fixupNoticeConversation();
53 require INSTALLDIR.'/db/core.php';
57 function updateSchemaCore()
59 printfnq("Upgrading core schema...");
61 $schema = Schema::get();
62 $schemaUpdater = new SchemaUpdater($schema);
63 foreach (tableDefs() as $table => $def) {
64 $schemaUpdater->register($table, $def);
66 $schemaUpdater->checkSchema();
71 function updateSchemaPlugins()
73 printfnq("Upgrading plugin schema...");
75 Event::handle('CheckSchema');
80 function fixupNoticeRendered()
82 printfnq("Ensuring all notices have rendered HTML...");
84 $notice = new Notice();
86 $notice->whereAdd('rendered IS NULL');
89 while ($notice->fetch()) {
90 $original = clone($notice);
91 $notice->rendered = common_render_content($notice->content, $notice);
92 $notice->update($original);
98 function fixupNoticeConversation()
100 printfnq("Ensuring all notices have a conversation ID...");
102 $notice = new Notice();
103 $notice->whereAdd('conversation is null');
104 $notice->orderBy('id'); // try to get originals before replies
107 while ($notice->fetch()) {
111 $orig = clone($notice);
113 if (empty($notice->reply_to)) {
114 $notice->conversation = $notice->id;
116 $reply = Notice::staticGet('id', $notice->reply_to);
119 $notice->conversation = $notice->id;
120 } else if (empty($reply->conversation)) {
121 $notice->conversation = $notice->id;
123 $notice->conversation = $reply->conversation;
130 $result = $notice->update($orig);
134 } catch (Exception $e) {
135 printv("Error setting conversation: " . $e->getMessage());
142 function fixupGroupURI()
144 printfnq("Ensuring all groups have an URI...");
146 $group = new User_group();
147 $group->whereAdd('uri IS NULL');
149 if ($group->find()) {
150 while ($group->fetch()) {
151 $orig = User_group::staticGet('id', $group->id);
152 $group->uri = $group->getUri();
153 $group->update($orig);
160 function initConversation()
162 printfnq("Ensuring all conversations have a row in conversation table...");
164 $notice = new Notice();
165 $notice->query('select distinct notice.conversation from notice '.
166 'where notice.conversation is not null '.
167 'and not exists (select conversation.id from conversation where id = notice.conversation)');
169 while ($notice->fetch()) {
171 $id = $notice->conversation;
173 $uri = common_local_url('conversation', array('id' => $id));
175 // @fixme db_dataobject won't save our value for an autoincrement
176 // so we're bypassing the insert wrappers
177 $conv = new Conversation();
178 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
182 $conv->escape(common_sql_now()));
191 printfnq("Ensuring all users have an inbox...");
194 $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
195 $user->orderBy('id');
199 while ($user->fetch()) {
202 $notice = new Notice();
204 $notice->selectAdd();
205 $notice->selectAdd('id');
206 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
207 $notice->whereAdd('subscription.subscriber = ' . $user->id);
208 $notice->whereAdd('notice.created >= subscription.created');
212 if ($notice->find()) {
213 while ($notice->fetch()) {
214 $ids[] = $notice->id;
220 $inbox = new Inbox();
221 $inbox->user_id = $user->id;
224 } catch (Exception $e) {
225 printv("Error initializing inbox: " . $e->getMessage());
233 function initLocalGroup()
235 printfnq("Ensuring all local user groups have a local_group...");
237 $group = new User_group();
238 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
241 while ($group->fetch()) {
243 // Hack to check for local groups
244 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
245 $lg = new Local_group();
247 $lg->group_id = $group->id;
248 $lg->nickname = $group->nickname;
249 $lg->created = $group->created; // XXX: common_sql_now() ?
250 $lg->modified = $group->modified;
254 } catch (Exception $e) {
255 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
262 function initNoticeReshare()
264 printfnq("Ensuring all reshares have the correct verb and object-type...");
266 $notice = new Notice();
267 $notice->whereAdd('repeat_of is not null');
268 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
270 if ($notice->find()) {
271 while ($notice->fetch()) {
273 $orig = Notice::staticGet('id', $notice->id);
274 $notice->verb = ActivityVerb::SHARE;
275 $notice->object_type = ActivityObject::ACTIVITY;
276 $notice->update($orig);
277 } catch (Exception $e) {
278 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());