]> git.mxchange.org Git - friendica.git/blob - src/Database/PostUpdate.php
Merge pull request #6209 from MrPetovan/task/move-config-to-php-array
[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 use Friendica\Database\DBA;
15
16 /**
17  * Post update functions
18  */
19 class PostUpdate
20 {
21         /**
22          * @brief Calls the post update functions
23          */
24         public static function update()
25         {
26                 if (!self::update1194()) {
27                         return false;
28                 }
29                 if (!self::update1206()) {
30                         return false;
31                 }
32                 if (!self::update1279()) {
33                         return false;
34                 }
35                 if (!self::update1281()) {
36                         return false;
37                 }
38
39                 return true;
40         }
41
42         /**
43          * @brief Updates the "global" field in the item table
44          *
45          * @return bool "true" when the job is done
46          */
47         private static function update1194()
48         {
49                 // Was the script completed?
50                 if (Config::get("system", "post_update_version") >= 1194) {
51                         return true;
52                 }
53
54                 Logger::log("Start", Logger::DEBUG);
55
56                 $end_id = Config::get("system", "post_update_1194_end");
57                 if (!$end_id) {
58                         $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
59                         if ($r) {
60                                 Config::set("system", "post_update_1194_end", $r[0]["id"]);
61                                 $end_id = Config::get("system", "post_update_1194_end");
62                         }
63                 }
64
65                 Logger::log("End ID: ".$end_id, Logger::DEBUG);
66
67                 $start_id = Config::get("system", "post_update_1194_start");
68
69                 $query1 = "SELECT `item`.`id` FROM `item` ";
70
71                 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
72
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`";
78
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));
82                 if (!$r) {
83                         Config::set("system", "post_update_version", 1194);
84                         Logger::log("Update is done", Logger::DEBUG);
85                         return true;
86                 } else {
87                         Config::set("system", "post_update_1194_start", $r[0]["id"]);
88                         $start_id = Config::get("system", "post_update_1194_start");
89                 }
90
91                 Logger::log("Start ID: ".$start_id, Logger::DEBUG);
92
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));
96                 if ($r) {
97                         $pos_id = $r[0]["id"];
98                 } else {
99                         $pos_id = $end_id;
100                 }
101                 Logger::log("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, Logger::DEBUG);
102
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));
106
107                 Logger::log("Done", Logger::DEBUG);
108         }
109
110         /**
111          * @brief update the "last-item" field in the "self" contact
112          *
113          * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
114          *
115          * @return bool "true" when the job is done
116          */
117         private static function update1206()
118         {
119                 // Was the script completed?
120                 if (Config::get("system", "post_update_version") >= 1206) {
121                         return true;
122                 }
123
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`
127                         FROM `user`
128                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
129
130                 if (!DBA::isResult($r)) {
131                         return false;
132                 }
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']]);
136                         }
137                 }
138
139                 Config::set("system", "post_update_version", 1206);
140                 Logger::log("Done", Logger::DEBUG);
141                 return true;
142         }
143
144         /**
145          * @brief update the item related tables
146          *
147          * @return bool "true" when the job is done
148          */
149         private static function update1279()
150         {
151                 // Was the script completed?
152                 if (Config::get("system", "post_update_version") >= 1279) {
153                         return true;
154                 }
155
156                 $id = Config::get("system", "post_update_version_1279_id", 0);
157
158                 Logger::log("Start from item " . $id, Logger::DEBUG);
159
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']);
164
165                 $start_id = $id;
166                 $rows = 0;
167                 $condition = ["`id` > ?", $id];
168                 $params = ['order' => ['id'], 'limit' => 10000];
169                 $items = Item::select($fields, $condition, $params);
170
171                 if (DBA::errorNo() != 0) {
172                         Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
173                         return false;
174                 }
175
176                 while ($item = Item::fetch($items)) {
177                         $id = $item['id'];
178
179                         if (empty($item['author-id'])) {
180                                 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
181                                         'photo' => $item['author-avatar'], 'network' => $item['network']];
182
183                                 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
184                         }
185
186                         if (empty($item['owner-id'])) {
187                                 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
188                                         'photo' => $item['owner-avatar'], 'network' => $item['network']];
189
190                                 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
191                         }
192
193                         if (empty($item['psid'])) {
194                                 $item['psid'] = PermissionSet::fetchIDForPost($item);
195                         } else {
196                                 $item['allow_cid'] = null;
197                                 $item['allow_gid'] = null;
198                                 $item['deny_cid'] = null;
199                                 $item['deny_gid'] = null;
200                         }
201
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;
209                                 }
210                         }
211
212                         self::createLanguage($item);
213
214                         if (!empty($item['icid']) && !empty($item['language'])) {
215                                 DBA::update('item-content', ['language' => $item['language']], ['id' => $item['icid']]);
216                         }
217                         unset($item['language']);
218
219                         Item::update($item, ['id' => $id]);
220
221                         ++$rows;
222                 }
223                 DBA::close($items);
224
225                 Config::set("system", "post_update_version_1279_id", $id);
226
227                 Logger::log("Processed rows: " . $rows . " - last processed item:  " . $id, Logger::DEBUG);
228
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);
242                         }
243
244                         Config::set("system", "post_update_version", 1279);
245                         Logger::log("Done", Logger::DEBUG);
246                         return true;
247                 }
248
249                 return false;
250         }
251
252         private static function createLanguage(&$item)
253         {
254                 if (empty($item['postopts'])) {
255                         return;
256                 }
257
258                 $opts = explode(',', $item['postopts']);
259
260                 $postopts = [];
261
262                 foreach ($opts as $opt) {
263                         if (strstr($opt, 'lang=')) {
264                                 $language = substr($opt, 5);
265                         } else {
266                                 $postopts[] = $opt;
267                         }
268                 }
269
270                 if (empty($language)) {
271                         return;
272                 }
273
274                 if (!empty($postopts)) {
275                         $item['postopts'] = implode(',', $postopts);
276                 } else {
277                         $item['postopts'] = null;
278                 }
279
280                 $lang_pairs = explode(':', $language);
281
282                 $lang_arr = [];
283
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];
288                         }
289                 }
290
291                 $item['language'] = json_encode($lang_arr);
292         }
293
294         /**
295          * @brief update item-uri data. Prerequisite for the next item structure update.
296          *
297          * @return bool "true" when the job is done
298          */
299         private static function update1281()
300         {
301                 // Was the script completed?
302                 if (Config::get("system", "post_update_version") >= 1281) {
303                         return true;
304                 }
305
306                 $id = Config::get("system", "post_update_version_1281_id", 0);
307
308                 Logger::log("Start from item " . $id, Logger::DEBUG);
309
310                 $fields = ['id', 'guid', 'uri', 'uri-id', 'parent-uri', 'parent-uri-id', 'thr-parent', 'thr-parent-id'];
311
312                 $start_id = $id;
313                 $rows = 0;
314                 $condition = ["`id` > ?", $id];
315                 $params = ['order' => ['id'], 'limit' => 10000];
316                 $items = DBA::select('item', $fields, $condition, $params);
317
318                 if (DBA::errorNo() != 0) {
319                         Logger::log('Database error ' . DBA::errorNo() . ':' . DBA::errorMessage());
320                         return false;
321                 }
322
323                 while ($item = DBA::fetch($items)) {
324                         $id = $item['id'];
325
326                         if (empty($item['uri'])) {
327                                 // Should not happen
328                                 continue;
329                         } elseif (empty($item['uri-id'])) {
330                                 $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
331                         }
332
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']);
337                         }
338
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']);
344                         }
345
346                         unset($item['id']);
347                         unset($item['guid']);
348                         unset($item['uri']);
349                         unset($item['parent-uri']);
350                         unset($item['thr-parent']);
351
352                         DBA::update('item', $item, ['id' => $id]);
353
354                         ++$rows;
355                 }
356                 DBA::close($items);
357
358                 Config::set("system", "post_update_version_1281_id", $id);
359
360                 Logger::log("Processed rows: " . $rows . " - last processed item:  " . $id, Logger::DEBUG);
361
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");
365
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");
368
369                         Config::set("system", "post_update_version", 1281);
370                         Logger::log("Done", Logger::DEBUG);
371                         return true;
372                 }
373
374                 return false;
375         }
376 }