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())
22 * @brief set the gcontact-id in all item entries
24 * This job has to be started multiple times until all entries are set.
25 * It isn't started in the update function since it would consume too much time and can be done in the background.
27 * @return bool "true" when the job is done
29 function post_update_1192() {
31 // Was the script completed?
32 if (get_config("system", "post_update_version") >= 1192)
35 // Check if the first step is done (Setting "gcontact-id" in the item table)
36 $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
38 // Are there unfinished entries in the thread table?
39 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
40 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
41 WHERE `thread`.`gcontact-id` = 0 AND
42 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
44 if ($r AND ($r[0]["total"] == 0)) {
45 set_config("system", "post_update_version", 1192);
49 // Update the thread table from the item table
50 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
51 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
52 WHERE `thread`.`gcontact-id` = 0 AND
53 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
59 foreach ($r AS $item) {
60 $index = $item["author-link"]."-".$item["uid"];
61 $item_arr[$index] = array("author-link" => $item["author-link"],
62 "uid" => $item["uid"],
63 "network" => $item["network"]);
66 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
67 foreach($item_arr AS $item) {
68 $gcontact_id = get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'],
69 "photo" => $item['author-avatar'], "name" => $item['author-name']));
70 q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
71 intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
77 * @brief Updates the "global" field in the item table
79 * @return bool "true" when the job is done
81 function post_update_1194() {
83 // Was the script completed?
84 if (get_config("system", "post_update_version") >= 1194)
87 logger("Start", LOGGER_DEBUG);
89 $end_id = get_config("system", "post_update_1194_end");
91 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
93 set_config("system", "post_update_1194_end", $r[0]["id"]);
94 $end_id = get_config("system", "post_update_1194_end");
98 logger("End ID: ".$end_id, LOGGER_DEBUG);
100 $start_id = get_config("system", "post_update_1194_start");
102 $query1 = "SELECT `item`.`id` FROM `item` ";
104 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
106 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
107 AND `item`.`visible` AND NOT `item`.`private`
108 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
109 AND `item`.`network` IN ('%s', '%s', '%s', '')
110 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
111 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
112 AND NOT `item`.`global`";
114 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
115 intval($start_id), intval($end_id),
116 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
118 set_config("system", "post_update_version", 1194);
119 logger("Update is done", LOGGER_DEBUG);
122 set_config("system", "post_update_1194_start", $r[0]["id"]);
123 $start_id = get_config("system", "post_update_1194_start");
126 logger("Start ID: ".$start_id, LOGGER_DEBUG);
128 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
129 intval($start_id), intval($end_id),
130 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
132 $pos_id = $r[0]["id"];
136 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
138 $r = q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
139 intval($start_id), intval($pos_id),
140 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
142 logger("Done", LOGGER_DEBUG);
146 * @brief set the author-id and owner-id in all item entries
148 * This job has to be started multiple times until all entries are set.
149 * It isn't started in the update function since it would consume too much time and can be done in the background.
151 * @return bool "true" when the job is done
153 function post_update_1198() {
155 // Was the script completed?
156 if (get_config("system", "post_update_version") >= 1198)
159 logger("Start", LOGGER_DEBUG);
161 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
162 $r = q("SELECT `author-link`, `owner-link`, `uid` FROM `item` WHERE `author-id` = 0 AND `owner-id` = 0 LIMIT 1000");
164 // Are there unfinished entries in the thread table?
165 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
166 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
167 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
168 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
170 if ($r AND ($r[0]["total"] == 0)) {
171 set_config("system", "post_update_version", 1198);
172 logger("Done", LOGGER_DEBUG);
176 // Update the thread table from the item table
177 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
178 SET `thread`.`author-id` = `item`.`author-id`,
179 `thread`.`owner-id` = `item`.`owner-id`
180 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
181 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
183 logger("Updated threads", LOGGER_DEBUG);
187 logger("Query done", LOGGER_DEBUG);
190 foreach ($r AS $item) {
191 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
192 $item_arr[$index] = array("author-link" => $item["author-link"],
193 "owner-link" => $item["owner-link"],
194 "uid" => $item["uid"]);
197 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
198 foreach($item_arr AS $item) {
199 $author_id = get_contact($item["author-link"], 0);
200 $owner_id = get_contact($item["owner-link"], 0);
208 q("UPDATE `item` SET `author-id` = %d, `owner-id` = %d
209 WHERE `uid` = %d AND `author-link` = '%s' AND `owner-link` = '%s'
210 AND `author-id` = 0 AND `owner-id` = 0",
211 intval($author_id), intval($owner_id), intval($item["uid"]),
212 dbesc($item["author-link"]), dbesc($item["owner-link"]));
215 logger("Updated items", LOGGER_DEBUG);