]> git.mxchange.org Git - friendica.git/blob - include/post_update.php
210ab79fecbdf346cf1e223c579ebf7e17b50d04
[friendica.git] / include / post_update.php
1 <?php
2 /**
3  * @file include/post_update.php
4  */
5
6 use Friendica\Core\Config;
7
8 /**
9  * @brief Calls the post update functions
10  */
11 function post_update() {
12
13         if (!post_update_1192()) {
14                 return;
15         }
16         if (!post_update_1194()) {
17                 return;
18         }
19         if (!post_update_1198()) {
20                 return;
21         }
22         if (!post_update_1206()) {
23                 return;
24         }
25 }
26
27 /**
28  * @brief set the gcontact-id in all item entries
29  *
30  * This job has to be started multiple times until all entries are set.
31  * It isn't started in the update function since it would consume too much time and can be done in the background.
32  *
33  * @return bool "true" when the job is done
34  */
35 function post_update_1192() {
36
37         // Was the script completed?
38         if (Config::get("system", "post_update_version") >= 1192)
39                 return true;
40
41         // Check if the first step is done (Setting "gcontact-id" in the item table)
42         $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
43         if (!$r) {
44                 // Are there unfinished entries in the thread table?
45                 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
46                         INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
47                         WHERE `thread`.`gcontact-id` = 0 AND
48                                 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
49
50                 if ($r && ($r[0]["total"] == 0)) {
51                         Config::set("system", "post_update_version", 1192);
52                         return true;
53                 }
54
55                 // Update the thread table from the item table
56                 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
57                                 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
58                         WHERE `thread`.`gcontact-id` = 0 AND
59                                 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
60
61                 return false;
62         }
63
64         $item_arr = array();
65         foreach ($r AS $item) {
66                 $index = $item["author-link"]."-".$item["uid"];
67                 $item_arr[$index] = array("author-link" => $item["author-link"],
68                                                 "uid" => $item["uid"],
69                                                 "network" => $item["network"]);
70         }
71
72         // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
73         foreach ($item_arr AS $item) {
74                 $gcontact_id = get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'],
75                                                 "photo" => $item['author-avatar'], "name" => $item['author-name']));
76                 q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
77                         intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
78         }
79         return false;
80 }
81
82 /**
83  * @brief Updates the "global" field in the item table
84  *
85  * @return bool "true" when the job is done
86  */
87 function post_update_1194() {
88
89         // Was the script completed?
90         if (Config::get("system", "post_update_version") >= 1194)
91                 return true;
92
93         logger("Start", LOGGER_DEBUG);
94
95         $end_id = Config::get("system", "post_update_1194_end");
96         if (!$end_id) {
97                 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
98                 if ($r) {
99                         Config::set("system", "post_update_1194_end", $r[0]["id"]);
100                         $end_id = Config::get("system", "post_update_1194_end");
101                 }
102         }
103
104         logger("End ID: ".$end_id, LOGGER_DEBUG);
105
106         $start_id = Config::get("system", "post_update_1194_start");
107
108         $query1 = "SELECT `item`.`id` FROM `item` ";
109
110         $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
111
112         $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
113                         AND `item`.`visible` AND NOT `item`.`private`
114                         AND NOT `item`.`deleted` AND NOT `item`.`moderated`
115                         AND `item`.`network` IN ('%s', '%s', '%s', '')
116                         AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
117                         AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
118                         AND NOT `item`.`global`";
119
120         $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 1",
121                 intval($start_id), intval($end_id),
122                 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
123         if (!$r) {
124                 Config::set("system", "post_update_version", 1194);
125                 logger("Update is done", LOGGER_DEBUG);
126                 return true;
127         } else {
128                 Config::set("system", "post_update_1194_start", $r[0]["id"]);
129                 $start_id = Config::get("system", "post_update_1194_start");
130         }
131
132         logger("Start ID: ".$start_id, LOGGER_DEBUG);
133
134         $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 1000,1",
135                 intval($start_id), intval($end_id),
136                 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
137         if ($r)
138                 $pos_id = $r[0]["id"];
139         else
140                 $pos_id = $end_id;
141
142         logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
143
144         $r = q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
145                 intval($start_id), intval($pos_id),
146                 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
147
148         logger("Done", LOGGER_DEBUG);
149 }
150
151 /**
152  * @brief set the author-id and owner-id in all item entries
153  *
154  * This job has to be started multiple times until all entries are set.
155  * It isn't started in the update function since it would consume too much time and can be done in the background.
156  *
157  * @return bool "true" when the job is done
158  */
159 function post_update_1198() {
160
161         // Was the script completed?
162         if (Config::get("system", "post_update_version") >= 1198)
163                 return true;
164
165         logger("Start", LOGGER_DEBUG);
166
167         // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
168         $r = q("SELECT `author-link`, `owner-link`, `uid` FROM `item` WHERE `author-id` = 0 AND `owner-id` = 0 LIMIT 100");
169         if (!$r) {
170                 // Are there unfinished entries in the thread table?
171                 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
172                         INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
173                         WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
174                                 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
175
176                 if ($r && ($r[0]["total"] == 0)) {
177                         Config::set("system", "post_update_version", 1198);
178                         logger("Done", LOGGER_DEBUG);
179                         return true;
180                 }
181
182                 // Update the thread table from the item table
183                 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
184                                 SET `thread`.`author-id` = `item`.`author-id`,
185                                 `thread`.`owner-id` = `item`.`owner-id`
186                         WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
187                                 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
188
189                 logger("Updated threads", LOGGER_DEBUG);
190                 if (dbm::is_result($r)) {
191                         Config::set("system", "post_update_version", 1198);
192                         logger("Done", LOGGER_DEBUG);
193                         return true;
194                 }
195                 return false;
196         }
197
198         logger("Query done", LOGGER_DEBUG);
199
200         $item_arr = array();
201         foreach ($r AS $item) {
202                 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
203                 $item_arr[$index] = array("author-link" => $item["author-link"],
204                                                 "owner-link" => $item["owner-link"],
205                                                 "uid" => $item["uid"]);
206         }
207
208         // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
209         foreach ($item_arr AS $item) {
210                 $author_id = get_contact($item["author-link"], 0);
211                 $owner_id = get_contact($item["owner-link"], 0);
212
213                 if ($author_id == 0)
214                         $author_id = -1;
215
216                 if ($owner_id == 0)
217                         $owner_id = -1;
218
219                 q("UPDATE `item` SET `author-id` = %d, `owner-id` = %d
220                         WHERE `uid` = %d AND `author-link` = '%s' AND `owner-link` = '%s'
221                                 AND `author-id` = 0 AND `owner-id` = 0",
222                         intval($author_id), intval($owner_id), intval($item["uid"]),
223                         dbesc($item["author-link"]), dbesc($item["owner-link"]));
224         }
225
226         logger("Updated items", LOGGER_DEBUG);
227         return false;
228 }
229
230 /**
231  * @brief update the "last-item" field in the "self" contact
232  *
233  * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
234  *
235  * @return bool "true" when the job is done
236  */
237 function post_update_1206() {
238         // Was the script completed?
239         if (Config::get("system", "post_update_version") >= 1206)
240                 return true;
241
242         logger("Start", LOGGER_DEBUG);
243         $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
244                 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
245                 FROM `user`
246                 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
247
248         if (!dbm::is_result($r)) {
249                 return false;
250         }
251         foreach ($r AS $user) {
252                 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
253                         q("UPDATE `contact` SET `last-item` = '%s' WHERE `id` = %d",
254                                 dbesc($user["lastitem_date"]),
255                                 intval($user["id"]));
256                 }
257         }
258
259         Config::set("system", "post_update_version", 1206);
260         logger("Done", LOGGER_DEBUG);
261         return true;
262 }