3 * @file src/Database/PostUpdate.php
5 namespace Friendica\Database;
7 use Friendica\Core\Config;
8 use Friendica\Model\Contact;
9 use Friendica\Model\Item;
10 use Friendica\Model\PermissionSet;
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()) {
33 if (!self::update1279()) {
39 * @brief Updates the "global" field in the item table
41 * @return bool "true" when the job is done
43 private static function update1194()
45 // Was the script completed?
46 if (Config::get("system", "post_update_version") >= 1194) {
50 logger("Start", LOGGER_DEBUG);
52 $end_id = Config::get("system", "post_update_1194_end");
54 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
56 Config::set("system", "post_update_1194_end", $r[0]["id"]);
57 $end_id = Config::get("system", "post_update_1194_end");
61 logger("End ID: ".$end_id, LOGGER_DEBUG);
63 $start_id = Config::get("system", "post_update_1194_start");
65 $query1 = "SELECT `item`.`id` FROM `item` ";
67 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
69 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
70 AND `item`.`visible` AND NOT `item`.`private`
71 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
72 AND `item`.`network` IN ('%s', '%s', '%s', '')
73 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
74 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
75 AND NOT `item`.`global`";
77 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
78 intval($start_id), intval($end_id),
79 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
81 Config::set("system", "post_update_version", 1194);
82 logger("Update is done", LOGGER_DEBUG);
85 Config::set("system", "post_update_1194_start", $r[0]["id"]);
86 $start_id = Config::get("system", "post_update_1194_start");
89 logger("Start ID: ".$start_id, LOGGER_DEBUG);
91 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
92 intval($start_id), intval($end_id),
93 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
95 $pos_id = $r[0]["id"];
99 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
101 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
102 intval($start_id), intval($pos_id),
103 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
105 logger("Done", LOGGER_DEBUG);
109 * @brief set the author-id and owner-id in all item entries
111 * This job has to be started multiple times until all entries are set.
112 * It isn't started in the update function since it would consume too much time and can be done in the background.
114 * @return bool "true" when the job is done
116 private static function update1198()
118 // Was the script completed?
119 if (Config::get("system", "post_update_version") >= 1198) {
123 logger("Start", LOGGER_DEBUG);
125 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
126 $fields = ['author-link', 'author-name', 'author-avatar', 'owner-link', 'owner-name', 'owner-avatar', 'network', 'uid'];
127 $r = DBA::select('item', $fields, ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
129 // Are there unfinished entries in the thread table?
130 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
131 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
132 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
133 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
135 if ($r && ($r[0]["total"] == 0)) {
136 Config::set("system", "post_update_version", 1198);
137 logger("Done", LOGGER_DEBUG);
141 // Update the thread table from the item table
142 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
143 SET `thread`.`author-id` = `item`.`author-id`,
144 `thread`.`owner-id` = `item`.`owner-id`
145 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
146 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
148 logger("Updated threads", LOGGER_DEBUG);
149 if (DBM::is_result($r)) {
150 Config::set("system", "post_update_version", 1198);
151 logger("Done", LOGGER_DEBUG);
157 logger("Query done", LOGGER_DEBUG);
160 foreach ($r as $item) {
161 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
162 $item_arr[$index] = ["author-link" => $item["author-link"],
163 "owner-link" => $item["owner-link"],
164 "uid" => $item["uid"]];
167 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
168 foreach ($item_arr as $item) {
169 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
170 'photo' => $item['author-avatar'], 'network' => $item['network']];
171 $author_id = Contact::getIdForURL($item["author-link"], 0, false, $default);
173 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
174 'photo' => $item['owner-avatar'], 'network' => $item['network']];
175 $owner_id = Contact::getIdForURL($item["owner-link"], 0, false, $default);
177 if ($author_id == 0) {
180 if ($owner_id == 0) {
183 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]);
186 logger("Updated items", LOGGER_DEBUG);
191 * @brief update the "last-item" field in the "self" contact
193 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
195 * @return bool "true" when the job is done
197 private static function update1206()
199 // Was the script completed?
200 if (Config::get("system", "post_update_version") >= 1206) {
204 logger("Start", LOGGER_DEBUG);
205 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
206 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
208 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
210 if (!DBM::is_result($r)) {
213 foreach ($r as $user) {
214 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
215 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
219 Config::set("system", "post_update_version", 1206);
220 logger("Done", LOGGER_DEBUG);
225 * @brief update the item related tables
227 * @return bool "true" when the job is done
229 private static function update1279()
231 // Was the script completed?
232 if (Config::get("system", "post_update_version") >= 1279) {
236 $id = Config::get("system", "post_update_version_1279_id", 0);
238 logger("Start from item " . $id, LOGGER_DEBUG);
240 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
241 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
242 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
247 $condition = ["`id` > ?", $id];
248 $params = ['order' => ['id'], 'limit' => 10000];
249 $items = Item::select($fields, $condition, $params);
250 while ($item = Item::fetch($items)) {
253 if (empty($item['author-id'])) {
254 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
255 'photo' => $item['author-avatar'], 'network' => $item['network']];
257 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
260 if (empty($item['owner-id'])) {
261 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
262 'photo' => $item['owner-avatar'], 'network' => $item['network']];
264 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
267 if (empty($item['psid'])) {
268 $item['psid'] = PermissionSet::fetchIDForPost($item);
271 if ($item['post-type'] == 0) {
272 if (!empty($item['type']) && ($item['type'] == 'note')) {
273 $item['post-type'] = Item::PT_PERSONAL_NOTE;
274 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
275 $item['post-type'] = Item::PT_IMAGE;
276 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
277 $item['post-type'] = Item::PT_PAGE;
281 Item::update($item, ['id' => $id]);
287 Config::set("system", "post_update_version_1279_id", $id);
289 logger("Processed rows: " . $rows . " - last processed item: " . $id, LOGGER_DEBUG);
291 if ($start_id == $id) {
292 Config::set("system", "post_update_version", 1279);
293 logger("Done", LOGGER_DEBUG);