]> git.mxchange.org Git - friendica.git/blob - include/cron.php
Merge remote-tracking branch 'refs/remotes/friendica/develop' into develop
[friendica.git] / include / cron.php
1 <?php
2 use \Friendica\Core\Config;
3
4 function cron_run(&$argv, &$argc){
5         global $a;
6
7         require_once('include/datetime.php');
8
9         // Poll contacts with specific parameters
10         if ($argc > 1) {
11                 cron_poll_contacts($argc, $argv);
12                 return;
13         }
14
15         $last = get_config('system','last_cron');
16
17         $poll_interval = intval(get_config('system','cron_interval'));
18         if (! $poll_interval) {
19                 $poll_interval = 10;
20         }
21         if ($last) {
22                 $next = $last + ($poll_interval * 60);
23                 if($next > time()) {
24                         logger('cron intervall not reached');
25                         return;
26                 }
27         }
28
29         logger('cron: start');
30
31         // run queue delivery process in the background
32         proc_run(PRIORITY_NEGLIGIBLE, "include/queue.php");
33
34         // run the process to discover global contacts in the background
35         proc_run(PRIORITY_LOW, "include/discover_poco.php");
36
37         // run the process to update locally stored global contacts in the background
38         proc_run(PRIORITY_LOW, "include/discover_poco.php", "checkcontact");
39
40         // Expire and remove user entries
41         proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "expire_and_remove_users");
42
43         // Check OStatus conversations
44         proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "ostatus_mentions");
45
46         // Check every conversation
47         proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "ostatus_conversations");
48
49         // Call possible post update functions
50         proc_run(PRIORITY_LOW, "include/cronjobs.php", "post_update");
51
52         // update nodeinfo data
53         proc_run(PRIORITY_LOW, "include/cronjobs.php", "nodeinfo");
54
55         // Clear cache entries
56         proc_run(PRIORITY_LOW, "include/cronjobs.php", "clear_cache");
57
58         // Repair missing Diaspora values in contacts
59         proc_run(PRIORITY_LOW, "include/cronjobs.php", "repair_diaspora");
60
61         // Repair entries in the database
62         proc_run(PRIORITY_LOW, "include/cronjobs.php", "repair_database");
63
64         // once daily run birthday_updates and then expire in background
65         $d1 = get_config('system','last_expire_day');
66         $d2 = intval(datetime_convert('UTC','UTC','now','d'));
67
68         if($d2 != intval($d1)) {
69
70                 proc_run(PRIORITY_LOW, "include/cronjobs.php", "update_contact_birthdays");
71
72                 proc_run(PRIORITY_LOW, "include/discover_poco.php", "update_server");
73
74                 proc_run(PRIORITY_LOW, "include/discover_poco.php", "suggestions");
75
76                 set_config('system','last_expire_day',$d2);
77
78                 proc_run(PRIORITY_LOW, 'include/expire.php');
79
80                 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
81
82                 proc_run(PRIORITY_LOW, "include/cronjobs.php", "update_photo_albums");
83         }
84
85         // Poll contacts
86         cron_poll_contacts($argc, $argv);
87
88         logger('cron: end');
89
90         set_config('system','last_cron', time());
91
92         return;
93 }
94
95 /**
96  * @brief Poll contacts for unreceived messages
97  *
98  * @param Integer $argc Number of command line arguments
99  * @param Array $argv Array of command line arguments
100  */
101 function cron_poll_contacts($argc, $argv) {
102         $manual_id  = 0;
103         $generation = 0;
104         $force      = false;
105         $restart    = false;
106
107         if (($argc > 1) && ($argv[1] == 'force')) {
108                 $force = true;
109         }
110         if (($argc > 1) && ($argv[1] == 'restart')) {
111                 $restart = true;
112                 $generation = intval($argv[2]);
113                 if (!$generation) {
114                         killme();
115                 }
116         }
117
118         if (($argc > 1) && intval($argv[1])) {
119                 $manual_id = intval($argv[1]);
120                 $force     = true;
121         }
122
123         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
124
125         reload_plugins();
126
127         $d = datetime_convert();
128
129         // Only poll from those with suitable relationships,
130         // and which have a polling address and ignore Diaspora since
131         // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
132
133         $abandon_days = intval(get_config('system','account_abandon_days'));
134         if ($abandon_days < 1) {
135                 $abandon_days = 0;
136         }
137         $abandon_sql = (($abandon_days)
138                 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
139                 : ''
140         );
141
142         $contacts = q("SELECT `contact`.`id` FROM `user`
143                         STRAIGHT_JOIN `contact`
144                         ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
145                                 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
146                                 AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
147                                 AND NOT `contact`.`archive`
148                         WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
149                 intval(CONTACT_IS_SHARING),
150                 intval(CONTACT_IS_FRIEND),
151                 dbesc(NETWORK_DFRN),
152                 dbesc(NETWORK_ZOT),
153                 dbesc(NETWORK_OSTATUS),
154                 dbesc(NETWORK_FEED),
155                 dbesc(NETWORK_MAIL),
156                 dbesc(NETWORK_MAIL2)
157         );
158
159         if (!count($contacts)) {
160                 return;
161         }
162
163         foreach ($contacts as $c) {
164
165                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
166                         intval($c['id'])
167                 );
168
169                 if (!dbm::is_result($res)) {
170                         continue;
171                 }
172
173                 foreach($res as $contact) {
174
175                         $xml = false;
176
177                         if ($manual_id) {
178                                 $contact['last-update'] = NULL_DATE;
179                         }
180
181                         if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
182                                 $contact['priority'] = 2;
183                         }
184
185                         if ($contact['subhub'] AND in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
186                                 // We should be getting everything via a hub. But just to be sure, let's check once a day.
187                                 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
188                                 // This also lets us update our subscription to the hub, and add or replace hubs in case it
189                                 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
190
191                                 $poll_interval = get_config('system','pushpoll_frequency');
192                                 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
193                         }
194
195                         if($contact['priority'] AND !$force) {
196
197                                 $update     = false;
198
199                                 $t = $contact['last-update'];
200
201                                 /**
202                                  * Based on $contact['priority'], should we poll this site now? Or later?
203                                  */
204
205                                 switch ($contact['priority']) {
206                                         case 5:
207                                                 if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month")) {
208                                                         $update = true;
209                                                 }
210                                                 break;
211                                         case 4:
212                                                 if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week")) {
213                                                         $update = true;
214                                                 }
215                                                 break;
216                                         case 3:
217                                                 if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) {
218                                                         $update = true;
219                                                 }
220                                                 break;
221                                         case 2:
222                                                 if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour")) {
223                                                         $update = true;
224                                                 }
225                                                 break;
226                                         case 1:
227                                         default:
228                                                 if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour")) {
229                                                         $update = true;
230                                                 }
231                                                 break;
232                                 }
233                                 if (!$update) {
234                                         continue;
235                                 }
236                         }
237
238                         logger("Polling ".$contact["network"]." ".$contact["id"]." ".$contact["nick"]." ".$contact["name"]);
239
240                         if (($contact['network'] == NETWORK_FEED) AND ($contact['priority'] <= 3)) {
241                                 proc_run(PRIORITY_MEDIUM, 'include/onepoll.php', intval($contact['id']));
242                         } else {
243                                 proc_run(PRIORITY_LOW, 'include/onepoll.php', intval($contact['id']));
244                         }
245                 }
246         }
247 }