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 NOT `item`.`global`";
75 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
76 intval($start_id), intval($end_id),
77 DBA::escape(NETWORK_DFRN), DBA::escape(NETWORK_DIASPORA), DBA::escape(NETWORK_OSTATUS));
79 Config::set("system", "post_update_version", 1194);
80 logger("Update is done", LOGGER_DEBUG);
83 Config::set("system", "post_update_1194_start", $r[0]["id"]);
84 $start_id = Config::get("system", "post_update_1194_start");
87 logger("Start ID: ".$start_id, LOGGER_DEBUG);
89 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
90 intval($start_id), intval($end_id),
91 DBA::escape(NETWORK_DFRN), DBA::escape(NETWORK_DIASPORA), DBA::escape(NETWORK_OSTATUS));
93 $pos_id = $r[0]["id"];
97 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
99 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
100 intval($start_id), intval($pos_id),
101 DBA::escape(NETWORK_DFRN), DBA::escape(NETWORK_DIASPORA), DBA::escape(NETWORK_OSTATUS));
103 logger("Done", LOGGER_DEBUG);
107 * @brief set the author-id and owner-id in all item entries
109 * This job has to be started multiple times until all entries are set.
110 * It isn't started in the update function since it would consume too much time and can be done in the background.
112 * @return bool "true" when the job is done
114 private static function update1198()
116 // Was the script completed?
117 if (Config::get("system", "post_update_version") >= 1198) {
121 logger("Start", LOGGER_DEBUG);
123 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
124 $fields = ['author-link', 'author-name', 'author-avatar', 'owner-link', 'owner-name', 'owner-avatar', 'network', 'uid'];
125 $r = DBA::select('item', $fields, ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
127 // Are there unfinished entries in the thread table?
128 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
129 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
130 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
131 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
133 if ($r && ($r[0]["total"] == 0)) {
134 Config::set("system", "post_update_version", 1198);
135 logger("Done", LOGGER_DEBUG);
139 // Update the thread table from the item table
140 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
141 SET `thread`.`author-id` = `item`.`author-id`,
142 `thread`.`owner-id` = `item`.`owner-id`
143 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
144 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
146 logger("Updated threads", LOGGER_DEBUG);
147 if (DBA::isResult($r)) {
148 Config::set("system", "post_update_version", 1198);
149 logger("Done", LOGGER_DEBUG);
155 logger("Query done", LOGGER_DEBUG);
158 foreach ($r as $item) {
159 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
160 $item_arr[$index] = ["author-link" => $item["author-link"],
161 "owner-link" => $item["owner-link"],
162 "uid" => $item["uid"]];
165 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
166 foreach ($item_arr as $item) {
167 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
168 'photo' => $item['author-avatar'], 'network' => $item['network']];
169 $author_id = Contact::getIdForURL($item["author-link"], 0, false, $default);
171 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
172 'photo' => $item['owner-avatar'], 'network' => $item['network']];
173 $owner_id = Contact::getIdForURL($item["owner-link"], 0, false, $default);
175 if ($author_id == 0) {
178 if ($owner_id == 0) {
181 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]);
184 logger("Updated items", LOGGER_DEBUG);
189 * @brief update the "last-item" field in the "self" contact
191 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
193 * @return bool "true" when the job is done
195 private static function update1206()
197 // Was the script completed?
198 if (Config::get("system", "post_update_version") >= 1206) {
202 logger("Start", LOGGER_DEBUG);
203 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
204 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
206 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
208 if (!DBA::isResult($r)) {
211 foreach ($r as $user) {
212 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
213 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
217 Config::set("system", "post_update_version", 1206);
218 logger("Done", LOGGER_DEBUG);
223 * @brief update the item related tables
225 * @return bool "true" when the job is done
227 private static function update1279()
229 // Was the script completed?
230 if (Config::get("system", "post_update_version") >= 1279) {
234 $id = Config::get("system", "post_update_version_1279_id", 0);
236 logger("Start from item " . $id, LOGGER_DEBUG);
238 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
239 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
240 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
241 'inform', 'postopts', 'icid']);
245 $condition = ["`id` > ?", $id];
246 $params = ['order' => ['id'], 'limit' => 10000];
247 $items = Item::select($fields, $condition, $params);
248 while ($item = Item::fetch($items)) {
251 if (empty($item['author-id'])) {
252 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
253 'photo' => $item['author-avatar'], 'network' => $item['network']];
255 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
258 if (empty($item['owner-id'])) {
259 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
260 'photo' => $item['owner-avatar'], 'network' => $item['network']];
262 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
265 if (!is_null($item['allow_cid']) && !is_null($item['allow_gid'])
266 && !is_null($item['deny_cid']) && !is_null($item['deny_gid'])) {
267 $item['psid'] = PermissionSet::fetchIDForPost($item);
269 $item['allow_cid'] = null;
270 $item['allow_gid'] = null;
271 $item['deny_cid'] = null;
272 $item['deny_gid'] = null;
275 if ($item['post-type'] == 0) {
276 if (!empty($item['type']) && ($item['type'] == 'note')) {
277 $item['post-type'] = Item::PT_PERSONAL_NOTE;
278 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
279 $item['post-type'] = Item::PT_IMAGE;
280 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
281 $item['post-type'] = Item::PT_PAGE;
285 self::createLanguage($item);
287 if (!empty($item['icid']) && !empty($item['language'])) {
288 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
290 unset($item['language']);
292 Item::update($item, ['id' => $id]);
298 Config::set("system", "post_update_version_1279_id", $id);
300 logger("Processed rows: " . $rows . " - last processed item: " . $id, LOGGER_DEBUG);
302 if ($start_id == $id) {
303 // Set all deprecated fields to "null" if they contain an empty string
304 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
305 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
306 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
307 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
308 'rendered-hash', 'rendered-html'];
309 foreach ($nullfields as $field) {
310 $fields = [$field => null];
311 $condition = [$field => ''];
312 logger("Setting '" . $field . "' to null if empty.", LOGGER_DEBUG);
313 // Important: This has to be a "DBA::update", not a "Item::update"
314 DBA::update('item', $fields, $condition);
317 Config::set("system", "post_update_version", 1279);
318 logger("Done", LOGGER_DEBUG);
325 private static function createLanguage(&$item)
327 if (empty($item['postopts'])) {
331 $opts = explode(',', $item['postopts']);
335 foreach ($opts as $opt) {
336 if (strstr($opt, 'lang=')) {
337 $language = substr($opt, 5);
343 if (empty($language)) {
347 if (!empty($postopts)) {
348 $item['postopts'] = implode(',', $postopts);
350 $item['postopts'] = null;
353 $lang_pairs = explode(':', $language);
357 foreach ($lang_pairs as $pair) {
358 $lang_pair_arr = explode(';', $pair);
359 if (count($lang_pair_arr) == 2) {
360 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
364 $item['language'] = json_encode($lang_arr);