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