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;
11 use Friendica\Model\Contact;
12 use Friendica\Model\Item;
13 use Friendica\Model\ItemURI;
14 use Friendica\Model\UserItem;
15 use Friendica\Model\PermissionSet;
18 * Post update functions
23 * Calls the post update functions
25 public static function update()
27 if (!self::update1194()) {
30 if (!self::update1206()) {
33 if (!self::update1279()) {
36 if (!self::update1281()) {
39 if (!self::update1297()) {
42 if (!self::update1322()) {
45 if (!self::update1329()) {
53 * Updates the "global" field in the item table
55 * @return bool "true" when the job is done
56 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
58 private static function update1194()
60 // Was the script completed?
61 if (DI::config()->get("system", "post_update_version") >= 1194) {
65 Logger::log("Start", Logger::DEBUG);
67 $end_id = DI::config()->get("system", "post_update_1194_end");
69 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
71 DI::config()->set("system", "post_update_1194_end", $r[0]["id"]);
72 $end_id = DI::config()->get("system", "post_update_1194_end");
76 Logger::log("End ID: ".$end_id, Logger::DEBUG);
78 $start_id = DI::config()->get("system", "post_update_1194_start");
80 $query1 = "SELECT `item`.`id` FROM `item` ";
82 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
84 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
85 AND `item`.`visible` AND NOT `item`.`private`
86 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
87 AND `item`.`network` IN ('%s', '%s', '%s', '')
88 AND NOT `item`.`global`";
90 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
91 intval($start_id), intval($end_id),
92 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
94 DI::config()->set("system", "post_update_version", 1194);
95 Logger::log("Update is done", Logger::DEBUG);
98 DI::config()->set("system", "post_update_1194_start", $r[0]["id"]);
99 $start_id = DI::config()->get("system", "post_update_1194_start");
102 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
104 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
105 intval($start_id), intval($end_id),
106 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
108 $pos_id = $r[0]["id"];
112 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
114 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
115 intval($start_id), intval($pos_id),
116 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
118 Logger::log("Done", Logger::DEBUG);
122 * update the "last-item" field in the "self" contact
124 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
126 * @return bool "true" when the job is done
127 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
129 private static function update1206()
131 // Was the script completed?
132 if (DI::config()->get("system", "post_update_version") >= 1206) {
136 Logger::log("Start", Logger::DEBUG);
137 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
138 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
140 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
142 if (!DBA::isResult($r)) {
145 foreach ($r as $user) {
146 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
147 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
151 DI::config()->set("system", "post_update_version", 1206);
152 Logger::log("Done", Logger::DEBUG);
157 * update the item related tables
159 * @return bool "true" when the job is done
160 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
161 * @throws \ImagickException
163 private static function update1279()
165 // Was the script completed?
166 if (DI::config()->get("system", "post_update_version") >= 1279) {
170 $id = DI::config()->get("system", "post_update_version_1279_id", 0);
172 Logger::log("Start from item " . $id, Logger::DEBUG);
174 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
175 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
176 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
177 'inform', 'postopts', 'icid']);
181 $condition = ["`id` > ?", $id];
182 $params = ['order' => ['id'], 'limit' => 10000];
183 $items = Item::select($fields, $condition, $params);
185 if (DBA::errorNo() != 0) {
186 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
190 while ($item = Item::fetch($items)) {
193 if (empty($item['author-id'])) {
194 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
195 'photo' => $item['author-avatar'], 'network' => $item['network']];
197 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
200 if (empty($item['owner-id'])) {
201 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
202 'photo' => $item['owner-avatar'], 'network' => $item['network']];
204 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
207 if (empty($item['psid'])) {
208 $item['psid'] = PermissionSet::fetchIDForPost($item);
210 $item['allow_cid'] = null;
211 $item['allow_gid'] = null;
212 $item['deny_cid'] = null;
213 $item['deny_gid'] = null;
216 if ($item['post-type'] == 0) {
217 if (!empty($item['type']) && ($item['type'] == 'note')) {
218 $item['post-type'] = Item::PT_PERSONAL_NOTE;
219 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
220 $item['post-type'] = Item::PT_IMAGE;
221 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
222 $item['post-type'] = Item::PT_PAGE;
226 self::createLanguage($item);
228 if (!empty($item['icid']) && !empty($item['language'])) {
229 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
231 unset($item['language']);
233 Item::update($item, ['id' => $id]);
239 DI::config()->set("system", "post_update_version_1279_id", $id);
241 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
243 if ($start_id == $id) {
244 // Set all deprecated fields to "null" if they contain an empty string
245 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
246 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
247 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
248 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
249 'rendered-hash', 'rendered-html'];
250 foreach ($nullfields as $field) {
251 $fields = [$field => null];
252 $condition = [$field => ''];
253 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
254 // Important: This has to be a "DBA::update", not a "Item::update"
255 DBA::update('item', $fields, $condition);
258 DI::config()->set("system", "post_update_version", 1279);
259 Logger::log("Done", Logger::DEBUG);
266 private static function createLanguage(&$item)
268 if (empty($item['postopts'])) {
272 $opts = explode(',', $item['postopts']);
276 foreach ($opts as $opt) {
277 if (strstr($opt, 'lang=')) {
278 $language = substr($opt, 5);
284 if (empty($language)) {
288 if (!empty($postopts)) {
289 $item['postopts'] = implode(',', $postopts);
291 $item['postopts'] = null;
294 $lang_pairs = explode(':', $language);
298 foreach ($lang_pairs as $pair) {
299 $lang_pair_arr = explode(';', $pair);
300 if (count($lang_pair_arr) == 2) {
301 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
305 $item['language'] = json_encode($lang_arr);
309 * update item-uri data. Prerequisite for the next item structure update.
311 * @return bool "true" when the job is done
312 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
314 private static function update1281()
316 // Was the script completed?
317 if (DI::config()->get("system", "post_update_version") >= 1281) {
321 $id = DI::config()->get("system", "post_update_version_1281_id", 0);
323 Logger::log("Start from item " . $id, Logger::DEBUG);
325 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
329 $condition = ["`id` > ?", $id];
330 $params = ['order' => ['id'], 'limit' => 10000];
331 $items = DBA::select('item', $fields, $condition, $params);
333 if (DBA::errorNo() != 0) {
334 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
338 while ($item = DBA::fetch($items)) {
341 if (empty($item['uri'])) {
344 } elseif (empty($item['uri-id'])) {
345 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
348 if (empty($item['parent-uri'])) {
349 $item['parent-uri-id'] = $item['uri-id'];
350 } elseif (empty($item['parent-uri-id'])) {
351 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
354 // Very old items don't have this field
355 if (empty($item['thr-parent'])) {
356 $item['thr-parent-id'] = $item['parent-uri-id'];
357 } elseif (empty($item['thr-parent-id'])) {
358 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
362 unset($item['guid']);
364 unset($item['parent-uri']);
365 unset($item['thr-parent']);
367 DBA::update('item', $item, ['id' => $id]);
373 DI::config()->set("system", "post_update_version_1281_id", $id);
375 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
377 if ($start_id == $id) {
378 Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
379 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");
381 Logger::log("Updating item-uri in item-content", Logger::DEBUG);
382 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");
384 DI::config()->set("system", "post_update_version", 1281);
385 Logger::log("Done", Logger::DEBUG);
393 * Set the delivery queue count to a negative value for all items preceding the feature.
395 * @return bool "true" when the job is done
396 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
398 private static function update1297()
400 // Was the script completed?
401 if (DI::config()->get('system', 'post_update_version') >= 1297) {
405 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
406 $max_iid = $max_item_delivery_data['iid'];
408 Logger::info('Start update1297 with max iid: ' . $max_iid);
410 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
412 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
414 if (DBA::errorNo() != 0) {
415 Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
419 Logger::info('Processed rows: ' . DBA::affectedRows());
421 DI::config()->set('system', 'post_update_version', 1297);
423 Logger::info('Done');
428 * Remove contact duplicates
430 * @return bool "true" when the job is done
431 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
433 private static function update1322()
435 // Was the script completed?
436 if (DI::config()->get('system', 'post_update_version') >= 1322) {
440 Logger::info('Start');
442 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
443 WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
444 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
445 AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
446 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
447 Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
449 while ($contact = DBA::fetch($contacts)) {
450 Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
451 Contact::removeDuplicates($contact['nurl'], $contact['uid']);
454 DBA::close($contact);
455 DI::config()->set('system', 'post_update_version', 1322);
457 Logger::info('Done');
463 * update user-item data with notifications
465 * @return bool "true" when the job is done
466 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
468 private static function update1329()
470 // Was the script completed?
471 if (DI::config()->get('system', 'post_update_version') >= 1329) {
475 $id = DI::config()->get('system', 'post_update_version_1329_id', 0);
477 Logger::info('Start', ['item' => $id]);
481 $condition = ["`id` > ?", $id];
482 $params = ['order' => ['id'], 'limit' => 10000];
483 $items = DBA::select('item', ['id'], $condition, $params);
485 if (DBA::errorNo() != 0) {
486 Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
490 while ($item = DBA::fetch($items)) {
493 UserItem::setNotification($item['id']);
499 DI::config()->set('system', 'post_update_version_1329_id', $id);
501 Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
503 if ($start_id == $id) {
504 DI::config()->set('system', 'post_update_version', 1329);
505 Logger::info('Done');