]> git.mxchange.org Git - friendica.git/blob - src/Database/PostUpdate.php
Group selection: Respect "pubmail" and ignore atchived or blocked contacts
[friendica.git] / src / Database / PostUpdate.php
1 <?php
2 /**
3  * @file src/Database/PostUpdate.php
4  */
5 namespace Friendica\Database;
6
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
15 /**
16  * Post update functions
17  */
18 class PostUpdate
19 {
20         /**
21          * @brief Calls the post update functions
22          */
23         public static function update()
24         {
25                 if (!self::update1194()) {
26                         return false;
27                 }
28                 if (!self::update1206()) {
29                         return false;
30                 }
31                 if (!self::update1279()) {
32                         return false;
33                 }
34                 if (!self::update1281()) {
35                         return false;
36                 }
37                 if (!self::update1297()) {
38                         return false;
39                 }
40                 if (!self::update1322()) {
41                         return false;
42                 }
43
44                 return true;
45         }
46
47         /**
48          * @brief Updates the "global" field in the item table
49          *
50          * @return bool "true" when the job is done
51          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
52          */
53         private static function update1194()
54         {
55                 // Was the script completed?
56                 if (Config::get("system", "post_update_version") >= 1194) {
57                         return true;
58                 }
59
60                 Logger::log("Start", Logger::DEBUG);
61
62                 $end_id = Config::get("system", "post_update_1194_end");
63                 if (!$end_id) {
64                         $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
65                         if ($r) {
66                                 Config::set("system", "post_update_1194_end", $r[0]["id"]);
67                                 $end_id = Config::get("system", "post_update_1194_end");
68                         }
69                 }
70
71                 Logger::log("End ID: ".$end_id, Logger::DEBUG);
72
73                 $start_id = Config::get("system", "post_update_1194_start");
74
75                 $query1 = "SELECT `item`.`id` FROM `item` ";
76
77                 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
78
79                 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
80                                 AND `item`.`visible` AND NOT `item`.`private`
81                                 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
82                                 AND `item`.`network` IN ('%s', '%s', '%s', '')
83                                 AND NOT `item`.`global`";
84
85                 $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 1",
86                         intval($start_id), intval($end_id),
87                         DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
88                 if (!$r) {
89                         Config::set("system", "post_update_version", 1194);
90                         Logger::log("Update is done", Logger::DEBUG);
91                         return true;
92                 } else {
93                         Config::set("system", "post_update_1194_start", $r[0]["id"]);
94                         $start_id = Config::get("system", "post_update_1194_start");
95                 }
96
97                 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
98
99                 $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 1000,1",
100                         intval($start_id), intval($end_id),
101                         DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
102                 if ($r) {
103                         $pos_id = $r[0]["id"];
104                 } else {
105                         $pos_id = $end_id;
106                 }
107                 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
108
109                 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
110                         intval($start_id), intval($pos_id),
111                         DBA::escape(Protocol::DFRN), DBA::escape(Protocol::DIASPORA), DBA::escape(Protocol::OSTATUS));
112
113                 Logger::log("Done", Logger::DEBUG);
114         }
115
116         /**
117          * @brief update the "last-item" field in the "self" contact
118          *
119          * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
120          *
121          * @return bool "true" when the job is done
122          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
123          */
124         private static function update1206()
125         {
126                 // Was the script completed?
127                 if (Config::get("system", "post_update_version") >= 1206) {
128                         return true;
129                 }
130
131                 Logger::log("Start", Logger::DEBUG);
132                 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
133                         (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
134                         FROM `user`
135                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
136
137                 if (!DBA::isResult($r)) {
138                         return false;
139                 }
140                 foreach ($r as $user) {
141                         if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
142                                 DBA::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
143                         }
144                 }
145
146                 Config::set("system", "post_update_version", 1206);
147                 Logger::log("Done", Logger::DEBUG);
148                 return true;
149         }
150
151         /**
152          * @brief update the item related tables
153          *
154          * @return bool "true" when the job is done
155          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
156          * @throws \ImagickException
157          */
158         private static function update1279()
159         {
160                 // Was the script completed?
161                 if (Config::get("system", "post_update_version") >= 1279) {
162                         return true;
163                 }
164
165                 $id = Config::get("system", "post_update_version_1279_id", 0);
166
167                 Logger::log("Start from item " . $id, Logger::DEBUG);
168
169                 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
170                         'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id',
171                         'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'psid', 'post-type', 'bookmark', 'type',
172                         'inform', 'postopts', 'icid']);
173
174                 $start_id = $id;
175                 $rows = 0;
176                 $condition = ["`id` > ?", $id];
177                 $params = ['order' => ['id'], 'limit' => 10000];
178                 $items = Item::select($fields, $condition, $params);
179
180                 if (DBA::errorNo() != 0) {
181                         Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
182                         return false;
183                 }
184
185                 while ($item = Item::fetch($items)) {
186                         $id = $item['id'];
187
188                         if (empty($item['author-id'])) {
189                                 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
190                                         'photo' => $item['author-avatar'], 'network' => $item['network']];
191
192                                 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
193                         }
194
195                         if (empty($item['owner-id'])) {
196                                 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
197                                         'photo' => $item['owner-avatar'], 'network' => $item['network']];
198
199                                 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
200                         }
201
202                         if (empty($item['psid'])) {
203                                 $item['psid'] = PermissionSet::fetchIDForPost($item);
204                         } else {
205                                 $item['allow_cid'] = null;
206                                 $item['allow_gid'] = null;
207                                 $item['deny_cid'] = null;
208                                 $item['deny_gid'] = null;
209                         }
210
211                         if ($item['post-type'] == 0) {
212                                 if (!empty($item['type']) && ($item['type'] == 'note')) {
213                                         $item['post-type'] = Item::PT_PERSONAL_NOTE;
214                                 } elseif (!empty($item['type']) && ($item['type'] == 'photo')) {
215                                         $item['post-type'] = Item::PT_IMAGE;
216                                 } elseif (!empty($item['bookmark']) && $item['bookmark']) {
217                                         $item['post-type'] = Item::PT_PAGE;
218                                 }
219                         }
220
221                         self::createLanguage($item);
222
223                         if (!empty($item['icid']) && !empty($item['language'])) {
224                                 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
225                         }
226                         unset($item['language']);
227
228                         Item::update($item, ['id' => $id]);
229
230                         ++$rows;
231                 }
232                 DBA::close($items);
233
234                 Config::set("system", "post_update_version_1279_id", $id);
235
236                 Logger::log("Processed rows: " . $rows . " - last processed item:  " . $id, Logger::DEBUG);
237
238                 if ($start_id == $id) {
239                         // Set all deprecated fields to "null" if they contain an empty string
240                         $nullfields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'postopts', 'inform', 'type',
241                                 'bookmark', 'file', 'location', 'coord', 'tag', 'plink', 'title', 'content-warning',
242                                 'body', 'app', 'verb', 'object-type', 'object', 'target-type', 'target',
243                                 'author-name', 'author-link', 'author-avatar', 'owner-name', 'owner-link', 'owner-avatar',
244                                 'rendered-hash', 'rendered-html'];
245                         foreach ($nullfields as $field) {
246                                 $fields = [$field => null];
247                                 $condition = [$field => ''];
248                                 Logger::log("Setting '" . $field . "' to null if empty.", Logger::DEBUG);
249                                 // Important: This has to be a "DBA::update", not a "Item::update"
250                                 DBA::update('item', $fields, $condition);
251                         }
252
253                         Config::set("system", "post_update_version", 1279);
254                         Logger::log("Done", Logger::DEBUG);
255                         return true;
256                 }
257
258                 return false;
259         }
260
261         private static function createLanguage(&$item)
262         {
263                 if (empty($item['postopts'])) {
264                         return;
265                 }
266
267                 $opts = explode(',', $item['postopts']);
268
269                 $postopts = [];
270
271                 foreach ($opts as $opt) {
272                         if (strstr($opt, 'lang=')) {
273                                 $language = substr($opt, 5);
274                         } else {
275                                 $postopts[] = $opt;
276                         }
277                 }
278
279                 if (empty($language)) {
280                         return;
281                 }
282
283                 if (!empty($postopts)) {
284                         $item['postopts'] = implode(',', $postopts);
285                 } else {
286                         $item['postopts'] = null;
287                 }
288
289                 $lang_pairs = explode(':', $language);
290
291                 $lang_arr = [];
292
293                 foreach ($lang_pairs as $pair) {
294                         $lang_pair_arr = explode(';', $pair);
295                         if (count($lang_pair_arr) == 2) {
296                                 $lang_arr[$lang_pair_arr[0]] = $lang_pair_arr[1];
297                         }
298                 }
299
300                 $item['language'] = json_encode($lang_arr);
301         }
302
303         /**
304          * @brief update item-uri data. Prerequisite for the next item structure update.
305          *
306          * @return bool "true" when the job is done
307          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
308          */
309         private static function update1281()
310         {
311                 // Was the script completed?
312                 if (Config::get("system", "post_update_version") >= 1281) {
313                         return true;
314                 }
315
316                 $id = Config::get("system", "post_update_version_1281_id", 0);
317
318                 Logger::log("Start from item " . $id, Logger::DEBUG);
319
320                 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
321
322                 $start_id = $id;
323                 $rows = 0;
324                 $condition = ["`id` > ?", $id];
325                 $params = ['order' => ['id'], 'limit' => 10000];
326                 $items = DBA::select('item', $fields, $condition, $params);
327
328                 if (DBA::errorNo() != 0) {
329                         Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
330                         return false;
331                 }
332
333                 while ($item = DBA::fetch($items)) {
334                         $id = $item['id'];
335
336                         if (empty($item['uri'])) {
337                                 // Should not happen
338                                 continue;
339                         } elseif (empty($item['uri-id'])) {
340                                 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
341                         }
342
343                         if (empty($item['parent-uri'])) {
344                                 $item['parent-uri-id'] = $item['uri-id'];
345                         } elseif (empty($item['parent-uri-id'])) {
346                                 $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
347                         }
348
349                         // Very old items don't have this field
350                         if (empty($item['thr-parent'])) {
351                                 $item['thr-parent-id'] = $item['parent-uri-id'];
352                         } elseif (empty($item['thr-parent-id'])) {
353                                 $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
354                         }
355
356                         unset($item['id']);
357                         unset($item['guid']);
358                         unset($item['uri']);
359                         unset($item['parent-uri']);
360                         unset($item['thr-parent']);
361
362                         DBA::update('item', $item, ['id' => $id]);
363
364                         ++$rows;
365                 }
366                 DBA::close($items);
367
368                 Config::set("system", "post_update_version_1281_id", $id);
369
370                 Logger::log("Processed rows: " . $rows . " - last processed item:  " . $id, Logger::DEBUG);
371
372                 if ($start_id == $id) {
373                         Logger::log("Updating item-uri in item-activity", Logger::DEBUG);
374                         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");
375
376                         Logger::log("Updating item-uri in item-content", Logger::DEBUG);
377                         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");
378
379                         Config::set("system", "post_update_version", 1281);
380                         Logger::log("Done", Logger::DEBUG);
381                         return true;
382                 }
383
384                 return false;
385         }
386
387         /**
388          * Set the delivery queue count to a negative value for all items preceding the feature.
389          *
390          * @return bool "true" when the job is done
391          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
392          */
393         private static function update1297()
394         {
395                 // Was the script completed?
396                 if (Config::get('system', 'post_update_version') >= 1297) {
397                         return true;
398                 }
399
400                 $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
401                 $max_iid = $max_item_delivery_data['iid'];
402
403                 Logger::info('Start update1297 with max iid: ' . $max_iid);
404
405                 $condition = ['`queue_count` = 0 AND `iid` < ?', $max_iid];
406
407                 DBA::update('item-delivery-data', ['queue_count' => -1], $condition);
408
409                 if (DBA::errorNo() != 0) {
410                         Logger::error('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
411                         return false;
412                 }
413
414                 Logger::info('Processed rows: ' . DBA::affectedRows());
415
416                 Config::set('system', 'post_update_version', 1297);
417
418                 Logger::info('Done');
419
420                 return true;
421         }
422         /**
423          * Remove contact duplicates
424          *
425          * @return bool "true" when the job is done
426          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
427          */
428         private static function update1322()
429         {
430                 // Was the script completed?
431                 if (Config::get('system', 'post_update_version') >= 1322) {
432                         return true;
433                 }
434
435                 Logger::info('Start');
436
437                 $contacts = DBA::p("SELECT `nurl`, `uid` FROM `contact`
438                         WHERE EXISTS (SELECT `nurl` FROM `contact` AS `c2`
439                                 WHERE `c2`.`nurl` = `contact`.`nurl` AND `c2`.`id` != `contact`.`id` AND `c2`.`uid` = `contact`.`uid` AND `c2`.`network` IN (?, ?, ?) AND NOT `deleted`)
440                         AND (`network` IN (?, ?, ?) OR (`uid` = ?)) AND NOT `deleted` GROUP BY `nurl`, `uid`",
441                         Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB,
442                         Protocol::DIASPORA, Protocol::OSTATUS, Protocol::ACTIVITYPUB, 0);
443
444                 while ($contact = DBA::fetch($contacts)) {
445                         Logger::info('Remove duplicates', ['nurl' => $contact['nurl'], 'uid' => $contact['uid']]);
446                         Contact::removeDuplicates($contact['nurl'], $contact['uid']);
447                 }
448
449                 DBA::close($contact);
450                 Config::set('system', 'post_update_version', 1322);
451
452                 Logger::info('Done');
453
454                 return true;
455         }
456 }