3 * @file include/post_update.php
6 use Friendica\Core\Config;
7 use Friendica\Database\DBM;
10 * @brief Calls the post update functions
12 function post_update() {
14 if (!post_update_1192()) {
17 if (!post_update_1194()) {
20 if (!post_update_1198()) {
23 if (!post_update_1206()) {
29 * @brief set the gcontact-id in all item entries
31 * This job has to be started multiple times until all entries are set.
32 * It isn't started in the update function since it would consume too much time and can be done in the background.
34 * @return bool "true" when the job is done
36 function post_update_1192() {
38 // Was the script completed?
39 if (Config::get("system", "post_update_version") >= 1192)
42 // Check if the first step is done (Setting "gcontact-id" in the item table)
43 $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
45 // Are there unfinished entries in the thread table?
46 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
47 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
48 WHERE `thread`.`gcontact-id` = 0 AND
49 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
51 if ($r && ($r[0]["total"] == 0)) {
52 Config::set("system", "post_update_version", 1192);
56 // Update the thread table from the item table
57 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
58 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
59 WHERE `thread`.`gcontact-id` = 0 AND
60 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
66 foreach ($r AS $item) {
67 $index = $item["author-link"]."-".$item["uid"];
68 $item_arr[$index] = array("author-link" => $item["author-link"],
69 "uid" => $item["uid"],
70 "network" => $item["network"]);
73 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
74 foreach ($item_arr AS $item) {
75 $gcontact_id = get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'],
76 "photo" => $item['author-avatar'], "name" => $item['author-name']));
77 q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
78 intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
84 * @brief Updates the "global" field in the item table
86 * @return bool "true" when the job is done
88 function post_update_1194() {
90 // Was the script completed?
91 if (Config::get("system", "post_update_version") >= 1194)
94 logger("Start", LOGGER_DEBUG);
96 $end_id = Config::get("system", "post_update_1194_end");
98 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
100 Config::set("system", "post_update_1194_end", $r[0]["id"]);
101 $end_id = Config::get("system", "post_update_1194_end");
105 logger("End ID: ".$end_id, LOGGER_DEBUG);
107 $start_id = Config::get("system", "post_update_1194_start");
109 $query1 = "SELECT `item`.`id` FROM `item` ";
111 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
113 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
114 AND `item`.`visible` AND NOT `item`.`private`
115 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
116 AND `item`.`network` IN ('%s', '%s', '%s', '')
117 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
118 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
119 AND NOT `item`.`global`";
121 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
122 intval($start_id), intval($end_id),
123 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
125 Config::set("system", "post_update_version", 1194);
126 logger("Update is done", LOGGER_DEBUG);
129 Config::set("system", "post_update_1194_start", $r[0]["id"]);
130 $start_id = Config::get("system", "post_update_1194_start");
133 logger("Start ID: ".$start_id, LOGGER_DEBUG);
135 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
136 intval($start_id), intval($end_id),
137 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
139 $pos_id = $r[0]["id"];
143 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
145 $r = q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
146 intval($start_id), intval($pos_id),
147 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
149 logger("Done", LOGGER_DEBUG);
153 * @brief set the author-id and owner-id in all item entries
155 * This job has to be started multiple times until all entries are set.
156 * It isn't started in the update function since it would consume too much time and can be done in the background.
158 * @return bool "true" when the job is done
160 function post_update_1198() {
162 // Was the script completed?
163 if (Config::get("system", "post_update_version") >= 1198)
166 logger("Start", LOGGER_DEBUG);
168 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
169 $r = q("SELECT `author-link`, `owner-link`, `uid` FROM `item` WHERE `author-id` = 0 AND `owner-id` = 0 LIMIT 100");
171 // Are there unfinished entries in the thread table?
172 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
173 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
174 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
175 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
177 if ($r && ($r[0]["total"] == 0)) {
178 Config::set("system", "post_update_version", 1198);
179 logger("Done", LOGGER_DEBUG);
183 // Update the thread table from the item table
184 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
185 SET `thread`.`author-id` = `item`.`author-id`,
186 `thread`.`owner-id` = `item`.`owner-id`
187 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
188 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
190 logger("Updated threads", LOGGER_DEBUG);
191 if (DBM::is_result($r)) {
192 Config::set("system", "post_update_version", 1198);
193 logger("Done", LOGGER_DEBUG);
199 logger("Query done", LOGGER_DEBUG);
202 foreach ($r AS $item) {
203 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
204 $item_arr[$index] = array("author-link" => $item["author-link"],
205 "owner-link" => $item["owner-link"],
206 "uid" => $item["uid"]);
209 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
210 foreach ($item_arr AS $item) {
211 $author_id = get_contact($item["author-link"], 0);
212 $owner_id = get_contact($item["owner-link"], 0);
220 q("UPDATE `item` SET `author-id` = %d, `owner-id` = %d
221 WHERE `uid` = %d AND `author-link` = '%s' AND `owner-link` = '%s'
222 AND `author-id` = 0 AND `owner-id` = 0",
223 intval($author_id), intval($owner_id), intval($item["uid"]),
224 dbesc($item["author-link"]), dbesc($item["owner-link"]));
227 logger("Updated items", LOGGER_DEBUG);
232 * @brief update the "last-item" field in the "self" contact
234 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
236 * @return bool "true" when the job is done
238 function post_update_1206() {
239 // Was the script completed?
240 if (Config::get("system", "post_update_version") >= 1206)
243 logger("Start", LOGGER_DEBUG);
244 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
245 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
247 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
249 if (!DBM::is_result($r)) {
252 foreach ($r AS $user) {
253 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
254 q("UPDATE `contact` SET `last-item` = '%s' WHERE `id` = %d",
255 dbesc($user["lastitem_date"]),
256 intval($user["id"]));
260 Config::set("system", "post_update_version", 1206);
261 logger("Done", LOGGER_DEBUG);