]> git.mxchange.org Git - friendica.git/blob - src/Database/PostUpdate.php
e4bfa710aa3ee8e359a819cb85de5cf835de82f1
[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                 $r = dba::select('item', ['author-link', 'owner-link', 'uid'], ['author-id' => 0, 'owner-id' => 0], ['limit' => 1000]);
124                 if (!$r) {
125                         // Are there unfinished entries in the thread table?
126                         $r = q("SELECT COUNT(*) AS `total` FROM `thread`
127                                 INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
128                                 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
129                                         (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
130
131                         if ($r && ($r[0]["total"] == 0)) {
132                                 Config::set("system", "post_update_version", 1198);
133                                 logger("Done", LOGGER_DEBUG);
134                                 return true;
135                         }
136
137                         // Update the thread table from the item table
138                         $r = q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
139                                         SET `thread`.`author-id` = `item`.`author-id`,
140                                         `thread`.`owner-id` = `item`.`owner-id`
141                                 WHERE `thread`.`author-id` = 0 AND `thread`.`owner-id` = 0 AND
142                                         (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
143
144                         logger("Updated threads", LOGGER_DEBUG);
145                         if (DBM::is_result($r)) {
146                                 Config::set("system", "post_update_version", 1198);
147                                 logger("Done", LOGGER_DEBUG);
148                                 return true;
149                         }
150                         return false;
151                 }
152
153                 logger("Query done", LOGGER_DEBUG);
154
155                 $item_arr = [];
156                 foreach ($r as $item) {
157                         $index = $item["author-link"]."-".$item["owner-link"]."-".$item["uid"];
158                         $item_arr[$index] = ["author-link" => $item["author-link"],
159                                                         "owner-link" => $item["owner-link"],
160                                                         "uid" => $item["uid"]];
161                 }
162
163                 // Set the "author-id" and "owner-id" in the item table and add a new public contact entry if needed
164                 foreach ($item_arr as $item) {
165                         $author_id = Contact::getIdForURL($item["author-link"]);
166                         $owner_id = Contact::getIdForURL($item["owner-link"]);
167
168                         if ($author_id == 0) {
169                                 $author_id = -1;
170                         }
171                         if ($owner_id == 0) {
172                                 $owner_id = -1;
173                         }
174                         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]);
175                 }
176
177                 logger("Updated items", LOGGER_DEBUG);
178                 return false;
179         }
180
181         /**
182          * @brief update the "last-item" field in the "self" contact
183          *
184          * This field avoids cost intensive calls in the admin panel and in "nodeinfo"
185          *
186          * @return bool "true" when the job is done
187          */
188         private static function update1206()
189         {
190                 // Was the script completed?
191                 if (Config::get("system", "post_update_version") >= 1206) {
192                         return true;
193                 }
194
195                 logger("Start", LOGGER_DEBUG);
196                 $r = q("SELECT `contact`.`id`, `contact`.`last-item`,
197                         (SELECT MAX(`changed`) FROM `item` USE INDEX (`uid_wall_changed`) WHERE `wall` AND `uid` = `user`.`uid`) AS `lastitem_date`
198                         FROM `user`
199                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`");
200
201                 if (!DBM::is_result($r)) {
202                         return false;
203                 }
204                 foreach ($r as $user) {
205                         if (!empty($user["lastitem_date"]) && ($user["lastitem_date"] > $user["last-item"])) {
206                                 dba::update('contact', ['last-item' => $user['lastitem_date']], ['id' => $user['id']]);
207                         }
208                 }
209
210                 Config::set("system", "post_update_version", 1206);
211                 logger("Done", LOGGER_DEBUG);
212                 return true;
213         }
214 }