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