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