3 use Friendica\Core\Config;
4 use Friendica\Core\Worker;
5 use Friendica\Database\DBM;
7 function cron_run(&$argv, &$argc){
10 require_once 'include/datetime.php';
12 // Poll contacts with specific parameters
14 cron_poll_contacts($argc, $argv);
18 $last = Config::get('system', 'last_cron');
20 $poll_interval = intval(Config::get('system', 'cron_interval'));
21 if (! $poll_interval) {
26 $next = $last + ($poll_interval * 60);
28 logger('cron intervall not reached');
33 logger('cron: start');
35 // run queue delivery process in the background
36 Worker::add(PRIORITY_NEGLIGIBLE, "queue");
38 // run the process to discover global contacts in the background
39 Worker::add(PRIORITY_LOW, "discover_poco");
41 // run the process to update locally stored global contacts in the background
42 Worker::add(PRIORITY_LOW, "discover_poco", "checkcontact");
44 // Expire and remove user entries
45 Worker::add(PRIORITY_MEDIUM, "cronjobs", "expire_and_remove_users");
47 // Call possible post update functions
48 Worker::add(PRIORITY_LOW, "cronjobs", "post_update");
50 // update nodeinfo data
51 Worker::add(PRIORITY_LOW, "cronjobs", "nodeinfo");
53 // Clear cache entries
54 Worker::add(PRIORITY_LOW, "cronjobs", "clear_cache");
56 // Repair missing Diaspora values in contacts
57 Worker::add(PRIORITY_LOW, "cronjobs", "repair_diaspora");
59 // Repair entries in the database
60 Worker::add(PRIORITY_LOW, "cronjobs", "repair_database");
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'));
66 if ($d2 != intval($d1)) {
68 Worker::add(PRIORITY_LOW, "cronjobs", "update_contact_birthdays");
70 Worker::add(PRIORITY_LOW, "discover_poco", "update_server");
72 Worker::add(PRIORITY_LOW, "discover_poco", "suggestions");
74 Config::set('system', 'last_expire_day', $d2);
76 Worker::add(PRIORITY_LOW, 'expire');
78 Worker::add(PRIORITY_MEDIUM, 'dbclean');
80 Worker::add(PRIORITY_LOW, "cronjobs", "update_photo_albums");
82 // Delete all done workerqueue entries
83 dba::delete('workerqueue', array('`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR'));
85 // check upstream version?
86 Worker::add(PRIORITY_LOW, 'checkversion');
90 cron_poll_contacts($argc, $argv);
94 Config::set('system', 'last_cron', time());
100 * @brief Poll contacts for unreceived messages
102 * @param Integer $argc Number of command line arguments
103 * @param Array $argv Array of command line arguments
105 function cron_poll_contacts($argc, $argv) {
111 if (($argc > 1) && ($argv[1] == 'force')) {
114 if (($argc > 1) && ($argv[1] == 'restart')) {
116 $generation = intval($argv[2]);
122 if (($argc > 1) && intval($argv[1])) {
123 $manual_id = intval($argv[1]);
127 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
129 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
133 $d = datetime_convert();
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.
139 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
140 if ($abandon_days < 1) {
143 $abandon_sql = (($abandon_days)
144 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
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),
159 dbesc(NETWORK_OSTATUS),
165 if (!DBM::is_result($contacts)) {
169 foreach ($contacts as $c) {
171 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
175 if (!DBM::is_result($res)) {
179 foreach ($res as $contact) {
184 $contact['last-update'] = NULL_DATE;
187 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
188 $contact['priority'] = 2;
191 if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
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'.
198 $poll_interval = Config::get('system', 'pushpoll_frequency');
199 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
202 if (($contact['priority'] >= 0) && !$force) {
205 $t = $contact['last-update'];
208 * Based on $contact['priority'], should we poll this site now? Or later?
210 switch ($contact['priority']) {
212 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
217 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
222 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
227 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
232 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
238 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
248 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
250 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
251 $priority = PRIORITY_MEDIUM;
253 $priority = PRIORITY_LOW;
255 Worker::add(array('priority' => $priority, 'dont_fork' => true), 'onepoll', (int)$contact['id']);