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 $fields = ['author-link', 'author-name', 'author-avatar', 'owner-link', 'owner-name', 'owner-avatar', 'network', 'uid'];
124 $r = dba::select('item', $fields, ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
126 // Are there unfinished entries in the thread table?
127 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
128 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
129 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
130 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
132 if ($r && ($r[0]["total"] == 0)) {
133 Config::set("system", "post_update_version", 1198);
134 logger("Done", LOGGER_DEBUG);
138 // Update the thread table from the item table
139 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
140 SET `thread`.`author-id` = `item`.`author-id`,
141 `thread`.`owner-id` = `item`.`owner-id`
142 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
143 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
145 logger("Updated threads", LOGGER_DEBUG);
146 if (DBM::is_result($r)) {
147 Config::set("system", "post_update_version", 1198);
148 logger("Done", LOGGER_DEBUG);
154 logger("Query done", LOGGER_DEBUG);
157 foreach ($r as $item) {
158 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
159 $item_arr[$index] = ["author-link" => $item["author-link"],
160 "owner-link" => $item["owner-link"],
161 "uid" => $item["uid"]];
164 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
165 foreach ($item_arr as $item) {
166 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
167 'photo' => $item['author-avatar'], 'network' => $item['network']];
168 $author_id = Contact::getIdForURL($item["author-link"], 0, false, $default);
170 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
171 'photo' => $item['owner-avatar'], 'network' => $item['network']];
172 $owner_id = Contact::getIdForURL($item["owner-link"], 0, false, $default);
174 if ($author_id == 0) {
177 if ($owner_id == 0) {
180 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]);
183 logger("Updated items", LOGGER_DEBUG);
188 * @brief update the "last-item" field in the "self" contact
190 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
192 * @return bool "true" when the job is done
194 private static function update1206()
196 // Was the script completed?
197 if (Config::get("system", "post_update_version") >= 1206) {
201 logger("Start", LOGGER_DEBUG);
202 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
203 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
205 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
207 if (!DBM::is_result($r)) {
210 foreach ($r as $user) {
211 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
212 dba::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
216 Config::set("system", "post_update_version", 1206);
217 logger("Done", LOGGER_DEBUG);