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