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