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