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