]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/upgrade.php
Merge branch '1.0.x' into 1.1.x
[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         initLocalGroup();
49         initNoticeReshare();
50     
51         initFaveURI();
52         initSubscriptionURI();
53         initGroupMemberURI();
54
55         initProfileLists();
56
57         Event::handle('EndUpgrade');
58     }
59 }
60
61 function tableDefs()
62 {
63         $schema = array();
64         require INSTALLDIR.'/db/core.php';
65         return $schema;
66 }
67
68 function updateSchemaCore()
69 {
70     printfnq("Upgrading core schema...");
71
72     $schema = Schema::get();
73     $schemaUpdater = new SchemaUpdater($schema);
74     foreach (tableDefs() as $table => $def) {
75         $schemaUpdater->register($table, $def);
76     }
77     $schemaUpdater->checkSchema();
78
79     printfnq("DONE.\n");
80 }
81
82 function updateSchemaPlugins()
83 {
84     printfnq("Upgrading plugin schema...");
85
86     Event::handle('CheckSchema');
87
88     printfnq("DONE.\n");
89 }
90
91 function fixupNoticeRendered()
92 {
93     printfnq("Ensuring all notices have rendered HTML...");
94
95     $notice = new Notice();
96
97     $notice->whereAdd('rendered IS NULL');
98     $notice->find();
99
100     while ($notice->fetch()) {
101         $original = clone($notice);
102         $notice->rendered = common_render_content($notice->content, $notice);
103         $notice->update($original);
104     }
105
106     printfnq("DONE.\n");
107 }
108
109 function fixupNoticeConversation()
110 {
111     printfnq("Ensuring all notices have a conversation ID...");
112
113     $notice = new Notice();
114     $notice->whereAdd('conversation is null');
115     $notice->orderBy('id'); // try to get originals before replies
116     $notice->find();
117
118     while ($notice->fetch()) {
119         try {
120             $cid = null;
121     
122             $orig = clone($notice);
123     
124             if (empty($notice->reply_to)) {
125                 $notice->conversation = $notice->id;
126             } else {
127                 $reply = Notice::staticGet('id', $notice->reply_to);
128
129                 if (empty($reply)) {
130                     $notice->conversation = $notice->id;
131                 } else if (empty($reply->conversation)) {
132                     $notice->conversation = $notice->id;
133                 } else {
134                     $notice->conversation = $reply->conversation;
135                 }
136         
137                 unset($reply);
138                 $reply = null;
139             }
140
141             $result = $notice->update($orig);
142
143             $orig = null;
144             unset($orig);
145         } catch (Exception $e) {
146             printv("Error setting conversation: " . $e->getMessage());
147         }
148     }
149
150     printfnq("DONE.\n");
151 }
152
153 function fixupGroupURI()
154 {
155     printfnq("Ensuring all groups have an URI...");
156
157     $group = new User_group();
158     $group->whereAdd('uri IS NULL');
159
160     if ($group->find()) {
161         while ($group->fetch()) {
162             $orig = User_group::staticGet('id', $group->id);
163             $group->uri = $group->getUri();
164             $group->update($orig);
165         }
166     }
167
168     printfnq("DONE.\n");
169 }
170
171 function initConversation()
172 {
173     printfnq("Ensuring all conversations have a row in conversation table...");
174
175     $notice = new Notice();
176     $notice->query('select distinct notice.conversation from notice '.
177                    'where notice.conversation is not null '.
178                    'and not exists (select conversation.id from conversation where id = notice.conversation)');
179
180     while ($notice->fetch()) {
181
182         $id = $notice->conversation;
183
184         $uri = common_local_url('conversation', array('id' => $id));
185
186         // @fixme db_dataobject won't save our value for an autoincrement
187         // so we're bypassing the insert wrappers
188         $conv = new Conversation();
189         $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
190         $sql = sprintf($sql,
191                        $id,
192                        $conv->escape($uri),
193                        $conv->escape(common_sql_now()));
194         $conv->query($sql);
195     }
196
197     printfnq("DONE.\n");
198 }
199
200 function initInbox()
201 {
202     printfnq("Ensuring all users have an inbox...");
203
204     $user = new User();
205     $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
206     $user->orderBy('id');
207
208     if ($user->find()) {
209
210         while ($user->fetch()) {
211
212             try {
213                 $notice = new Notice();
214
215                 $notice->selectAdd();
216                 $notice->selectAdd('id');
217                 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
218                 $notice->whereAdd('subscription.subscriber = ' . $user->id);
219                 $notice->whereAdd('notice.created >= subscription.created');
220
221                 $ids = array();
222
223                 if ($notice->find()) {
224                     while ($notice->fetch()) {
225                         $ids[] = $notice->id;
226                     }
227                 }
228
229                 $notice = null;
230
231                 $inbox = new Inbox();
232                 $inbox->user_id = $user->id;
233                 $inbox->pack($ids);
234                 $inbox->insert();
235             } catch (Exception $e) {
236                 printv("Error initializing inbox: " . $e->getMessage());
237             }
238         }
239     }
240
241     printfnq("DONE.\n");
242 }
243
244 function initLocalGroup()
245 {
246     printfnq("Ensuring all local user groups have a local_group...");
247
248     $group = new User_group();
249     $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
250     $group->find();
251
252     while ($group->fetch()) {
253         try {
254             // Hack to check for local groups
255             if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
256                 $lg = new Local_group();
257
258                 $lg->group_id = $group->id;
259                 $lg->nickname = $group->nickname;
260                 $lg->created  = $group->created; // XXX: common_sql_now() ?
261                 $lg->modified = $group->modified;
262
263                 $lg->insert();
264             }
265         } catch (Exception $e) {
266             printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
267         }
268     }
269
270     printfnq("DONE.\n");
271 }
272
273 function initNoticeReshare()
274 {
275     printfnq("Ensuring all reshares have the correct verb and object-type...");
276     
277     $notice = new Notice();
278     $notice->whereAdd('repeat_of is not null');
279     $notice->whereAdd('(verb != "'.ActivityVerb::SHARE.'" OR object_type != "'.ActivityObject::ACTIVITY.'")');
280
281     if ($notice->find()) {
282         while ($notice->fetch()) {
283             try {
284                 $orig = Notice::staticGet('id', $notice->id);
285                 $notice->verb = ActivityVerb::SHARE;
286                 $notice->object_type = ActivityObject::ACTIVITY;
287                 $notice->update($orig);
288             } catch (Exception $e) {
289                 printfv("Error updating verb and object_type for {$notice->id}:" . $e->getMessage());
290             }
291         }
292     }
293
294     printfnq("DONE.\n");
295 }
296
297 function initFaveURI() 
298 {
299     printfnq("Ensuring all faves have a URI...");
300
301     $fave = new Fave();
302     $fave->whereAdd('uri IS NULL');
303
304     if ($fave->find()) {
305         while ($fave->fetch()) {
306             try {
307                 $fave->decache();
308                 $fave->query(sprintf('update fave '.
309                                      'set uri = "%s", '.
310                                      '    modified = "%s" '.
311                                      'where user_id = %d '.
312                                      'and notice_id = %d',
313                                      Fave::newURI($fave->user_id, $fave->notice_id, $fave->modified),
314                                      common_sql_date(strtotime($fave->modified)),
315                                      $fave->user_id,
316                                      $fave->notice_id));
317             } catch (Exception $e) {
318                 common_log(LOG_ERR, "Error updated fave URI: " . $e->getMessage());
319             }
320         }
321     }
322
323     printfnq("DONE.\n");
324 }
325
326 function initSubscriptionURI()
327 {
328     printfnq("Ensuring all subscriptions have a URI...");
329
330     $sub = new Subscription();
331     $sub->whereAdd('uri IS NULL');
332
333     if ($sub->find()) {
334         while ($sub->fetch()) {
335             try {
336                 $sub->decache();
337                 $sub->query(sprintf('update subscription '.
338                                     'set uri = "%s" '.
339                                     'where subscriber = %d '.
340                                     'and subscribed = %d',
341                                     Subscription::newURI($sub->subscriber, $sub->subscribed, $sub->created),
342                                     $sub->subscriber,
343                                     $sub->subscribed));
344             } catch (Exception $e) {
345                 common_log(LOG_ERR, "Error updated subscription URI: " . $e->getMessage());
346             }
347         }
348     }
349
350     printfnq("DONE.\n");
351 }
352
353 function initGroupMemberURI()
354 {
355     printfnq("Ensuring all group memberships have a URI...");
356
357     $mem = new Group_member();
358     $mem->whereAdd('uri IS NULL');
359
360     if ($mem->find()) {
361         while ($mem->fetch()) {
362             try {
363                 $mem->decache();
364                 $mem->query(sprintf('update group_member set uri = "%s" '.
365                                     'where profile_id = %d ' . 
366                                     'and group_id = %d ',
367                                     Group_member::newURI($mem->profile_id, $mem->group_id, $mem->created),
368                                     $mem->profile_id,
369                                     $mem->group_id));
370             } catch (Exception $e) {
371                 common_log(LOG_ERR, "Error updated membership URI: " . $e->getMessage());  
372           }
373         }
374     }
375
376     printfnq("DONE.\n");
377 }
378
379 function initProfileLists()
380 {
381     printfnq("Ensuring all profile tags have a corresponding list...");
382
383     $ptag = new Profile_tag();
384     $ptag->selectAdd();
385     $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
386     $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list '.
387                     'where profile_tag.tagger = profile_list.tagger '.
388                     'and profile_tag.tag = profile_list.tag)');
389     $ptag->groupBy('tagger, tag');
390     $ptag->orderBy('tagger, tag');
391
392     if ($ptag->find()) {
393         while ($ptag->fetch()) {
394             $plist = new Profile_list();
395
396             $plist->tagger   = $ptag->tagger;
397             $plist->tag      = $ptag->tag;
398             $plist->private  = 0;
399             $plist->created  = common_sql_now();
400             $plist->modified = $plist->created;
401             $plist->mainpage = common_local_url('showprofiletag',
402                                                 array('tagger' => $plist->getTagger()->nickname,
403                                                       'tag'    => $plist->tag));;
404
405             $plist->tagged_count     = $ptag->tagged_count;
406             $plist->subscriber_count = 0;
407
408             $plist->insert();
409
410             $orig = clone($plist);
411             // After insert since it uses auto-generated ID
412             $plist->uri      = common_local_url('profiletagbyid',
413                                         array('id' => $plist->id, 'tagger_id' => $plist->tagger));
414
415             $plist->update($orig);
416         }
417     }
418
419     printfnq("DONE.\n");
420 }
421
422 main();