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 case 'file_redirection':
92 $schemadef = $schemaUpdater->schema->getTableDef($table);
93 if (isset($schemadef['fields']['urlhash'])) {
94 // We already have the urlhash field, so no need to migrate it.
97 echo "\nFound old $table table, upgrading it to contain 'urlhash' field...\n";
98 // We have to create a urlhash that is _not_ the primary key,
99 // transfer data and THEN run checkSchema
100 $schemadef['fields']['urlhash'] = array (
103 'description' => 'sha256 of destination URL after following redirections',
105 $schemaUpdater->schema->ensureTable($table, $schemadef);
108 $classname = ucfirst($table);
109 $tablefix = new $classname;
110 // urlhash is hash('sha256', $url) in the File table
111 echo "Updating urlhash fields in $table table...\n";
112 // Maybe very MySQL specific :(
113 $tablefix->query(sprintf('UPDATE %1$s SET %2$s=%3$s;',
114 $schemaUpdater->schema->quoteIdentifier($table),
116 // The line below is "result of sha256 on column `url`"
119 echo "Resuming core schema upgrade...";
124 function updateSchemaPlugins()
126 printfnq("Upgrading plugin schema...");
128 Event::handle('CheckSchema');
133 function fixupNoticeRendered()
135 printfnq("Ensuring all notices have rendered HTML...");
137 $notice = new Notice();
139 $notice->whereAdd('rendered IS NULL');
142 while ($notice->fetch()) {
143 $original = clone($notice);
144 $notice->rendered = common_render_content($notice->content, $notice);
145 $notice->update($original);
151 function fixupNoticeConversation()
153 printfnq("Ensuring all notices have a conversation ID...");
155 $notice = new Notice();
156 $notice->whereAdd('conversation is null');
157 $notice->orderBy('id'); // try to get originals before replies
160 while ($notice->fetch()) {
164 $orig = clone($notice);
166 if (empty($notice->reply_to)) {
167 $notice->conversation = $notice->id;
169 $reply = Notice::getKV('id', $notice->reply_to);
172 $notice->conversation = $notice->id;
173 } else if (empty($reply->conversation)) {
174 $notice->conversation = $notice->id;
176 $notice->conversation = $reply->conversation;
183 $result = $notice->update($orig);
187 } catch (Exception $e) {
188 printv("Error setting conversation: " . $e->getMessage());
195 function fixupGroupURI()
197 printfnq("Ensuring all groups have an URI...");
199 $group = new User_group();
200 $group->whereAdd('uri IS NULL');
202 if ($group->find()) {
203 while ($group->fetch()) {
204 $orig = User_group::getKV('id', $group->id);
205 $group->uri = $group->getUri();
206 $group->update($orig);
213 function initConversation()
215 printfnq("Ensuring all conversations have a row in conversation table...");
217 $notice = new Notice();
218 $notice->query('select distinct notice.conversation from notice '.
219 'where notice.conversation is not null '.
220 'and not exists (select conversation.id from conversation where id = notice.conversation)');
222 while ($notice->fetch()) {
224 $id = $notice->conversation;
226 $uri = common_local_url('conversation', array('id' => $id));
228 // @fixme db_dataobject won't save our value for an autoincrement
229 // so we're bypassing the insert wrappers
230 $conv = new Conversation();
231 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
235 $conv->escape(common_sql_now()));
242 function fixupConversationURIs()
244 printfnq("Ensuring all conversations have a URI...");
246 $conv = new Conversation();
247 $conv->whereAdd('uri IS NULL');
251 while ($conv->fetch()) {
252 $uri = common_local_url('conversation', array('id' => $conv->id));
253 $sql = sprintf('UPDATE conversation SET uri="%1$s" WHERE id="%2$d";',
254 $conv->escape($uri), $conv->id);
256 if (($conv->N-++$rounds) % 500 == 0) {
257 printfnq(sprintf(' %d items left...', $conv->N-$rounds));
265 function initGroupProfileId()
267 printfnq("Ensuring all User_group entries have a Profile and profile_id...");
269 $group = new User_group();
270 $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
273 while ($group->fetch()) {
275 // We must create a new, incrementally assigned profile_id
276 $profile = new Profile();
277 $profile->nickname = $group->nickname;
278 $profile->fullname = $group->fullname;
279 $profile->profileurl = $group->mainpage;
280 $profile->homepage = $group->homepage;
281 $profile->bio = $group->description;
282 $profile->location = $group->location;
283 $profile->created = $group->created;
284 $profile->modified = $group->modified;
286 $profile->query('BEGIN');
287 $id = $profile->insert();
289 $profile->query('ROLLBACK');
290 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
292 $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
293 $profile->query('COMMIT');
296 } catch (Exception $e) {
297 printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
304 function initLocalGroup()
306 printfnq("Ensuring all local user groups have a local_group...");
308 $group = new User_group();
309 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
312 while ($group->fetch()) {
314 // Hack to check for local groups
315 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
316 $lg = new Local_group();
318 $lg->group_id = $group->id;
319 $lg->nickname = $group->nickname;
320 $lg->created = $group->created; // XXX: common_sql_now() ?
321 $lg->modified = $group->modified;
325 } catch (Exception $e) {
326 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
333 function initNoticeReshare()
335 printfnq("Ensuring all reshares have the correct verb and object-type...");
337 $notice = new Notice();
338 $notice->whereAdd('repeat_of is not null');
339 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
341 if ($notice->find()) {
342 while ($notice->fetch()) {
344 $orig = Notice::getKV('id', $notice->id);
345 $notice->verb = ActivityVerb::SHARE;
346 $notice->object_type = ActivityObject::ACTIVITY;
347 $notice->update($orig);
348 } catch (Exception $e) {
349 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
357 function initSubscriptionURI()
359 printfnq("Ensuring all subscriptions have a URI...");
361 $sub = new Subscription();
362 $sub->whereAdd('uri IS NULL');
365 while ($sub->fetch()) {
368 $sub->query(sprintf('update subscription '.
370 'where subscriber = %d '.
371 'and subscribed = %d',
372 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
375 } catch (Exception $e) {
376 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
384 function initGroupMemberURI()
386 printfnq("Ensuring all group memberships have a URI...");
388 $mem = new Group_member();
389 $mem->whereAdd('uri IS NULL');
392 while ($mem->fetch()) {
395 $mem->query(sprintf('update group_member set uri = "%s" '.
396 'where profile_id = %d ' .
397 'and group_id = %d ',
398 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
401 } catch (Exception $e) {
402 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
410 function initProfileLists()
412 printfnq("Ensuring all profile tags have a corresponding list...");
414 $ptag = new Profile_tag();
416 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
417 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
418 'where profile_tag.tagger = profile_list.tagger '.
419 'and profile_tag.tag = profile_list.tag)');
420 $ptag->groupBy('tagger, tag');
421 $ptag->orderBy('tagger, tag');
424 while ($ptag->fetch()) {
425 $plist = new Profile_list();
427 $plist->tagger = $ptag->tagger;
428 $plist->tag = $ptag->tag;
430 $plist->created = common_sql_now();
431 $plist->modified = $plist->created;
432 $plist->mainpage = common_local_url('showprofiletag',
433 array('tagger' => $plist->getTagger()->nickname,
434 'tag' => $plist->tag));;
436 $plist->tagged_count = $ptag->tagged_count;
437 $plist->subscriber_count = 0;
441 $orig = clone($plist);
442 // After insert since it uses auto-generated ID
443 $plist->uri = common_local_url('profiletagbyid',
444 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
446 $plist->update($orig);
454 * Added as we now store interpretd width and height in File table.
456 function fixupFileGeometry()
458 printfnq("Ensuring width and height is set for supported local File objects...");
461 $file->whereAdd('filename IS NOT NULL'); // local files
462 $file->whereAdd('width IS NULL OR width = 0');
465 while ($file->fetch()) {
466 // Set file geometrical properties if available
468 $image = ImageFile::fromFileObject($file);
469 } catch (ServerException $e) {
470 // We couldn't make out an image from the file.
473 $orig = clone($file);
474 $file->width = $image->width;
475 $file->height = $image->height;
476 $file->update($orig);
478 // FIXME: Do this more automagically inside ImageFile or so.
479 if ($image->getPath() != $file->getPath()) {
490 * File_thumbnail objects for local Files store their own filenames in the database.
492 function deleteLocalFileThumbnailsWithoutFilename()
494 printfnq("Removing all local File_thumbnail entries without filename property...");
497 $file->whereAdd('filename IS NOT NULL'); // local files
500 // Looping through local File entries
501 while ($file->fetch()) {
502 $thumbs = new File_thumbnail();
503 $thumbs->file_id = $file->id;
504 $thumbs->whereAdd('filename IS NULL');
505 // Checking if there were any File_thumbnail entries without filename
506 if (!$thumbs->find()) {
509 // deleting incomplete entry to allow regeneration
510 while ($thumbs->fetch()) {
520 * Delete File_thumbnail entries where the referenced file does not exist.
522 function deleteMissingLocalFileThumbnails()
524 printfnq("Removing all local File_thumbnail entries without existing files...");
526 $thumbs = new File_thumbnail();
527 $thumbs->whereAdd('filename IS NOT NULL'); // only fill in names where they're missing
528 // Checking if there were any File_thumbnail entries without filename
529 if ($thumbs->find()) {
530 while ($thumbs->fetch()) {
531 if (!file_exists(File_thumbnail::path($thumbs->filename))) {