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\ItemURI;
11 use Friendica\Model\PermissionSet;
12 use Friendica\Database\DBA;
14 require_once 'include/dba.php';
17 * Post update functions
22 * @brief Calls the post update functions
24 public static function update()
26 if (!self::update1194()) {
29 if (!self::update1198()) {
32 if (!self::update1206()) {
35 if (!self::update1279()) {
38 if (!self::update1281()) {
46 * @brief Updates the "global" field in the item table
48 * @return bool "true" when the job is done
50 private static function update1194()
52 // Was the script completed?
53 if (Config::get("system", "post_update_version") >= 1194) {
57 logger("Start", LOGGER_DEBUG);
59 $end_id = Config::get("system", "post_update_1194_end");
61 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
63 Config::set("system", "post_update_1194_end", $r[0]["id"]);
64 $end_id = Config::get("system", "post_update_1194_end");
68 logger("End ID: ".$end_id, LOGGER_DEBUG);
70 $start_id = Config::get("system", "post_update_1194_start");
72 $query1 = "SELECT `item`.`id` FROM `item` ";
74 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
76 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
77 AND `item`.`visible` AND NOT `item`.`private`
78 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
79 AND `item`.`network` IN ('%s', '%s', '%s', '')
80 AND NOT `item`.`global`";
82 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
83 intval($start_id), intval($end_id),
84 DBA::escape(NETWORK_DFRN), DBA::escape(NETWORK_DIASPORA), DBA::escape(NETWORK_OSTATUS));
86 Config::set("system", "post_update_version", 1194);
87 logger("Update is done", LOGGER_DEBUG);
90 Config::set("system", "post_update_1194_start", $r[0]["id"]);
91 $start_id = Config::get("system", "post_update_1194_start");
94 logger("Start ID: ".$start_id, LOGGER_DEBUG);
96 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
97 intval($start_id), intval($end_id),
98 DBA::escape(NETWORK_DFRN), DBA::escape(NETWORK_DIASPORA), DBA::escape(NETWORK_OSTATUS));
100 $pos_id = $r[0]["id"];
104 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
106 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
107 intval($start_id), intval($pos_id),
108 DBA::escape(NETWORK_DFRN), DBA::escape(NETWORK_DIASPORA), DBA::escape(NETWORK_OSTATUS));
110 logger("Done", LOGGER_DEBUG);
114 * @brief set the author-id and owner-id in all item entries
116 * This job has to be started multiple times until all entries are set.
117 * It isn't started in the update function since it would consume too much time and can be done in the background.
119 * @return bool "true" when the job is done
121 private static function update1198()
123 // Was the script completed?
124 if (Config::get("system", "post_update_version") >= 1198) {
128 logger("Start", LOGGER_DEBUG);
130 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
131 $fields = ['author-link', 'author-name', 'author-avatar', 'owner-link', 'owner-name', 'owner-avatar', 'network', 'uid'];
132 $r = DBA::select('item', $fields, ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
134 // Are there unfinished entries in the thread table?
135 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
136 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
137 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
138 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
140 if ($r && ($r[0]["total"] == 0)) {
141 Config::set("system", "post_update_version", 1198);
142 logger("Done", LOGGER_DEBUG);
146 // Update the thread table from the item table
147 $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
148 SET `thread`.`author-id` = `item`.`author-id`,
149 `thread`.`owner-id` = `item`.`owner-id`
150 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
151 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
153 logger("Updated threads", LOGGER_DEBUG);
154 if (DBA::isResult($r)) {
155 Config::set("system", "post_update_version", 1198);
156 logger("Done", LOGGER_DEBUG);
162 logger("Query done", LOGGER_DEBUG);
165 foreach ($r as $item) {
166 $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
167 $item_arr[$index] = ["author-link" => $item["author-link"],
168 "owner-link" => $item["owner-link"],
169 "uid" => $item["uid"]];
172 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
173 foreach ($item_arr as $item) {
174 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
175 'photo' => $item['author-avatar'], 'network' => $item['network']];
176 $author_id = Contact::getIdForURL($item["author-link"], 0, false, $default);
178 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
179 'photo' => $item['owner-avatar'], 'network' => $item['network']];
180 $owner_id = Contact::getIdForURL($item["owner-link"], 0, false, $default);
182 if ($author_id == 0) {
185 if ($owner_id == 0) {
188 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]);
191 logger("Updated items", LOGGER_DEBUG);
196 * @brief update the "last-item" field in the "self" contact
198 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
200 * @return bool "true" when the job is done
202 private static function update1206()
204 // Was the script completed?
205 if (Config::get("system", "post_update_version") >= 1206) {
209 logger("Start", LOGGER_DEBUG);
210 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
211 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
213 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
215 if (!DBA::isResult($r)) {
218 foreach ($r as $user) {
219 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
220 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
224 Config::set("system", "post_update_version", 1206);
225 logger("Done", LOGGER_DEBUG);
230 * @brief update the item related tables
232 * @return bool "true" when the job is done
234 private static function update1279()
236 // Was the script completed?
237 if (Config::get("system", "post_update_version") >= 1279) {
241 $id = Config::get("system", "post_update_version_1279_id", 0);
243 logger("Start from item " . $id, LOGGER_DEBUG);
245 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
246 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
247 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
248 'inform', 'postopts', 'icid']);
252 $condition = ["`id` > ?", $id];
253 $params = ['order' => ['id'], 'limit' => 10000];
254 $items = Item::select($fields, $condition, $params);
255 while ($item = Item::fetch($items)) {
258 if (empty($item['author-id'])) {
259 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
260 'photo' => $item['author-avatar'], 'network' => $item['network']];
262 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
265 if (empty($item['owner-id'])) {
266 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
267 'photo' => $item['owner-avatar'], 'network' => $item['network']];
269 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
272 if (empty($item['psid'])) {
273 $item['psid'] = PermissionSet::fetchIDForPost($item);
275 $item['allow_cid'] = null;
276 $item['allow_gid'] = null;
277 $item['deny_cid'] = null;
278 $item['deny_gid'] = null;
281 if ($item['post-type'] == 0) {
282 if (!empty($item['type']) && ($item['type'] == 'note')) {
283 $item['post-type'] = Item::PT_PERSONAL_NOTE;
284 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
285 $item['post-type'] = Item::PT_IMAGE;
286 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
287 $item['post-type'] = Item::PT_PAGE;
291 self::createLanguage($item);
293 if (!empty($item['icid']) && !empty($item['language'])) {
294 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
296 unset($item['language']);
298 Item::update($item, ['id' => $id]);
304 Config::set("system", "post_update_version_1279_id", $id);
306 logger("Processed rows: " . $rows . " - last processed item: " . $id, LOGGER_DEBUG);
308 if ($start_id == $id) {
309 // Set all deprecated fields to "null" if they contain an empty string
310 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
311 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
312 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
313 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
314 'rendered-hash', 'rendered-html'];
315 foreach ($nullfields as $field) {
316 $fields = [$field => null];
317 $condition = [$field => ''];
318 logger("Setting '" . $field . "' to null if empty.", LOGGER_DEBUG);
319 // Important: This has to be a "DBA::update", not a "Item::update"
320 DBA::update('item', $fields, $condition);
323 Config::set("system", "post_update_version", 1279);
324 logger("Done", LOGGER_DEBUG);
331 private static function createLanguage(&$item)
333 if (empty($item['postopts'])) {
337 $opts = explode(',', $item['postopts']);
341 foreach ($opts as $opt) {
342 if (strstr($opt, 'lang=')) {
343 $language = substr($opt, 5);
349 if (empty($language)) {
353 if (!empty($postopts)) {
354 $item['postopts'] = implode(',', $postopts);
356 $item['postopts'] = null;
359 $lang_pairs = explode(':', $language);
363 foreach ($lang_pairs as $pair) {
364 $lang_pair_arr = explode(';', $pair);
365 if (count($lang_pair_arr) == 2) {
366 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
370 $item['language'] = json_encode($lang_arr);
374 * @brief update item-uri data. Prerequisite for the next item structure update.
376 * @return bool "true" when the job is done
378 private static function update1281()
380 // Was the script completed?
381 if (Config::get("system", "post_update_version") >= 1281) {
385 $id = Config::get("system", "post_update_version_1281_id", 0);
387 logger("Start from item " . $id, LOGGER_DEBUG);
389 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
393 $condition = ["`id` > ?", $id];
394 $params = ['order' => ['id'], 'limit' => 10000];
395 $items = DBA::select('item', $fields, $condition, $params);
396 while ($item = DBA::fetch($items)) {
399 if (empty($item['uri'])) {
402 } elseif (empty($item['uri-id'])) {
403 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
406 if (empty($item['parent-uri'])) {
407 $item['parent-uri-id'] = $item['uri-id'];
408 } elseif (empty($item['parent-uri-id'])) {
409 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
412 // Very old items don't have this field
413 if (empty($item['thr-parent'])) {
414 $item['thr-parent-id'] = $item['parent-uri-id'];
415 } elseif (empty($item['thr-parent-id'])) {
416 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
420 unset($item['guid']);
422 unset($item['parent-uri']);
423 unset($item['thr-parent']);
425 DBA::update('item', $item, ['id' => $id]);
431 Config::set("system", "post_update_version_1281_id", $id);
433 logger("Processed rows: " . $rows . " - last processed item: " . $id, LOGGER_DEBUG);
435 if ($start_id == $id) {
436 logger("Updating item-uri in item-activity", LOGGER_DEBUG);
437 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");
439 logger("Updating item-uri in item-content", LOGGER_DEBUG);
440 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");
442 Config::set("system", "post_update_version", 1281);
443 logger("Done", LOGGER_DEBUG);