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