3 * @file src/Database/PostUpdate.php
5 namespace Friendica\Database;
7 use Friendica\Core\Logger;
8 use Friendica\Core\Protocol;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Item;
12 use Friendica\Model\ItemURI;
13 use Friendica\Model\UserItem;
14 use Friendica\Model\PermissionSet;
17 * Post update functions
22 * Calls the post update functions
24 public static function update()
26 if (!self::update1194()) {
29 if (!self::update1206()) {
32 if (!self::update1279()) {
35 if (!self::update1281()) {
38 if (!self::update1297()) {
41 if (!self::update1322()) {
44 if (!self::update1329()) {
52 * Updates the "global" field in the item table
54 * @return bool "true" when the job is done
55 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
57 private static function update1194()
59 // Was the script completed?
60 if (DI::config()->get("system", "post_update_version") >= 1194) {
64 Logger::log("Start", Logger::DEBUG);
66 $end_id = DI::config()->get("system", "post_update_1194_end");
68 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
70 DI::config()->set("system", "post_update_1194_end", $r[0]["id"]);
71 $end_id = DI::config()->get("system", "post_update_1194_end");
75 Logger::log("End ID: ".$end_id, Logger::DEBUG);
77 $start_id = DI::config()->get("system", "post_update_1194_start");
79 $query1 = "SELECT `item`.`id` FROM `item` ";
81 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
83 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
84 AND `item`.`visible` AND NOT `item`.`private`
85 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
86 AND `item`.`network` IN ('%s', '%s', '%s', '')
87 AND NOT `item`.`global`";
89 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
90 intval($start_id), intval($end_id),
91 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
93 DI::config()->set("system", "post_update_version", 1194);
94 Logger::log("Update is done", Logger::DEBUG);
97 DI::config()->set("system", "post_update_1194_start", $r[0]["id"]);
98 $start_id = DI::config()->get("system", "post_update_1194_start");
101 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
103 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
104 intval($start_id), intval($end_id),
105 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
107 $pos_id = $r[0]["id"];
111 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
113 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
114 intval($start_id), intval($pos_id),
115 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
117 Logger::log("Done", Logger::DEBUG);
121 * update the "last-item" field in the "self" contact
123 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
125 * @return bool "true" when the job is done
126 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
128 private static function update1206()
130 // Was the script completed?
131 if (DI::config()->get("system", "post_update_version") >= 1206) {
135 Logger::log("Start", Logger::DEBUG);
136 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
137 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
139 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
141 if (!DBA::isResult($r)) {
144 foreach ($r as $user) {
145 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
146 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
150 DI::config()->set("system", "post_update_version", 1206);
151 Logger::log("Done", Logger::DEBUG);
156 * update the item related tables
158 * @return bool "true" when the job is done
159 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
160 * @throws \ImagickException
162 private static function update1279()
164 // Was the script completed?
165 if (DI::config()->get("system", "post_update_version") >= 1279) {
169 $id = DI::config()->get("system", "post_update_version_1279_id", 0);
171 Logger::log("Start from item " . $id, Logger::DEBUG);
173 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
174 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
175 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
176 'inform', 'postopts', 'icid']);
180 $condition = ["`id` > ?", $id];
181 $params = ['order' => ['id'], 'limit' => 10000];
182 $items = Item::select($fields, $condition, $params);
184 if (DBA::errorNo() != 0) {
185 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
189 while ($item = Item::fetch($items)) {
192 if (empty($item['author-id'])) {
193 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
194 'photo' => $item['author-avatar'], 'network' => $item['network']];
196 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
199 if (empty($item['owner-id'])) {
200 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
201 'photo' => $item['owner-avatar'], 'network' => $item['network']];
203 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
206 if (empty($item['psid'])) {
207 $item['psid'] = PermissionSet::getIdFromACL(
216 $item['allow_cid'] = null;
217 $item['allow_gid'] = null;
218 $item['deny_cid'] = null;
219 $item['deny_gid'] = null;
221 if ($item['post-type'] == 0) {
222 if (!empty($item['type']) && ($item['type'] == 'note')) {
223 $item['post-type'] = Item::PT_PERSONAL_NOTE;
224 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
225 $item['post-type'] = Item::PT_IMAGE;
226 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
227 $item['post-type'] = Item::PT_PAGE;
231 self::createLanguage($item);
233 if (!empty($item['icid']) && !empty($item['language'])) {
234 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
236 unset($item['language']);
238 Item::update($item, ['id' => $id]);
244 DI::config()->set("system", "post_update_version_1279_id", $id);
246 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
248 if ($start_id == $id) {
249 // Set all deprecated fields to "null" if they contain an empty string
250 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
251 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
252 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
253 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
254 'rendered-hash', 'rendered-html'];
255 foreach ($nullfields as $field) {
256 $fields = [$field => null];
257 $condition = [$field => ''];
258 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
259 // Important: This has to be a "DBA::update", not a "Item::update"
260 DBA::update('item', $fields, $condition);
263 DI::config()->set("system", "post_update_version", 1279);
264 Logger::log("Done", Logger::DEBUG);
271 private static function createLanguage(&$item)
273 if (empty($item['postopts'])) {
277 $opts = explode(',', $item['postopts']);
281 foreach ($opts as $opt) {
282 if (strstr($opt, 'lang=')) {
283 $language = substr($opt, 5);
289 if (empty($language)) {
293 if (!empty($postopts)) {
294 $item['postopts'] = implode(',', $postopts);
296 $item['postopts'] = null;
299 $lang_pairs = explode(':', $language);
303 foreach ($lang_pairs as $pair) {
304 $lang_pair_arr = explode(';', $pair);
305 if (count($lang_pair_arr) == 2) {
306 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
310 $item['language'] = json_encode($lang_arr);
314 * update item-uri data. Prerequisite for the next item structure update.
316 * @return bool "true" when the job is done
317 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
319 private static function update1281()
321 // Was the script completed?
322 if (DI::config()->get("system", "post_update_version") >= 1281) {
326 $id = DI::config()->get("system", "post_update_version_1281_id", 0);
328 Logger::log("Start from item " . $id, Logger::DEBUG);
330 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
334 $condition = ["`id` > ?", $id];
335 $params = ['order' => ['id'], 'limit' => 10000];
336 $items = DBA::select('item', $fields, $condition, $params);
338 if (DBA::errorNo() != 0) {
339 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
343 while ($item = DBA::fetch($items)) {
346 if (empty($item['uri'])) {
349 } elseif (empty($item['uri-id'])) {
350 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
353 if (empty($item['parent-uri'])) {
354 $item['parent-uri-id'] = $item['uri-id'];
355 } elseif (empty($item['parent-uri-id'])) {
356 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
359 // Very old items don't have this field
360 if (empty($item['thr-parent'])) {
361 $item['thr-parent-id'] = $item['parent-uri-id'];
362 } elseif (empty($item['thr-parent-id'])) {
363 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
367 unset($item['guid']);
369 unset($item['parent-uri']);
370 unset($item['thr-parent']);
372 DBA::update('item', $item, ['id' => $id]);
378 DI::config()->set("system", "post_update_version_1281_id", $id);
380 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
382 if ($start_id == $id) {
383 Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
384 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");
386 Logger::log("Updating item-uri in item-content", Logger::DEBUG);
387 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");
389 DI::config()->set("system", "post_update_version", 1281);
390 Logger::log("Done", Logger::DEBUG);
398 * Set the delivery queue count to a negative value for all items preceding the feature.
400 * @return bool "true" when the job is done
401 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
403 private static function update1297()
405 // Was the script completed?
406 if (DI::config()->get('system', 'post_update_version') >= 1297) {
410 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
411 $max_iid = $max_item_delivery_data['iid'];
413 Logger::info('Start update1297 with max iid: ' . $max_iid);
415 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
417 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
419 if (DBA::errorNo() != 0) {
420 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
424 Logger::info('Processed rows: ' . DBA::affectedRows());
426 DI::config()->set('system', 'post_update_version', 1297);
428 Logger::info('Done');
433 * Remove contact duplicates
435 * @return bool "true" when the job is done
436 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
438 private static function update1322()
440 // Was the script completed?
441 if (DI::config()->get('system', 'post_update_version') >= 1322) {
445 Logger::info('Start');
447 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
448 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
449 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
450 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
451 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
452 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
454 while ($contact = DBA::fetch($contacts)) {
455 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
456 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
459 DBA::close($contact);
460 DI::config()->set('system', 'post_update_version', 1322);
462 Logger::info('Done');
468 * update user-item data with notifications
470 * @return bool "true" when the job is done
471 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
473 private static function update1329()
475 // Was the script completed?
476 if (DI::config()->get('system', 'post_update_version') >= 1329) {
480 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
482 Logger::info('Start', ['item' => $id]);
486 $condition = ["`id` > ?", $id];
487 $params = ['order' => ['id'], 'limit' => 10000];
488 $items = DBA::select('item', ['id'], $condition, $params);
490 if (DBA::errorNo() != 0) {
491 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
495 while ($item = DBA::fetch($items)) {
498 UserItem::setNotification($item['id']);
504 DI::config()->set('system', 'post_update_version_1329_id', $id);
506 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
508 if ($start_id == $id) {
509 DI::config()->set('system', 'post_update_version', 1329);
510 Logger::info('Done');