3 * @file src/Database/PostUpdate.php
5 namespace Friendica\Database;
7 use Friendica\Core\Config;
8 use Friendica\Database\DBM;
9 use Friendica\Model\Contact;
10 use Friendica\Model\GContact;
13 require_once 'include/dba.php';
16 * Post update functions
21 * @brief Calls the post update functions
23 public static function update()
25 if (!self::update1192()) {
28 if (!self::update1194()) {
31 if (!self::update1198()) {
34 if (!self::update1206()) {
40 * @brief set the gcontact-id in all item entries
42 * This job has to be started multiple times until all entries are set.
43 * It isn't started in the update function since it would consume too much time and can be done in the background.
45 * @return bool "true" when the job is done
47 private static function update1192()
49 // Was the script completed?
50 if (Config::get("system", "post_update_version") >= 1192) {
54 // Check if the first step is done (Setting "gcontact-id" in the item table)
55 $r = dba::select('item', ['author-link', 'author-name', 'author-avatar', 'uid', 'network'], ['gcontact-id' => 0], ['limit' => 1000]);
57 // Are there unfinished entries in the thread table?
58 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
59 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
60 WHERE `thread`.`gcontact-id` = 0 AND
61 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
63 if ($r && ($r[0]["total"] == 0)) {
64 Config::set("system", "post_update_version", 1192);
68 // Update the thread table from the item table
69 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
70 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
71 WHERE `thread`.`gcontact-id` = 0 AND
72 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
78 foreach ($r as $item) {
79 $index = $item["author-link"]."-".$item["uid"];
80 $item_arr[$index] = ["author-link" => $item["author-link"],
81 "uid" => $item["uid"],
82 "network" => $item["network"]];
85 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
86 foreach ($item_arr as $item) {
87 $gcontact_id = GContact::getId(["url" => $item['author-link'], "network" => $item['network'],
88 "photo" => $item['author-avatar'], "name" => $item['author-name']]);
89 dba::update('item', ['gcontact-id' => $gcontact_id], ['uid' => $item['uid'], 'author-link' => $item['author-link'], 'gcontact-id' => 0]);
95 * @brief Updates the "global" field in the item table
97 * @return bool "true" when the job is done
99 private static function update1194()
101 // Was the script completed?
102 if (Config::get("system", "post_update_version") >= 1194) {
106 logger("Start", LOGGER_DEBUG);
108 $end_id = Config::get("system", "post_update_1194_end");
110 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
112 Config::set("system", "post_update_1194_end", $r[0]["id"]);
113 $end_id = Config::get("system", "post_update_1194_end");
117 logger("End ID: ".$end_id, LOGGER_DEBUG);
119 $start_id = Config::get("system", "post_update_1194_start");
121 $query1 = "SELECT `item`.`id` FROM `item` ";
123 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
125 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
126 AND `item`.`visible` AND NOT `item`.`private`
127 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
128 AND `item`.`network` IN ('%s', '%s', '%s', '')
129 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
130 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
131 AND NOT `item`.`global`";
133 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
134 intval($start_id), intval($end_id),
135 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
137 Config::set("system", "post_update_version", 1194);
138 logger("Update is done", LOGGER_DEBUG);
141 Config::set("system", "post_update_1194_start", $r[0]["id"]);
142 $start_id = Config::get("system", "post_update_1194_start");
145 logger("Start ID: ".$start_id, LOGGER_DEBUG);
147 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
148 intval($start_id), intval($end_id),
149 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
151 $pos_id = $r[0]["id"];
155 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
157 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
158 intval($start_id), intval($pos_id),
159 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
161 logger("Done", LOGGER_DEBUG);
165 * @brief set the author-id and owner-id in all item entries
167 * This job has to be started multiple times until all entries are set.
168 * It isn't started in the update function since it would consume too much time and can be done in the background.
170 * @return bool "true" when the job is done
172 private static function update1198()
174 // Was the script completed?
175 if (Config::get("system", "post_update_version") >= 1198) {
179 logger("Start", LOGGER_DEBUG);
181 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
182 $r = dba::select('item', ['author-link', 'owner-link', 'uid'], ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
184 // Are there unfinished entries in the thread table?
185 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
186 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
187 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
188 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
190 if ($r && ($r[0]["total"] == 0)) {
191 Config::set("system", "post_update_version", 1198);
192 logger("Done", LOGGER_DEBUG);
196 // Update the thread table from the item table
197 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
198 SET `thread`.`author-id` = `item`.`author-id`,
199 `thread`.`owner-id` = `item`.`owner-id`
200 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
201 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
203 logger("Updated threads", LOGGER_DEBUG);
204 if (DBM::is_result($r)) {
205 Config::set("system", "post_update_version", 1198);
206 logger("Done", LOGGER_DEBUG);
212 logger("Query done", LOGGER_DEBUG);
215 foreach ($r as $item) {
216 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
217 $item_arr[$index] = ["author-link" => $item["author-link"],
218 "owner-link" => $item["owner-link"],
219 "uid" => $item["uid"]];
222 // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
223 foreach ($item_arr as $item) {
224 $author_id = Contact::getIdForURL($item["author-link"], 0);
225 $owner_id = Contact::getIdForURL($item["owner-link"], 0);
233 dba::update('item', ['author-id' => $author_id, 'owner-id' => $owner_id], ['uid' => $item['uid'], 'author-link' => $item['author-link'], 'owner-link' => $item['owner-link'], 'author-id' => 0, 'owner-id' => 0]);
236 logger("Updated items", LOGGER_DEBUG);
241 * @brief update the "last-item" field in the "self" contact
243 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
245 * @return bool "true" when the job is done
247 private static function update1206()
249 // Was the script completed?
250 if (Config::get("system", "post_update_version") >= 1206) {
254 logger("Start", LOGGER_DEBUG);
255 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
256 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
258 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
260 if (!DBM::is_result($r)) {
263 foreach ($r as $user) {
264 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
265 dba::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
269 Config::set("system", "post_update_version", 1206);
270 logger("Done", LOGGER_DEBUG);