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')) {
38 updateSchemaPlugins();
40 // These replace old "fixup_*" scripts
42 fixupNoticeRendered();
43 fixupNoticeConversation();
47 deleteLocalFileThumbnailsWithoutFilename();
48 deleteMissingLocalFileThumbnails();
54 initSubscriptionURI();
59 Event::handle('EndUpgrade');
66 require INSTALLDIR.'/db/core.php';
70 function updateSchemaCore()
72 printfnq("Upgrading core schema...");
74 $schema = Schema::get();
75 $schemaUpdater = new SchemaUpdater($schema);
76 foreach (tableDefs() as $table => $def) {
77 $schemaUpdater->register($table, $def);
79 $schemaUpdater->checkSchema();
84 function updateSchemaPlugins()
86 printfnq("Upgrading plugin schema...");
88 Event::handle('CheckSchema');
93 function fixupNoticeRendered()
95 printfnq("Ensuring all notices have rendered HTML...");
97 $notice = new Notice();
99 $notice->whereAdd('rendered IS NULL');
102 while ($notice->fetch()) {
103 $original = clone($notice);
104 $notice->rendered = common_render_content($notice->content, $notice);
105 $notice->update($original);
111 function fixupNoticeConversation()
113 printfnq("Ensuring all notices have a conversation ID...");
115 $notice = new Notice();
116 $notice->whereAdd('conversation is null');
117 $notice->orderBy('id'); // try to get originals before replies
120 while ($notice->fetch()) {
124 $orig = clone($notice);
126 if (empty($notice->reply_to)) {
127 $notice->conversation = $notice->id;
129 $reply = Notice::getKV('id', $notice->reply_to);
132 $notice->conversation = $notice->id;
133 } else if (empty($reply->conversation)) {
134 $notice->conversation = $notice->id;
136 $notice->conversation = $reply->conversation;
143 $result = $notice->update($orig);
147 } catch (Exception $e) {
148 printv("Error setting conversation: " . $e->getMessage());
155 function fixupGroupURI()
157 printfnq("Ensuring all groups have an URI...");
159 $group = new User_group();
160 $group->whereAdd('uri IS NULL');
162 if ($group->find()) {
163 while ($group->fetch()) {
164 $orig = User_group::getKV('id', $group->id);
165 $group->uri = $group->getUri();
166 $group->update($orig);
173 function initConversation()
175 printfnq("Ensuring all conversations have a row in conversation table...");
177 $notice = new Notice();
178 $notice->query('select distinct notice.conversation from notice '.
179 'where notice.conversation is not null '.
180 'and not exists (select conversation.id from conversation where id = notice.conversation)');
182 while ($notice->fetch()) {
184 $id = $notice->conversation;
186 $uri = common_local_url('conversation', array('id' => $id));
188 // @fixme db_dataobject won't save our value for an autoincrement
189 // so we're bypassing the insert wrappers
190 $conv = new Conversation();
191 $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
195 $conv->escape(common_sql_now()));
202 function initGroupProfileId()
204 printfnq("Ensuring all User_group entries have a Profile and profile_id...");
206 $group = new User_group();
207 $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
210 while ($group->fetch()) {
212 // We must create a new, incrementally assigned profile_id
213 $profile = new Profile();
214 $profile->nickname = $group->nickname;
215 $profile->fullname = $group->fullname;
216 $profile->profileurl = $group->mainpage;
217 $profile->homepage = $group->homepage;
218 $profile->bio = $group->description;
219 $profile->location = $group->location;
220 $profile->created = $group->created;
221 $profile->modified = $group->modified;
223 $profile->query('BEGIN');
224 $id = $profile->insert();
226 $profile->query('ROLLBACK');
227 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
229 $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
230 $profile->query('COMMIT');
233 } catch (Exception $e) {
234 printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
241 function initLocalGroup()
243 printfnq("Ensuring all local user groups have a local_group...");
245 $group = new User_group();
246 $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
249 while ($group->fetch()) {
251 // Hack to check for local groups
252 if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
253 $lg = new Local_group();
255 $lg->group_id = $group->id;
256 $lg->nickname = $group->nickname;
257 $lg->created = $group->created; // XXX: common_sql_now() ?
258 $lg->modified = $group->modified;
262 } catch (Exception $e) {
263 printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
270 function initNoticeReshare()
272 printfnq("Ensuring all reshares have the correct verb and object-type...");
274 $notice = new Notice();
275 $notice->whereAdd('repeat_of is not null');
276 $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
278 if ($notice->find()) {
279 while ($notice->fetch()) {
281 $orig = Notice::getKV('id', $notice->id);
282 $notice->verb = ActivityVerb::SHARE;
283 $notice->object_type = ActivityObject::ACTIVITY;
284 $notice->update($orig);
285 } catch (Exception $e) {
286 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
294 function initSubscriptionURI()
296 printfnq("Ensuring all subscriptions have a URI...");
298 $sub = new Subscription();
299 $sub->whereAdd('uri IS NULL');
302 while ($sub->fetch()) {
305 $sub->query(sprintf('update subscription '.
307 'where subscriber = %d '.
308 'and subscribed = %d',
309 Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
312 } catch (Exception $e) {
313 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
321 function initGroupMemberURI()
323 printfnq("Ensuring all group memberships have a URI...");
325 $mem = new Group_member();
326 $mem->whereAdd('uri IS NULL');
329 while ($mem->fetch()) {
332 $mem->query(sprintf('update group_member set uri = "%s" '.
333 'where profile_id = %d ' .
334 'and group_id = %d ',
335 Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
338 } catch (Exception $e) {
339 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());
347 function initProfileLists()
349 printfnq("Ensuring all profile tags have a corresponding list...");
351 $ptag = new Profile_tag();
353 $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
354 $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
355 'where profile_tag.tagger = profile_list.tagger '.
356 'and profile_tag.tag = profile_list.tag)');
357 $ptag->groupBy('tagger, tag');
358 $ptag->orderBy('tagger, tag');
361 while ($ptag->fetch()) {
362 $plist = new Profile_list();
364 $plist->tagger = $ptag->tagger;
365 $plist->tag = $ptag->tag;
367 $plist->created = common_sql_now();
368 $plist->modified = $plist->created;
369 $plist->mainpage = common_local_url('showprofiletag',
370 array('tagger' => $plist->getTagger()->nickname,
371 'tag' => $plist->tag));;
373 $plist->tagged_count = $ptag->tagged_count;
374 $plist->subscriber_count = 0;
378 $orig = clone($plist);
379 // After insert since it uses auto-generated ID
380 $plist->uri = common_local_url('profiletagbyid',
381 array('id' => $plist->id, 'tagger_id' => $plist->tagger));
383 $plist->update($orig);
391 * Added as we now store interpretd width and height in File table.
393 function fixupFileGeometry()
395 printfnq("Ensuring width and height is set for supported local File objects...");
398 $file->whereAdd('filename IS NOT NULL'); // local files
399 $file->whereAdd('width IS NULL OR width = 0');
402 while ($file->fetch()) {
403 // Set file geometrical properties if available
405 $image = ImageFile::fromFileObject($file);
406 } catch (ServerException $e) {
407 // We couldn't make out an image from the file.
410 $orig = clone($file);
411 $file->width = $image->width;
412 $file->height = $image->height;
413 $file->update($orig);
415 // FIXME: Do this more automagically inside ImageFile or so.
416 if ($image->getPath() != $file->getPath()) {
427 * File_thumbnail objects for local Files store their own filenames in the database.
429 function deleteLocalFileThumbnailsWithoutFilename()
431 printfnq("Removing all local File_thumbnail entries without filename property...");
434 $file->whereAdd('filename IS NOT NULL'); // local files
437 // Looping through local File entries
438 while ($file->fetch()) {
439 $thumbs = new File_thumbnail();
440 $thumbs->file_id = $file->id;
441 $thumbs->whereAdd('filename IS NULL');
442 // Checking if there were any File_thumbnail entries without filename
443 if (!$thumbs->find()) {
446 // deleting incomplete entry to allow regeneration
447 while ($thumbs->fetch()) {
457 * Delete File_thumbnail entries where the referenced file does not exist.
459 function deleteMissingLocalFileThumbnails()
461 printfnq("Removing all local File_thumbnail entries without existing files...");
463 $thumbs = new File_thumbnail();
464 $thumbs->whereAdd('filename IS NOT NULL'); // only fill in names where they're missing
465 // Checking if there were any File_thumbnail entries without filename
466 if ($thumbs->find()) {
467 while ($thumbs->fetch()) {
468 if (!file_exists(File_thumbnail::path($thumbs->filename))) {