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