]> git.mxchange.org Git - friendica.git/blob - src/Database/PostUpdate.php
Fix tests (#5400)
[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::update1276()) {
35                         return;
36                 }
37         }
38
39         /**
40          * @brief Updates the "global" field in the item table
41          *
42          * @return bool "true" when the job is done
43          */
44         private static function update1194()
45         {
46                 // Was the script completed?
47                 if (Config::get("system", "post_update_version") >= 1194) {
48                         return true;
49                 }
50
51                 logger("Start", LOGGER_DEBUG);
52
53                 $end_id = Config::get("system", "post_update_1194_end");
54                 if (!$end_id) {
55                         $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
56                         if ($r) {
57                                 Config::set("system", "post_update_1194_end", $r[0]["id"]);
58                                 $end_id = Config::get("system", "post_update_1194_end");
59                         }
60                 }
61
62                 logger("End ID: ".$end_id, LOGGER_DEBUG);
63
64                 $start_id = Config::get("system", "post_update_1194_start");
65
66                 $query1 = "SELECT `item`.`id` FROM `item` ";
67
68                 $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
69
70                 $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
71                                 AND `item`.`visible` AND NOT `item`.`private`
72                                 AND NOT `item`.`deleted` AND NOT `item`.`moderated`
73                                 AND `item`.`network` IN ('%s', '%s', '%s', '')
74                                 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
75                                 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
76                                 AND NOT `item`.`global`";
77
78                 $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 1",
79                         intval($start_id), intval($end_id),
80                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
81                 if (!$r) {
82                         Config::set("system", "post_update_version", 1194);
83                         logger("Update is done", LOGGER_DEBUG);
84                         return true;
85                 } else {
86                         Config::set("system", "post_update_1194_start", $r[0]["id"]);
87                         $start_id = Config::get("system", "post_update_1194_start");
88                 }
89
90                 logger("Start ID: ".$start_id, LOGGER_DEBUG);
91
92                 $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 1000,1",
93                         intval($start_id), intval($end_id),
94                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
95                 if ($r) {
96                         $pos_id = $r[0]["id"];
97                 } else {
98                         $pos_id = $end_id;
99                 }
100                 logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
101
102                 q("UPDATE `item` ".$query2." SET `item`.`global` = 1 ".$query3,
103                         intval($start_id), intval($pos_id),
104                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
105
106                 logger("Done", LOGGER_DEBUG);
107         }
108
109         /**
110          * @brief set the author-id and owner-id in all item entries
111          *
112          * This job has to be started multiple times until all entries are set.
113          * It isn't started in the update function since it would consume too much time and can be done in the background.
114          *
115          * @return bool "true" when the job is done
116          */
117         private static function update1198()
118         {
119                 // Was the script completed?
120                 if (Config::get("system", "post_update_version") >= 1198) {
121                         return true;
122                 }
123
124                 logger("Start", LOGGER_DEBUG);
125
126                 // Check if the first step is done (Setting "author-id" and "owner-id" in the item table)
127                 $fields = ['author-link', 'author-name', 'author-avatar', 'owner-link', 'owner-name', 'owner-avatar', 'network', 'uid'];
128                 $r = dba::select('item', $fields, ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
129                 if (!$r) {
130                         // Are there unfinished entries in the thread table?
131                         $r = q("SELECT COUNT(*) AS `total` FROM `thread`
132                                 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
133                                 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
134                                         (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
135
136                         if ($r && ($r[0]["total"] == 0)) {
137                                 Config::set("system", "post_update_version", 1198);
138                                 logger("Done", LOGGER_DEBUG);
139                                 return true;
140                         }
141
142                         // Update the thread table from the item table
143                         $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
144                                         SET `thread`.`author-id` = `item`.`author-id`,
145                                         `thread`.`owner-id` = `item`.`owner-id`
146                                 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
147                                         (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
148
149                         logger("Updated threads", LOGGER_DEBUG);
150                         if (DBM::is_result($r)) {
151                                 Config::set("system", "post_update_version", 1198);
152                                 logger("Done", LOGGER_DEBUG);
153                                 return true;
154                         }
155                         return false;
156                 }
157
158                 logger("Query done", LOGGER_DEBUG);
159
160                 $item_arr = [];
161                 foreach ($r as $item) {
162                         $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
163                         $item_arr[$index] = ["author-link" => $item["author-link"],
164                                                         "owner-link" => $item["owner-link"],
165                                                         "uid" => $item["uid"]];
166                 }
167
168                 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
169                 foreach ($item_arr as $item) {
170                         $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
171                                 'photo' => $item['author-avatar'], 'network' => $item['network']];
172                         $author_id = Contact::getIdForURL($item["author-link"], 0, false, $default);
173
174                         $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
175                                 'photo' => $item['owner-avatar'], 'network' => $item['network']];
176                         $owner_id = Contact::getIdForURL($item["owner-link"], 0, false, $default);
177
178                         if ($author_id == 0) {
179                                 $author_id = -1;
180                         }
181                         if ($owner_id == 0) {
182                                 $owner_id = -1;
183                         }
184                         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]);
185                 }
186
187                 logger("Updated items", LOGGER_DEBUG);
188                 return false;
189         }
190
191         /**
192          * @brief update the "last-item" field in the "self" contact
193          *
194          * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
195          *
196          * @return bool "true" when the job is done
197          */
198         private static function update1206()
199         {
200                 // Was the script completed?
201                 if (Config::get("system", "post_update_version") >= 1206) {
202                         return true;
203                 }
204
205                 logger("Start", LOGGER_DEBUG);
206                 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
207                         (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
208                         FROM `user`
209                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
210
211                 if (!DBM::is_result($r)) {
212                         return false;
213                 }
214                 foreach ($r as $user) {
215                         if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
216                                 dba::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
217                         }
218                 }
219
220                 Config::set("system", "post_update_version", 1206);
221                 logger("Done", LOGGER_DEBUG);
222                 return true;
223         }
224
225         /**
226          * @brief update the item related tables
227          *
228          * @return bool "true" when the job is done
229          */
230         private static function update1276()
231         {
232                 // Was the script completed?
233                 if (Config::get("system", "post_update_version") >= 1276) {
234                         return true;
235                 }
236
237                 $id = Config::get("system", "post_update_version_1276_id", 0);
238
239                 logger("Start from item " . $id, LOGGER_DEBUG);
240
241                 $fields = array_merge(Item::MIXED_CONTENT_FIELDLIST, ['network', 'author-id', 'owner-id', 'tag', 'file',
242                         'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'id']);
243
244                 $start_id = $id;
245                 $rows = 0;
246                 $condition = ["`id` > ?", $id];
247                 $params = ['order' => ['id'], 'limit' => 10000];
248                 $items = Item::select($fields, $condition, $params);
249                 while ($item = Item::fetch($items)) {
250                         $id = $item['id'];
251
252                         if (empty($item['author-id'])) {
253                                 $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
254                                         'photo' => $item['author-avatar'], 'network' => $item['network']];
255
256                                 $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
257                         }
258
259                         if (empty($item['owner-id'])) {
260                                 $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
261                                         'photo' => $item['owner-avatar'], 'network' => $item['network']];
262
263                                 $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
264                         }
265
266                         Item::update($item, ['id' => $id]);
267
268                         ++$rows;
269                 }
270                 dba::close($items);
271
272                 Config::set("system", "post_update_version_1276_id", $id);
273
274                 logger("Processed rows: " . $rows . " - last processed item:  " . $id, LOGGER_DEBUG);
275
276                 if ($start_id == $id) {
277                         Config::set("system", "post_update_version", 1276);
278                         logger("Done", LOGGER_DEBUG);
279                         return true;
280                 }
281
282                 return false;
283         }
284 }