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();
52 initSubscriptionURI();
57 Event::handle('EndUpgrade');
64 require INSTALLDIR.'/db/core.php';
68 function updateSchemaCore()
70 printfnq("Upgrading core schema...");
72 $schema = Schema::get();
73 $schemaUpdater = new SchemaUpdater($schema);
74 foreach (tableDefs() as $table => $def) {
75 $schemaUpdater->register($table, $def);
77 $schemaUpdater->checkSchema();
82 function updateSchemaPlugins()
84 printfnq("Upgrading plugin schema...");
86 Event::handle('CheckSchema');
91 function fixupNoticeRendered()
93 printfnq("Ensuring all notices have rendered HTML...");
95 $notice = new Notice();
97 $notice->whereAdd('rendered IS NULL');
100 while ($notice->fetch()) {
101 $original = clone($notice);
102 $notice->rendered = common_render_content($notice->content, $notice);
103 $notice->update($original);
109 function fixupNoticeConversation()
111 printfnq("Ensuring all notices have a conversation ID...");
113 $notice = new Notice();
114 $notice->whereAdd('conversation is null');
115 $notice->orderBy('id'); // try to get originals before replies
118 while ($notice->fetch()) {
122 $orig = clone($notice);
124 if (empty($notice->reply_to)) {
125 $notice->conversation = $notice->id;
127 $reply = Notice::getKV('id', $notice->reply_to);
130 $notice->conversation = $notice->id;
131 } else if (empty($reply->conversation)) {
132 $notice->conversation = $notice->id;
134 $notice->conversation = $reply->conversation;
141 $result = $notice->update($orig);
145 } catch (Exception $e) {
146 printv("Error setting conversation: " . $e->getMessage());
153 function fixupGroupURI()
155 printfnq("Ensuring all groups have an URI...");
157 $group = new User_group();
158 $group->whereAdd('uri IS NULL');
160 if ($group->find()) {
161 while ($group->fetch()) {
162 $orig = User_group::getKV('id', $group->id);
163 $group->uri = $group->getUri();
164 $group->update($orig);
171 function initConversation()
173 printfnq("Ensuring all conversations have a row in conversation table...");
175 $notice = new Notice();
176 $notice->query('select distinct notice.conversation from notice '.
177 'where notice.conversation is not null '.
178 'and not exists (select conversation.id from conversation where id = notice.conversation)');
180 while ($notice->fetch()) {
182 $id = $notice->conversation;
184 $uri = common_local_url('conversation', array('id' => $id));
186 // @fixme db_dataobject won't save our value for an autoincrement
187 // so we're bypassing the insert wrappers
188 $conv = new Conversation();
189 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
193 $conv->escape(common_sql_now()));
202 printfnq("Ensuring all users have an inbox...");
205 $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
206 $user->orderBy('id');
210 while ($user->fetch()) {
213 $notice = new Notice();
215 $notice->selectAdd();
216 $notice->selectAdd('id');
217 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
218 $notice->whereAdd('subscription.subscriber = ' . $user->id);
219 $notice->whereAdd('notice.created >= subscription.created');
223 if ($notice->find()) {
224 while ($notice->fetch()) {
225 $ids[] = $notice->id;
231 $inbox = new Inbox();
232 $inbox->user_id = $user->id;
235 } catch (Exception $e) {
236 printv("Error initializing inbox: " . $e->getMessage());
244 function initLocalGroup()
246 printfnq("Ensuring all local user groups have a local_group...");
248 $group = new User_group();
249 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
252 while ($group->fetch()) {
254 // Hack to check for local groups
255 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
256 $lg = new Local_group();
258 $lg->group_id = $group->id;
259 $lg->nickname = $group->nickname;
260 $lg->created = $group->created; // XXX: common_sql_now() ?
261 $lg->modified = $group->modified;
265 } catch (Exception $e) {
266 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
273 function initNoticeReshare()
275 printfnq("Ensuring all reshares have the correct verb and object-type...");
277 $notice = new Notice();
278 $notice->whereAdd('repeat_of is not null');
279 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
281 if ($notice->find()) {
282 while ($notice->fetch()) {
284 $orig = Notice::getKV('id', $notice->id);
285 $notice->verb = ActivityVerb::SHARE;
286 $notice->object_type = ActivityObject::ACTIVITY;
287 $notice->update($orig);
288 } catch (Exception $e) {
289 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
297 function initFaveURI()
299 printfnq("Ensuring all faves have a URI...");
302 $fave->whereAdd('uri IS NULL');
305 while ($fave->fetch()) {
308 $fave->query(sprintf('update fave '.
311 'where user_id = %d '.
312 'and notice_id = %d',
313 Fave::newURI($fave->user_id, $fave->notice_id, $fave->modified),
314 common_sql_date(strtotime($fave->modified)),
317 } catch (Exception $e) {
318 common_log(LOG_ERR, "Error updated fave URI: " . $e->getMessage());
326 function initSubscriptionURI()
328 printfnq("Ensuring all subscriptions have a URI...");
330 $sub = new Subscription();
331 $sub->whereAdd('uri IS NULL');
334 while ($sub->fetch()) {
337 $sub->query(sprintf('update subscription '.
339 'where subscriber = %d '.
340 'and subscribed = %d',
341 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
344 } catch (Exception $e) {
345 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
353 function initGroupMemberURI()
355 printfnq("Ensuring all group memberships have a URI...");
357 $mem = new Group_member();
358 $mem->whereAdd('uri IS NULL');
361 while ($mem->fetch()) {
364 $mem->query(sprintf('update group_member set uri = "%s" '.
365 'where profile_id = %d ' .
366 'and group_id = %d ',
367 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
370 } catch (Exception $e) {
371 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
379 function initProfileLists()
381 printfnq("Ensuring all profile tags have a corresponding list...");
383 $ptag = new Profile_tag();
385 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
386 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
387 'where profile_tag.tagger = profile_list.tagger '.
388 'and profile_tag.tag = profile_list.tag)');
389 $ptag->groupBy('tagger, tag');
390 $ptag->orderBy('tagger, tag');
393 while ($ptag->fetch()) {
394 $plist = new Profile_list();
396 $plist->tagger = $ptag->tagger;
397 $plist->tag = $ptag->tag;
399 $plist->created = common_sql_now();
400 $plist->modified = $plist->created;
401 $plist->mainpage = common_local_url('showprofiletag',
402 array('tagger' => $plist->getTagger()->nickname,
403 'tag' => $plist->tag));;
405 $plist->tagged_count = $ptag->tagged_count;
406 $plist->subscriber_count = 0;
410 $orig = clone($plist);
411 // After insert since it uses auto-generated ID
412 $plist->uri = common_local_url('profiletagbyid',
413 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
415 $plist->update($orig);