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