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