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;
14 use Friendica\Database\DBA;
17 * Post update functions
22 * @brief 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()) {
43 * @brief Updates the "global" field in the item table
45 * @return bool "true" when the job is done
47 private static function update1194()
49 // Was the script completed?
50 if (Config::get("system", "post_update_version") >= 1194) {
54 Logger::log("Start", Logger::DEBUG);
56 $end_id = Config::get("system", "post_update_1194_end");
58 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
60 Config::set("system", "post_update_1194_end", $r[0]["id"]);
61 $end_id = Config::get("system", "post_update_1194_end");
65 Logger::log("End ID: ".$end_id, Logger::DEBUG);
67 $start_id = Config::get("system", "post_update_1194_start");
69 $query1 = "SELECT `item`.`id` FROM `item` ";
71 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
73 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
74 AND `item`.`visible` AND NOT `item`.`private`
75 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
76 AND `item`.`network` IN ('%s', '%s', '%s', '')
77 AND NOT `item`.`global`";
79 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
80 intval($start_id), intval($end_id),
81 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
83 Config::set("system", "post_update_version", 1194);
84 Logger::log("Update is done", Logger::DEBUG);
87 Config::set("system", "post_update_1194_start", $r[0]["id"]);
88 $start_id = Config::get("system", "post_update_1194_start");
91 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
93 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
94 intval($start_id), intval($end_id),
95 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
97 $pos_id = $r[0]["id"];
101 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
103 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
104 intval($start_id), intval($pos_id),
105 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
107 Logger::log("Done", Logger::DEBUG);
111 * @brief update the "last-item" field in the "self" contact
113 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
115 * @return bool "true" when the job is done
117 private static function update1206()
119 // Was the script completed?
120 if (Config::get("system", "post_update_version") >= 1206) {
124 Logger::log("Start", Logger::DEBUG);
125 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
126 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
128 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
130 if (!DBA::isResult($r)) {
133 foreach ($r as $user) {
134 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
135 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
139 Config::set("system", "post_update_version", 1206);
140 Logger::log("Done", Logger::DEBUG);
145 * @brief update the item related tables
147 * @return bool "true" when the job is done
149 private static function update1279()
151 // Was the script completed?
152 if (Config::get("system", "post_update_version") >= 1279) {
156 $id = Config::get("system", "post_update_version_1279_id", 0);
158 Logger::log("Start from item " . $id, Logger::DEBUG);
160 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
161 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
162 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
163 'inform', 'postopts', 'icid']);
167 $condition = ["`id` > ?", $id];
168 $params = ['order' => ['id'], 'limit' => 10000];
169 $items = Item::select($fields, $condition, $params);
171 if (DBA::errorNo() != 0) {
172 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
176 while ($item = Item::fetch($items)) {
179 if (empty($item['author-id'])) {
180 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
181 'photo' => $item['author-avatar'], 'network' => $item['network']];
183 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
186 if (empty($item['owner-id'])) {
187 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
188 'photo' => $item['owner-avatar'], 'network' => $item['network']];
190 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
193 if (empty($item['psid'])) {
194 $item['psid'] = PermissionSet::fetchIDForPost($item);
196 $item['allow_cid'] = null;
197 $item['allow_gid'] = null;
198 $item['deny_cid'] = null;
199 $item['deny_gid'] = null;
202 if ($item['post-type'] == 0) {
203 if (!empty($item['type']) && ($item['type'] == 'note')) {
204 $item['post-type'] = Item::PT_PERSONAL_NOTE;
205 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
206 $item['post-type'] = Item::PT_IMAGE;
207 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
208 $item['post-type'] = Item::PT_PAGE;
212 self::createLanguage($item);
214 if (!empty($item['icid']) && !empty($item['language'])) {
215 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
217 unset($item['language']);
219 Item::update($item, ['id' => $id]);
225 Config::set("system", "post_update_version_1279_id", $id);
227 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
229 if ($start_id == $id) {
230 // Set all deprecated fields to "null" if they contain an empty string
231 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
232 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
233 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
234 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
235 'rendered-hash', 'rendered-html'];
236 foreach ($nullfields as $field) {
237 $fields = [$field => null];
238 $condition = [$field => ''];
239 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
240 // Important: This has to be a "DBA::update", not a "Item::update"
241 DBA::update('item', $fields, $condition);
244 Config::set("system", "post_update_version", 1279);
245 Logger::log("Done", Logger::DEBUG);
252 private static function createLanguage(&$item)
254 if (empty($item['postopts'])) {
258 $opts = explode(',', $item['postopts']);
262 foreach ($opts as $opt) {
263 if (strstr($opt, 'lang=')) {
264 $language = substr($opt, 5);
270 if (empty($language)) {
274 if (!empty($postopts)) {
275 $item['postopts'] = implode(',', $postopts);
277 $item['postopts'] = null;
280 $lang_pairs = explode(':', $language);
284 foreach ($lang_pairs as $pair) {
285 $lang_pair_arr = explode(';', $pair);
286 if (count($lang_pair_arr) == 2) {
287 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
291 $item['language'] = json_encode($lang_arr);
295 * @brief update item-uri data. Prerequisite for the next item structure update.
297 * @return bool "true" when the job is done
299 private static function update1281()
301 // Was the script completed?
302 if (Config::get("system", "post_update_version") >= 1281) {
306 $id = Config::get("system", "post_update_version_1281_id", 0);
308 Logger::log("Start from item " . $id, Logger::DEBUG);
310 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
314 $condition = ["`id` > ?", $id];
315 $params = ['order' => ['id'], 'limit' => 10000];
316 $items = DBA::select('item', $fields, $condition, $params);
318 if (DBA::errorNo() != 0) {
319 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
323 while ($item = DBA::fetch($items)) {
326 if (empty($item['uri'])) {
329 } elseif (empty($item['uri-id'])) {
330 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
333 if (empty($item['parent-uri'])) {
334 $item['parent-uri-id'] = $item['uri-id'];
335 } elseif (empty($item['parent-uri-id'])) {
336 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
339 // Very old items don't have this field
340 if (empty($item['thr-parent'])) {
341 $item['thr-parent-id'] = $item['parent-uri-id'];
342 } elseif (empty($item['thr-parent-id'])) {
343 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
347 unset($item['guid']);
349 unset($item['parent-uri']);
350 unset($item['thr-parent']);
352 DBA::update('item', $item, ['id' => $id]);
358 Config::set("system", "post_update_version_1281_id", $id);
360 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
362 if ($start_id == $id) {
363 Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
364 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");
366 Logger::log("Updating item-uri in item-content", Logger::DEBUG);
367 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");
369 Config::set("system", "post_update_version", 1281);
370 Logger::log("Done", Logger::DEBUG);