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()) {
37 if (!self::update1275()) {
43 * @brief Updates the "global" field in the item table
45 * @return bool "true" when the job is done
47 private static function update1194()
49 // Was the script completed?
50 if (Config::get("system", "post_update_version") >= 1194) {
54 logger("Start", LOGGER_DEBUG);
56 $end_id = Config::get("system", "post_update_1194_end");
58 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
60 Config::set("system", "post_update_1194_end", $r[0]["id"]);
61 $end_id = Config::get("system", "post_update_1194_end");
65 logger("End ID: ".$end_id, LOGGER_DEBUG);
67 $start_id = Config::get("system", "post_update_1194_start");
69 $query1 = "SELECT `item`.`id` FROM `item` ";
71 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
73 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
74 AND `item`.`visible` AND NOT `item`.`private`
75 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
76 AND `item`.`network` IN ('%s', '%s', '%s', '')
77 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
78 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
79 AND NOT `item`.`global`";
81 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
82 intval($start_id), intval($end_id),
83 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
85 Config::set("system", "post_update_version", 1194);
86 logger("Update is done", LOGGER_DEBUG);
89 Config::set("system", "post_update_1194_start", $r[0]["id"]);
90 $start_id = Config::get("system", "post_update_1194_start");
93 logger("Start ID: ".$start_id, LOGGER_DEBUG);
95 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
96 intval($start_id), intval($end_id),
97 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
99 $pos_id = $r[0]["id"];
103 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
105 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
106 intval($start_id), intval($pos_id),
107 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
109 logger("Done", LOGGER_DEBUG);
113 * @brief set the author-id and owner-id in all item entries
115 * This job has to be started multiple times until all entries are set.
116 * It isn't started in the update function since it would consume too much time and can be done in the background.
118 * @return bool "true" when the job is done
120 private static function update1198()
122 // Was the script completed?
123 if (Config::get("system", "post_update_version") >= 1198) {
127 logger("Start", LOGGER_DEBUG);
129 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
130 $fields = ['author-link', 'author-name', 'author-avatar', 'owner-link', 'owner-name', 'owner-avatar', 'network', 'uid'];
131 $r = dba::select('item', $fields, ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
133 // Are there unfinished entries in the thread table?
134 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
135 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
136 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
137 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
139 if ($r && ($r[0]["total"] == 0)) {
140 Config::set("system", "post_update_version", 1198);
141 logger("Done", LOGGER_DEBUG);
145 // Update the thread table from the item table
146 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
147 SET `thread`.`author-id` = `item`.`author-id`,
148 `thread`.`owner-id` = `item`.`owner-id`
149 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
150 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
152 logger("Updated threads", LOGGER_DEBUG);
153 if (DBM::is_result($r)) {
154 Config::set("system", "post_update_version", 1198);
155 logger("Done", LOGGER_DEBUG);
161 logger("Query done", LOGGER_DEBUG);
164 foreach ($r as $item) {
165 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
166 $item_arr[$index] = ["author-link" => $item["author-link"],
167 "owner-link" => $item["owner-link"],
168 "uid" => $item["uid"]];
171 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
172 foreach ($item_arr as $item) {
173 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
174 'photo' => $item['author-avatar'], 'network' => $item['network']];
175 $author_id = Contact::getIdForURL($item["author-link"], 0, false, $default);
177 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
178 'photo' => $item['owner-avatar'], 'network' => $item['network']];
179 $owner_id = Contact::getIdForURL($item["owner-link"], 0, false, $default);
181 if ($author_id == 0) {
184 if ($owner_id == 0) {
187 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]);
190 logger("Updated items", LOGGER_DEBUG);
195 * @brief update the "last-item" field in the "self" contact
197 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
199 * @return bool "true" when the job is done
201 private static function update1206()
203 // Was the script completed?
204 if (Config::get("system", "post_update_version") >= 1206) {
208 logger("Start", LOGGER_DEBUG);
209 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
210 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
212 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
214 if (!DBM::is_result($r)) {
217 foreach ($r as $user) {
218 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
219 dba::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
223 Config::set("system", "post_update_version", 1206);
224 logger("Done", LOGGER_DEBUG);
229 * @brief update the "item-content" table
231 * @return bool "true" when the job is done
233 private static function update1274()
235 // Was the script completed?
236 if (Config::get("system", "post_update_version") >= 1274) {
240 logger("Start", LOGGER_DEBUG);
242 $fields = ['id', 'title', 'content-warning', 'body', 'location', 'tag', 'file',
243 'coord', 'app', 'rendered-hash', 'rendered-html', 'verb',
244 'object-type', 'object', 'target-type', 'target', 'plink',
245 'author-id', 'owner-id'];
247 $condition = ["`icid` IS NULL"];
248 $params = ['limit' => 10000];
249 $items = Item::select($fields, $condition, $params);
251 if (!DBM::is_result($items)) {
252 Config::set("system", "post_update_version", 1274);
253 logger("Done", LOGGER_DEBUG);
259 while ($item = Item::fetch($items)) {
260 // Clearing the author and owner data if there is an id.
261 if ($item['author-id'] > 0) {
262 $item['author-name'] = '';
263 $item['author-link'] = '';
264 $item['author-avatar'] = '';
267 if ($item['owner-id'] > 0) {
268 $item['owner-name'] = '';
269 $item['owner-link'] = '';
270 $item['owner-avatar'] = '';
273 Item::update($item, ['id' => $item['id']]);
278 logger("Processed rows: " . $rows, LOGGER_DEBUG);
282 * @brief update the "item-activity" table
284 * @return bool "true" when the job is done
286 private static function update1275()
288 // Was the script completed?
289 if (Config::get("system", "post_update_version") >= 1275) {
293 logger("Start", LOGGER_DEBUG);
295 $fields = ['id', 'verb'];
297 $condition = ["`iaid` IS NULL AND NOT `icid` IS NULL AND `verb` IN (?, ?, ?, ?, ?)",
298 ACTIVITY_LIKE, ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE];
300 $params = ['limit' => 10000];
301 $items = Item::select($fields, $condition, $params);
303 if (!DBM::is_result($items)) {
304 Config::set("system", "post_update_version", 1275);
305 logger("Done", LOGGER_DEBUG);
311 while ($item = Item::fetch($items)) {
312 Item::update($item, ['id' => $item['id']]);
317 logger("Processed rows: " . $rows, LOGGER_DEBUG);