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.php';
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,
109 $notice->getProfile(),
110 $notice->hasParent() ? $notice->getParent() : null);
111 $notice->update($original);
117 function fixupNoticeConversation()
119 printfnq("Ensuring all notices have a conversation ID...");
121 $notice = new Notice();
122 $notice->whereAdd('conversation is null');
123 $notice->orderBy('id'); // try to get originals before replies
126 while ($notice->fetch()) {
130 $orig = clone($notice);
132 if (empty($notice->reply_to)) {
133 $notice->conversation = $notice->id;
135 $reply = Notice::getKV('id', $notice->reply_to);
138 $notice->conversation = $notice->id;
139 } else if (empty($reply->conversation)) {
140 $notice->conversation = $notice->id;
142 $notice->conversation = $reply->conversation;
149 $result = $notice->update($orig);
153 } catch (Exception $e) {
154 printv("Error setting conversation: " . $e->getMessage());
161 function fixupGroupURI()
163 printfnq("Ensuring all groups have an URI...");
165 $group = new User_group();
166 $group->whereAdd('uri IS NULL');
168 if ($group->find()) {
169 while ($group->fetch()) {
170 $orig = User_group::getKV('id', $group->id);
171 $group->uri = $group->getUri();
172 $group->update($orig);
179 function initConversation()
181 printfnq("Ensuring all conversations have a row in conversation table...");
183 $notice = new Notice();
184 $notice->query('select distinct notice.conversation from notice '.
185 'where notice.conversation is not null '.
186 'and not exists (select conversation.id from conversation where id = notice.conversation)');
188 while ($notice->fetch()) {
190 $id = $notice->conversation;
192 $uri = common_local_url('conversation', array('id' => $id));
194 // @fixme db_dataobject won't save our value for an autoincrement
195 // so we're bypassing the insert wrappers
196 $conv = new Conversation();
197 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
201 $conv->escape(common_sql_now()));
208 function fixupConversationURIs()
210 printfnq("Ensuring all conversations have a URI...");
212 $conv = new Conversation();
213 $conv->whereAdd('uri IS NULL');
217 while ($conv->fetch()) {
218 $uri = common_local_url('conversation', array('id' => $conv->id));
219 $sql = sprintf('UPDATE conversation SET uri="%1$s" WHERE id="%2$d";',
220 $conv->escape($uri), $conv->id);
222 if (($conv->N-++$rounds) % 500 == 0) {
223 printfnq(sprintf(' %d items left...', $conv->N-$rounds));
231 function initGroupProfileId()
233 printfnq("Ensuring all User_group entries have a Profile and profile_id...");
235 $group = new User_group();
236 $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
239 while ($group->fetch()) {
241 // We must create a new, incrementally assigned profile_id
242 $profile = new Profile();
243 $profile->nickname = $group->nickname;
244 $profile->fullname = $group->fullname;
245 $profile->profileurl = $group->mainpage;
246 $profile->homepage = $group->homepage;
247 $profile->bio = $group->description;
248 $profile->location = $group->location;
249 $profile->created = $group->created;
250 $profile->modified = $group->modified;
252 $profile->query('BEGIN');
253 $id = $profile->insert();
255 $profile->query('ROLLBACK');
256 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
258 $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
259 $profile->query('COMMIT');
262 } catch (Exception $e) {
263 printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
270 function initLocalGroup()
272 printfnq("Ensuring all local user groups have a local_group...");
274 $group = new User_group();
275 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
278 while ($group->fetch()) {
280 // Hack to check for local groups
281 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
282 $lg = new Local_group();
284 $lg->group_id = $group->id;
285 $lg->nickname = $group->nickname;
286 $lg->created = $group->created; // XXX: common_sql_now() ?
287 $lg->modified = $group->modified;
291 } catch (Exception $e) {
292 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
299 function initNoticeReshare()
301 printfnq("Ensuring all reshares have the correct verb and object-type...");
303 $notice = new Notice();
304 $notice->whereAdd('repeat_of is not null');
305 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
307 if ($notice->find()) {
308 while ($notice->fetch()) {
310 $orig = Notice::getKV('id', $notice->id);
311 $notice->verb = ActivityVerb::SHARE;
312 $notice->object_type = ActivityObject::ACTIVITY;
313 $notice->update($orig);
314 } catch (Exception $e) {
315 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
323 function initSubscriptionURI()
325 printfnq("Ensuring all subscriptions have a URI...");
327 $sub = new Subscription();
328 $sub->whereAdd('uri IS NULL');
331 while ($sub->fetch()) {
334 $sub->query(sprintf('update subscription '.
336 'where subscriber = %d '.
337 'and subscribed = %d',
338 $sub->escape(Subscription::newUri($sub->getSubscriber(), $sub->getSubscribed(), $sub->created)),
341 } catch (Exception $e) {
342 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
350 function initGroupMemberURI()
352 printfnq("Ensuring all group memberships have a URI...");
354 $mem = new Group_member();
355 $mem->whereAdd('uri IS NULL');
358 while ($mem->fetch()) {
361 $mem->query(sprintf('update group_member set uri = "%s" '.
362 'where profile_id = %d ' .
363 'and group_id = %d ',
364 Group_member::newUri(Profile::getByID($mem->profile_id), User_group::getByID($mem->group_id), $mem->created),
367 } catch (Exception $e) {
368 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
376 function initProfileLists()
378 printfnq("Ensuring all profile tags have a corresponding list...");
380 $ptag = new Profile_tag();
382 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
383 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
384 'where profile_tag.tagger = profile_list.tagger '.
385 'and profile_tag.tag = profile_list.tag)');
386 $ptag->groupBy('tagger, tag');
387 $ptag->orderBy('tagger, tag');
390 while ($ptag->fetch()) {
391 $plist = new Profile_list();
393 $plist->tagger = $ptag->tagger;
394 $plist->tag = $ptag->tag;
396 $plist->created = common_sql_now();
397 $plist->modified = $plist->created;
398 $plist->mainpage = common_local_url('showprofiletag',
399 array('tagger' => $plist->getTagger()->nickname,
400 'tag' => $plist->tag));;
402 $plist->tagged_count = $ptag->tagged_count;
403 $plist->subscriber_count = 0;
407 $orig = clone($plist);
408 // After insert since it uses auto-generated ID
409 $plist->uri = common_local_url('profiletagbyid',
410 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
412 $plist->update($orig);
420 * Added as we now store interpretd width and height in File table.
422 function fixupFileGeometry()
424 printfnq("Ensuring width and height is set for supported local File objects...");
427 $file->whereAdd('filename IS NOT NULL'); // local files
428 $file->whereAdd('width IS NULL OR width = 0');
431 while ($file->fetch()) {
432 // Set file geometrical properties if available
434 $image = ImageFile::fromFileObject($file);
435 } catch (ServerException $e) {
436 // We couldn't make out an image from the file.
439 $orig = clone($file);
440 $file->width = $image->width;
441 $file->height = $image->height;
442 $file->update($orig);
444 // FIXME: Do this more automagically inside ImageFile or so.
445 if ($image->getPath() != $file->getPath()) {
456 * File_thumbnail objects for local Files store their own filenames in the database.
458 function deleteLocalFileThumbnailsWithoutFilename()
460 printfnq("Removing all local File_thumbnail entries without filename property...");
463 $file->whereAdd('filename IS NOT NULL'); // local files
466 // Looping through local File entries
467 while ($file->fetch()) {
468 $thumbs = new File_thumbnail();
469 $thumbs->file_id = $file->id;
470 $thumbs->whereAdd('filename IS NULL');
471 // Checking if there were any File_thumbnail entries without filename
472 if (!$thumbs->find()) {
475 // deleting incomplete entry to allow regeneration
476 while ($thumbs->fetch()) {
486 * Delete File_thumbnail entries where the referenced file does not exist.
488 function deleteMissingLocalFileThumbnails()
490 printfnq("Removing all local File_thumbnail entries without existing files...");
492 $thumbs = new File_thumbnail();
493 $thumbs->whereAdd('filename IS NOT NULL'); // only fill in names where they're missing
494 // Checking if there were any File_thumbnail entries without filename
495 if ($thumbs->find()) {
496 while ($thumbs->fetch()) {
499 } catch (FileNotFoundException $e) {
509 * Files are now stored with their hash, so let's generate for previously uploaded files.
511 function setFilehashOnLocalFiles()
513 printfnq('Ensuring all local files have the filehash field set...');
516 $file->whereAdd('filename IS NOT NULL'); // local files
517 $file->whereAdd('filehash IS NULL', 'AND'); // without filehash value
520 while ($file->fetch()) {
522 $orig = clone($file);
523 $file->filehash = hash_file(File::FILEHASH_ALG, $file->getPath());
524 $file->update($orig);
525 } catch (FileNotFoundException $e) {
526 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";