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')) {
37 fixupConversationURIs();
40 updateSchemaPlugins();
42 // These replace old "fixup_*" scripts
44 fixupNoticeRendered();
45 fixupNoticeConversation();
49 deleteLocalFileThumbnailsWithoutFilename();
50 deleteMissingLocalFileThumbnails();
56 initSubscriptionURI();
61 Event::handle('EndUpgrade');
68 require INSTALLDIR.'/db/core.php';
72 function updateSchemaCore()
74 printfnq("Upgrading core schema...");
76 $schema = Schema::get();
77 $schemaUpdater = new SchemaUpdater($schema);
78 foreach (tableDefs() as $table => $def) {
79 preAlterFixes($schemaUpdater, $table);
80 $schemaUpdater->register($table, $def);
82 $schemaUpdater->checkSchema();
87 function preAlterFixes($schemaUpdater, $table)
91 $schemadef = $schemaUpdater->schema->getTableDef($table);
92 if (isset($schemadef['fields']['urlhash'])) {
93 // We already have the urlhash field, so no need to migrate it.
96 printfnq("\nFound old $table table, upgrading it to contain 'urlhash' field...\n");
97 // We have to create a urlhash that is _not_ the primary key,
98 // transfer data and THEN run checkSchema
99 $schemadef['fields']['urlhash'] = array (
102 'description' => 'sha256 of destination URL after following redirections',
104 $schemaUpdater->schema->ensureTable($table, $schemadef);
107 $filefix = new File();
108 // urlhash is hash('sha256', $url) in the File table
109 printfnq("Updating urlhash fields in $table table...\n");
110 $filefix->query(sprintf('UPDATE %1$s SET %2$s=%3$s;',
111 $schemaUpdater->schema->quoteIdentifier($table),
113 // The line below is "result of sha256 on column `url`"
116 printfnq("Resuming core schema upgrade...");
121 function updateSchemaPlugins()
123 printfnq("Upgrading plugin schema...");
125 Event::handle('CheckSchema');
130 function fixupNoticeRendered()
132 printfnq("Ensuring all notices have rendered HTML...");
134 $notice = new Notice();
136 $notice->whereAdd('rendered IS NULL');
139 while ($notice->fetch()) {
140 $original = clone($notice);
141 $notice->rendered = common_render_content($notice->content, $notice);
142 $notice->update($original);
148 function fixupNoticeConversation()
150 printfnq("Ensuring all notices have a conversation ID...");
152 $notice = new Notice();
153 $notice->whereAdd('conversation is null');
154 $notice->orderBy('id'); // try to get originals before replies
157 while ($notice->fetch()) {
161 $orig = clone($notice);
163 if (empty($notice->reply_to)) {
164 $notice->conversation = $notice->id;
166 $reply = Notice::getKV('id', $notice->reply_to);
169 $notice->conversation = $notice->id;
170 } else if (empty($reply->conversation)) {
171 $notice->conversation = $notice->id;
173 $notice->conversation = $reply->conversation;
180 $result = $notice->update($orig);
184 } catch (Exception $e) {
185 printv("Error setting conversation: " . $e->getMessage());
192 function fixupGroupURI()
194 printfnq("Ensuring all groups have an URI...");
196 $group = new User_group();
197 $group->whereAdd('uri IS NULL');
199 if ($group->find()) {
200 while ($group->fetch()) {
201 $orig = User_group::getKV('id', $group->id);
202 $group->uri = $group->getUri();
203 $group->update($orig);
210 function initConversation()
212 printfnq("Ensuring all conversations have a row in conversation table...");
214 $notice = new Notice();
215 $notice->query('select distinct notice.conversation from notice '.
216 'where notice.conversation is not null '.
217 'and not exists (select conversation.id from conversation where id = notice.conversation)');
219 while ($notice->fetch()) {
221 $id = $notice->conversation;
223 $uri = common_local_url('conversation', array('id' => $id));
225 // @fixme db_dataobject won't save our value for an autoincrement
226 // so we're bypassing the insert wrappers
227 $conv = new Conversation();
228 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
232 $conv->escape(common_sql_now()));
239 function fixupConversationURIs()
241 printfnq("Ensuring all conversations have a URI...");
243 $conv = new Conversation();
244 $conv->whereAdd('uri IS NULL');
248 while ($conv->fetch()) {
249 $uri = common_local_url('conversation', array('id' => $conv->id));
250 $sql = sprintf('UPDATE conversation SET uri="%1$s" WHERE id="%2$d";',
251 $conv->escape($uri), $conv->id);
253 if (($conv->N-++$rounds) % 500 == 0) {
254 printfnq(sprintf(' %d items left...', $conv->N-$rounds));
262 function initGroupProfileId()
264 printfnq("Ensuring all User_group entries have a Profile and profile_id...");
266 $group = new User_group();
267 $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
270 while ($group->fetch()) {
272 // We must create a new, incrementally assigned profile_id
273 $profile = new Profile();
274 $profile->nickname = $group->nickname;
275 $profile->fullname = $group->fullname;
276 $profile->profileurl = $group->mainpage;
277 $profile->homepage = $group->homepage;
278 $profile->bio = $group->description;
279 $profile->location = $group->location;
280 $profile->created = $group->created;
281 $profile->modified = $group->modified;
283 $profile->query('BEGIN');
284 $id = $profile->insert();
286 $profile->query('ROLLBACK');
287 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
289 $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
290 $profile->query('COMMIT');
293 } catch (Exception $e) {
294 printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
301 function initLocalGroup()
303 printfnq("Ensuring all local user groups have a local_group...");
305 $group = new User_group();
306 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
309 while ($group->fetch()) {
311 // Hack to check for local groups
312 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
313 $lg = new Local_group();
315 $lg->group_id = $group->id;
316 $lg->nickname = $group->nickname;
317 $lg->created = $group->created; // XXX: common_sql_now() ?
318 $lg->modified = $group->modified;
322 } catch (Exception $e) {
323 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
330 function initNoticeReshare()
332 printfnq("Ensuring all reshares have the correct verb and object-type...");
334 $notice = new Notice();
335 $notice->whereAdd('repeat_of is not null');
336 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
338 if ($notice->find()) {
339 while ($notice->fetch()) {
341 $orig = Notice::getKV('id', $notice->id);
342 $notice->verb = ActivityVerb::SHARE;
343 $notice->object_type = ActivityObject::ACTIVITY;
344 $notice->update($orig);
345 } catch (Exception $e) {
346 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
354 function initSubscriptionURI()
356 printfnq("Ensuring all subscriptions have a URI...");
358 $sub = new Subscription();
359 $sub->whereAdd('uri IS NULL');
362 while ($sub->fetch()) {
365 $sub->query(sprintf('update subscription '.
367 'where subscriber = %d '.
368 'and subscribed = %d',
369 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
372 } catch (Exception $e) {
373 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
381 function initGroupMemberURI()
383 printfnq("Ensuring all group memberships have a URI...");
385 $mem = new Group_member();
386 $mem->whereAdd('uri IS NULL');
389 while ($mem->fetch()) {
392 $mem->query(sprintf('update group_member set uri = "%s" '.
393 'where profile_id = %d ' .
394 'and group_id = %d ',
395 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
398 } catch (Exception $e) {
399 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
407 function initProfileLists()
409 printfnq("Ensuring all profile tags have a corresponding list...");
411 $ptag = new Profile_tag();
413 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
414 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
415 'where profile_tag.tagger = profile_list.tagger '.
416 'and profile_tag.tag = profile_list.tag)');
417 $ptag->groupBy('tagger, tag');
418 $ptag->orderBy('tagger, tag');
421 while ($ptag->fetch()) {
422 $plist = new Profile_list();
424 $plist->tagger = $ptag->tagger;
425 $plist->tag = $ptag->tag;
427 $plist->created = common_sql_now();
428 $plist->modified = $plist->created;
429 $plist->mainpage = common_local_url('showprofiletag',
430 array('tagger' => $plist->getTagger()->nickname,
431 'tag' => $plist->tag));;
433 $plist->tagged_count = $ptag->tagged_count;
434 $plist->subscriber_count = 0;
438 $orig = clone($plist);
439 // After insert since it uses auto-generated ID
440 $plist->uri = common_local_url('profiletagbyid',
441 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
443 $plist->update($orig);
451 * Added as we now store interpretd width and height in File table.
453 function fixupFileGeometry()
455 printfnq("Ensuring width and height is set for supported local File objects...");
458 $file->whereAdd('filename IS NOT NULL'); // local files
459 $file->whereAdd('width IS NULL OR width = 0');
462 while ($file->fetch()) {
463 // Set file geometrical properties if available
465 $image = ImageFile::fromFileObject($file);
466 } catch (ServerException $e) {
467 // We couldn't make out an image from the file.
470 $orig = clone($file);
471 $file->width = $image->width;
472 $file->height = $image->height;
473 $file->update($orig);
475 // FIXME: Do this more automagically inside ImageFile or so.
476 if ($image->getPath() != $file->getPath()) {
487 * File_thumbnail objects for local Files store their own filenames in the database.
489 function deleteLocalFileThumbnailsWithoutFilename()
491 printfnq("Removing all local File_thumbnail entries without filename property...");
494 $file->whereAdd('filename IS NOT NULL'); // local files
497 // Looping through local File entries
498 while ($file->fetch()) {
499 $thumbs = new File_thumbnail();
500 $thumbs->file_id = $file->id;
501 $thumbs->whereAdd('filename IS NULL');
502 // Checking if there were any File_thumbnail entries without filename
503 if (!$thumbs->find()) {
506 // deleting incomplete entry to allow regeneration
507 while ($thumbs->fetch()) {
517 * Delete File_thumbnail entries where the referenced file does not exist.
519 function deleteMissingLocalFileThumbnails()
521 printfnq("Removing all local File_thumbnail entries without existing files...");
523 $thumbs = new File_thumbnail();
524 $thumbs->whereAdd('filename IS NOT NULL'); // only fill in names where they're missing
525 // Checking if there were any File_thumbnail entries without filename
526 if ($thumbs->find()) {
527 while ($thumbs->fetch()) {
528 if (!file_exists(File_thumbnail::path($thumbs->filename))) {