3 * @file include/post_update.php
6 use Friendica\Core\Config;
9 * @brief Calls the post update functions
11 function post_update() {
13 if (!post_update_1192()) {
16 if (!post_update_1194()) {
19 if (!post_update_1198()) {
22 if (!post_update_1206()) {
28 * @brief set the gcontact-id in all item entries
30 * This job has to be started multiple times until all entries are set.
31 * It isn't started in the update function since it would consume too much time and can be done in the background.
33 * @return bool "true" when the job is done
35 function post_update_1192() {
37 // Was the script completed?
38 if (Config::get("system", "post_update_version") >= 1192)
41 // Check if the first step is done (Setting "gcontact-id" in the item table)
42 $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
44 // Are there unfinished entries in the thread table?
45 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
46 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
47 WHERE `thread`.`gcontact-id` = 0 AND
48 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
50 if ($r && ($r[0]["total"] == 0)) {
51 Config::set("system", "post_update_version", 1192);
55 // Update the thread table from the item table
56 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
57 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
58 WHERE `thread`.`gcontact-id` = 0 AND
59 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
65 foreach ($r AS $item) {
66 $index = $item["author-link"]."-".$item["uid"];
67 $item_arr[$index] = array("author-link" => $item["author-link"],
68 "uid" => $item["uid"],
69 "network" => $item["network"]);
72 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
73 foreach ($item_arr AS $item) {
74 $gcontact_id = get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'],
75 "photo" => $item['author-avatar'], "name" => $item['author-name']));
76 q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
77 intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
83 * @brief Updates the "global" field in the item table
85 * @return bool "true" when the job is done
87 function post_update_1194() {
89 // Was the script completed?
90 if (Config::get("system", "post_update_version") >= 1194)
93 logger("Start", LOGGER_DEBUG);
95 $end_id = Config::get("system", "post_update_1194_end");
97 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
99 Config::set("system", "post_update_1194_end", $r[0]["id"]);
100 $end_id = Config::get("system", "post_update_1194_end");
104 logger("End ID: ".$end_id, LOGGER_DEBUG);
106 $start_id = Config::get("system", "post_update_1194_start");
108 $query1 = "SELECT `item`.`id` FROM `item` ";
110 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
112 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
113 AND `item`.`visible` AND NOT `item`.`private`
114 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
115 AND `item`.`network` IN ('%s', '%s', '%s', '')
116 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
117 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
118 AND NOT `item`.`global`";
120 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
121 intval($start_id), intval($end_id),
122 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
124 Config::set("system", "post_update_version", 1194);
125 logger("Update is done", LOGGER_DEBUG);
128 Config::set("system", "post_update_1194_start", $r[0]["id"]);
129 $start_id = Config::get("system", "post_update_1194_start");
132 logger("Start ID: ".$start_id, LOGGER_DEBUG);
134 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
135 intval($start_id), intval($end_id),
136 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
138 $pos_id = $r[0]["id"];
142 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
144 $r = q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
145 intval($start_id), intval($pos_id),
146 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
148 logger("Done", LOGGER_DEBUG);
152 * @brief set the author-id and owner-id in all item entries
154 * This job has to be started multiple times until all entries are set.
155 * It isn't started in the update function since it would consume too much time and can be done in the background.
157 * @return bool "true" when the job is done
159 function post_update_1198() {
161 // Was the script completed?
162 if (Config::get("system", "post_update_version") >= 1198)
165 logger("Start", LOGGER_DEBUG);
167 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
168 $r = q("SELECT `author-link`, `owner-link`, `uid` FROM `item` WHERE `author-id` = 0 AND `owner-id` = 0 LIMIT 100");
170 // Are there unfinished entries in the thread table?
171 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
172 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
173 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
174 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
176 if ($r && ($r[0]["total"] == 0)) {
177 Config::set("system", "post_update_version", 1198);
178 logger("Done", LOGGER_DEBUG);
182 // Update the thread table from the item table
183 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
184 SET `thread`.`author-id` = `item`.`author-id`,
185 `thread`.`owner-id` = `item`.`owner-id`
186 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
187 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
189 logger("Updated threads", LOGGER_DEBUG);
190 if (dbm::is_result($r)) {
191 Config::set("system", "post_update_version", 1198);
192 logger("Done", LOGGER_DEBUG);
198 logger("Query done", LOGGER_DEBUG);
201 foreach ($r AS $item) {
202 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
203 $item_arr[$index] = array("author-link" => $item["author-link"],
204 "owner-link" => $item["owner-link"],
205 "uid" => $item["uid"]);
208 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
209 foreach ($item_arr AS $item) {
210 $author_id = get_contact($item["author-link"], 0);
211 $owner_id = get_contact($item["owner-link"], 0);
219 q("UPDATE `item` SET `author-id` = %d, `owner-id` = %d
220 WHERE `uid` = %d AND `author-link` = '%s' AND `owner-link` = '%s'
221 AND `author-id` = 0 AND `owner-id` = 0",
222 intval($author_id), intval($owner_id), intval($item["uid"]),
223 dbesc($item["author-link"]), dbesc($item["owner-link"]));
226 logger("Updated items", LOGGER_DEBUG);
231 * @brief update the "last-item" field in the "self" contact
233 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
235 * @return bool "true" when the job is done
237 function post_update_1206() {
238 // Was the script completed?
239 if (Config::get("system", "post_update_version") >= 1206)
242 logger("Start", LOGGER_DEBUG);
243 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
244 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
246 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
248 if (!dbm::is_result($r)) {
251 foreach ($r AS $user) {
252 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
253 q("UPDATE `contact` SET `last-item` = '%s' WHERE `id` = %d",
254 dbesc($user["lastitem_date"]),
255 intval($user["id"]));
259 Config::set("system", "post_update_version", 1206);
260 logger("Done", LOGGER_DEBUG);