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('BeforePluginCheckSchema');
92 Event::handle('CheckSchema');
97 function fixupNoticeRendered()
99 printfnq("Ensuring all notices have rendered HTML...");
101 $notice = new Notice();
103 $notice->whereAdd('rendered IS NULL');
106 while ($notice->fetch()) {
107 $original = clone($notice);
108 $notice->rendered = common_render_content($notice->content, $notice);
109 $notice->update($original);
115 function fixupNoticeConversation()
117 printfnq("Ensuring all notices have a conversation ID...");
119 $notice = new Notice();
120 $notice->whereAdd('conversation is null');
121 $notice->orderBy('id'); // try to get originals before replies
124 while ($notice->fetch()) {
128 $orig = clone($notice);
130 if (empty($notice->reply_to)) {
131 $notice->conversation = $notice->id;
133 $reply = Notice::getKV('id', $notice->reply_to);
136 $notice->conversation = $notice->id;
137 } else if (empty($reply->conversation)) {
138 $notice->conversation = $notice->id;
140 $notice->conversation = $reply->conversation;
147 $result = $notice->update($orig);
151 } catch (Exception $e) {
152 printv("Error setting conversation: " . $e->getMessage());
159 function fixupGroupURI()
161 printfnq("Ensuring all groups have an URI...");
163 $group = new User_group();
164 $group->whereAdd('uri IS NULL');
166 if ($group->find()) {
167 while ($group->fetch()) {
168 $orig = User_group::getKV('id', $group->id);
169 $group->uri = $group->getUri();
170 $group->update($orig);
177 function initConversation()
179 printfnq("Ensuring all conversations have a row in conversation table...");
181 $notice = new Notice();
182 $notice->query('select distinct notice.conversation from notice '.
183 'where notice.conversation is not null '.
184 'and not exists (select conversation.id from conversation where id = notice.conversation)');
186 while ($notice->fetch()) {
188 $id = $notice->conversation;
190 $uri = common_local_url('conversation', array('id' => $id));
192 // @fixme db_dataobject won't save our value for an autoincrement
193 // so we're bypassing the insert wrappers
194 $conv = new Conversation();
195 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
199 $conv->escape(common_sql_now()));
206 function fixupConversationURIs()
208 printfnq("Ensuring all conversations have a URI...");
210 $conv = new Conversation();
211 $conv->whereAdd('uri IS NULL');
215 while ($conv->fetch()) {
216 $uri = common_local_url('conversation', array('id' => $conv->id));
217 $sql = sprintf('UPDATE conversation SET uri="%1$s" WHERE id="%2$d";',
218 $conv->escape($uri), $conv->id);
220 if (($conv->N-++$rounds) % 500 == 0) {
221 printfnq(sprintf(' %d items left...', $conv->N-$rounds));
229 function initGroupProfileId()
231 printfnq("Ensuring all User_group entries have a Profile and profile_id...");
233 $group = new User_group();
234 $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
237 while ($group->fetch()) {
239 // We must create a new, incrementally assigned profile_id
240 $profile = new Profile();
241 $profile->nickname = $group->nickname;
242 $profile->fullname = $group->fullname;
243 $profile->profileurl = $group->mainpage;
244 $profile->homepage = $group->homepage;
245 $profile->bio = $group->description;
246 $profile->location = $group->location;
247 $profile->created = $group->created;
248 $profile->modified = $group->modified;
250 $profile->query('BEGIN');
251 $id = $profile->insert();
253 $profile->query('ROLLBACK');
254 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
256 $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
257 $profile->query('COMMIT');
260 } catch (Exception $e) {
261 printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
268 function initLocalGroup()
270 printfnq("Ensuring all local user groups have a local_group...");
272 $group = new User_group();
273 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
276 while ($group->fetch()) {
278 // Hack to check for local groups
279 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
280 $lg = new Local_group();
282 $lg->group_id = $group->id;
283 $lg->nickname = $group->nickname;
284 $lg->created = $group->created; // XXX: common_sql_now() ?
285 $lg->modified = $group->modified;
289 } catch (Exception $e) {
290 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
297 function initNoticeReshare()
299 printfnq("Ensuring all reshares have the correct verb and object-type...");
301 $notice = new Notice();
302 $notice->whereAdd('repeat_of is not null');
303 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
305 if ($notice->find()) {
306 while ($notice->fetch()) {
308 $orig = Notice::getKV('id', $notice->id);
309 $notice->verb = ActivityVerb::SHARE;
310 $notice->object_type = ActivityObject::ACTIVITY;
311 $notice->update($orig);
312 } catch (Exception $e) {
313 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
321 function initSubscriptionURI()
323 printfnq("Ensuring all subscriptions have a URI...");
325 $sub = new Subscription();
326 $sub->whereAdd('uri IS NULL');
329 while ($sub->fetch()) {
332 $sub->query(sprintf('update subscription '.
334 'where subscriber = %d '.
335 'and subscribed = %d',
336 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
339 } catch (Exception $e) {
340 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
348 function initGroupMemberURI()
350 printfnq("Ensuring all group memberships have a URI...");
352 $mem = new Group_member();
353 $mem->whereAdd('uri IS NULL');
356 while ($mem->fetch()) {
359 $mem->query(sprintf('update group_member set uri = "%s" '.
360 'where profile_id = %d ' .
361 'and group_id = %d ',
362 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
365 } catch (Exception $e) {
366 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
374 function initProfileLists()
376 printfnq("Ensuring all profile tags have a corresponding list...");
378 $ptag = new Profile_tag();
380 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
381 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
382 'where profile_tag.tagger = profile_list.tagger '.
383 'and profile_tag.tag = profile_list.tag)');
384 $ptag->groupBy('tagger, tag');
385 $ptag->orderBy('tagger, tag');
388 while ($ptag->fetch()) {
389 $plist = new Profile_list();
391 $plist->tagger = $ptag->tagger;
392 $plist->tag = $ptag->tag;
394 $plist->created = common_sql_now();
395 $plist->modified = $plist->created;
396 $plist->mainpage = common_local_url('showprofiletag',
397 array('tagger' => $plist->getTagger()->nickname,
398 'tag' => $plist->tag));;
400 $plist->tagged_count = $ptag->tagged_count;
401 $plist->subscriber_count = 0;
405 $orig = clone($plist);
406 // After insert since it uses auto-generated ID
407 $plist->uri = common_local_url('profiletagbyid',
408 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
410 $plist->update($orig);
418 * Added as we now store interpretd width and height in File table.
420 function fixupFileGeometry()
422 printfnq("Ensuring width and height is set for supported local File objects...");
425 $file->whereAdd('filename IS NOT NULL'); // local files
426 $file->whereAdd('width IS NULL OR width = 0');
429 while ($file->fetch()) {
430 // Set file geometrical properties if available
432 $image = ImageFile::fromFileObject($file);
433 } catch (ServerException $e) {
434 // We couldn't make out an image from the file.
437 $orig = clone($file);
438 $file->width = $image->width;
439 $file->height = $image->height;
440 $file->update($orig);
442 // FIXME: Do this more automagically inside ImageFile or so.
443 if ($image->getPath() != $file->getPath()) {
454 * File_thumbnail objects for local Files store their own filenames in the database.
456 function deleteLocalFileThumbnailsWithoutFilename()
458 printfnq("Removing all local File_thumbnail entries without filename property...");
461 $file->whereAdd('filename IS NOT NULL'); // local files
464 // Looping through local File entries
465 while ($file->fetch()) {
466 $thumbs = new File_thumbnail();
467 $thumbs->file_id = $file->id;
468 $thumbs->whereAdd('filename IS NULL');
469 // Checking if there were any File_thumbnail entries without filename
470 if (!$thumbs->find()) {
473 // deleting incomplete entry to allow regeneration
474 while ($thumbs->fetch()) {
484 * Delete File_thumbnail entries where the referenced file does not exist.
486 function deleteMissingLocalFileThumbnails()
488 printfnq("Removing all local File_thumbnail entries without existing files...");
490 $thumbs = new File_thumbnail();
491 $thumbs->whereAdd('filename IS NOT NULL'); // only fill in names where they're missing
492 // Checking if there were any File_thumbnail entries without filename
493 if ($thumbs->find()) {
494 while ($thumbs->fetch()) {
497 } catch (FileNotFoundException $e) {
507 * Files are now stored with their hash, so let's generate for previously uploaded files.
509 function setFilehashOnLocalFiles()
511 printfnq('Ensuring all local files have the filehash field set...');
514 $file->whereAdd('filename IS NOT NULL'); // local files
515 $file->whereAdd('filehash IS NULL', 'AND'); // without filehash value
518 while ($file->fetch()) {
520 $orig = clone($file);
521 $file->filehash = hash_file(File::FILEHASH_ALG, $file->getPath());
522 $file->update($orig);
523 } catch (FileNotFoundException $e) {
524 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";