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