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 $schemaUpdater->register($table, $def);
81 $schemaUpdater->checkSchema();
86 function updateSchemaPlugins()
88 printfnq("Upgrading plugin schema...");
90 Event::handle('CheckSchema');
95 function fixupNoticeRendered()
97 printfnq("Ensuring all notices have rendered HTML...");
99 $notice = new Notice();
101 $notice->whereAdd('rendered IS NULL');
104 while ($notice->fetch()) {
105 $original = clone($notice);
106 $notice->rendered = common_render_content($notice->content, $notice);
107 $notice->update($original);
113 function fixupNoticeConversation()
115 printfnq("Ensuring all notices have a conversation ID...");
117 $notice = new Notice();
118 $notice->whereAdd('conversation is null');
119 $notice->orderBy('id'); // try to get originals before replies
122 while ($notice->fetch()) {
126 $orig = clone($notice);
128 if (empty($notice->reply_to)) {
129 $notice->conversation = $notice->id;
131 $reply = Notice::getKV('id', $notice->reply_to);
134 $notice->conversation = $notice->id;
135 } else if (empty($reply->conversation)) {
136 $notice->conversation = $notice->id;
138 $notice->conversation = $reply->conversation;
145 $result = $notice->update($orig);
149 } catch (Exception $e) {
150 printv("Error setting conversation: " . $e->getMessage());
157 function fixupGroupURI()
159 printfnq("Ensuring all groups have an URI...");
161 $group = new User_group();
162 $group->whereAdd('uri IS NULL');
164 if ($group->find()) {
165 while ($group->fetch()) {
166 $orig = User_group::getKV('id', $group->id);
167 $group->uri = $group->getUri();
168 $group->update($orig);
175 function initConversation()
177 printfnq("Ensuring all conversations have a row in conversation table...");
179 $notice = new Notice();
180 $notice->query('select distinct notice.conversation from notice '.
181 'where notice.conversation is not null '.
182 'and not exists (select conversation.id from conversation where id = notice.conversation)');
184 while ($notice->fetch()) {
186 $id = $notice->conversation;
188 $uri = common_local_url('conversation', array('id' => $id));
190 // @fixme db_dataobject won't save our value for an autoincrement
191 // so we're bypassing the insert wrappers
192 $conv = new Conversation();
193 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
197 $conv->escape(common_sql_now()));
204 function fixupConversationURIs()
206 printfnq("Ensuring all conversations have a URI...");
208 $conv = new Conversation();
209 $conv->whereAdd('uri IS NULL');
213 while ($conv->fetch()) {
214 $uri = common_local_url('conversation', array('id' => $conv->id));
215 $sql = sprintf('UPDATE conversation SET uri="%1$s" WHERE id="%2$d";',
216 $conv->escape($uri), $conv->id);
218 if (($conv->N-++$rounds) % 500 == 0) {
219 printfnq(sprintf(' %d items left...', $conv->N-$rounds));
227 function initGroupProfileId()
229 printfnq("Ensuring all User_group entries have a Profile and profile_id...");
231 $group = new User_group();
232 $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
235 while ($group->fetch()) {
237 // We must create a new, incrementally assigned profile_id
238 $profile = new Profile();
239 $profile->nickname = $group->nickname;
240 $profile->fullname = $group->fullname;
241 $profile->profileurl = $group->mainpage;
242 $profile->homepage = $group->homepage;
243 $profile->bio = $group->description;
244 $profile->location = $group->location;
245 $profile->created = $group->created;
246 $profile->modified = $group->modified;
248 $profile->query('BEGIN');
249 $id = $profile->insert();
251 $profile->query('ROLLBACK');
252 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
254 $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
255 $profile->query('COMMIT');
258 } catch (Exception $e) {
259 printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
266 function initLocalGroup()
268 printfnq("Ensuring all local user groups have a local_group...");
270 $group = new User_group();
271 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
274 while ($group->fetch()) {
276 // Hack to check for local groups
277 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
278 $lg = new Local_group();
280 $lg->group_id = $group->id;
281 $lg->nickname = $group->nickname;
282 $lg->created = $group->created; // XXX: common_sql_now() ?
283 $lg->modified = $group->modified;
287 } catch (Exception $e) {
288 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
295 function initNoticeReshare()
297 printfnq("Ensuring all reshares have the correct verb and object-type...");
299 $notice = new Notice();
300 $notice->whereAdd('repeat_of is not null');
301 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
303 if ($notice->find()) {
304 while ($notice->fetch()) {
306 $orig = Notice::getKV('id', $notice->id);
307 $notice->verb = ActivityVerb::SHARE;
308 $notice->object_type = ActivityObject::ACTIVITY;
309 $notice->update($orig);
310 } catch (Exception $e) {
311 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
319 function initSubscriptionURI()
321 printfnq("Ensuring all subscriptions have a URI...");
323 $sub = new Subscription();
324 $sub->whereAdd('uri IS NULL');
327 while ($sub->fetch()) {
330 $sub->query(sprintf('update subscription '.
332 'where subscriber = %d '.
333 'and subscribed = %d',
334 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
337 } catch (Exception $e) {
338 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
346 function initGroupMemberURI()
348 printfnq("Ensuring all group memberships have a URI...");
350 $mem = new Group_member();
351 $mem->whereAdd('uri IS NULL');
354 while ($mem->fetch()) {
357 $mem->query(sprintf('update group_member set uri = "%s" '.
358 'where profile_id = %d ' .
359 'and group_id = %d ',
360 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
363 } catch (Exception $e) {
364 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
372 function initProfileLists()
374 printfnq("Ensuring all profile tags have a corresponding list...");
376 $ptag = new Profile_tag();
378 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
379 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
380 'where profile_tag.tagger = profile_list.tagger '.
381 'and profile_tag.tag = profile_list.tag)');
382 $ptag->groupBy('tagger, tag');
383 $ptag->orderBy('tagger, tag');
386 while ($ptag->fetch()) {
387 $plist = new Profile_list();
389 $plist->tagger = $ptag->tagger;
390 $plist->tag = $ptag->tag;
392 $plist->created = common_sql_now();
393 $plist->modified = $plist->created;
394 $plist->mainpage = common_local_url('showprofiletag',
395 array('tagger' => $plist->getTagger()->nickname,
396 'tag' => $plist->tag));;
398 $plist->tagged_count = $ptag->tagged_count;
399 $plist->subscriber_count = 0;
403 $orig = clone($plist);
404 // After insert since it uses auto-generated ID
405 $plist->uri = common_local_url('profiletagbyid',
406 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
408 $plist->update($orig);
416 * Added as we now store interpretd width and height in File table.
418 function fixupFileGeometry()
420 printfnq("Ensuring width and height is set for supported local File objects...");
423 $file->whereAdd('filename IS NOT NULL'); // local files
424 $file->whereAdd('width IS NULL OR width = 0');
427 while ($file->fetch()) {
428 // Set file geometrical properties if available
430 $image = ImageFile::fromFileObject($file);
431 } catch (ServerException $e) {
432 // We couldn't make out an image from the file.
435 $orig = clone($file);
436 $file->width = $image->width;
437 $file->height = $image->height;
438 $file->update($orig);
440 // FIXME: Do this more automagically inside ImageFile or so.
441 if ($image->getPath() != $file->getPath()) {
452 * File_thumbnail objects for local Files store their own filenames in the database.
454 function deleteLocalFileThumbnailsWithoutFilename()
456 printfnq("Removing all local File_thumbnail entries without filename property...");
459 $file->whereAdd('filename IS NOT NULL'); // local files
462 // Looping through local File entries
463 while ($file->fetch()) {
464 $thumbs = new File_thumbnail();
465 $thumbs->file_id = $file->id;
466 $thumbs->whereAdd('filename IS NULL');
467 // Checking if there were any File_thumbnail entries without filename
468 if (!$thumbs->find()) {
471 // deleting incomplete entry to allow regeneration
472 while ($thumbs->fetch()) {
482 * Delete File_thumbnail entries where the referenced file does not exist.
484 function deleteMissingLocalFileThumbnails()
486 printfnq("Removing all local File_thumbnail entries without existing files...");
488 $thumbs = new File_thumbnail();
489 $thumbs->whereAdd('filename IS NOT NULL'); // only fill in names where they're missing
490 // Checking if there were any File_thumbnail entries without filename
491 if ($thumbs->find()) {
492 while ($thumbs->fetch()) {
493 if (!file_exists(File_thumbnail::path($thumbs->filename))) {