]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/upgrade.php
Add support (and upgrade path) for group profiles
[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         initInbox();
46         fixupGroupURI();
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 initInbox()
202 {
203     printfnq("Ensuring all users have an inbox...");
204
205     $user = new User();
206     $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
207     $user->orderBy('id');
208
209     if ($user->find()) {
210
211         while ($user->fetch()) {
212
213             try {
214                 $notice = new Notice();
215
216                 $notice->selectAdd();
217                 $notice->selectAdd('id');
218                 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
219                 $notice->whereAdd('subscription.subscriber = ' . $user->id);
220                 $notice->whereAdd('notice.created >= subscription.created');
221
222                 $ids = array();
223
224                 if ($notice->find()) {
225                     while ($notice->fetch()) {
226                         $ids[] = $notice->id;
227                     }
228                 }
229
230                 $notice = null;
231
232                 $inbox = new Inbox();
233                 $inbox->user_id = $user->id;
234                 $inbox->pack($ids);
235                 $inbox->insert();
236             } catch (Exception $e) {
237                 printv("Error initializing inbox: " . $e->getMessage());
238             }
239         }
240     }
241
242     printfnq("DONE.\n");
243 }
244
245 function initGroupProfileId()
246 {
247     printfnq("Ensuring all User_group entries have a Profile and profile_id...");
248
249     $group = new User_group();
250     $group->whereAdd('NOT EXISTS (SELECT id FROM profile WHERE id = user_group.profile_id)');
251     $group->find();
252
253     while ($group->fetch()) {
254         try {
255             // We must create a new, incrementally assigned profile_id
256             $profile = new Profile();
257             $profile->nickname   = $group->nickname;
258             $profile->fullname   = $group->fullname;
259             $profile->profileurl = $group->mainpage;
260             $profile->homepage   = $group->homepage;
261             $profile->bio        = $group->description;
262             $profile->location   = $group->location;
263             $profile->created    = $group->created;
264             $profile->modified   = $group->modified;
265
266             $profile->query('BEGIN');
267             $id = $profile->insert();
268             if (empty($id)) {
269                 $profile->query('ROLLBACK');
270                 throw new Exception('Profile insertion failed, profileurl: '.$profile->profileurl);
271             }
272             $group->query("UPDATE user_group SET profile_id={$id} WHERE id={$group->id}");
273             $profile->query('COMMIT');
274
275             $profile->free();
276         } catch (Exception $e) {
277             printfv("Error initializing Profile for group {$group->nickname}:" . $e->getMessage());
278         }
279     }
280
281     printfnq("DONE.\n");
282 }
283
284 function initLocalGroup()
285 {
286     printfnq("Ensuring all local user groups have a local_group...");
287
288     $group = new User_group();
289     $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
290     $group->find();
291
292     while ($group->fetch()) {
293         try {
294             // Hack to check for local groups
295             if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
296                 $lg = new Local_group();
297
298                 $lg->group_id = $group->id;
299                 $lg->nickname = $group->nickname;
300                 $lg->created  = $group->created; // XXX: common_sql_now() ?
301                 $lg->modified = $group->modified;
302
303                 $lg->insert();
304             }
305         } catch (Exception $e) {
306             printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
307         }
308     }
309
310     printfnq("DONE.\n");
311 }
312
313 function initNoticeReshare()
314 {
315     printfnq("Ensuring all reshares have the correct verb and object-type...");
316     
317     $notice = new Notice();
318     $notice->whereAdd('repeat_of is not null');
319     $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
320
321     if ($notice->find()) {
322         while ($notice->fetch()) {
323             try {
324                 $orig = Notice::getKV('id', $notice->id);
325                 $notice->verb = ActivityVerb::SHARE;
326                 $notice->object_type = ActivityObject::ACTIVITY;
327                 $notice->update($orig);
328             } catch (Exception $e) {
329                 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
330             }
331         }
332     }
333
334     printfnq("DONE.\n");
335 }
336
337 function initFaveURI() 
338 {
339     printfnq("Ensuring all faves have a URI...");
340
341     $fave = new Fave();
342     $fave->whereAdd('uri IS NULL');
343
344     if ($fave->find()) {
345         while ($fave->fetch()) {
346             try {
347                 $fave->decache();
348                 $fave->query(sprintf('update fave '.
349                                      'set uri = "%s", '.
350                                      '    modified = "%s" '.
351                                      'where user_id = %d '.
352                                      'and notice_id = %d',
353                                      Fave::newURI($fave->user_id, $fave->notice_id, $fave->modified),
354                                      common_sql_date(strtotime($fave->modified)),
355                                      $fave->user_id,
356                                      $fave->notice_id));
357             } catch (Exception $e) {
358                 common_log(LOG_ERR, "Error updated fave URI: " . $e->getMessage());
359             }
360         }
361     }
362
363     printfnq("DONE.\n");
364 }
365
366 function initSubscriptionURI()
367 {
368     printfnq("Ensuring all subscriptions have a URI...");
369
370     $sub = new Subscription();
371     $sub->whereAdd('uri IS NULL');
372
373     if ($sub->find()) {
374         while ($sub->fetch()) {
375             try {
376                 $sub->decache();
377                 $sub->query(sprintf('update subscription '.
378                                     'set uri = "%s" '.
379                                     'where subscriber = %d '.
380                                     'and subscribed = %d',
381                                     Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
382                                     $sub->subscriber,
383                                     $sub->subscribed));
384             } catch (Exception $e) {
385                 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
386             }
387         }
388     }
389
390     printfnq("DONE.\n");
391 }
392
393 function initGroupMemberURI()
394 {
395     printfnq("Ensuring all group memberships have a URI...");
396
397     $mem = new Group_member();
398     $mem->whereAdd('uri IS NULL');
399
400     if ($mem->find()) {
401         while ($mem->fetch()) {
402             try {
403                 $mem->decache();
404                 $mem->query(sprintf('update group_member set uri = "%s" '.
405                                     'where profile_id = %d ' . 
406                                     'and group_id = %d ',
407                                     Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
408                                     $mem->profile_id,
409                                     $mem->group_id));
410             } catch (Exception $e) {
411                 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());  
412           }
413         }
414     }
415
416     printfnq("DONE.\n");
417 }
418
419 function initProfileLists()
420 {
421     printfnq("Ensuring all profile tags have a corresponding list...");
422
423     $ptag = new Profile_tag();
424     $ptag->selectAdd();
425     $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
426     $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
427                     'where profile_tag.tagger = profile_list.tagger '.
428                     'and profile_tag.tag = profile_list.tag)');
429     $ptag->groupBy('tagger, tag');
430     $ptag->orderBy('tagger, tag');
431
432     if ($ptag->find()) {
433         while ($ptag->fetch()) {
434             $plist = new Profile_list();
435
436             $plist->tagger   = $ptag->tagger;
437             $plist->tag      = $ptag->tag;
438             $plist->private  = 0;
439             $plist->created  = common_sql_now();
440             $plist->modified = $plist->created;
441             $plist->mainpage = common_local_url('showprofiletag',
442                                                 array('tagger' => $plist->getTagger()->nickname,
443                                                       'tag'    => $plist->tag));;
444
445             $plist->tagged_count     = $ptag->tagged_count;
446             $plist->subscriber_count = 0;
447
448             $plist->insert();
449
450             $orig = clone($plist);
451             // After insert since it uses auto-generated ID
452             $plist->uri      = common_local_url('profiletagbyid',
453                                         array('id' => $plist->id, 'tagger_id' => $plist->tagger));
454
455             $plist->update($orig);
456         }
457     }
458
459     printfnq("DONE.\n");
460 }
461
462 main();