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