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();
51 require INSTALLDIR.'/db/core.php';
55 function updateSchemaCore()
57 printfnq("Upgrading core schema...");
59 $schema = Schema::get();
60 $schemaUpdater = new SchemaUpdater($schema);
61 foreach (tableDefs() as $table => $def) {
62 $schemaUpdater->register($table, $def);
64 $schemaUpdater->checkSchema();
69 function updateSchemaPlugins()
71 printfnq("Upgrading plugin schema...");
73 Event::handle('CheckSchema');
78 function fixupNoticeRendered()
80 printfnq("Ensuring all notices have rendered HTML...");
82 $notice = new Notice();
84 $notice->whereAdd('rendered IS NULL');
87 while ($notice->fetch()) {
88 $original = clone($notice);
89 $notice->rendered = common_render_content($notice->content, $notice);
90 $notice->update($original);
96 function fixupNoticeConversation()
98 printfnq("Ensuring all notices have a conversation ID...");
100 $notice = new Notice();
101 $notice->whereAdd('conversation is null');
102 $notice->orderBy('id'); // try to get originals before replies
105 while ($notice->fetch()) {
109 $orig = clone($notice);
111 if (empty($notice->reply_to)) {
112 $notice->conversation = $notice->id;
114 $reply = Notice::staticGet('id', $notice->reply_to);
117 $notice->conversation = $notice->id;
118 } else if (empty($reply->conversation)) {
119 $notice->conversation = $notice->id;
121 $notice->conversation = $reply->conversation;
128 $result = $notice->update($orig);
132 } catch (Exception $e) {
133 printv("Error setting conversation: " . $e->getMessage());
140 function fixupGroupURI()
142 printfnq("Ensuring all groups have an URI...");
144 $group = new User_group();
145 $group->whereAdd('uri IS NULL');
147 if ($group->find()) {
148 while ($group->fetch()) {
149 $orig = User_group::staticGet('id', $group->id);
150 $group->uri = $group->getUri();
151 $group->update($orig);
158 function initConversation()
160 printfnq("Ensuring all conversations have a row in conversation table...");
162 $notice = new Notice();
163 $notice->query('select distinct notice.conversation from notice '.
164 'where notice.conversation is not null '.
165 'and not exists (select conversation.id from conversation where id = notice.conversation)');
167 while ($notice->fetch()) {
169 $id = $notice->conversation;
171 $uri = common_local_url('conversation', array('id' => $id));
173 // @fixme db_dataobject won't save our value for an autoincrement
174 // so we're bypassing the insert wrappers
175 $conv = new Conversation();
176 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
180 $conv->escape(common_sql_now()));
189 printfnq("Ensuring all users have an inbox...");
192 $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
193 $user->orderBy('id');
197 while ($user->fetch()) {
200 $notice = new Notice();
202 $notice->selectAdd();
203 $notice->selectAdd('id');
204 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
205 $notice->whereAdd('subscription.subscriber = ' . $user->id);
206 $notice->whereAdd('notice.created >= subscription.created');
210 if ($notice->find()) {
211 while ($notice->fetch()) {
212 $ids[] = $notice->id;
218 $inbox = new Inbox();
219 $inbox->user_id = $user->id;
222 } catch (Exception $e) {
223 printv("Error initializing inbox: " . $e->getMessage());