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();
51 setFilehashOnLocalFiles();
57 initSubscriptionURI();
62 Event::handle('EndUpgrade');
69 require INSTALLDIR.'/db/core.php';
73 function updateSchemaCore()
75 printfnq("Upgrading core schema...");
77 $schema = Schema::get();
78 $schemaUpdater = new SchemaUpdater($schema);
79 foreach (tableDefs() as $table => $def) {
80 $schemaUpdater->register($table, $def);
82 $schemaUpdater->checkSchema();
87 function updateSchemaPlugins()
89 printfnq("Upgrading plugin schema...");
91 Event::handle('CheckSchema');
96 function fixupNoticeRendered()
98 printfnq("Ensuring all notices have rendered HTML...");
100 $notice = new Notice();
102 $notice->whereAdd('rendered IS NULL');
105 while ($notice->fetch()) {
106 $original = clone($notice);
107 $notice->rendered = common_render_content($notice->content, $notice);
108 $notice->update($original);
114 function fixupNoticeConversation()
116 printfnq("Ensuring all notices have a conversation ID...");
118 $notice = new Notice();
119 $notice->whereAdd('conversation is null');
120 $notice->orderBy('id'); // try to get originals before replies
123 while ($notice->fetch()) {
127 $orig = clone($notice);
129 if (empty($notice->reply_to)) {
130 $notice->conversation = $notice->id;
132 $reply = Notice::getKV('id', $notice->reply_to);
135 $notice->conversation = $notice->id;
136 } else if (empty($reply->conversation)) {
137 $notice->conversation = $notice->id;
139 $notice->conversation = $reply->conversation;
146 $result = $notice->update($orig);
150 } catch (Exception $e) {
151 printv("Error setting conversation: " . $e->getMessage());
158 function fixupGroupURI()
160 printfnq("Ensuring all groups have an URI...");
162 $group = new User_group();
163 $group->whereAdd('uri IS NULL');
165 if ($group->find()) {
166 while ($group->fetch()) {
167 $orig = User_group::getKV('id', $group->id);
168 $group->uri = $group->getUri();
169 $group->update($orig);
176 function initConversation()
178 printfnq("Ensuring all conversations have a row in conversation table...");
180 $notice = new Notice();
181 $notice->query('select distinct notice.conversation from notice '.
182 'where notice.conversation is not null '.
183 'and not exists (select conversation.id from conversation where id = notice.conversation)');
185 while ($notice->fetch()) {
187 $id = $notice->conversation;
189 $uri = common_local_url('conversation', array('id' => $id));
191 // @fixme db_dataobject won't save our value for an autoincrement
192 // so we're bypassing the insert wrappers
193 $conv = new Conversation();
194 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
198 $conv->escape(common_sql_now()));
205 function fixupConversationURIs()
207 printfnq("Ensuring all conversations have a URI...");
209 $conv = new Conversation();
210 $conv->whereAdd('uri IS NULL');
214 while ($conv->fetch()) {
215 $uri = common_local_url('conversation', array('id' => $conv->id));
216 $sql = sprintf('UPDATE conversation SET uri="%1$s" WHERE id="%2$d";',
217 $conv->escape($uri), $conv->id);
219 if (($conv->N-++$rounds) % 500 == 0) {
220 printfnq(sprintf(' %d items left...', $conv->N-$rounds));
228 function initGroupProfileId()
230 printfnq("Ensuring all User_group entries have a Profile and profile_id...");
232 $group = new User_group();
233 $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
236 while ($group->fetch()) {
238 // We must create a new, incrementally assigned profile_id
239 $profile = new Profile();
240 $profile->nickname = $group->nickname;
241 $profile->fullname = $group->fullname;
242 $profile->profileurl = $group->mainpage;
243 $profile->homepage = $group->homepage;
244 $profile->bio = $group->description;
245 $profile->location = $group->location;
246 $profile->created = $group->created;
247 $profile->modified = $group->modified;
249 $profile->query('BEGIN');
250 $id = $profile->insert();
252 $profile->query('ROLLBACK');
253 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
255 $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
256 $profile->query('COMMIT');
259 } catch (Exception $e) {
260 printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
267 function initLocalGroup()
269 printfnq("Ensuring all local user groups have a local_group...");
271 $group = new User_group();
272 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
275 while ($group->fetch()) {
277 // Hack to check for local groups
278 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
279 $lg = new Local_group();
281 $lg->group_id = $group->id;
282 $lg->nickname = $group->nickname;
283 $lg->created = $group->created; // XXX: common_sql_now() ?
284 $lg->modified = $group->modified;
288 } catch (Exception $e) {
289 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
296 function initNoticeReshare()
298 printfnq("Ensuring all reshares have the correct verb and object-type...");
300 $notice = new Notice();
301 $notice->whereAdd('repeat_of is not null');
302 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
304 if ($notice->find()) {
305 while ($notice->fetch()) {
307 $orig = Notice::getKV('id', $notice->id);
308 $notice->verb = ActivityVerb::SHARE;
309 $notice->object_type = ActivityObject::ACTIVITY;
310 $notice->update($orig);
311 } catch (Exception $e) {
312 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
320 function initSubscriptionURI()
322 printfnq("Ensuring all subscriptions have a URI...");
324 $sub = new Subscription();
325 $sub->whereAdd('uri IS NULL');
328 while ($sub->fetch()) {
331 $sub->query(sprintf('update subscription '.
333 'where subscriber = %d '.
334 'and subscribed = %d',
335 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
338 } catch (Exception $e) {
339 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
347 function initGroupMemberURI()
349 printfnq("Ensuring all group memberships have a URI...");
351 $mem = new Group_member();
352 $mem->whereAdd('uri IS NULL');
355 while ($mem->fetch()) {
358 $mem->query(sprintf('update group_member set uri = "%s" '.
359 'where profile_id = %d ' .
360 'and group_id = %d ',
361 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
364 } catch (Exception $e) {
365 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
373 function initProfileLists()
375 printfnq("Ensuring all profile tags have a corresponding list...");
377 $ptag = new Profile_tag();
379 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
380 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
381 'where profile_tag.tagger = profile_list.tagger '.
382 'and profile_tag.tag = profile_list.tag)');
383 $ptag->groupBy('tagger, tag');
384 $ptag->orderBy('tagger, tag');
387 while ($ptag->fetch()) {
388 $plist = new Profile_list();
390 $plist->tagger = $ptag->tagger;
391 $plist->tag = $ptag->tag;
393 $plist->created = common_sql_now();
394 $plist->modified = $plist->created;
395 $plist->mainpage = common_local_url('showprofiletag',
396 array('tagger' => $plist->getTagger()->nickname,
397 'tag' => $plist->tag));;
399 $plist->tagged_count = $ptag->tagged_count;
400 $plist->subscriber_count = 0;
404 $orig = clone($plist);
405 // After insert since it uses auto-generated ID
406 $plist->uri = common_local_url('profiletagbyid',
407 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
409 $plist->update($orig);
417 * Added as we now store interpretd width and height in File table.
419 function fixupFileGeometry()
421 printfnq("Ensuring width and height is set for supported local File objects...");
424 $file->whereAdd('filename IS NOT NULL'); // local files
425 $file->whereAdd('width IS NULL OR width = 0');
428 while ($file->fetch()) {
429 // Set file geometrical properties if available
431 $image = ImageFile::fromFileObject($file);
432 } catch (ServerException $e) {
433 // We couldn't make out an image from the file.
436 $orig = clone($file);
437 $file->width = $image->width;
438 $file->height = $image->height;
439 $file->update($orig);
441 // FIXME: Do this more automagically inside ImageFile or so.
442 if ($image->getPath() != $file->getPath()) {
453 * File_thumbnail objects for local Files store their own filenames in the database.
455 function deleteLocalFileThumbnailsWithoutFilename()
457 printfnq("Removing all local File_thumbnail entries without filename property...");
460 $file->whereAdd('filename IS NOT NULL'); // local files
463 // Looping through local File entries
464 while ($file->fetch()) {
465 $thumbs = new File_thumbnail();
466 $thumbs->file_id = $file->id;
467 $thumbs->whereAdd('filename IS NULL');
468 // Checking if there were any File_thumbnail entries without filename
469 if (!$thumbs->find()) {
472 // deleting incomplete entry to allow regeneration
473 while ($thumbs->fetch()) {
483 * Delete File_thumbnail entries where the referenced file does not exist.
485 function deleteMissingLocalFileThumbnails()
487 printfnq("Removing all local File_thumbnail entries without existing files...");
489 $thumbs = new File_thumbnail();
490 $thumbs->whereAdd('filename IS NOT NULL'); // only fill in names where they're missing
491 // Checking if there were any File_thumbnail entries without filename
492 if ($thumbs->find()) {
493 while ($thumbs->fetch()) {
496 } catch (FileNotFoundException $e) {
506 * Files are now stored with their hash, so let's generate for previously uploaded files.
508 function setFilehashOnLocalFiles()
510 printfnq('Ensuring all local files have the filehash field set...');
513 $file->whereAdd('filename IS NOT NULL'); // local files
514 $file->whereAdd('filehash IS NULL', 'AND'); // without filehash value
517 while ($file->fetch()) {
519 $orig = clone($file);
520 $file->filehash = hash_file(File::FILEHASH_ALG, $file->getPath());
521 $file->update($orig);
522 } catch (FileNotFoundException $e) {
523 echo "\n WARNING: file ID {$file->id} does not exist on path '{$e->path}'. If there is no file system error, run: php scripts/clean_file_table.php";