3 * @file include/post_update.php
6 use Friendica\Core\Config;
7 use Friendica\Database\DBM;
8 use Friendica\Model\Contact;
9 use Friendica\Model\GContact;
12 * @brief Calls the post update functions
14 function post_update() {
16 if (!post_update_1192()) {
19 if (!post_update_1194()) {
22 if (!post_update_1198()) {
25 if (!post_update_1206()) {
31 * @brief set the gcontact-id in all item entries
33 * This job has to be started multiple times until all entries are set.
34 * It isn't started in the update function since it would consume too much time and can be done in the background.
36 * @return bool "true" when the job is done
38 function post_update_1192() {
40 // Was the script completed?
41 if (Config::get("system", "post_update_version") >= 1192)
44 // Check if the first step is done (Setting "gcontact-id" in the item table)
45 $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
47 // Are there unfinished entries in the thread table?
48 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
49 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
50 WHERE `thread`.`gcontact-id` = 0 AND
51 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
53 if ($r && ($r[0]["total"] == 0)) {
54 Config::set("system", "post_update_version", 1192);
58 // Update the thread table from the item table
59 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
60 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
61 WHERE `thread`.`gcontact-id` = 0 AND
62 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
68 foreach ($r AS $item) {
69 $index = $item["author-link"]."-".$item["uid"];
70 $item_arr[$index] = ["author-link" => $item["author-link"],
71 "uid" => $item["uid"],
72 "network" => $item["network"]];
75 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
76 foreach ($item_arr AS $item) {
77 $gcontact_id = GContact::getId(["url" => $item['author-link'], "network" => $item['network'],
78 "photo" => $item['author-avatar'], "name" => $item['author-name']]);
79 q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
80 intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
86 * @brief Updates the "global" field in the item table
88 * @return bool "true" when the job is done
90 function post_update_1194() {
92 // Was the script completed?
93 if (Config::get("system", "post_update_version") >= 1194)
96 logger("Start", LOGGER_DEBUG);
98 $end_id = Config::get("system", "post_update_1194_end");
100 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
102 Config::set("system", "post_update_1194_end", $r[0]["id"]);
103 $end_id = Config::get("system", "post_update_1194_end");
107 logger("End ID: ".$end_id, LOGGER_DEBUG);
109 $start_id = Config::get("system", "post_update_1194_start");
111 $query1 = "SELECT `item`.`id` FROM `item` ";
113 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
115 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
116 AND `item`.`visible` AND NOT `item`.`private`
117 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
118 AND `item`.`network` IN ('%s', '%s', '%s', '')
119 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
120 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
121 AND NOT `item`.`global`";
123 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
124 intval($start_id), intval($end_id),
125 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
127 Config::set("system", "post_update_version", 1194);
128 logger("Update is done", LOGGER_DEBUG);
131 Config::set("system", "post_update_1194_start", $r[0]["id"]);
132 $start_id = Config::get("system", "post_update_1194_start");
135 logger("Start ID: ".$start_id, LOGGER_DEBUG);
137 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
138 intval($start_id), intval($end_id),
139 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
141 $pos_id = $r[0]["id"];
145 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
147 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
148 intval($start_id), intval($pos_id),
149 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
151 logger("Done", LOGGER_DEBUG);
155 * @brief set the author-id and owner-id in all item entries
157 * This job has to be started multiple times until all entries are set.
158 * It isn't started in the update function since it would consume too much time and can be done in the background.
160 * @return bool "true" when the job is done
162 function post_update_1198() {
164 // Was the script completed?
165 if (Config::get("system", "post_update_version") >= 1198)
168 logger("Start", LOGGER_DEBUG);
170 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
171 $r = q("SELECT `author-link`, `owner-link`, `uid` FROM `item` WHERE `author-id` = 0 AND `owner-id` = 0 LIMIT 100");
173 // Are there unfinished entries in the thread table?
174 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
175 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
176 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
177 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
179 if ($r && ($r[0]["total"] == 0)) {
180 Config::set("system", "post_update_version", 1198);
181 logger("Done", LOGGER_DEBUG);
185 // Update the thread table from the item table
186 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
187 SET `thread`.`author-id` = `item`.`author-id`,
188 `thread`.`owner-id` = `item`.`owner-id`
189 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
190 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
192 logger("Updated threads", LOGGER_DEBUG);
193 if (DBM::is_result($r)) {
194 Config::set("system", "post_update_version", 1198);
195 logger("Done", LOGGER_DEBUG);
201 logger("Query done", LOGGER_DEBUG);
204 foreach ($r AS $item) {
205 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
206 $item_arr[$index] = ["author-link" => $item["author-link"],
207 "owner-link" => $item["owner-link"],
208 "uid" => $item["uid"]];
211 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
212 foreach ($item_arr AS $item) {
213 $author_id = Contact::getIdForURL($item["author-link"], 0);
214 $owner_id = Contact::getIdForURL($item["owner-link"], 0);
222 q("UPDATE `item` SET `author-id` = %d, `owner-id` = %d
223 WHERE `uid` = %d AND `author-link` = '%s' AND `owner-link` = '%s'
224 AND `author-id` = 0 AND `owner-id` = 0",
225 intval($author_id), intval($owner_id), intval($item["uid"]),
226 dbesc($item["author-link"]), dbesc($item["owner-link"]));
229 logger("Updated items", LOGGER_DEBUG);
234 * @brief update the "last-item" field in the "self" contact
236 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
238 * @return bool "true" when the job is done
240 function post_update_1206() {
241 // Was the script completed?
242 if (Config::get("system", "post_update_version") >= 1206)
245 logger("Start", LOGGER_DEBUG);
246 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
247 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
249 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
251 if (!DBM::is_result($r)) {
254 foreach ($r AS $user) {
255 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
256 q("UPDATE `contact` SET `last-item` = '%s' WHERE `id` = %d",
257 dbesc($user["lastitem_date"]),
258 intval($user["id"]));
262 Config::set("system", "post_update_version", 1206);
263 logger("Done", LOGGER_DEBUG);