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\Item;
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::update1194()) {
28 if (!self::update1198()) {
31 if (!self::update1206()) {
34 if (!self::update1274()) {
40 * @brief Updates the "global" field in the item table
42 * @return bool "true" when the job is done
44 private static function update1194()
46 // Was the script completed?
47 if (Config::get("system", "post_update_version") >= 1194) {
51 logger("Start", LOGGER_DEBUG);
53 $end_id = Config::get("system", "post_update_1194_end");
55 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
57 Config::set("system", "post_update_1194_end", $r[0]["id"]);
58 $end_id = Config::get("system", "post_update_1194_end");
62 logger("End ID: ".$end_id, LOGGER_DEBUG);
64 $start_id = Config::get("system", "post_update_1194_start");
66 $query1 = "SELECT `item`.`id` FROM `item` ";
68 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
70 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
71 AND `item`.`visible` AND NOT `item`.`private`
72 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
73 AND `item`.`network` IN ('%s', '%s', '%s', '')
74 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
75 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
76 AND NOT `item`.`global`";
78 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
79 intval($start_id), intval($end_id),
80 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
82 Config::set("system", "post_update_version", 1194);
83 logger("Update is done", LOGGER_DEBUG);
86 Config::set("system", "post_update_1194_start", $r[0]["id"]);
87 $start_id = Config::get("system", "post_update_1194_start");
90 logger("Start ID: ".$start_id, LOGGER_DEBUG);
92 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
93 intval($start_id), intval($end_id),
94 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
96 $pos_id = $r[0]["id"];
100 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
102 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
103 intval($start_id), intval($pos_id),
104 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
106 logger("Done", LOGGER_DEBUG);
110 * @brief set the author-id and owner-id in all item entries
112 * This job has to be started multiple times until all entries are set.
113 * It isn't started in the update function since it would consume too much time and can be done in the background.
115 * @return bool "true" when the job is done
117 private static function update1198()
119 // Was the script completed?
120 if (Config::get("system", "post_update_version") >= 1198) {
124 logger("Start", LOGGER_DEBUG);
126 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
127 $fields = ['author-link', 'author-name', 'author-avatar', 'owner-link', 'owner-name', 'owner-avatar', 'network', 'uid'];
128 $r = dba::select('item', $fields, ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
130 // Are there unfinished entries in the thread table?
131 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
132 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
133 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
134 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
136 if ($r && ($r[0]["total"] == 0)) {
137 Config::set("system", "post_update_version", 1198);
138 logger("Done", LOGGER_DEBUG);
142 // Update the thread table from the item table
143 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
144 SET `thread`.`author-id` = `item`.`author-id`,
145 `thread`.`owner-id` = `item`.`owner-id`
146 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
147 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
149 logger("Updated threads", LOGGER_DEBUG);
150 if (DBM::is_result($r)) {
151 Config::set("system", "post_update_version", 1198);
152 logger("Done", LOGGER_DEBUG);
158 logger("Query done", LOGGER_DEBUG);
161 foreach ($r as $item) {
162 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
163 $item_arr[$index] = ["author-link" => $item["author-link"],
164 "owner-link" => $item["owner-link"],
165 "uid" => $item["uid"]];
168 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
169 foreach ($item_arr as $item) {
170 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
171 'photo' => $item['author-avatar'], 'network' => $item['network']];
172 $author_id = Contact::getIdForURL($item["author-link"], 0, false, $default);
174 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
175 'photo' => $item['owner-avatar'], 'network' => $item['network']];
176 $owner_id = Contact::getIdForURL($item["owner-link"], 0, false, $default);
178 if ($author_id == 0) {
181 if ($owner_id == 0) {
184 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]);
187 logger("Updated items", LOGGER_DEBUG);
192 * @brief update the "last-item" field in the "self" contact
194 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
196 * @return bool "true" when the job is done
198 private static function update1206()
200 // Was the script completed?
201 if (Config::get("system", "post_update_version") >= 1206) {
205 logger("Start", LOGGER_DEBUG);
206 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
207 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
209 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
211 if (!DBM::is_result($r)) {
214 foreach ($r as $user) {
215 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
216 dba::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
220 Config::set("system", "post_update_version", 1206);
221 logger("Done", LOGGER_DEBUG);
226 * @brief update the "item-content" table
228 * @return bool "true" when the job is done
230 private static function update1274()
232 // Was the script completed?
233 if (Config::get("system", "post_update_version") >= 1274) {
237 logger("Start", LOGGER_DEBUG);
239 $fields = ['id', 'title', 'content-warning', 'body', 'location', 'tag', 'file',
240 'coord', 'app', 'rendered-hash', 'rendered-html', 'verb',
241 'object-type', 'object', 'target-type', 'target', 'plink'];
243 $condition = ["`icid` IS NULL"];
244 $params = ['limit' => 10000];
245 $items = Item::select($fields, $condition, $params);
247 if (!DBM::is_result($items)) {
248 Config::set("system", "post_update_version", 1274);
249 logger("Done", LOGGER_DEBUG);
255 while ($item = Item::fetch($items)) {
256 Item::update($item, ['id' => $item['id']]);
261 logger("Processed rows: " . $rows, LOGGER_DEBUG);