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()) {
26 * @brief set the gcontact-id in all item entries
28 * This job has to be started multiple times until all entries are set.
29 * It isn't started in the update function since it would consume too much time and can be done in the background.
31 * @return bool "true" when the job is done
33 function post_update_1192() {
35 // Was the script completed?
36 if (get_config("system", "post_update_version") >= 1192)
39 // Check if the first step is done (Setting "gcontact-id" in the item table)
40 $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
42 // Are there unfinished entries in the thread table?
43 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
44 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
45 WHERE `thread`.`gcontact-id` = 0 AND
46 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
48 if ($r AND ($r[0]["total"] == 0)) {
49 set_config("system", "post_update_version", 1192);
53 // Update the thread table from the item table
54 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
55 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
56 WHERE `thread`.`gcontact-id` = 0 AND
57 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
63 foreach ($r AS $item) {
64 $index = $item["author-link"]."-".$item["uid"];
65 $item_arr[$index] = array("author-link" => $item["author-link"],
66 "uid" => $item["uid"],
67 "network" => $item["network"]);
70 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
71 foreach($item_arr AS $item) {
72 $gcontact_id = get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'],
73 "photo" => $item['author-avatar'], "name" => $item['author-name']));
74 q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
75 intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
81 * @brief Updates the "global" field in the item table
83 * @return bool "true" when the job is done
85 function post_update_1194() {
87 // Was the script completed?
88 if (get_config("system", "post_update_version") >= 1194)
91 logger("Start", LOGGER_DEBUG);
93 $end_id = get_config("system", "post_update_1194_end");
95 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
97 set_config("system", "post_update_1194_end", $r[0]["id"]);
98 $end_id = get_config("system", "post_update_1194_end");
102 logger("End ID: ".$end_id, LOGGER_DEBUG);
104 $start_id = get_config("system", "post_update_1194_start");
106 $query1 = "SELECT `item`.`id` FROM `item` ";
108 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
110 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
111 AND `item`.`visible` AND NOT `item`.`private`
112 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
113 AND `item`.`network` IN ('%s', '%s', '%s', '')
114 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
115 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
116 AND NOT `item`.`global`";
118 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
119 intval($start_id), intval($end_id),
120 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
122 set_config("system", "post_update_version", 1194);
123 logger("Update is done", LOGGER_DEBUG);
126 set_config("system", "post_update_1194_start", $r[0]["id"]);
127 $start_id = get_config("system", "post_update_1194_start");
130 logger("Start ID: ".$start_id, LOGGER_DEBUG);
132 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
133 intval($start_id), intval($end_id),
134 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
136 $pos_id = $r[0]["id"];
140 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
142 $r = q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
143 intval($start_id), intval($pos_id),
144 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
146 logger("Done", LOGGER_DEBUG);
150 * @brief set the author-id and owner-id in all item entries
152 * This job has to be started multiple times until all entries are set.
153 * It isn't started in the update function since it would consume too much time and can be done in the background.
155 * @return bool "true" when the job is done
157 function post_update_1198() {
159 // Was the script completed?
160 if (get_config("system", "post_update_version") >= 1198)
163 logger("Start", LOGGER_DEBUG);
165 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
166 $r = q("SELECT `author-link`, `owner-link`, `uid` FROM `item` WHERE `author-id` = 0 AND `owner-id` = 0 LIMIT 100");
168 // Are there unfinished entries in the thread table?
169 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
170 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
171 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
172 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
174 if ($r AND ($r[0]["total"] == 0)) {
175 set_config("system", "post_update_version", 1198);
176 logger("Done", LOGGER_DEBUG);
180 // Update the thread table from the item table
181 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
182 SET `thread`.`author-id` = `item`.`author-id`,
183 `thread`.`owner-id` = `item`.`owner-id`
184 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
185 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
187 logger("Updated threads", LOGGER_DEBUG);
188 if (dbm::is_result($r)) {
189 set_config("system", "post_update_version", 1198);
190 logger("Done", LOGGER_DEBUG);
196 logger("Query done", LOGGER_DEBUG);
199 foreach ($r AS $item) {
200 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
201 $item_arr[$index] = array("author-link" => $item["author-link"],
202 "owner-link" => $item["owner-link"],
203 "uid" => $item["uid"]);
206 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
207 foreach($item_arr AS $item) {
208 $author_id = get_contact($item["author-link"], 0);
209 $owner_id = get_contact($item["owner-link"], 0);
217 q("UPDATE `item` SET `author-id` = %d, `owner-id` = %d
218 WHERE `uid` = %d AND `author-link` = '%s' AND `owner-link` = '%s'
219 AND `author-id` = 0 AND `owner-id` = 0",
220 intval($author_id), intval($owner_id), intval($item["uid"]),
221 dbesc($item["author-link"]), dbesc($item["owner-link"]));
224 logger("Updated items", LOGGER_DEBUG);
229 * @brief update the "last-item" field in the "self" contact
231 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
233 * @return bool "true" when the job is done
235 function post_update_1206() {
236 // Was the script completed?
237 if (get_config("system", "post_update_version") >= 1206)
240 logger("Start", LOGGER_DEBUG);
241 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
242 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
244 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
246 if (!dbm::is_result($r)) {
249 foreach ($r AS $user) {
250 if (!empty($user["lastitem_date"]) AND ($user["lastitem_date"] > $user["last-item"])) {
251 q("UPDATE `contact` SET `last-item` = '%s' WHERE `id` = %d",
252 dbesc($user["lastitem_date"]),
253 intval($user["id"]));
257 set_config("system", "post_update_version", 1206);
258 logger("Done", LOGGER_DEBUG);