3 * @file include/post_update.php
6 use Friendica\Core\Config;
7 use Friendica\Database\DBM;
8 use Friendica\Model\GlobalContact;
11 * @brief Calls the post update functions
13 function post_update() {
15 if (!post_update_1192()) {
18 if (!post_update_1194()) {
21 if (!post_update_1198()) {
24 if (!post_update_1206()) {
30 * @brief set the gcontact-id in all item entries
32 * This job has to be started multiple times until all entries are set.
33 * It isn't started in the update function since it would consume too much time and can be done in the background.
35 * @return bool "true" when the job is done
37 function post_update_1192() {
39 // Was the script completed?
40 if (Config::get("system", "post_update_version") >= 1192)
43 // Check if the first step is done (Setting "gcontact-id" in the item table)
44 $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
46 // Are there unfinished entries in the thread table?
47 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
48 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
49 WHERE `thread`.`gcontact-id` = 0 AND
50 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
52 if ($r && ($r[0]["total"] == 0)) {
53 Config::set("system", "post_update_version", 1192);
57 // Update the thread table from the item table
58 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
59 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
60 WHERE `thread`.`gcontact-id` = 0 AND
61 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
67 foreach ($r AS $item) {
68 $index = $item["author-link"]."-".$item["uid"];
69 $item_arr[$index] = array("author-link" => $item["author-link"],
70 "uid" => $item["uid"],
71 "network" => $item["network"]);
74 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
75 foreach ($item_arr AS $item) {
76 $gcontact_id = GlobalContact::getId(array("url" => $item['author-link'], "network" => $item['network'],
77 "photo" => $item['author-avatar'], "name" => $item['author-name']));
78 q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
79 intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
85 * @brief Updates the "global" field in the item table
87 * @return bool "true" when the job is done
89 function post_update_1194() {
91 // Was the script completed?
92 if (Config::get("system", "post_update_version") >= 1194)
95 logger("Start", LOGGER_DEBUG);
97 $end_id = Config::get("system", "post_update_1194_end");
99 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
101 Config::set("system", "post_update_1194_end", $r[0]["id"]);
102 $end_id = Config::get("system", "post_update_1194_end");
106 logger("End ID: ".$end_id, LOGGER_DEBUG);
108 $start_id = Config::get("system", "post_update_1194_start");
110 $query1 = "SELECT `item`.`id` FROM `item` ";
112 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
114 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
115 AND `item`.`visible` AND NOT `item`.`private`
116 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
117 AND `item`.`network` IN ('%s', '%s', '%s', '')
118 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
119 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
120 AND NOT `item`.`global`";
122 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
123 intval($start_id), intval($end_id),
124 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
126 Config::set("system", "post_update_version", 1194);
127 logger("Update is done", LOGGER_DEBUG);
130 Config::set("system", "post_update_1194_start", $r[0]["id"]);
131 $start_id = Config::get("system", "post_update_1194_start");
134 logger("Start ID: ".$start_id, LOGGER_DEBUG);
136 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
137 intval($start_id), intval($end_id),
138 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
140 $pos_id = $r[0]["id"];
144 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
146 $r = q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
147 intval($start_id), intval($pos_id),
148 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
150 logger("Done", LOGGER_DEBUG);
154 * @brief set the author-id and owner-id in all item entries
156 * This job has to be started multiple times until all entries are set.
157 * It isn't started in the update function since it would consume too much time and can be done in the background.
159 * @return bool "true" when the job is done
161 function post_update_1198() {
163 // Was the script completed?
164 if (Config::get("system", "post_update_version") >= 1198)
167 logger("Start", LOGGER_DEBUG);
169 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
170 $r = q("SELECT `author-link`, `owner-link`, `uid` FROM `item` WHERE `author-id` = 0 AND `owner-id` = 0 LIMIT 100");
172 // Are there unfinished entries in the thread table?
173 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
174 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
175 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
176 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
178 if ($r && ($r[0]["total"] == 0)) {
179 Config::set("system", "post_update_version", 1198);
180 logger("Done", LOGGER_DEBUG);
184 // Update the thread table from the item table
185 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
186 SET `thread`.`author-id` = `item`.`author-id`,
187 `thread`.`owner-id` = `item`.`owner-id`
188 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
189 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
191 logger("Updated threads", LOGGER_DEBUG);
192 if (DBM::is_result($r)) {
193 Config::set("system", "post_update_version", 1198);
194 logger("Done", LOGGER_DEBUG);
200 logger("Query done", LOGGER_DEBUG);
203 foreach ($r AS $item) {
204 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
205 $item_arr[$index] = array("author-link" => $item["author-link"],
206 "owner-link" => $item["owner-link"],
207 "uid" => $item["uid"]);
210 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
211 foreach ($item_arr AS $item) {
212 $author_id = get_contact($item["author-link"], 0);
213 $owner_id = get_contact($item["owner-link"], 0);
221 q("UPDATE `item` SET `author-id` = %d, `owner-id` = %d
222 WHERE `uid` = %d AND `author-link` = '%s' AND `owner-link` = '%s'
223 AND `author-id` = 0 AND `owner-id` = 0",
224 intval($author_id), intval($owner_id), intval($item["uid"]),
225 dbesc($item["author-link"]), dbesc($item["owner-link"]));
228 logger("Updated items", LOGGER_DEBUG);
233 * @brief update the "last-item" field in the "self" contact
235 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
237 * @return bool "true" when the job is done
239 function post_update_1206() {
240 // Was the script completed?
241 if (Config::get("system", "post_update_version") >= 1206)
244 logger("Start", LOGGER_DEBUG);
245 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
246 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
248 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
250 if (!DBM::is_result($r)) {
253 foreach ($r AS $user) {
254 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
255 q("UPDATE `contact` SET `last-item` = '%s' WHERE `id` = %d",
256 dbesc($user["lastitem_date"]),
257 intval($user["id"]));
261 Config::set("system", "post_update_version", 1206);
262 logger("Done", LOGGER_DEBUG);