]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/upgrade.php
initialize the local_group table
[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     updateSchemaCore();
37     updateSchemaPlugins();
38
39     // These replace old "fixup_*" scripts
40
41     fixupNoticeRendered();
42     fixupNoticeConversation();
43     initConversation();
44     initInbox();
45     fixupGroupURI();
46     initLocalGroup();
47 }
48
49 function tableDefs()
50 {
51         $schema = array();
52         require INSTALLDIR.'/db/core.php';
53         return $schema;
54 }
55
56 function updateSchemaCore()
57 {
58     printfnq("Upgrading core schema...");
59
60     $schema = Schema::get();
61     $schemaUpdater = new SchemaUpdater($schema);
62     foreach (tableDefs() as $table => $def) {
63         $schemaUpdater->register($table, $def);
64     }
65     $schemaUpdater->checkSchema();
66
67     printfnq("DONE.\n");
68 }
69
70 function updateSchemaPlugins()
71 {
72     printfnq("Upgrading plugin schema...");
73
74     Event::handle('CheckSchema');
75
76     printfnq("DONE.\n");
77 }
78
79 function fixupNoticeRendered()
80 {
81     printfnq("Ensuring all notices have rendered HTML...");
82
83     $notice = new Notice();
84
85     $notice->whereAdd('rendered IS NULL');
86     $notice->find();
87
88     while ($notice->fetch()) {
89         $original = clone($notice);
90         $notice->rendered = common_render_content($notice->content, $notice);
91         $notice->update($original);
92     }
93
94     printfnq("DONE.\n");
95 }
96
97 function fixupNoticeConversation()
98 {
99     printfnq("Ensuring all notices have a conversation ID...");
100
101     $notice = new Notice();
102     $notice->whereAdd('conversation is null');
103     $notice->orderBy('id'); // try to get originals before replies
104     $notice->find();
105
106     while ($notice->fetch()) {
107         try {
108             $cid = null;
109     
110             $orig = clone($notice);
111     
112             if (empty($notice->reply_to)) {
113                 $notice->conversation = $notice->id;
114             } else {
115                 $reply = Notice::staticGet('id', $notice->reply_to);
116
117                 if (empty($reply)) {
118                     $notice->conversation = $notice->id;
119                 } else if (empty($reply->conversation)) {
120                     $notice->conversation = $notice->id;
121                 } else {
122                     $notice->conversation = $reply->conversation;
123                 }
124         
125                 unset($reply);
126                 $reply = null;
127             }
128
129             $result = $notice->update($orig);
130
131             $orig = null;
132             unset($orig);
133         } catch (Exception $e) {
134             printv("Error setting conversation: " . $e->getMessage());
135         }
136     }
137
138     printfnq("DONE.\n");
139 }
140
141 function fixupGroupURI()
142 {
143     printfnq("Ensuring all groups have an URI...");
144
145     $group = new User_group();
146     $group->whereAdd('uri IS NULL');
147
148     if ($group->find()) {
149         while ($group->fetch()) {
150             $orig = User_group::staticGet('id', $group->id);
151             $group->uri = $group->getUri();
152             $group->update($orig);
153         }
154     }
155
156     printfnq("DONE.\n");
157 }
158
159 function initConversation()
160 {
161     printfnq("Ensuring all conversations have a row in conversation table...");
162
163     $notice = new Notice();
164     $notice->query('select distinct notice.conversation from notice '.
165                    'where notice.conversation is not null '.
166                    'and not exists (select conversation.id from conversation where id = notice.conversation)');
167
168     while ($notice->fetch()) {
169
170         $id = $notice->conversation;
171
172         $uri = common_local_url('conversation', array('id' => $id));
173
174         // @fixme db_dataobject won't save our value for an autoincrement
175         // so we're bypassing the insert wrappers
176         $conv = new Conversation();
177         $sql = "insert into conversation (id,uri,created) values(%d,'%s','%s')";
178         $sql = sprintf($sql,
179                        $id,
180                        $conv->escape($uri),
181                        $conv->escape(common_sql_now()));
182         $conv->query($sql);
183     }
184
185     printfnq("DONE.\n");
186 }
187
188 function initInbox()
189 {
190     printfnq("Ensuring all users have an inbox...");
191
192     $user = new User();
193     $user->whereAdd('not exists (select user_id from inbox where user_id = user.id)');
194     $user->orderBy('id');
195
196     if ($user->find()) {
197
198         while ($user->fetch()) {
199
200             try {
201                 $notice = new Notice();
202
203                 $notice->selectAdd();
204                 $notice->selectAdd('id');
205                 $notice->joinAdd(array('profile_id', 'subscription:subscribed'));
206                 $notice->whereAdd('subscription.subscriber = ' . $user->id);
207                 $notice->whereAdd('notice.created >= subscription.created');
208
209                 $ids = array();
210
211                 if ($notice->find()) {
212                     while ($notice->fetch()) {
213                         $ids[] = $notice->id;
214                     }
215                 }
216
217                 $notice = null;
218
219                 $inbox = new Inbox();
220                 $inbox->user_id = $user->id;
221                 $inbox->pack($ids);
222                 $inbox->insert();
223             } catch (Exception $e) {
224                 printv("Error initializing inbox: " . $e->getMessage());
225             }
226         }
227     }
228
229     printfnq("DONE.\n");
230 }
231
232 function initLocalGroup()
233 {
234     printfnq("Ensuring all local user groups have a local_group...");
235
236     $group = new User_group();
237     $group->whereAdd('NOT EXISTS (select group_id from local_group where group_id = user_group.id)');
238     $group->find();
239
240     while ($group->fetch()) {
241         try {
242             // Hack to check for local groups
243             if ($group->getUri() == common_local_url('groupbyid', array('id' => $group->id))) {
244                 $lg = new Local_group();
245
246                 $lg->group_id = $group->id;
247                 $lg->nickname = $group->nickname;
248                 $lg->created  = $group->created; // XXX: common_sql_now() ?
249                 $lg->modified = $group->modified;
250
251                 $lg->insert();
252             }
253         } catch (Exception $e) {
254             printfv("Error initializing local group for {$group->nickname}:" . $e->getMessage());
255         }
256     }
257
258     printfnq("DONE.\n");
259 }
260
261 main();