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;
16 require_once 'include/dba.php';
19 * Post update functions
24 * @brief Calls the post update functions
26 public static function update()
28 if (!self::update1194()) {
31 if (!self::update1206()) {
34 if (!self::update1279()) {
37 if (!self::update1281()) {
45 * @brief Updates the "global" field in the item table
47 * @return bool "true" when the job is done
49 private static function update1194()
51 // Was the script completed?
52 if (Config::get("system", "post_update_version") >= 1194) {
56 Logger::log("Start", Logger::DEBUG);
58 $end_id = Config::get("system", "post_update_1194_end");
60 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
62 Config::set("system", "post_update_1194_end", $r[0]["id"]);
63 $end_id = Config::get("system", "post_update_1194_end");
67 Logger::log("End ID: ".$end_id, Logger::DEBUG);
69 $start_id = Config::get("system", "post_update_1194_start");
71 $query1 = "SELECT `item`.`id` FROM `item` ";
73 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
75 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
76 AND `item`.`visible` AND NOT `item`.`private`
77 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
78 AND `item`.`network` IN ('%s', '%s', '%s', '')
79 AND NOT `item`.`global`";
81 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1",
82 intval($start_id), intval($end_id),
83 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
85 Config::set("system", "post_update_version", 1194);
86 Logger::log("Update is done", Logger::DEBUG);
89 Config::set("system", "post_update_1194_start", $r[0]["id"]);
90 $start_id = Config::get("system", "post_update_1194_start");
93 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
95 $r = q($query1.$query2.$query3." ORDER BY `item`.`id` LIMIT 1000,1",
96 intval($start_id), intval($end_id),
97 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
99 $pos_id = $r[0]["id"];
103 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
105 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
106 intval($start_id), intval($pos_id),
107 DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
109 Logger::log("Done", Logger::DEBUG);
113 * @brief update the "last-item" field in the "self" contact
115 * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
117 * @return bool "true" when the job is done
119 private static function update1206()
121 // Was the script completed?
122 if (Config::get("system", "post_update_version") >= 1206) {
126 Logger::log("Start", Logger::DEBUG);
127 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
128 (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
130 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
132 if (!DBA::isResult($r)) {
135 foreach ($r as $user) {
136 if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
137 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
141 Config::set("system", "post_update_version", 1206);
142 Logger::log("Done", Logger::DEBUG);
147 * @brief update the item related tables
149 * @return bool "true" when the job is done
151 private static function update1279()
153 // Was the script completed?
154 if (Config::get("system", "post_update_version") >= 1279) {
158 $id = Config::get("system", "post_update_version_1279_id", 0);
160 Logger::log("Start from item " . $id, Logger::DEBUG);
162 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
163 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
164 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
165 'inform', 'postopts', 'icid']);
169 $condition = ["`id` > ?", $id];
170 $params = ['order' => ['id'], 'limit' => 10000];
171 $items = Item::select($fields, $condition, $params);
173 if (DBA::errorNo() != 0) {
174 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
178 while ($item = Item::fetch($items)) {
181 if (empty($item['author-id'])) {
182 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
183 'photo' => $item['author-avatar'], 'network' => $item['network']];
185 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
188 if (empty($item['owner-id'])) {
189 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
190 'photo' => $item['owner-avatar'], 'network' => $item['network']];
192 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
195 if (empty($item['psid'])) {
196 $item['psid'] = PermissionSet::fetchIDForPost($item);
198 $item['allow_cid'] = null;
199 $item['allow_gid'] = null;
200 $item['deny_cid'] = null;
201 $item['deny_gid'] = null;
204 if ($item['post-type'] == 0) {
205 if (!empty($item['type']) && ($item['type'] == 'note')) {
206 $item['post-type'] = Item::PT_PERSONAL_NOTE;
207 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
208 $item['post-type'] = Item::PT_IMAGE;
209 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
210 $item['post-type'] = Item::PT_PAGE;
214 self::createLanguage($item);
216 if (!empty($item['icid']) && !empty($item['language'])) {
217 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
219 unset($item['language']);
221 Item::update($item, ['id' => $id]);
227 Config::set("system", "post_update_version_1279_id", $id);
229 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
231 if ($start_id == $id) {
232 // Set all deprecated fields to "null" if they contain an empty string
233 $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
234 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
235 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
236 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
237 'rendered-hash', 'rendered-html'];
238 foreach ($nullfields as $field) {
239 $fields = [$field => null];
240 $condition = [$field => ''];
241 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
242 // Important: This has to be a "DBA::update", not a "Item::update"
243 DBA::update('item', $fields, $condition);
246 Config::set("system", "post_update_version", 1279);
247 Logger::log("Done", Logger::DEBUG);
254 private static function createLanguage(&$item)
256 if (empty($item['postopts'])) {
260 $opts = explode(',', $item['postopts']);
264 foreach ($opts as $opt) {
265 if (strstr($opt, 'lang=')) {
266 $language = substr($opt, 5);
272 if (empty($language)) {
276 if (!empty($postopts)) {
277 $item['postopts'] = implode(',', $postopts);
279 $item['postopts'] = null;
282 $lang_pairs = explode(':', $language);
286 foreach ($lang_pairs as $pair) {
287 $lang_pair_arr = explode(';', $pair);
288 if (count($lang_pair_arr) == 2) {
289 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
293 $item['language'] = json_encode($lang_arr);
297 * @brief update item-uri data. Prerequisite for the next item structure update.
299 * @return bool "true" when the job is done
301 private static function update1281()
303 // Was the script completed?
304 if (Config::get("system", "post_update_version") >= 1281) {
308 $id = Config::get("system", "post_update_version_1281_id", 0);
310 Logger::log("Start from item " . $id, Logger::DEBUG);
312 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
316 $condition = ["`id` > ?", $id];
317 $params = ['order' => ['id'], 'limit' => 10000];
318 $items = DBA::select('item', $fields, $condition, $params);
320 if (DBA::errorNo() != 0) {
321 Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
325 while ($item = DBA::fetch($items)) {
328 if (empty($item['uri'])) {
331 } elseif (empty($item['uri-id'])) {
332 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
335 if (empty($item['parent-uri'])) {
336 $item['parent-uri-id'] = $item['uri-id'];
337 } elseif (empty($item['parent-uri-id'])) {
338 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
341 // Very old items don't have this field
342 if (empty($item['thr-parent'])) {
343 $item['thr-parent-id'] = $item['parent-uri-id'];
344 } elseif (empty($item['thr-parent-id'])) {
345 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
349 unset($item['guid']);
351 unset($item['parent-uri']);
352 unset($item['thr-parent']);
354 DBA::update('item', $item, ['id' => $id]);
360 Config::set("system", "post_update_version_1281_id", $id);
362 Logger::log("Processed rows: " . $rows . " - last processed item: " . $id, Logger::DEBUG);
364 if ($start_id == $id) {
365 Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
366 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");
368 Logger::log("Updating item-uri in item-content", Logger::DEBUG);
369 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");
371 Config::set("system", "post_update_version", 1281);
372 Logger::log("Done", Logger::DEBUG);