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