2 namespace Friendica\Worker;
4 use Friendica\Core\Config;
5 use Friendica\Core\Worker;
6 use Friendica\Database\DBM;
9 public static function execute($parameter = '', $generation = 0) {
12 require_once 'include/datetime.php';
14 // Poll contacts with specific parameters
15 if (!empty($parameter)) {
16 self::pollContacts($parameter, $generation);
20 $last = Config::get('system', 'last_cron');
22 $poll_interval = intval(Config::get('system', 'cron_interval'));
23 if (! $poll_interval) {
28 $next = $last + ($poll_interval * 60);
30 logger('cron intervall not reached');
35 logger('cron: start');
37 // run queue delivery process in the background
38 Worker::add(PRIORITY_NEGLIGIBLE, "queue");
40 // run the process to discover global contacts in the background
41 Worker::add(PRIORITY_LOW, "discover_poco");
43 // run the process to update locally stored global contacts in the background
44 Worker::add(PRIORITY_LOW, "discover_poco", "checkcontact");
46 // Expire and remove user entries
47 Worker::add(PRIORITY_MEDIUM, "cronjobs", "expire_and_remove_users");
49 // Call possible post update functions
50 Worker::add(PRIORITY_LOW, "cronjobs", "post_update");
52 // update nodeinfo data
53 Worker::add(PRIORITY_LOW, "cronjobs", "nodeinfo");
55 // Clear cache entries
56 Worker::add(PRIORITY_LOW, "cronjobs", "clear_cache");
58 // Repair missing Diaspora values in contacts
59 Worker::add(PRIORITY_LOW, "cronjobs", "repair_diaspora");
61 // Repair entries in the database
62 Worker::add(PRIORITY_LOW, "cronjobs", "repair_database");
64 // once daily run birthday_updates and then expire in background
65 $d1 = Config::get('system', 'last_expire_day');
66 $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
68 if ($d2 != intval($d1)) {
70 Worker::add(PRIORITY_LOW, "cronjobs", "update_contact_birthdays");
72 Worker::add(PRIORITY_LOW, "discover_poco", "update_server");
74 Worker::add(PRIORITY_LOW, "discover_poco", "suggestions");
76 Config::set('system', 'last_expire_day', $d2);
78 Worker::add(PRIORITY_LOW, 'expire');
80 Worker::add(PRIORITY_MEDIUM, 'dbclean');
82 Worker::add(PRIORITY_LOW, "cronjobs", "update_photo_albums");
84 // Delete all done workerqueue entries
85 dba::delete('workerqueue', array('`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR'));
87 // check upstream version?
88 Worker::add(PRIORITY_LOW, 'CheckVersion');
92 self::pollContacts($parameter, $generation);
96 Config::set('system', 'last_cron', time());
102 * @brief Poll contacts for unreceived messages
104 * @todo Currently it seems as if the following parameter aren't used at all ...
106 * @param string $parameter Parameter (force, restart, ...) for the contact polling
107 * @param integer $generation
109 private static function pollContacts($parameter, $generation) {
115 if ($parameter == 'force') {
118 if ($parameter == 'restart') {
120 $generation = intval($generation);
126 if (intval($parameter)) {
127 $manual_id = intval($parameter);
131 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
133 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
137 $d = datetime_convert();
139 // Only poll from those with suitable relationships,
140 // and which have a polling address and ignore Diaspora since
141 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
143 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
144 if ($abandon_days < 1) {
147 $abandon_sql = (($abandon_days)
148 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
152 $contacts = q("SELECT `contact`.`id` FROM `user`
153 STRAIGHT_JOIN `contact`
154 ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
155 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
156 AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
157 AND NOT `contact`.`archive`
158 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
159 intval(CONTACT_IS_SHARING),
160 intval(CONTACT_IS_FRIEND),
163 dbesc(NETWORK_OSTATUS),
169 if (!DBM::is_result($contacts)) {
173 foreach ($contacts as $c) {
175 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
179 if (!DBM::is_result($res)) {
183 foreach ($res as $contact) {
188 $contact['last-update'] = NULL_DATE;
191 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
192 $contact['priority'] = 2;
195 if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
197 * We should be getting everything via a hub. But just to be sure, let's check once a day.
198 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
199 * This also lets us update our subscription to the hub, and add or replace hubs in case it
200 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
202 $poll_interval = Config::get('system', 'pushpoll_frequency');
203 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
206 if (($contact['priority'] >= 0) && !$force) {
209 $t = $contact['last-update'];
212 * Based on $contact['priority'], should we poll this site now? Or later?
214 switch ($contact['priority']) {
216 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
221 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
226 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
231 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
236 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
242 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
252 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
254 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
255 $priority = PRIORITY_MEDIUM;
257 $priority = PRIORITY_LOW;
259 Worker::add(array('priority' => $priority, 'dont_fork' => true), 'OnePoll', (int)$contact['id']);