]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/upgrade.php
Dynamically generate thumbnails (see full text)
[quix0rs-gnu-social.git] / scripts / upgrade.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a distributed open-source microblogging tool
5  * Copyright (C) 2008-2011 StatusNet, Inc.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
22
23 $shortoptions = 'x::';
24 $longoptions = array('extensions=');
25
26 $helptext = <<<END_OF_UPGRADE_HELP
27 php upgrade.php [options]
28 Upgrade database schema and data to latest software
29
30 END_OF_UPGRADE_HELP;
31
32 require_once INSTALLDIR.'/scripts/commandline.inc';
33
34 function main()
35 {
36     if (Event::handle('StartUpgrade')) {
37         updateSchemaCore();
38         updateSchemaPlugins();
39
40         // These replace old "fixup_*" scripts
41
42         fixupNoticeRendered();
43         fixupNoticeConversation();
44         initConversation();
45         fixupGroupURI();
46         fixupFileGeometry();
47
48         initGroupProfileId();
49         initLocalGroup();
50         initNoticeReshare();
51     
52         initFaveURI();
53         initSubscriptionURI();
54         initGroupMemberURI();
55
56         initProfileLists();
57
58         Event::handle('EndUpgrade');
59     }
60 }
61
62 function tableDefs()
63 {
64         $schema = array();
65         require INSTALLDIR.'/db/core.php';
66         return $schema;
67 }
68
69 function updateSchemaCore()
70 {
71     printfnq("Upgrading core schema...");
72
73     $schema = Schema::get();
74     $schemaUpdater = new SchemaUpdater($schema);
75     foreach (tableDefs() as $table => $def) {
76         $schemaUpdater->register($table, $def);
77     }
78     $schemaUpdater->checkSchema();
79
80     printfnq("DONE.\n");
81 }
82
83 function updateSchemaPlugins()
84 {
85     printfnq("Upgrading plugin schema...");
86
87     Event::handle('CheckSchema');
88
89     printfnq("DONE.\n");
90 }
91
92 function fixupNoticeRendered()
93 {
94     printfnq("Ensuring all notices have rendered HTML...");
95
96     $notice = new Notice();
97
98     $notice->whereAdd('rendered IS NULL');
99     $notice->find();
100
101     while ($notice->fetch()) {
102         $original = clone($notice);
103         $notice->rendered = common_render_content($notice->content, $notice);
104         $notice->update($original);
105     }
106
107     printfnq("DONE.\n");
108 }
109
110 function fixupNoticeConversation()
111 {
112     printfnq("Ensuring all notices have a conversation ID...");
113
114     $notice = new Notice();
115     $notice->whereAdd('conversation is null');
116     $notice->orderBy('id'); // try to get originals before replies
117     $notice->find();
118
119     while ($notice->fetch()) {
120         try {
121             $cid = null;
122     
123             $orig = clone($notice);
124     
125             if (empty($notice->reply_to)) {
126                 $notice->conversation = $notice->id;
127             } else {
128                 $reply = Notice::getKV('id', $notice->reply_to);
129
130                 if (empty($reply)) {
131                     $notice->conversation = $notice->id;
132                 } else if (empty($reply->conversation)) {
133                     $notice->conversation = $notice->id;
134                 } else {
135                     $notice->conversation = $reply->conversation;
136                 }
137         
138                 unset($reply);
139                 $reply = null;
140             }
141
142             $result = $notice->update($orig);
143
144             $orig = null;
145             unset($orig);
146         } catch (Exception $e) {
147             printv("Error setting conversation: " . $e->getMessage());
148         }
149     }
150
151     printfnq("DONE.\n");
152 }
153
154 function fixupGroupURI()
155 {
156     printfnq("Ensuring all groups have an URI...");
157
158     $group = new User_group();
159     $group->whereAdd('uri IS NULL');
160
161     if ($group->find()) {
162         while ($group->fetch()) {
163             $orig = User_group::getKV('id', $group->id);
164             $group->uri = $group->getUri();
165             $group->update($orig);
166         }
167     }
168
169     printfnq("DONE.\n");
170 }
171
172 function initConversation()
173 {
174     printfnq("Ensuring all conversations have a row in conversation table...");
175
176     $notice = new Notice();
177     $notice->query('select distinct notice.conversation from notice '.
178                    'where notice.conversation is not null '.
179                    'and not exists (select conversation.id from conversation where id = notice.conversation)');
180
181     while ($notice->fetch()) {
182
183         $id = $notice->conversation;
184
185         $uri = common_local_url('conversation', array('id' => $id));
186
187         // @fixme db_dataobject won't save our value for an autoincrement
188         // so we're bypassing the insert wrappers
189         $conv = new Conversation();
190         $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
191         $sql = sprintf($sql,
192                        $id,
193                        $conv->escape($uri),
194                        $conv->escape(common_sql_now()));
195         $conv->query($sql);
196     }
197
198     printfnq("DONE.\n");
199 }
200
201 function initGroupProfileId()
202 {
203     printfnq("Ensuring all User_group entries have a Profile and profile_id...");
204
205     $group = new User_group();
206     $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
207     $group->find();
208
209     while ($group->fetch()) {
210         try {
211             // We must create a new, incrementally assigned profile_id
212             $profile = new Profile();
213             $profile->nickname   = $group->nickname;
214             $profile->fullname   = $group->fullname;
215             $profile->profileurl = $group->mainpage;
216             $profile->homepage   = $group->homepage;
217             $profile->bio        = $group->description;
218             $profile->location   = $group->location;
219             $profile->created    = $group->created;
220             $profile->modified   = $group->modified;
221
222             $profile->query('BEGIN');
223             $id = $profile->insert();
224             if (empty($id)) {
225                 $profile->query('ROLLBACK');
226                 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
227             }
228             $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
229             $profile->query('COMMIT');
230
231             $profile->free();
232         } catch (Exception $e) {
233             printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
234         }
235     }
236
237     printfnq("DONE.\n");
238 }
239
240 function initLocalGroup()
241 {
242     printfnq("Ensuring all local user groups have a local_group...");
243
244     $group = new User_group();
245     $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
246     $group->find();
247
248     while ($group->fetch()) {
249         try {
250             // Hack to check for local groups
251             if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
252                 $lg = new Local_group();
253
254                 $lg->group_id = $group->id;
255                 $lg->nickname = $group->nickname;
256                 $lg->created  = $group->created; // XXX: common_sql_now() ?
257                 $lg->modified = $group->modified;
258
259                 $lg->insert();
260             }
261         } catch (Exception $e) {
262             printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
263         }
264     }
265
266     printfnq("DONE.\n");
267 }
268
269 function initNoticeReshare()
270 {
271     printfnq("Ensuring all reshares have the correct verb and object-type...");
272     
273     $notice = new Notice();
274     $notice->whereAdd('repeat_of is not null');
275     $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
276
277     if ($notice->find()) {
278         while ($notice->fetch()) {
279             try {
280                 $orig = Notice::getKV('id', $notice->id);
281                 $notice->verb = ActivityVerb::SHARE;
282                 $notice->object_type = ActivityObject::ACTIVITY;
283                 $notice->update($orig);
284             } catch (Exception $e) {
285                 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
286             }
287         }
288     }
289
290     printfnq("DONE.\n");
291 }
292
293 function initFaveURI() 
294 {
295     printfnq("Ensuring all faves have a URI...");
296
297     $fave = new Fave();
298     $fave->whereAdd('uri IS NULL');
299
300     if ($fave->find()) {
301         while ($fave->fetch()) {
302             try {
303                 $fave->decache();
304                 $fave->query(sprintf('update fave '.
305                                      'set uri = "%s", '.
306                                      '    modified = "%s" '.
307                                      'where user_id = %d '.
308                                      'and notice_id = %d',
309                                      Fave::newURI($fave->user_id, $fave->notice_id, $fave->modified),
310                                      common_sql_date(strtotime($fave->modified)),
311                                      $fave->user_id,
312                                      $fave->notice_id));
313             } catch (Exception $e) {
314                 common_log(LOG_ERR, "Error updated fave URI: " . $e->getMessage());
315             }
316         }
317     }
318
319     printfnq("DONE.\n");
320 }
321
322 function initSubscriptionURI()
323 {
324     printfnq("Ensuring all subscriptions have a URI...");
325
326     $sub = new Subscription();
327     $sub->whereAdd('uri IS NULL');
328
329     if ($sub->find()) {
330         while ($sub->fetch()) {
331             try {
332                 $sub->decache();
333                 $sub->query(sprintf('update subscription '.
334                                     'set uri = "%s" '.
335                                     'where subscriber = %d '.
336                                     'and subscribed = %d',
337                                     Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
338                                     $sub->subscriber,
339                                     $sub->subscribed));
340             } catch (Exception $e) {
341                 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
342             }
343         }
344     }
345
346     printfnq("DONE.\n");
347 }
348
349 function initGroupMemberURI()
350 {
351     printfnq("Ensuring all group memberships have a URI...");
352
353     $mem = new Group_member();
354     $mem->whereAdd('uri IS NULL');
355
356     if ($mem->find()) {
357         while ($mem->fetch()) {
358             try {
359                 $mem->decache();
360                 $mem->query(sprintf('update group_member set uri = "%s" '.
361                                     'where profile_id = %d ' . 
362                                     'and group_id = %d ',
363                                     Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
364                                     $mem->profile_id,
365                                     $mem->group_id));
366             } catch (Exception $e) {
367                 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());  
368           }
369         }
370     }
371
372     printfnq("DONE.\n");
373 }
374
375 function initProfileLists()
376 {
377     printfnq("Ensuring all profile tags have a corresponding list...");
378
379     $ptag = new Profile_tag();
380     $ptag->selectAdd();
381     $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
382     $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
383                     'where profile_tag.tagger = profile_list.tagger '.
384                     'and profile_tag.tag = profile_list.tag)');
385     $ptag->groupBy('tagger, tag');
386     $ptag->orderBy('tagger, tag');
387
388     if ($ptag->find()) {
389         while ($ptag->fetch()) {
390             $plist = new Profile_list();
391
392             $plist->tagger   = $ptag->tagger;
393             $plist->tag      = $ptag->tag;
394             $plist->private  = 0;
395             $plist->created  = common_sql_now();
396             $plist->modified = $plist->created;
397             $plist->mainpage = common_local_url('showprofiletag',
398                                                 array('tagger' => $plist->getTagger()->nickname,
399                                                       'tag'    => $plist->tag));;
400
401             $plist->tagged_count     = $ptag->tagged_count;
402             $plist->subscriber_count = 0;
403
404             $plist->insert();
405
406             $orig = clone($plist);
407             // After insert since it uses auto-generated ID
408             $plist->uri      = common_local_url('profiletagbyid',
409                                         array('id' => $plist->id, 'tagger_id' => $plist->tagger));
410
411             $plist->update($orig);
412         }
413     }
414
415     printfnq("DONE.\n");
416 }
417
418 /*
419  * Added as we now store interpretd width and height in File table.
420  */
421 function fixupFileGeometry()
422 {
423     printfnq("Ensuring width and height is set for supported local File objects...");
424
425     $file = new File();
426     $file->whereAdd('filename IS NOT NULL');    // local files
427     $file->whereAdd('width IS NULL OR width = 0');
428
429     if ($file->find()) {
430         while ($file->fetch()) {
431             // Add support for video sizes too
432             try {
433                 $image = new ImageFile($file->id, $file->getPath());
434             } catch (UnsupportedMediaException $e) {
435                 continue;
436             }
437             $orig = clone($file);
438             $file->width = $image->width;
439             $file->height = $image->height;
440             $file->update($orig);
441         }
442     }
443
444     printfnq("DONE.\n");
445 }
446
447 main();