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