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;
12 require_once 'include/dba.php';
15 * Post update functions
20 * @brief Calls the post update functions
22 public static function update()
24 if (!self::update1194()) {
27 if (!self::update1198()) {
30 if (!self::update1206()) {
36 * @brief Updates the "global" field in the item table
38 * @return bool "true" when the job is done
40 private static function update1194()
42 // Was the script completed?
43 if (Config::get("system", "post_update_version") >= 1194) {
47 logger("Start", LOGGER_DEBUG);
49 $end_id = Config::get("system", "post_update_1194_end");
51 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
53 Config::set("system", "post_update_1194_end", $r[0]["id"]);
54 $end_id = Config::get("system", "post_update_1194_end");
58 logger("End ID: ".$end_id, LOGGER_DEBUG);
60 $start_id = Config::get("system", "post_update_1194_start");
62 $query1 = "SELECT `item`.`id` FROM `item` ";
64 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
66 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
67 AND `item`.`visible` AND NOT `item`.`private`
68 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
69 AND `item`.`network` IN ('%s', '%s', '%s', '')
70 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
71 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
72 AND NOT `item`.`global`";
74 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
75 intval($start_id), intval($end_id),
76 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
78 Config::set("system", "post_update_version", 1194);
79 logger("Update is done", LOGGER_DEBUG);
82 Config::set("system", "post_update_1194_start", $r[0]["id"]);
83 $start_id = Config::get("system", "post_update_1194_start");
86 logger("Start ID: ".$start_id, LOGGER_DEBUG);
88 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
89 intval($start_id), intval($end_id),
90 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
92 $pos_id = $r[0]["id"];
96 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
98 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
99 intval($start_id), intval($pos_id),
100 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
102 logger("Done", LOGGER_DEBUG);
106 * @brief set the author-id and owner-id in all item entries
108 * This job has to be started multiple times until all entries are set.
109 * It isn't started in the update function since it would consume too much time and can be done in the background.
111 * @return bool "true" when the job is done
113 private static function update1198()
115 // Was the script completed?
116 if (Config::get("system", "post_update_version") >= 1198) {
120 logger("Start", LOGGER_DEBUG);
122 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
123 $r = dba::select('item', ['author-link', 'owner-link', 'uid'], ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
125 // Are there unfinished entries in the thread table?
126 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
127 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
128 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
129 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
131 if ($r && ($r[0]["total"] == 0)) {
132 Config::set("system", "post_update_version", 1198);
133 logger("Done", LOGGER_DEBUG);
137 // Update the thread table from the item table
138 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
139 SET `thread`.`author-id` = `item`.`author-id`,
140 `thread`.`owner-id` = `item`.`owner-id`
141 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
142 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
144 logger("Updated threads", LOGGER_DEBUG);
145 if (DBM::is_result($r)) {
146 Config::set("system", "post_update_version", 1198);
147 logger("Done", LOGGER_DEBUG);
153 logger("Query done", LOGGER_DEBUG);
156 foreach ($r as $item) {
157 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
158 $item_arr[$index] = ["author-link" => $item["author-link"],
159 "owner-link" => $item["owner-link"],
160 "uid" => $item["uid"]];
163 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
164 foreach ($item_arr as $item) {
165 $author_id = Contact::getIdForURL($item["author-link"]);
166 $owner_id = Contact::getIdForURL($item["owner-link"]);
174 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]);
177 logger("Updated items", LOGGER_DEBUG);
182 * @brief update the "last-item" field in the "self" contact
184 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
186 * @return bool "true" when the job is done
188 private static function update1206()
190 // Was the script completed?
191 if (Config::get("system", "post_update_version") >= 1206) {
195 logger("Start", LOGGER_DEBUG);
196 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
197 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
199 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
201 if (!DBM::is_result($r)) {
204 foreach ($r as $user) {
205 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
206 dba::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
210 Config::set("system", "post_update_version", 1206);
211 logger("Done", LOGGER_DEBUG);