]> git.mxchange.org Git - friendica.git/blob - src/Database/PostUpdate.php
88bec31b74f42d5e4fbe0e0c75b31193b4e17a20
[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\Database\DBM;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Item;
11 use dba;
12
13 require_once 'include/dba.php';
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;
27                 }
28                 if (!self::update1198()) {
29                         return;
30                 }
31                 if (!self::update1206()) {
32                         return;
33                 }
34                 if (!self::update1274()) {
35                         return;
36                 }
37                 if (!self::update1275()) {
38                         return;
39                 }
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("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("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 `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
78                                 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
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                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
84                 if (!$r) {
85                         Config::set("system", "post_update_version", 1194);
86                         logger("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("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                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
98                 if ($r) {
99                         $pos_id = $r[0]["id"];
100                 } else {
101                         $pos_id = $end_id;
102                 }
103                 logger("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                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
108
109                 logger("Done", LOGGER_DEBUG);
110         }
111
112         /**
113          * @brief set the author-id and owner-id in all item entries
114          *
115          * This job has to be started multiple times until all entries are set.
116          * It isn't started in the update function since it would consume too much time and can be done in the background.
117          *
118          * @return bool "true" when the job is done
119          */
120         private static function update1198()
121         {
122                 // Was the script completed?
123                 if (Config::get("system", "post_update_version") >= 1198) {
124                         return true;
125                 }
126
127                 logger("Start", LOGGER_DEBUG);
128
129                 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
130                 $fields = ['author-link', 'author-name', 'author-avatar', 'owner-link', 'owner-name', 'owner-avatar', 'network', 'uid'];
131                 $r = dba::select('item', $fields, ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
132                 if (!$r) {
133                         // Are there unfinished entries in the thread table?
134                         $r = q("SELECT COUNT(*) AS `total` FROM `thread`
135                                 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
136                                 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
137                                         (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
138
139                         if ($r && ($r[0]["total"] == 0)) {
140                                 Config::set("system", "post_update_version", 1198);
141                                 logger("Done", LOGGER_DEBUG);
142                                 return true;
143                         }
144
145                         // Update the thread table from the item table
146                         $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
147                                         SET `thread`.`author-id` = `item`.`author-id`,
148                                         `thread`.`owner-id` = `item`.`owner-id`
149                                 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
150                                         (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
151
152                         logger("Updated threads", LOGGER_DEBUG);
153                         if (DBM::is_result($r)) {
154                                 Config::set("system", "post_update_version", 1198);
155                                 logger("Done", LOGGER_DEBUG);
156                                 return true;
157                         }
158                         return false;
159                 }
160
161                 logger("Query done", LOGGER_DEBUG);
162
163                 $item_arr = [];
164                 foreach ($r as $item) {
165                         $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
166                         $item_arr[$index] = ["author-link" => $item["author-link"],
167                                                         "owner-link" => $item["owner-link"],
168                                                         "uid" => $item["uid"]];
169                 }
170
171                 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
172                 foreach ($item_arr as $item) {
173                         $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
174                                 'photo' => $item['author-avatar'], 'network' => $item['network']];
175                         $author_id = Contact::getIdForURL($item["author-link"], 0, false, $default);
176
177                         $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
178                                 'photo' => $item['owner-avatar'], 'network' => $item['network']];
179                         $owner_id = Contact::getIdForURL($item["owner-link"], 0, false, $default);
180
181                         if ($author_id == 0) {
182                                 $author_id = -1;
183                         }
184                         if ($owner_id == 0) {
185                                 $owner_id = -1;
186                         }
187                         dba::update('item', ['author-id' => $author_id, 'owner-id' => $owner_id], ['uid' => $item['uid'], 'author-link' => $item['author-link'], 'owner-link' => $item['owner-link'], 'author-id' => 0, 'owner-id' => 0]);
188                 }
189
190                 logger("Updated items", LOGGER_DEBUG);
191                 return false;
192         }
193
194         /**
195          * @brief update the "last-item" field in the "self" contact
196          *
197          * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
198          *
199          * @return bool "true" when the job is done
200          */
201         private static function update1206()
202         {
203                 // Was the script completed?
204                 if (Config::get("system", "post_update_version") >= 1206) {
205                         return true;
206                 }
207
208                 logger("Start", LOGGER_DEBUG);
209                 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
210                         (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
211                         FROM `user`
212                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
213
214                 if (!DBM::is_result($r)) {
215                         return false;
216                 }
217                 foreach ($r as $user) {
218                         if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
219                                 dba::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
220                         }
221                 }
222
223                 Config::set("system", "post_update_version", 1206);
224                 logger("Done", LOGGER_DEBUG);
225                 return true;
226         }
227
228         /**
229          * @brief update the "item-content" table
230          *
231          * @return bool "true" when the job is done
232          */
233         private static function update1274()
234         {
235                 // Was the script completed?
236                 if (Config::get("system", "post_update_version") >= 1274) {
237                         return true;
238                 }
239
240                 logger("Start", LOGGER_DEBUG);
241
242                 $fields = ['id', 'title', 'content-warning', 'body', 'location', 'tag', 'file',
243                         'coord', 'app', 'rendered-hash', 'rendered-html', 'verb',
244                         'object-type', 'object', 'target-type', 'target', 'plink',
245                         'author-id', 'owner-id'];
246
247                 $condition = ["`icid` IS NULL"];
248                 $params = ['limit' => 10000];
249                 $items = Item::select($fields, $condition, $params);
250
251                 if (!DBM::is_result($items)) {
252                         Config::set("system", "post_update_version", 1274);
253                         logger("Done", LOGGER_DEBUG);
254                         return true;
255                 }
256
257                 $rows = 0;
258
259                 while ($item = Item::fetch($items)) {
260                         // Clearing the author and owner data if there is an id.
261                         if ($item['author-id'] > 0) {
262                                 $item['author-name'] = '';
263                                 $item['author-link'] = '';
264                                 $item['author-avatar'] = '';
265                         }
266
267                         if ($item['owner-id'] > 0) {
268                                 $item['owner-name'] = '';
269                                 $item['owner-link'] = '';
270                                 $item['owner-avatar'] = '';
271                         }
272
273                         Item::update($item, ['id' => $item['id']]);
274                         ++$rows;
275                 }
276                 dba::close($items);
277
278                 logger("Processed rows: " . $rows, LOGGER_DEBUG);
279                 return true;
280         }
281         /**
282          * @brief update the "item-activity" table
283          *
284          * @return bool "true" when the job is done
285          */
286         private static function update1275()
287         {
288                 // Was the script completed?
289                 if (Config::get("system", "post_update_version") >= 1275) {
290                         return true;
291                 }
292
293                 logger("Start", LOGGER_DEBUG);
294
295                 $fields = ['id', 'verb'];
296
297                 $condition = ["`iaid` IS NULL AND NOT `icid` IS NULL AND `verb` IN (?, ?, ?, ?, ?)",
298                         ACTIVITY_LIKE, ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE];
299
300                 $params = ['limit' => 10000];
301                 $items = Item::select($fields, $condition, $params);
302
303                 if (!DBM::is_result($items)) {
304                         Config::set("system", "post_update_version", 1275);
305                         logger("Done", LOGGER_DEBUG);
306                         return true;
307                 }
308
309                 $rows = 0;
310
311                 while ($item = Item::fetch($items)) {
312                         Item::update($item, ['id' => $item['id']]);
313                         ++$rows;
314                 }
315                 dba::close($items);
316
317                 logger("Processed rows: " . $rows, LOGGER_DEBUG);
318                 return true;
319         }
320 }