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