]> git.mxchange.org Git - friendica.git/blob - include/cron.php
Frequently repair of database entries
[friendica.git] / include / cron.php
1 <?php
2 if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
3         $directory = dirname($_SERVER["argv"][0]);
4
5         if (substr($directory, 0, 1) != "/")
6                 $directory = $_SERVER["PWD"]."/".$directory;
7
8         $directory = realpath($directory."/..");
9
10         chdir($directory);
11 }
12
13 require_once("boot.php");
14
15
16 function cron_run(&$argv, &$argc){
17         global $a, $db;
18
19         if(is_null($a)) {
20                 $a = new App;
21         }
22
23         if(is_null($db)) {
24                 @include(".htconfig.php");
25                 require_once("include/dba.php");
26                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
27                 unset($db_host, $db_user, $db_pass, $db_data);
28         };
29
30
31         require_once('include/session.php');
32         require_once('include/datetime.php');
33         require_once('library/simplepie/simplepie.inc');
34         require_once('include/items.php');
35         require_once('include/Contact.php');
36         require_once('include/email.php');
37         require_once('include/socgraph.php');
38         require_once('include/pidfile.php');
39         require_once('mod/nodeinfo.php');
40
41         load_config('config');
42         load_config('system');
43
44         $maxsysload = intval(get_config('system','maxloadavg'));
45         if($maxsysload < 1)
46                 $maxsysload = 50;
47
48         $load = current_load();
49         if($load) {
50                 if(intval($load) > $maxsysload) {
51                         logger('system: load ' . $load . ' too high. cron deferred to next scheduled run.');
52                         return;
53                 }
54         }
55
56         $last = get_config('system','last_cron');
57
58         $poll_interval = intval(get_config('system','cron_interval'));
59         if(! $poll_interval)
60                 $poll_interval = 10;
61
62         if($last) {
63                 $next = $last + ($poll_interval * 60);
64                 if($next > time()) {
65                         logger('cron intervall not reached');
66                         return;
67                 }
68         }
69
70         $lockpath = get_lockpath();
71         if ($lockpath != '') {
72                 $pidfile = new pidfile($lockpath, 'cron');
73                 if($pidfile->is_already_running()) {
74                         logger("cron: Already running");
75                         if ($pidfile->running_time() > 9*60) {
76                                 $pidfile->kill();
77                                 logger("cron: killed stale process");
78                                 // Calling a new instance
79                                 proc_run('php','include/cron.php');
80                         }
81                         exit;
82                 }
83         }
84
85
86
87         $a->set_baseurl(get_config('system','url'));
88
89         load_hooks();
90
91         logger('cron: start');
92
93         // run queue delivery process in the background
94
95         proc_run('php',"include/queue.php");
96
97         // run diaspora photo queue process in the background
98
99         proc_run('php',"include/dsprphotoq.php");
100
101         // run the process to discover global contacts in the background
102
103         proc_run('php',"include/discover_poco.php");
104
105         // run the process to update locally stored global contacts in the background
106
107         proc_run('php',"include/discover_poco.php", "checkcontact");
108
109         // expire any expired accounts
110
111         q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
112                 AND `account_expires_on` != '0000-00-00 00:00:00'
113                 AND `account_expires_on` < UTC_TIMESTAMP() ");
114
115         // delete user and contact records for recently removed accounts
116
117         $r = q("SELECT * FROM `user` WHERE `account_removed` = 1 AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
118         if ($r) {
119                 foreach($r as $user) {
120                         q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
121                         q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
122                 }
123         }
124
125         $abandon_days = intval(get_config('system','account_abandon_days'));
126         if($abandon_days < 1)
127                 $abandon_days = 0;
128
129         // Check OStatus conversations
130         // Check only conversations with mentions (for a longer time)
131         check_conversations(true);
132
133         // Check every conversation
134         check_conversations(false);
135
136         // Set the gcontact-id in the item table if missing
137         item_set_gcontact();
138
139         // update nodeinfo data
140         nodeinfo_cron();
141
142         /// @TODO Regenerate usage statistics
143         // q("ANALYZE TABLE `item`");
144
145         // once daily run birthday_updates and then expire in background
146
147         $d1 = get_config('system','last_expire_day');
148         $d2 = intval(datetime_convert('UTC','UTC','now','d'));
149
150         if($d2 != intval($d1)) {
151
152                 update_contact_birthdays();
153
154                 proc_run('php',"include/discover_poco.php", "suggestions");
155
156                 set_config('system','last_expire_day',$d2);
157
158                 proc_run('php','include/expire.php');
159         }
160
161         // Clear cache entries
162         cron_clear_cache($a);
163
164         // Repair missing Diaspora values in contacts
165         cron_repair_diaspora($a);
166
167         // Repair entries in the database
168         cron_repair_database();
169
170         $manual_id  = 0;
171         $generation = 0;
172         $force      = false;
173         $restart    = false;
174
175         if(($argc > 1) && ($argv[1] == 'force'))
176                 $force = true;
177
178         if(($argc > 1) && ($argv[1] == 'restart')) {
179                 $restart = true;
180                 $generation = intval($argv[2]);
181                 if(! $generation)
182                         killme();
183         }
184
185         if(($argc > 1) && intval($argv[1])) {
186                 $manual_id = intval($argv[1]);
187                 $force     = true;
188         }
189
190         $interval = intval(get_config('system','poll_interval'));
191         if(! $interval)
192                 $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
193
194         // If we are using the worker we don't need a delivery interval
195         if (get_config("system", "worker"))
196                 $interval = false;
197
198         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
199
200         reload_plugins();
201
202         $d = datetime_convert();
203
204         // Only poll from those with suitable relationships,
205         // and which have a polling address and ignore Diaspora since
206         // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
207
208         $abandon_sql = (($abandon_days)
209                 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
210                 : ''
211         );
212
213         $contacts = q("SELECT `contact`.`id` FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
214                 WHERE `rel` IN (%d, %d) AND `poll` != '' AND `network` IN ('%s', '%s', '%s', '%s', '%s', '%s')
215                 $sql_extra
216                 AND NOT `self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly` AND NOT `contact`.`archive`
217                 AND NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
218                 intval(CONTACT_IS_SHARING),
219                 intval(CONTACT_IS_FRIEND),
220                 dbesc(NETWORK_DFRN),
221                 dbesc(NETWORK_ZOT),
222                 dbesc(NETWORK_OSTATUS),
223                 dbesc(NETWORK_FEED),
224                 dbesc(NETWORK_MAIL),
225                 dbesc(NETWORK_MAIL2)
226         );
227
228         if(! count($contacts)) {
229                 return;
230         }
231
232         foreach($contacts as $c) {
233
234                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
235                         intval($c['id'])
236                 );
237
238                 if((! $res) || (! count($res)))
239                         continue;
240
241                 foreach($res as $contact) {
242
243                         $xml = false;
244
245                         if($manual_id)
246                                 $contact['last-update'] = '0000-00-00 00:00:00';
247
248                         if(in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS)))
249                                 $contact['priority'] = 2;
250
251                         if($contact['subhub'] AND in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
252                                 // We should be getting everything via a hub. But just to be sure, let's check once a day.
253                                 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
254                                 // This also lets us update our subscription to the hub, and add or replace hubs in case it
255                                 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
256
257                                 $poll_interval = get_config('system','pushpoll_frequency');
258                                 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
259                         }
260
261                         if($contact['priority'] AND !$force) {
262
263                                 $update     = false;
264
265                                 $t = $contact['last-update'];
266
267                                 /**
268                                  * Based on $contact['priority'], should we poll this site now? Or later?
269                                  */
270
271                                 switch ($contact['priority']) {
272                                         case 5:
273                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
274                                                         $update = true;
275                                                 break;
276                                         case 4:
277                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
278                                                         $update = true;
279                                                 break;
280                                         case 3:
281                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
282                                                         $update = true;
283                                                 break;
284                                         case 2:
285                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
286                                                         $update = true;
287                                                 break;
288                                         case 1:
289                                         default:
290                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
291                                                         $update = true;
292                                                 break;
293                                 }
294                                 if(!$update)
295                                         continue;
296                         }
297
298                         logger("Polling ".$contact["network"]." ".$contact["id"]." ".$contact["nick"]." ".$contact["name"]);
299
300                         proc_run('php','include/onepoll.php',$contact['id']);
301
302                         if($interval)
303                                 @time_sleep_until(microtime(true) + (float) $interval);
304                 }
305         }
306
307         logger('cron: end');
308
309         set_config('system','last_cron', time());
310
311         return;
312 }
313
314 /**
315  * @brief Clear cache entries
316  *
317  * @param App $a
318  */
319 function cron_clear_cache(&$a) {
320
321         $last = get_config('system','cache_last_cleared');
322
323         if($last) {
324                 $next = $last + (3600); // Once per hour
325                 $clear_cache = ($next <= time());
326         } else
327                 $clear_cache = true;
328
329         if (!$clear_cache)
330                 return;
331
332         // clear old cache
333         Cache::clear();
334
335         // clear old item cache files
336         clear_cache();
337
338         // clear cache for photos
339         clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
340
341         // clear smarty cache
342         clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
343
344         // clear cache for image proxy
345         if (!get_config("system", "proxy_disabled")) {
346                 clear_cache($a->get_basepath(), $a->get_basepath()."/proxy");
347
348                 $cachetime = get_config('system','proxy_cache_time');
349                 if (!$cachetime) $cachetime = PROXY_DEFAULT_TIME;
350
351                 q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
352         }
353
354         // Delete the cached OEmbed entries that are older than one year
355         q("DELETE FROM `oembed` WHERE `created` < NOW() - INTERVAL 1 YEAR");
356
357         // Delete the cached "parse_url" entries that are older than one year
358         q("DELETE FROM `parsed_url` WHERE `created` < NOW() - INTERVAL 1 YEAR");
359
360         // Maximum table size in megabyte
361         $max_tablesize = intval(get_config('system','optimize_max_tablesize')) * 1000000;
362         if ($max_tablesize == 0)
363                 $max_tablesize = 100 * 1000000; // Default are 100 MB
364
365         // Minimum fragmentation level in percent
366         $fragmentation_level = intval(get_config('system','optimize_fragmentation')) / 100;
367         if ($fragmentation_level == 0)
368                 $fragmentation_level = 0.3; // Default value is 30%
369
370         // Optimize some tables that need to be optimized
371         $r = q("SHOW TABLE STATUS");
372         foreach($r as $table) {
373
374                 // Don't optimize tables that are too large
375                 if ($table["Data_length"] > $max_tablesize)
376                         continue;
377
378                 // Don't optimize empty tables
379                 if ($table["Data_length"] == 0)
380                         continue;
381
382                 // Calculate fragmentation
383                 $fragmentation = $table["Data_free"] / $table["Data_length"];
384
385                 logger("Table ".$table["Name"]." - Fragmentation level: ".round($fragmentation * 100, 2), LOGGER_DEBUG);
386
387                 // Don't optimize tables that needn't to be optimized
388                 if ($fragmentation < $fragmentation_level)
389                         continue;
390
391                 // So optimize it
392                 logger("Optimize Table ".$table["Name"], LOGGER_DEBUG);
393                 q("OPTIMIZE TABLE `%s`", dbesc($table["Name"]));
394         }
395
396         set_config('system','cache_last_cleared', time());
397 }
398
399 /**
400  * @brief Repair missing values in Diaspora contacts
401  *
402  * @param App $a
403  */
404 function cron_repair_diaspora(&$a) {
405         $r = q("SELECT `id`, `url` FROM `contact`
406                 WHERE `network` = '%s' AND (`batch` = '' OR `notify` = '' OR `poll` = '' OR pubkey = '')
407                         ORDER BY RAND() LIMIT 50", dbesc(NETWORK_DIASPORA));
408         if ($r) {
409                 foreach ($r AS $contact) {
410                         if (poco_reachable($contact["url"])) {
411                                 $data = probe_url($contact["url"]);
412                                 if ($data["network"] == NETWORK_DIASPORA) {
413                                         logger("Repair contact ".$contact["id"]." ".$contact["url"], LOGGER_DEBUG);
414                                         q("UPDATE `contact` SET `batch` = '%s', `notify` = '%s', `poll` = '%s', pubkey = '%s' WHERE `id` = %d",
415                                                 dbesc($data["batch"]), dbesc($data["notify"]), dbesc($data["poll"]), dbesc($data["pubkey"]),
416                                                 intval($contact["id"]));
417                                 }
418                         }
419                 }
420         }
421 }
422
423 /**
424  * @brief Do some repairs in database entries
425  *
426  */
427 function cron_repair_database() {
428
429         // Set the parent if it wasn't set. (Shouldn't happen - but does sometimes)
430         // This call is very "cheap" so we can do it at any time without a problem
431         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");
432
433         /// @todo
434         // - remove duplicated contacts with uid=0 (We could do this at the place where the contacts are stored)
435         // - remove thread entries without item
436         // - remove sign entries without item
437         // - remove children when parent got lost
438         // - set contact-id in item when not present
439 }
440
441 if (array_search(__file__,get_included_files())===0){
442         cron_run($_SERVER["argv"],$_SERVER["argc"]);
443         killme();
444 }