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();
50 require INSTALLDIR.'/db/core.php';
54 function updateSchemaCore()
56 printfnq("Upgrading core schema...");
58 $schema = Schema::get();
59 $schemaUpdater = new SchemaUpdater($schema);
60 foreach (tableDefs() as $table => $def) {
61 $schemaUpdater->register($table, $def);
63 $schemaUpdater->checkSchema();
68 function updateSchemaPlugins()
70 printfnq("Upgrading plugin schema...");
72 Event::handle('CheckSchema');
77 function fixupNoticeRendered()
79 printfnq("Ensuring all notices have rendered HTML...");
81 $notice = new Notice();
83 $notice->whereAdd('rendered IS NULL');
86 while ($notice->fetch()) {
87 $original = clone($notice);
88 $notice->rendered = common_render_content($notice->content, $notice);
89 $notice->update($original);
95 function fixupNoticeConversation()
97 printfnq("Ensuring all notices have a conversation ID...");
99 $notice = new Notice();
100 $notice->whereAdd('conversation is null');
101 $notice->orderBy('id'); // try to get originals before replies
104 while ($notice->fetch()) {
108 $orig = clone($notice);
110 if (empty($notice->reply_to)) {
111 $notice->conversation = $notice->id;
113 $reply = Notice::staticGet('id', $notice->reply_to);
116 $notice->conversation = $notice->id;
117 } else if (empty($reply->conversation)) {
118 $notice->conversation = $notice->id;
120 $notice->conversation = $reply->conversation;
127 $result = $notice->update($orig);
131 } catch (Exception $e) {
132 printv("Error setting conversation: " . $e->getMessage());
139 function fixupGroupURI()
141 printfnq("Ensuring all groups have an URI...");
143 $group = new User_group();
144 $group->whereAdd('uri IS NULL');
146 if ($group->find()) {
147 while ($group->fetch()) {
148 $orig = User_group::staticGet('id', $group->id);
149 $group->uri = $group->getUri();
150 $group->update($orig);
157 function initConversation()
159 printfnq("Ensuring all conversations have a row in conversation table...");
161 $notice = new Notice();
162 $notice->query('select distinct notice.conversation from notice '.
163 'where notice.conversation is not null '.
164 'and not exists (select conversation.id from conversation where id = notice.conversation)');
166 while ($notice->fetch()) {
168 $id = $notice->conversation;
170 $uri = common_local_url('conversation', array('id' => $id));
172 // @fixme db_dataobject won't save our value for an autoincrement
173 // so we're bypassing the insert wrappers
174 $conv = new Conversation();
175 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
179 $conv->escape(common_sql_now()));