3 * @file src/Database/PostUpdate.php
5 namespace Friendica\Database;
7 use Friendica\Core\Config;
8 use Friendica\Core\Protocol;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Item;
11 use Friendica\Model\ItemURI;
12 use Friendica\Model\PermissionSet;
13 use Friendica\Database\DBA;
15 require_once 'include/dba.php';
18 * Post update functions
23 * @brief 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()) {
44 * @brief Updates the "global" field in the item table
46 * @return bool "true" when the job is done
48 private static function update1194()
50 // Was the script completed?
51 if (Config::get("system", "post_update_version") >= 1194) {
55 logger("Start", LOGGER_DEBUG);
57 $end_id = Config::get("system", "post_update_1194_end");
59 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
61 Config::set("system", "post_update_1194_end", $r[0]["id"]);
62 $end_id = Config::get("system", "post_update_1194_end");
66 logger("End ID: ".$end_id, LOGGER_DEBUG);
68 $start_id = Config::get("system", "post_update_1194_start");
70 $query1 = "SELECT `item`.`id` FROM `item` ";
72 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
74 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
75 AND `item`.`visible` AND NOT `item`.`private`
76 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
77 AND `item`.`network` IN ('%s', '%s', '%s', '')
78 AND NOT `item`.`global`";
80 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
81 intval($start_id), intval($end_id),
82 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
84 Config::set("system", "post_update_version", 1194);
85 logger("Update is done", LOGGER_DEBUG);
88 Config::set("system", "post_update_1194_start", $r[0]["id"]);
89 $start_id = Config::get("system", "post_update_1194_start");
92 logger("Start ID: ".$start_id, LOGGER_DEBUG);
94 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
95 intval($start_id), intval($end_id),
96 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
98 $pos_id = $r[0]["id"];
102 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
104 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
105 intval($start_id), intval($pos_id),
106 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
108 logger("Done", LOGGER_DEBUG);
112 * @brief update the "last-item" field in the "self" contact
114 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
116 * @return bool "true" when the job is done
118 private static function update1206()
120 // Was the script completed?
121 if (Config::get("system", "post_update_version") >= 1206) {
125 logger("Start", LOGGER_DEBUG);
126 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
127 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
129 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
131 if (!DBA::isResult($r)) {
134 foreach ($r as $user) {
135 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
136 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
140 Config::set("system", "post_update_version", 1206);
141 logger("Done", LOGGER_DEBUG);
146 * @brief update the item related tables
148 * @return bool "true" when the job is done
150 private static function update1279()
152 // Was the script completed?
153 if (Config::get("system", "post_update_version") >= 1279) {
157 $id = Config::get("system", "post_update_version_1279_id", 0);
159 logger("Start from item " . $id, LOGGER_DEBUG);
161 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
162 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
163 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
164 'inform', 'postopts', 'icid']);
168 $condition = ["`id` > ?", $id];
169 $params = ['order' => ['id'], 'limit' => 10000];
170 $items = Item::select($fields, $condition, $params);
172 if (DBA::errorNo() != 0) {
173 logger('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
177 while ($item = Item::fetch($items)) {
180 if (empty($item['author-id'])) {
181 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
182 'photo' => $item['author-avatar'], 'network' => $item['network']];
184 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
187 if (empty($item['owner-id'])) {
188 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
189 'photo' => $item['owner-avatar'], 'network' => $item['network']];
191 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
194 if (empty($item['psid'])) {
195 $item['psid'] = PermissionSet::fetchIDForPost($item);
197 $item['allow_cid'] = null;
198 $item['allow_gid'] = null;
199 $item['deny_cid'] = null;
200 $item['deny_gid'] = null;
203 if ($item['post-type'] == 0) {
204 if (!empty($item['type']) && ($item['type'] == 'note')) {
205 $item['post-type'] = Item::PT_PERSONAL_NOTE;
206 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
207 $item['post-type'] = Item::PT_IMAGE;
208 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
209 $item['post-type'] = Item::PT_PAGE;
213 self::createLanguage($item);
215 if (!empty($item['icid']) && !empty($item['language'])) {
216 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
218 unset($item['language']);
220 Item::update($item, ['id' => $id]);
226 Config::set("system", "post_update_version_1279_id", $id);
228 logger("Processed rows: " . $rows . " - last processed item: " . $id, LOGGER_DEBUG);
230 if ($start_id == $id) {
231 // Set all deprecated fields to "null" if they contain an empty string
232 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
233 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
234 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
235 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
236 'rendered-hash', 'rendered-html'];
237 foreach ($nullfields as $field) {
238 $fields = [$field => null];
239 $condition = [$field => ''];
240 logger("Setting '" . $field . "' to null if empty.", LOGGER_DEBUG);
241 // Important: This has to be a "DBA::update", not a "Item::update"
242 DBA::update('item', $fields, $condition);
245 Config::set("system", "post_update_version", 1279);
246 logger("Done", LOGGER_DEBUG);
253 private static function createLanguage(&$item)
255 if (empty($item['postopts'])) {
259 $opts = explode(',', $item['postopts']);
263 foreach ($opts as $opt) {
264 if (strstr($opt, 'lang=')) {
265 $language = substr($opt, 5);
271 if (empty($language)) {
275 if (!empty($postopts)) {
276 $item['postopts'] = implode(',', $postopts);
278 $item['postopts'] = null;
281 $lang_pairs = explode(':', $language);
285 foreach ($lang_pairs as $pair) {
286 $lang_pair_arr = explode(';', $pair);
287 if (count($lang_pair_arr) == 2) {
288 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
292 $item['language'] = json_encode($lang_arr);
296 * @brief update item-uri data. Prerequisite for the next item structure update.
298 * @return bool "true" when the job is done
300 private static function update1281()
302 // Was the script completed?
303 if (Config::get("system", "post_update_version") >= 1281) {
307 $id = Config::get("system", "post_update_version_1281_id", 0);
309 logger("Start from item " . $id, LOGGER_DEBUG);
311 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
315 $condition = ["`id` > ?", $id];
316 $params = ['order' => ['id'], 'limit' => 10000];
317 $items = DBA::select('item', $fields, $condition, $params);
319 if (DBA::errorNo() != 0) {
320 logger('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
324 while ($item = DBA::fetch($items)) {
327 if (empty($item['uri'])) {
330 } elseif (empty($item['uri-id'])) {
331 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
334 if (empty($item['parent-uri'])) {
335 $item['parent-uri-id'] = $item['uri-id'];
336 } elseif (empty($item['parent-uri-id'])) {
337 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
340 // Very old items don't have this field
341 if (empty($item['thr-parent'])) {
342 $item['thr-parent-id'] = $item['parent-uri-id'];
343 } elseif (empty($item['thr-parent-id'])) {
344 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
348 unset($item['guid']);
350 unset($item['parent-uri']);
351 unset($item['thr-parent']);
353 DBA::update('item', $item, ['id' => $id]);
359 Config::set("system", "post_update_version_1281_id", $id);
361 logger("Processed rows: " . $rows . " - last processed item: " . $id, LOGGER_DEBUG);
363 if ($start_id == $id) {
364 logger("Updating item-uri in item-activity", LOGGER_DEBUG);
365 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");
367 logger("Updating item-uri in item-content", LOGGER_DEBUG);
368 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");
370 Config::set("system", "post_update_version", 1281);
371 logger("Done", LOGGER_DEBUG);