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