3 * @file src/Database/PostUpdate.php
5 namespace Friendica\Database;
7 use Friendica\Core\Config;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Protocol;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Item;
12 use Friendica\Model\ItemURI;
13 use Friendica\Model\PermissionSet;
16 * Post update functions
21 * @brief Calls the post update functions
23 public static function update()
25 if (!self::update1194()) {
28 if (!self::update1206()) {
31 if (!self::update1279()) {
34 if (!self::update1281()) {
37 if (!self::update1297()) {
40 if (!self::update1322()) {
48 * @brief Updates the "global" field in the item table
50 * @return bool "true" when the job is done
51 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
53 private static function update1194()
55 // Was the script completed?
56 if (Config::get("system", "post_update_version") >= 1194) {
60 Logger::log("Start", Logger::DEBUG);
62 $end_id = Config::get("system", "post_update_1194_end");
64 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
66 Config::set("system", "post_update_1194_end", $r[0]["id"]);
67 $end_id = Config::get("system", "post_update_1194_end");
71 Logger::log("End ID: ".$end_id, Logger::DEBUG);
73 $start_id = Config::get("system", "post_update_1194_start");
75 $query1 = "SELECT `item`.`id` FROM `item` ";
77 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
79 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
80 AND `item`.`visible` AND NOT `item`.`private`
81 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
82 AND `item`.`network` IN ('%s', '%s', '%s', '')
83 AND NOT `item`.`global`";
85 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
86 intval($start_id), intval($end_id),
87 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
89 Config::set("system", "post_update_version", 1194);
90 Logger::log("Update is done", Logger::DEBUG);
93 Config::set("system", "post_update_1194_start", $r[0]["id"]);
94 $start_id = Config::get("system", "post_update_1194_start");
97 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
99 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
100 intval($start_id), intval($end_id),
101 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
103 $pos_id = $r[0]["id"];
107 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
109 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
110 intval($start_id), intval($pos_id),
111 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
113 Logger::log("Done", Logger::DEBUG);
117 * @brief update the "last-item" field in the "self" contact
119 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
121 * @return bool "true" when the job is done
122 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
124 private static function update1206()
126 // Was the script completed?
127 if (Config::get("system", "post_update_version") >= 1206) {
131 Logger::log("Start", Logger::DEBUG);
132 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
133 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
135 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
137 if (!DBA::isResult($r)) {
140 foreach ($r as $user) {
141 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
142 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
146 Config::set("system", "post_update_version", 1206);
147 Logger::log("Done", Logger::DEBUG);
152 * @brief update the item related tables
154 * @return bool "true" when the job is done
155 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
156 * @throws \ImagickException
158 private static function update1279()
160 // Was the script completed?
161 if (Config::get("system", "post_update_version") >= 1279) {
165 $id = Config::get("system", "post_update_version_1279_id", 0);
167 Logger::log("Start from item " . $id, Logger::DEBUG);
169 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
170 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
171 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
172 'inform', 'postopts', 'icid']);
176 $condition = ["`id` > ?", $id];
177 $params = ['order' => ['id'], 'limit' => 10000];
178 $items = Item::select($fields, $condition, $params);
180 if (DBA::errorNo() != 0) {
181 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
185 while ($item = Item::fetch($items)) {
188 if (empty($item['author-id'])) {
189 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
190 'photo' => $item['author-avatar'], 'network' => $item['network']];
192 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
195 if (empty($item['owner-id'])) {
196 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
197 'photo' => $item['owner-avatar'], 'network' => $item['network']];
199 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
202 if (empty($item['psid'])) {
203 $item['psid'] = PermissionSet::fetchIDForPost($item);
205 $item['allow_cid'] = null;
206 $item['allow_gid'] = null;
207 $item['deny_cid'] = null;
208 $item['deny_gid'] = null;
211 if ($item['post-type'] == 0) {
212 if (!empty($item['type']) && ($item['type'] == 'note')) {
213 $item['post-type'] = Item::PT_PERSONAL_NOTE;
214 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
215 $item['post-type'] = Item::PT_IMAGE;
216 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
217 $item['post-type'] = Item::PT_PAGE;
221 self::createLanguage($item);
223 if (!empty($item['icid']) && !empty($item['language'])) {
224 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
226 unset($item['language']);
228 Item::update($item, ['id' => $id]);
234 Config::set("system", "post_update_version_1279_id", $id);
236 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
238 if ($start_id == $id) {
239 // Set all deprecated fields to "null" if they contain an empty string
240 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
241 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
242 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
243 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
244 'rendered-hash', 'rendered-html'];
245 foreach ($nullfields as $field) {
246 $fields = [$field => null];
247 $condition = [$field => ''];
248 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
249 // Important: This has to be a "DBA::update", not a "Item::update"
250 DBA::update('item', $fields, $condition);
253 Config::set("system", "post_update_version", 1279);
254 Logger::log("Done", Logger::DEBUG);
261 private static function createLanguage(&$item)
263 if (empty($item['postopts'])) {
267 $opts = explode(',', $item['postopts']);
271 foreach ($opts as $opt) {
272 if (strstr($opt, 'lang=')) {
273 $language = substr($opt, 5);
279 if (empty($language)) {
283 if (!empty($postopts)) {
284 $item['postopts'] = implode(',', $postopts);
286 $item['postopts'] = null;
289 $lang_pairs = explode(':', $language);
293 foreach ($lang_pairs as $pair) {
294 $lang_pair_arr = explode(';', $pair);
295 if (count($lang_pair_arr) == 2) {
296 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
300 $item['language'] = json_encode($lang_arr);
304 * @brief update item-uri data. Prerequisite for the next item structure update.
306 * @return bool "true" when the job is done
307 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
309 private static function update1281()
311 // Was the script completed?
312 if (Config::get("system", "post_update_version") >= 1281) {
316 $id = Config::get("system", "post_update_version_1281_id", 0);
318 Logger::log("Start from item " . $id, Logger::DEBUG);
320 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
324 $condition = ["`id` > ?", $id];
325 $params = ['order' => ['id'], 'limit' => 10000];
326 $items = DBA::select('item', $fields, $condition, $params);
328 if (DBA::errorNo() != 0) {
329 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
333 while ($item = DBA::fetch($items)) {
336 if (empty($item['uri'])) {
339 } elseif (empty($item['uri-id'])) {
340 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
343 if (empty($item['parent-uri'])) {
344 $item['parent-uri-id'] = $item['uri-id'];
345 } elseif (empty($item['parent-uri-id'])) {
346 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
349 // Very old items don't have this field
350 if (empty($item['thr-parent'])) {
351 $item['thr-parent-id'] = $item['parent-uri-id'];
352 } elseif (empty($item['thr-parent-id'])) {
353 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
357 unset($item['guid']);
359 unset($item['parent-uri']);
360 unset($item['thr-parent']);
362 DBA::update('item', $item, ['id' => $id]);
368 Config::set("system", "post_update_version_1281_id", $id);
370 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
372 if ($start_id == $id) {
373 Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
374 DBA::e("UPDATE `item-activity` INNER JOIN `item-uri` ON `item-uri`.`uri` = `item-activity`.`uri` SET `item-activity`.`uri-id` = `item-uri`.`id` WHERE `item-activity`.`uri-id` IS NULL");
376 Logger::log("Updating item-uri in item-content", Logger::DEBUG);
377 DBA::e("UPDATE `item-content` INNER JOIN `item-uri` ON `item-uri`.`uri` = `item-content`.`uri` SET `item-content`.`uri-id` = `item-uri`.`id` WHERE `item-content`.`uri-id` IS NULL");
379 Config::set("system", "post_update_version", 1281);
380 Logger::log("Done", Logger::DEBUG);
388 * Set the delivery queue count to a negative value for all items preceding the feature.
390 * @return bool "true" when the job is done
391 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
393 private static function update1297()
395 // Was the script completed?
396 if (Config::get('system', 'post_update_version') >= 1297) {
400 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
401 $max_iid = $max_item_delivery_data['iid'];
403 Logger::info('Start update1297 with max iid: ' . $max_iid);
405 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
407 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
409 if (DBA::errorNo() != 0) {
410 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
414 Logger::info('Processed rows: ' . DBA::affectedRows());
416 Config::set('system', 'post_update_version', 1297);
418 Logger::info('Done');
423 * Set the delivery queue count to a negative value for all items preceding the feature.
425 * @return bool "true" when the job is done
426 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
428 private static function update1322()
430 // Was the script completed?
431 if (Config::get('system', 'post_update_version') >= 1322) {
435 Logger::info('Start');
437 $contacts = DBA::p("SELECT ANY_VALUE(`id`) AS `id`, ANY_VALUE(`nurl`) AS `nurl` FROM `contact`
438 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
439 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?))
440 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) GROUP BY `nurl`, `uid`",
441 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
442 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
444 while ($contact = DBA::fetch($contacts)) {
445 Logger::info('Remove duplicates', ['id' => $contact['id']]);
446 Contact::handleDuplicateByID($contact['id']);
449 DBA::close($contact);
450 Config::set('system', 'post_update_version', 1322);
452 Logger::info('Done');