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();
52 require INSTALLDIR.'/db/core.php';
56 function updateSchemaCore()
58 printfnq("Upgrading core schema...");
60 $schema = Schema::get();
61 $schemaUpdater = new SchemaUpdater($schema);
62 foreach (tableDefs() as $table => $def) {
63 $schemaUpdater->register($table, $def);
65 $schemaUpdater->checkSchema();
70 function updateSchemaPlugins()
72 printfnq("Upgrading plugin schema...");
74 Event::handle('CheckSchema');
79 function fixupNoticeRendered()
81 printfnq("Ensuring all notices have rendered HTML...");
83 $notice = new Notice();
85 $notice->whereAdd('rendered IS NULL');
88 while ($notice->fetch()) {
89 $original = clone($notice);
90 $notice->rendered = common_render_content($notice->content, $notice);
91 $notice->update($original);
97 function fixupNoticeConversation()
99 printfnq("Ensuring all notices have a conversation ID...");
101 $notice = new Notice();
102 $notice->whereAdd('conversation is null');
103 $notice->orderBy('id'); // try to get originals before replies
106 while ($notice->fetch()) {
110 $orig = clone($notice);
112 if (empty($notice->reply_to)) {
113 $notice->conversation = $notice->id;
115 $reply = Notice::staticGet('id', $notice->reply_to);
118 $notice->conversation = $notice->id;
119 } else if (empty($reply->conversation)) {
120 $notice->conversation = $notice->id;
122 $notice->conversation = $reply->conversation;
129 $result = $notice->update($orig);
133 } catch (Exception $e) {
134 printv("Error setting conversation: " . $e->getMessage());
141 function fixupGroupURI()
143 printfnq("Ensuring all groups have an URI...");
145 $group = new User_group();
146 $group->whereAdd('uri IS NULL');
148 if ($group->find()) {
149 while ($group->fetch()) {
150 $orig = User_group::staticGet('id', $group->id);
151 $group->uri = $group->getUri();
152 $group->update($orig);
159 function initConversation()
161 printfnq("Ensuring all conversations have a row in conversation table...");
163 $notice = new Notice();
164 $notice->query('select distinct notice.conversation from notice '.
165 'where notice.conversation is not null '.
166 'and not exists (select conversation.id from conversation where id = notice.conversation)');
168 while ($notice->fetch()) {
170 $id = $notice->conversation;
172 $uri = common_local_url('conversation', array('id' => $id));
174 // @fixme db_dataobject won't save our value for an autoincrement
175 // so we're bypassing the insert wrappers
176 $conv = new Conversation();
177 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
181 $conv->escape(common_sql_now()));
190 printfnq("Ensuring all users have an inbox...");
193 $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
194 $user->orderBy('id');
198 while ($user->fetch()) {
201 $notice = new Notice();
203 $notice->selectAdd();
204 $notice->selectAdd('id');
205 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
206 $notice->whereAdd('subscription.subscriber = ' . $user->id);
207 $notice->whereAdd('notice.created >= subscription.created');
211 if ($notice->find()) {
212 while ($notice->fetch()) {
213 $ids[] = $notice->id;
219 $inbox = new Inbox();
220 $inbox->user_id = $user->id;
223 } catch (Exception $e) {
224 printv("Error initializing inbox: " . $e->getMessage());
232 function initLocalGroup()
234 printfnq("Ensuring all local user groups have a local_group...");
236 $group = new User_group();
237 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
240 while ($group->fetch()) {
242 // Hack to check for local groups
243 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
244 $lg = new Local_group();
246 $lg->group_id = $group->id;
247 $lg->nickname = $group->nickname;
248 $lg->created = $group->created; // XXX: common_sql_now() ?
249 $lg->modified = $group->modified;
253 } catch (Exception $e) {
254 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());