]> git.mxchange.org Git - friendica.git/blob - include/post_update.php
2143ac3d6fab955f63e9dd1d40f6207be3f1bbc7
[friendica.git] / include / post_update.php
1 <?php
2 /**
3  * @brief Calls the post update functions
4  */
5 function post_update() {
6
7         if (!post_update_1192())
8                 return;
9
10         if (!post_update_1195())
11                 return;
12 }
13
14 /**
15  * @brief set the gcontact-id in all item entries
16  *
17  * This job has to be started multiple times until all entries are set.
18  * It isn't started in the update function since it would consume too much time and can be done in the background.
19  *
20  * @return bool "true" when the job is done
21  */
22 function post_update_1192() {
23
24         // Was the script completed?
25         if (get_config("system", "post_update_version") >= 1192)
26                 return true;
27
28         // Check if the first step is done (Setting "gcontact-id" in the item table)
29         $r = q("SELECT `author-link`, `author-name`, `author-avatar`, `uid`, `network` FROM `item` WHERE `gcontact-id` = 0 LIMIT 1000");
30         if (!$r) {
31                 // Are there unfinished entries in the thread table?
32                 $r = q("SELECT COUNT(*) AS `total` FROM `thread`
33                         INNER JOIN `item` ON `item`.`id` =`thread`.`iid`
34                         WHERE `thread`.`gcontact-id` = 0 AND
35                                 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
36
37                 if ($r AND ($r[0]["total"] == 0)) {
38                         set_config("system", "post_update_version", 1192);
39                         return true;
40                 }
41
42                 // Update the thread table from the item table
43                 q("UPDATE `thread` INNER JOIN `item` ON `item`.`id`=`thread`.`iid`
44                                 SET `thread`.`gcontact-id` = `item`.`gcontact-id`
45                         WHERE `thread`.`gcontact-id` = 0 AND
46                                 (`thread`.`uid` IN (SELECT `uid` from `user`) OR `thread`.`uid` = 0)");
47
48                 return false;
49         }
50
51         $item_arr = array();
52         foreach ($r AS $item) {
53                 $index = $item["author-link"]."-".$item["uid"];
54                 $item_arr[$index] = array("author-link" => $item["author-link"],
55                                                 "uid" => $item["uid"],
56                                                 "network" => $item["network"]);
57         }
58
59         // Set the "gcontact-id" in the item table and add a new gcontact entry if needed
60         foreach($item_arr AS $item) {
61                 $gcontact_id = get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'],
62                                                 "photo" => $item['author-avatar'], "name" => $item['author-name']));
63                 q("UPDATE `item` SET `gcontact-id` = %d WHERE `uid` = %d AND `author-link` = '%s' AND `gcontact-id` = 0",
64                         intval($gcontact_id), intval($item["uid"]), dbesc($item["author-link"]));
65         }
66         return false;
67 }
68
69 /**
70  * @brief Updates the "shadow" field in the item table
71  *
72  * @return bool "true" when the job is done
73  */
74 function post_update_1195() {
75
76         // Was the script completed?
77         if (get_config("system", "post_update_version") >= 1195)
78                 return true;
79
80         $end_id = get_config("system", "post_update_1195_end");
81         if (!$end_id) {
82                 $r = q("SELECT `id` FROM `item` WHERE `uid` != 0 ORDER BY `id` DESC LIMIT 1");
83                 if ($r) {
84                         set_config("system", "post_update_1195_end", $r[0]["id"]);
85                         $end_id = get_config("system", "post_update_1195_end");
86                 }
87         }
88
89         $start_id = get_config("system", "post_update_1195_start");
90
91         $query1 = "SELECT `item`.`id` FROM `item` ";
92
93         $query2 = "INNER JOIN `item` AS `shadow` ON `item`.`uri` = `shadow`.`uri` AND `shadow`.`uid` = 0 ";
94
95         $query3 = "WHERE `item`.`uid` != 0 AND `item`.`id` >= %d AND `item`.`id` <= %d
96                         AND `item`.`visible` AND NOT `item`.`private`
97                         AND NOT `item`.`deleted` AND NOT `item`.`moderated`
98                         AND `item`.`network` IN ('%s', '%s', '%s', '')
99                         AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
100                         AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
101                         AND `item`.`shadow` = 0";
102
103         $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 1",
104                 intval($start_id), intval($end_id),
105                 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
106         if (!$r) {
107                 set_config("system", "post_update_version", 1195);
108                 return true;
109         } else {
110                 set_config("system", "post_update_1195_start", $r[0]["id"]);
111                 $start_id = get_config("system", "post_update_1195_start");
112         }
113
114
115         $r = q($query1.$query2.$query3."  ORDER BY `item`.`id` LIMIT 10000,1",
116                 intval($start_id), intval($end_id),
117                 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
118         if ($r)
119                 $pos_id = $r[0]["id"];
120         else
121                 $pos_id = $end_id;
122
123         logger("Progress: Start: ".$start_id." position: ".$pos_id." end: ".$end_id, LOGGER_DEBUG);
124
125         $r = q("UPDATE `item` ".$query2." SET `item`.`shadow` = `shadow`.`id` ".$query3,
126                 intval($start_id), intval($pos_id),
127                 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
128 }
129 ?>