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 initSubscriptionURI();
58 require INSTALLDIR.'/db/core.php';
62 function updateSchemaCore()
64 printfnq("Upgrading core schema...");
66 $schema = Schema::get();
67 $schemaUpdater = new SchemaUpdater($schema);
68 foreach (tableDefs() as $table => $def) {
69 $schemaUpdater->register($table, $def);
71 $schemaUpdater->checkSchema();
76 function updateSchemaPlugins()
78 printfnq("Upgrading plugin schema...");
80 Event::handle('CheckSchema');
85 function fixupNoticeRendered()
87 printfnq("Ensuring all notices have rendered HTML...");
89 $notice = new Notice();
91 $notice->whereAdd('rendered IS NULL');
94 while ($notice->fetch()) {
95 $original = clone($notice);
96 $notice->rendered = common_render_content($notice->content, $notice);
97 $notice->update($original);
103 function fixupNoticeConversation()
105 printfnq("Ensuring all notices have a conversation ID...");
107 $notice = new Notice();
108 $notice->whereAdd('conversation is null');
109 $notice->orderBy('id'); // try to get originals before replies
112 while ($notice->fetch()) {
116 $orig = clone($notice);
118 if (empty($notice->reply_to)) {
119 $notice->conversation = $notice->id;
121 $reply = Notice::staticGet('id', $notice->reply_to);
124 $notice->conversation = $notice->id;
125 } else if (empty($reply->conversation)) {
126 $notice->conversation = $notice->id;
128 $notice->conversation = $reply->conversation;
135 $result = $notice->update($orig);
139 } catch (Exception $e) {
140 printv("Error setting conversation: " . $e->getMessage());
147 function fixupGroupURI()
149 printfnq("Ensuring all groups have an URI...");
151 $group = new User_group();
152 $group->whereAdd('uri IS NULL');
154 if ($group->find()) {
155 while ($group->fetch()) {
156 $orig = User_group::staticGet('id', $group->id);
157 $group->uri = $group->getUri();
158 $group->update($orig);
165 function initConversation()
167 printfnq("Ensuring all conversations have a row in conversation table...");
169 $notice = new Notice();
170 $notice->query('select distinct notice.conversation from notice '.
171 'where notice.conversation is not null '.
172 'and not exists (select conversation.id from conversation where id = notice.conversation)');
174 while ($notice->fetch()) {
176 $id = $notice->conversation;
178 $uri = common_local_url('conversation', array('id' => $id));
180 // @fixme db_dataobject won't save our value for an autoincrement
181 // so we're bypassing the insert wrappers
182 $conv = new Conversation();
183 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
187 $conv->escape(common_sql_now()));
196 printfnq("Ensuring all users have an inbox...");
199 $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
200 $user->orderBy('id');
204 while ($user->fetch()) {
207 $notice = new Notice();
209 $notice->selectAdd();
210 $notice->selectAdd('id');
211 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
212 $notice->whereAdd('subscription.subscriber = ' . $user->id);
213 $notice->whereAdd('notice.created >= subscription.created');
217 if ($notice->find()) {
218 while ($notice->fetch()) {
219 $ids[] = $notice->id;
225 $inbox = new Inbox();
226 $inbox->user_id = $user->id;
229 } catch (Exception $e) {
230 printv("Error initializing inbox: " . $e->getMessage());
238 function initLocalGroup()
240 printfnq("Ensuring all local user groups have a local_group...");
242 $group = new User_group();
243 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
246 while ($group->fetch()) {
248 // Hack to check for local groups
249 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
250 $lg = new Local_group();
252 $lg->group_id = $group->id;
253 $lg->nickname = $group->nickname;
254 $lg->created = $group->created; // XXX: common_sql_now() ?
255 $lg->modified = $group->modified;
259 } catch (Exception $e) {
260 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
267 function initNoticeReshare()
269 printfnq("Ensuring all reshares have the correct verb and object-type...");
271 $notice = new Notice();
272 $notice->whereAdd('repeat_of is not null');
273 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
275 if ($notice->find()) {
276 while ($notice->fetch()) {
278 $orig = Notice::staticGet('id', $notice->id);
279 $notice->verb = ActivityVerb::SHARE;
280 $notice->object_type = ActivityObject::ACTIVITY;
281 $notice->update($orig);
282 } catch (Exception $e) {
283 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
291 function initFaveURI()
293 printfnq("Ensuring all faves have a URI...");
296 $fave->whereAdd('uri IS NULL');
299 while ($fave->fetch()) {
302 $fave->query(sprintf('update fave '.
305 'where user_id = %d '.
306 'and notice_id = %d',
307 Fave::newURI($fave->user_id, $fave->notice_id, $fave->modified),
308 common_sql_date(strtotime($fave->modified)),
311 } catch (Exception $e) {
312 common_log(LOG_ERR, "Error updated fave URI: " . $e->getMessage());
320 function initSubscriptionURI()
322 printfnq("Ensuring all subscriptions have a URI...");
324 $sub = new Subscription();
325 $sub->whereAdd('uri IS NULL');
328 while ($sub->fetch()) {
331 $sub->query(sprintf('update subscription '.
333 'where subscriber = %d '.
334 'and subscribed = %d',
335 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
338 } catch (Exception $e) {
339 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
347 function initGroupMemberURI()
349 printfnq("Ensuring all group memberships have a URI...");
351 $mem = new Group_member();
352 $mem->whereAdd('uri IS NULL');
355 while ($mem->fetch()) {
358 $mem->query(sprintf('update group_member set uri = "%s" '.
359 'where profile_id = %d ' .
360 'and group_id = %d ',
361 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
364 } catch (Exception $e) {
365 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());