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