]> git.mxchange.org Git - friendica.git/blob - include/cronjobs.php
a2fc99825176fa95695d536d50ae280254a86c6f
[friendica.git] / include / cronjobs.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5
6 function cronjobs_run(&$argv, &$argc){
7         global $a;
8
9         require_once('include/Probe.php');
10         require_once 'include/datetime.php';
11         require_once 'include/ostatus.php';
12         require_once 'include/post_update.php';
13         require_once 'mod/nodeinfo.php';
14         require_once 'include/photos.php';
15         require_once 'include/user.php';
16         require_once 'include/socgraph.php';
17
18         // No parameter set? So return
19         if ($argc <= 1) {
20                 return;
21         }
22
23         logger("Starting cronjob ".$argv[1], LOGGER_DEBUG);
24
25         // Check OStatus conversations
26         // Check only conversations with mentions (for a longer time)
27         if ($argv[1] == 'ostatus_mentions') {
28                 ostatus::check_conversations(true);
29                 return;
30         }
31
32         // Check every conversation
33         if ($argv[1] == 'ostatus_conversations') {
34                 ostatus::check_conversations(false);
35                 return;
36         }
37
38         // Call possible post update functions
39         // see include/post_update.php for more details
40         if ($argv[1] == 'post_update') {
41                 post_update();
42                 return;
43         }
44
45         // update nodeinfo data
46         if ($argv[1] == 'nodeinfo') {
47                 nodeinfo_cron();
48                 return;
49         }
50
51         // Expire and remove user entries
52         if ($argv[1] == 'expire_and_remove_users') {
53                 cron_expire_and_remove_users();
54                 return;
55         }
56
57         if ($argv[1] == 'update_contact_birthdays') {
58                 update_contact_birthdays();
59                 return;
60         }
61
62         if ($argv[1] == 'update_photo_albums') {
63                 cron_update_photo_albums();
64                 return;
65         }
66
67         // Clear cache entries
68         if ($argv[1] == 'clear_cache') {
69                 cron_clear_cache($a);
70                 return;
71         }
72
73         // Repair missing Diaspora values in contacts
74         if ($argv[1] == 'repair_diaspora') {
75                 cron_repair_diaspora($a);
76                 return;
77         }
78
79         // Repair entries in the database
80         if ($argv[1] == 'repair_database') {
81                 cron_repair_database();
82                 return;
83         }
84
85         logger("Xronjob ".$argv[1]." is unknown.", LOGGER_DEBUG);
86
87         return;
88 }
89
90 /**
91  * @brief Update the cached values for the number of photo albums per user
92  */
93 function cron_update_photo_albums() {
94         $r = q("SELECT `uid` FROM `user` WHERE NOT `account_expired` AND NOT `account_removed`");
95         if (!dbm::is_result($r)) {
96                 return;
97         }
98
99         foreach ($r AS $user) {
100                 photo_albums($user['uid'], true);
101         }
102 }
103
104 /**
105  * @brief Expire and remove user entries
106  */
107 function cron_expire_and_remove_users() {
108         // expire any expired accounts
109         q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
110                 AND `account_expires_on` > '%s'
111                 AND `account_expires_on` < UTC_TIMESTAMP()", dbesc(NULL_DATE));
112
113         // delete user and contact records for recently removed accounts
114         $r = q("SELECT * FROM `user` WHERE `account_removed` AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
115         if (dbm::is_result($r)) {
116                 foreach ($r as $user) {
117                         q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
118                         q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
119                 }
120         }
121 }
122
123 /**
124  * @brief Clear cache entries
125  *
126  * @param App $a
127  */
128 function cron_clear_cache(App $a) {
129
130         $last = get_config('system','cache_last_cleared');
131
132         if ($last) {
133                 $next = $last + (3600); // Once per hour
134                 $clear_cache = ($next <= time());
135         } else {
136                 $clear_cache = true;
137         }
138
139         if (!$clear_cache) {
140                 return;
141         }
142
143         // clear old cache
144         Cache::clear();
145
146         // clear old item cache files
147         clear_cache();
148
149         // clear cache for photos
150         clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
151
152         // clear smarty cache
153         clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
154
155         // clear cache for image proxy
156         if (!get_config("system", "proxy_disabled")) {
157                 clear_cache($a->get_basepath(), $a->get_basepath()."/proxy");
158
159                 $cachetime = get_config('system','proxy_cache_time');
160                 if (!$cachetime) {
161                         $cachetime = PROXY_DEFAULT_TIME;
162                 }
163                 q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
164         }
165
166         // Delete the cached OEmbed entries that are older than one year
167         q("DELETE FROM `oembed` WHERE `created` < NOW() - INTERVAL 3 MONTH");
168
169         // Delete the cached "parse_url" entries that are older than one year
170         q("DELETE FROM `parsed_url` WHERE `created` < NOW() - INTERVAL 3 MONTH");
171
172         // Maximum table size in megabyte
173         $max_tablesize = intval(get_config('system','optimize_max_tablesize')) * 1000000;
174         if ($max_tablesize == 0) {
175                 $max_tablesize = 100 * 1000000; // Default are 100 MB
176         }
177         if ($max_tablesize > 0) {
178                 // Minimum fragmentation level in percent
179                 $fragmentation_level = intval(get_config('system','optimize_fragmentation')) / 100;
180                 if ($fragmentation_level == 0) {
181                         $fragmentation_level = 0.3; // Default value is 30%
182                 }
183
184                 // Optimize some tables that need to be optimized
185                 $r = q("SHOW TABLE STATUS");
186                 foreach ($r as $table) {
187
188                         // Don't optimize tables that are too large
189                         if ($table["Data_length"] > $max_tablesize) {
190                                 continue;
191                         }
192
193                         // Don't optimize empty tables
194                         if ($table["Data_length"] == 0) {
195                                 continue;
196                         }
197
198                         // Calculate fragmentation
199                         $fragmentation = $table["Data_free"] / ($table["Data_length"] + $table["Index_length"]);
200
201                         logger("Table ".$table["Name"]." - Fragmentation level: ".round($fragmentation * 100, 2), LOGGER_DEBUG);
202
203                         // Don't optimize tables that needn't to be optimized
204                         if ($fragmentation < $fragmentation_level) {
205                                 continue;
206                         }
207
208                         // So optimize it
209                         logger("Optimize Table ".$table["Name"], LOGGER_DEBUG);
210                         q("OPTIMIZE TABLE `%s`", dbesc($table["Name"]));
211                 }
212         }
213
214         set_config('system','cache_last_cleared', time());
215 }
216
217 /**
218  * @brief Repair missing values in Diaspora contacts
219  *
220  * @param App $a
221  */
222 function cron_repair_diaspora(App $a) {
223
224         $starttime = time();
225
226         $r = q("SELECT `id`, `url` FROM `contact`
227                 WHERE `network` = '%s' AND (`batch` = '' OR `notify` = '' OR `poll` = '' OR pubkey = '')
228                         ORDER BY RAND() LIMIT 50", dbesc(NETWORK_DIASPORA));
229         if (!dbm::is_result($r)) {
230                 return;
231         }
232
233         foreach ($r AS $contact) {
234                 // Quit the loop after 3 minutes
235                 if (time() > ($starttime + 180)) {
236                         return;
237                 }
238
239                 if (!poco_reachable($contact["url"])) {
240                         continue;
241                 }
242
243                 $data = Probe::uri($contact["url"]);
244                 if ($data["network"] != NETWORK_DIASPORA) {
245                         continue;
246                 }
247
248                 logger("Repair contact ".$contact["id"]." ".$contact["url"], LOGGER_DEBUG);
249                 q("UPDATE `contact` SET `batch` = '%s', `notify` = '%s', `poll` = '%s', pubkey = '%s' WHERE `id` = %d",
250                         dbesc($data["batch"]), dbesc($data["notify"]), dbesc($data["poll"]), dbesc($data["pubkey"]),
251                         intval($contact["id"]));
252         }
253 }
254
255 /**
256  * @brief Do some repairs in database entries
257  *
258  */
259 function cron_repair_database() {
260
261         // Sometimes there seem to be issues where the "self" contact vanishes.
262         // We haven't found the origin of the problem by now.
263         $r = q("SELECT `uid` FROM `user` WHERE NOT EXISTS (SELECT `uid` FROM `contact` WHERE `contact`.`uid` = `user`.`uid` AND `contact`.`self`)");
264         if (dbm::is_result($r)) {
265                 foreach ($r AS $user) {
266                         logger('Create missing self contact for user '.$user['uid']);
267                         user_create_self_contact($user['uid']);
268                 }
269         }
270
271         // Set the parent if it wasn't set. (Shouldn't happen - but does sometimes)
272         // This call is very "cheap" so we can do it at any time without a problem
273         q("UPDATE `item` INNER JOIN `item` AS `parent` ON `parent`.`uri` = `item`.`parent-uri` AND `parent`.`uid` = `item`.`uid` SET `item`.`parent` = `parent`.`id` WHERE `item`.`parent` = 0");
274
275         // There was an issue where the nick vanishes from the contact table
276         q("UPDATE `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid` SET `nick` = `nickname` WHERE `self` AND `nick`=''");
277
278         // Update the global contacts for local users
279         $r = q("SELECT `uid` FROM `user` WHERE `verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired`");
280         if (dbm::is_result($r)) {
281                 foreach ($r AS $user) {
282                         update_gcontact_for_user($user["uid"]);
283                 }
284         }
285
286         /// @todo
287         /// - remove thread entries without item
288         /// - remove sign entries without item
289         /// - remove children when parent got lost
290         /// - set contact-id in item when not present
291 }