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';
36 if (Event::handle('StartUpgrade')) {
38 updateSchemaPlugins();
40 // These replace old "fixup_*" scripts
42 fixupNoticeRendered();
43 fixupNoticeConversation();
53 initSubscriptionURI();
58 Event::handle('EndUpgrade');
65 require INSTALLDIR.'/db/core.php';
69 function updateSchemaCore()
71 printfnq("Upgrading core schema...");
73 $schema = Schema::get();
74 $schemaUpdater = new SchemaUpdater($schema);
75 foreach (tableDefs() as $table => $def) {
76 $schemaUpdater->register($table, $def);
78 $schemaUpdater->checkSchema();
83 function updateSchemaPlugins()
85 printfnq("Upgrading plugin schema...");
87 Event::handle('CheckSchema');
92 function fixupNoticeRendered()
94 printfnq("Ensuring all notices have rendered HTML...");
96 $notice = new Notice();
98 $notice->whereAdd('rendered IS NULL');
101 while ($notice->fetch()) {
102 $original = clone($notice);
103 $notice->rendered = common_render_content($notice->content, $notice);
104 $notice->update($original);
110 function fixupNoticeConversation()
112 printfnq("Ensuring all notices have a conversation ID...");
114 $notice = new Notice();
115 $notice->whereAdd('conversation is null');
116 $notice->orderBy('id'); // try to get originals before replies
119 while ($notice->fetch()) {
123 $orig = clone($notice);
125 if (empty($notice->reply_to)) {
126 $notice->conversation = $notice->id;
128 $reply = Notice::getKV('id', $notice->reply_to);
131 $notice->conversation = $notice->id;
132 } else if (empty($reply->conversation)) {
133 $notice->conversation = $notice->id;
135 $notice->conversation = $reply->conversation;
142 $result = $notice->update($orig);
146 } catch (Exception $e) {
147 printv("Error setting conversation: " . $e->getMessage());
154 function fixupGroupURI()
156 printfnq("Ensuring all groups have an URI...");
158 $group = new User_group();
159 $group->whereAdd('uri IS NULL');
161 if ($group->find()) {
162 while ($group->fetch()) {
163 $orig = User_group::getKV('id', $group->id);
164 $group->uri = $group->getUri();
165 $group->update($orig);
172 function initConversation()
174 printfnq("Ensuring all conversations have a row in conversation table...");
176 $notice = new Notice();
177 $notice->query('select distinct notice.conversation from notice '.
178 'where notice.conversation is not null '.
179 'and not exists (select conversation.id from conversation where id = notice.conversation)');
181 while ($notice->fetch()) {
183 $id = $notice->conversation;
185 $uri = common_local_url('conversation', array('id' => $id));
187 // @fixme db_dataobject won't save our value for an autoincrement
188 // so we're bypassing the insert wrappers
189 $conv = new Conversation();
190 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
194 $conv->escape(common_sql_now()));
203 printfnq("Ensuring all users have an inbox...");
206 $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
207 $user->orderBy('id');
211 while ($user->fetch()) {
214 $notice = new Notice();
216 $notice->selectAdd();
217 $notice->selectAdd('id');
218 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
219 $notice->whereAdd('subscription.subscriber = ' . $user->id);
220 $notice->whereAdd('notice.created >= subscription.created');
224 if ($notice->find()) {
225 while ($notice->fetch()) {
226 $ids[] = $notice->id;
232 $inbox = new Inbox();
233 $inbox->user_id = $user->id;
236 } catch (Exception $e) {
237 printv("Error initializing inbox: " . $e->getMessage());
245 function initGroupProfileId()
247 printfnq("Ensuring all User_group entries have a Profile and profile_id...");
249 $group = new User_group();
250 $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
253 while ($group->fetch()) {
255 // We must create a new, incrementally assigned profile_id
256 $profile = new Profile();
257 $profile->nickname = $group->nickname;
258 $profile->fullname = $group->fullname;
259 $profile->profileurl = $group->mainpage;
260 $profile->homepage = $group->homepage;
261 $profile->bio = $group->description;
262 $profile->location = $group->location;
263 $profile->created = $group->created;
264 $profile->modified = $group->modified;
266 $profile->query('BEGIN');
267 $id = $profile->insert();
269 $profile->query('ROLLBACK');
270 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
272 $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
273 $profile->query('COMMIT');
276 } catch (Exception $e) {
277 printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
284 function initLocalGroup()
286 printfnq("Ensuring all local user groups have a local_group...");
288 $group = new User_group();
289 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
292 while ($group->fetch()) {
294 // Hack to check for local groups
295 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
296 $lg = new Local_group();
298 $lg->group_id = $group->id;
299 $lg->nickname = $group->nickname;
300 $lg->created = $group->created; // XXX: common_sql_now() ?
301 $lg->modified = $group->modified;
305 } catch (Exception $e) {
306 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
313 function initNoticeReshare()
315 printfnq("Ensuring all reshares have the correct verb and object-type...");
317 $notice = new Notice();
318 $notice->whereAdd('repeat_of is not null');
319 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
321 if ($notice->find()) {
322 while ($notice->fetch()) {
324 $orig = Notice::getKV('id', $notice->id);
325 $notice->verb = ActivityVerb::SHARE;
326 $notice->object_type = ActivityObject::ACTIVITY;
327 $notice->update($orig);
328 } catch (Exception $e) {
329 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
337 function initFaveURI()
339 printfnq("Ensuring all faves have a URI...");
342 $fave->whereAdd('uri IS NULL');
345 while ($fave->fetch()) {
348 $fave->query(sprintf('update fave '.
351 'where user_id = %d '.
352 'and notice_id = %d',
353 Fave::newURI($fave->user_id, $fave->notice_id, $fave->modified),
354 common_sql_date(strtotime($fave->modified)),
357 } catch (Exception $e) {
358 common_log(LOG_ERR, "Error updated fave URI: " . $e->getMessage());
366 function initSubscriptionURI()
368 printfnq("Ensuring all subscriptions have a URI...");
370 $sub = new Subscription();
371 $sub->whereAdd('uri IS NULL');
374 while ($sub->fetch()) {
377 $sub->query(sprintf('update subscription '.
379 'where subscriber = %d '.
380 'and subscribed = %d',
381 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
384 } catch (Exception $e) {
385 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
393 function initGroupMemberURI()
395 printfnq("Ensuring all group memberships have a URI...");
397 $mem = new Group_member();
398 $mem->whereAdd('uri IS NULL');
401 while ($mem->fetch()) {
404 $mem->query(sprintf('update group_member set uri = "%s" '.
405 'where profile_id = %d ' .
406 'and group_id = %d ',
407 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
410 } catch (Exception $e) {
411 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
419 function initProfileLists()
421 printfnq("Ensuring all profile tags have a corresponding list...");
423 $ptag = new Profile_tag();
425 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
426 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
427 'where profile_tag.tagger = profile_list.tagger '.
428 'and profile_tag.tag = profile_list.tag)');
429 $ptag->groupBy('tagger, tag');
430 $ptag->orderBy('tagger, tag');
433 while ($ptag->fetch()) {
434 $plist = new Profile_list();
436 $plist->tagger = $ptag->tagger;
437 $plist->tag = $ptag->tag;
439 $plist->created = common_sql_now();
440 $plist->modified = $plist->created;
441 $plist->mainpage = common_local_url('showprofiletag',
442 array('tagger' => $plist->getTagger()->nickname,
443 'tag' => $plist->tag));;
445 $plist->tagged_count = $ptag->tagged_count;
446 $plist->subscriber_count = 0;
450 $orig = clone($plist);
451 // After insert since it uses auto-generated ID
452 $plist->uri = common_local_url('profiletagbyid',
453 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
455 $plist->update($orig);