3 * @file include/post_update.php
7 * @brief Calls the post update functions
9 function post_update() {
11 if (!post_update_1192())
14 if (!post_update_1194())
17 if (!post_update_1198())
20 if (!post_update_1206())
25 * @brief set the gcontact-id in all item entries
27 * This job has to be started multiple times until all entries are set.
28 * It isn't started in the update function since it would consume too much time and can be done in the background.
30 * @return bool "true" when the job is done
32 function post_update_1192() {
34 // Was the script completed?
35 if (get_config("system", "post_update_version") >= 1192)
38 // Check if the first step is done (Setting "gcontact-id" in the item table)
39 $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
41 // Are there unfinished entries in the thread table?
42 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
43 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
44 WHERE `thread`.`gcontact-id` = 0 AND
45 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
47 if ($r AND ($r[0]["total"] == 0)) {
48 set_config("system", "post_update_version", 1192);
52 // Update the thread table from the item table
53 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
54 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
55 WHERE `thread`.`gcontact-id` = 0 AND
56 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
62 foreach ($r AS $item) {
63 $index = $item["author-link"]."-".$item["uid"];
64 $item_arr[$index] = array("author-link" => $item["author-link"],
65 "uid" => $item["uid"],
66 "network" => $item["network"]);
69 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
70 foreach($item_arr AS $item) {
71 $gcontact_id = get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'],
72 "photo" => $item['author-avatar'], "name" => $item['author-name']));
73 q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
74 intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
80 * @brief Updates the "global" field in the item table
82 * @return bool "true" when the job is done
84 function post_update_1194() {
86 // Was the script completed?
87 if (get_config("system", "post_update_version") >= 1194)
90 logger("Start", LOGGER_DEBUG);
92 $end_id = get_config("system", "post_update_1194_end");
94 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
96 set_config("system", "post_update_1194_end", $r[0]["id"]);
97 $end_id = get_config("system", "post_update_1194_end");
101 logger("End ID: ".$end_id, LOGGER_DEBUG);
103 $start_id = get_config("system", "post_update_1194_start");
105 $query1 = "SELECT `item`.`id` FROM `item` ";
107 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
109 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
110 AND `item`.`visible` AND NOT `item`.`private`
111 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
112 AND `item`.`network` IN ('%s', '%s', '%s', '')
113 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
114 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
115 AND NOT `item`.`global`";
117 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
118 intval($start_id), intval($end_id),
119 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
121 set_config("system", "post_update_version", 1194);
122 logger("Update is done", LOGGER_DEBUG);
125 set_config("system", "post_update_1194_start", $r[0]["id"]);
126 $start_id = get_config("system", "post_update_1194_start");
129 logger("Start ID: ".$start_id, LOGGER_DEBUG);
131 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
132 intval($start_id), intval($end_id),
133 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
135 $pos_id = $r[0]["id"];
139 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
141 $r = q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
142 intval($start_id), intval($pos_id),
143 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
145 logger("Done", LOGGER_DEBUG);
149 * @brief set the author-id and owner-id in all item entries
151 * This job has to be started multiple times until all entries are set.
152 * It isn't started in the update function since it would consume too much time and can be done in the background.
154 * @return bool "true" when the job is done
156 function post_update_1198() {
158 // Was the script completed?
159 if (get_config("system", "post_update_version") >= 1198)
162 logger("Start", LOGGER_DEBUG);
164 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
165 $r = q("SELECT `author-link`, `owner-link`, `uid` FROM `item` WHERE `author-id` = 0 AND `owner-id` = 0 LIMIT 100");
167 // Are there unfinished entries in the thread table?
168 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
169 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
170 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
171 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
173 if ($r AND ($r[0]["total"] == 0)) {
174 set_config("system", "post_update_version", 1198);
175 logger("Done", LOGGER_DEBUG);
179 // Update the thread table from the item table
180 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
181 SET `thread`.`author-id` = `item`.`author-id`,
182 `thread`.`owner-id` = `item`.`owner-id`
183 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
184 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
186 logger("Updated threads", LOGGER_DEBUG);
187 if (dbm::is_result($r)) {
188 set_config("system", "post_update_version", 1198);
189 logger("Done", LOGGER_DEBUG);
195 logger("Query done", LOGGER_DEBUG);
198 foreach ($r AS $item) {
199 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
200 $item_arr[$index] = array("author-link" => $item["author-link"],
201 "owner-link" => $item["owner-link"],
202 "uid" => $item["uid"]);
205 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
206 foreach($item_arr AS $item) {
207 $author_id = get_contact($item["author-link"], 0);
208 $owner_id = get_contact($item["owner-link"], 0);
216 q("UPDATE `item` SET `author-id` = %d, `owner-id` = %d
217 WHERE `uid` = %d AND `author-link` = '%s' AND `owner-link` = '%s'
218 AND `author-id` = 0 AND `owner-id` = 0",
219 intval($author_id), intval($owner_id), intval($item["uid"]),
220 dbesc($item["author-link"]), dbesc($item["owner-link"]));
223 logger("Updated items", LOGGER_DEBUG);
228 * @brief update the "last-item" field in the "self" contact
230 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
232 * @return bool "true" when the job is done
234 function post_update_1206() {
235 // Was the script completed?
236 if (get_config("system", "post_update_version") >= 1206)
239 logger("Start", LOGGER_DEBUG);
240 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
241 (SELECT MAX(`changed`) FROM `item` FORCE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
243 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
245 if (!dbm::is_result($r))
248 foreach ($r AS $user) {
249 if (!empty($user["lastitem_date"]) AND ($user["lastitem_date"] > $user["last-item"]))
250 q("UPDATE `contact` SET `last-item` = '%s' WHERE `id` = %d",
251 dbesc($user["lastitem_date"]),
252 intval($user["id"]));
255 set_config("system", "post_update_version", 1206);
256 logger("Done", LOGGER_DEBUG);