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()));
201 function initGroupProfileId()
203 printfnq("Ensuring all User_group entries have a Profile and profile_id...");
205 $group = new User_group();
206 $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
209 while ($group->fetch()) {
211 // We must create a new, incrementally assigned profile_id
212 $profile = new Profile();
213 $profile->nickname = $group->nickname;
214 $profile->fullname = $group->fullname;
215 $profile->profileurl = $group->mainpage;
216 $profile->homepage = $group->homepage;
217 $profile->bio = $group->description;
218 $profile->location = $group->location;
219 $profile->created = $group->created;
220 $profile->modified = $group->modified;
222 $profile->query('BEGIN');
223 $id = $profile->insert();
225 $profile->query('ROLLBACK');
226 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
228 $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
229 $profile->query('COMMIT');
232 } catch (Exception $e) {
233 printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
240 function initLocalGroup()
242 printfnq("Ensuring all local user groups have a local_group...");
244 $group = new User_group();
245 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
248 while ($group->fetch()) {
250 // Hack to check for local groups
251 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
252 $lg = new Local_group();
254 $lg->group_id = $group->id;
255 $lg->nickname = $group->nickname;
256 $lg->created = $group->created; // XXX: common_sql_now() ?
257 $lg->modified = $group->modified;
261 } catch (Exception $e) {
262 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
269 function initNoticeReshare()
271 printfnq("Ensuring all reshares have the correct verb and object-type...");
273 $notice = new Notice();
274 $notice->whereAdd('repeat_of is not null');
275 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
277 if ($notice->find()) {
278 while ($notice->fetch()) {
280 $orig = Notice::getKV('id', $notice->id);
281 $notice->verb = ActivityVerb::SHARE;
282 $notice->object_type = ActivityObject::ACTIVITY;
283 $notice->update($orig);
284 } catch (Exception $e) {
285 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
293 function initFaveURI()
295 printfnq("Ensuring all faves have a URI...");
298 $fave->whereAdd('uri IS NULL');
301 while ($fave->fetch()) {
304 $fave->query(sprintf('update fave '.
307 'where user_id = %d '.
308 'and notice_id = %d',
309 Fave::newURI($fave->user_id, $fave->notice_id, $fave->modified),
310 common_sql_date(strtotime($fave->modified)),
313 } catch (Exception $e) {
314 common_log(LOG_ERR, "Error updated fave URI: " . $e->getMessage());
322 function initSubscriptionURI()
324 printfnq("Ensuring all subscriptions have a URI...");
326 $sub = new Subscription();
327 $sub->whereAdd('uri IS NULL');
330 while ($sub->fetch()) {
333 $sub->query(sprintf('update subscription '.
335 'where subscriber = %d '.
336 'and subscribed = %d',
337 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
340 } catch (Exception $e) {
341 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
349 function initGroupMemberURI()
351 printfnq("Ensuring all group memberships have a URI...");
353 $mem = new Group_member();
354 $mem->whereAdd('uri IS NULL');
357 while ($mem->fetch()) {
360 $mem->query(sprintf('update group_member set uri = "%s" '.
361 'where profile_id = %d ' .
362 'and group_id = %d ',
363 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
366 } catch (Exception $e) {
367 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
375 function initProfileLists()
377 printfnq("Ensuring all profile tags have a corresponding list...");
379 $ptag = new Profile_tag();
381 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
382 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
383 'where profile_tag.tagger = profile_list.tagger '.
384 'and profile_tag.tag = profile_list.tag)');
385 $ptag->groupBy('tagger, tag');
386 $ptag->orderBy('tagger, tag');
389 while ($ptag->fetch()) {
390 $plist = new Profile_list();
392 $plist->tagger = $ptag->tagger;
393 $plist->tag = $ptag->tag;
395 $plist->created = common_sql_now();
396 $plist->modified = $plist->created;
397 $plist->mainpage = common_local_url('showprofiletag',
398 array('tagger' => $plist->getTagger()->nickname,
399 'tag' => $plist->tag));;
401 $plist->tagged_count = $ptag->tagged_count;
402 $plist->subscriber_count = 0;
406 $orig = clone($plist);
407 // After insert since it uses auto-generated ID
408 $plist->uri = common_local_url('profiletagbyid',
409 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
411 $plist->update($orig);
419 * Added as we now store interpretd width and height in File table.
421 function fixupFileGeometry()
423 printfnq("Ensuring width and height is set for supported local File objects...");
426 $file->whereAdd('filename IS NOT NULL'); // local files
427 $file->whereAdd('width IS NULL OR width = 0');
430 while ($file->fetch()) {
431 // Set file geometrical properties if available
433 $image = ImageFile::fromFileObject($file);
434 } catch (ServerException $e) {
435 // We couldn't make out an image from the file.
438 $orig = clone($file);
439 $file->width = $image->width;
440 $file->height = $image->height;
441 $file->update($orig);
443 // FIXME: Do this more automagically inside ImageFile or so.
444 if ($image->getPath() != $file->getPath()) {