]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
expire and gprobe now moved as well
[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` FROM `user`
157                                 STRAIGHT_JOIN `contact`
158                                 ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
159                                         AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
160                                         AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
161                                         AND NOT `contact`.`archive`
162                                 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
163                         intval(CONTACT_IS_SHARING),
164                         intval(CONTACT_IS_FRIEND),
165                         dbesc(NETWORK_DFRN),
166                         dbesc(NETWORK_ZOT),
167                         dbesc(NETWORK_OSTATUS),
168                         dbesc(NETWORK_FEED),
169                         dbesc(NETWORK_MAIL),
170                         dbesc(NETWORK_MAIL2)
171                 );
172
173                 if (!DBM::is_result($contacts)) {
174                         return;
175                 }
176
177                 foreach ($contacts as $c) {
178
179                         $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
180                                 intval($c['id'])
181                         );
182
183                         if (!DBM::is_result($res)) {
184                                 continue;
185                         }
186
187                         foreach ($res as $contact) {
188
189                                 $xml = false;
190
191                                 if ($manual_id) {
192                                         $contact['last-update'] = NULL_DATE;
193                                 }
194
195                                 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
196                                         $contact['priority'] = 2;
197                                 }
198
199                                 if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
200                                         /*
201                                          * We should be getting everything via a hub. But just to be sure, let's check once a day.
202                                          * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
203                                          * This also lets us update our subscription to the hub, and add or replace hubs in case it
204                                          * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
205                                          */
206                                         $poll_interval = Config::get('system', 'pushpoll_frequency');
207                                         $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
208                                 }
209
210                                 if (($contact['priority'] >= 0) && !$force) {
211                                         $update = false;
212
213                                         $t = $contact['last-update'];
214
215                                         /*
216                                          * Based on $contact['priority'], should we poll this site now? Or later?
217                                          */
218                                         switch ($contact['priority']) {
219                                                 case 5:
220                                                         if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
221                                                                 $update = true;
222                                                         }
223                                                         break;
224                                                 case 4:
225                                                         if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
226                                                                 $update = true;
227                                                         }
228                                                         break;
229                                                 case 3:
230                                                         if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
231                                                                 $update = true;
232                                                         }
233                                                         break;
234                                                 case 2:
235                                                         if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
236                                                                 $update = true;
237                                                         }
238                                                         break;
239                                                 case 1:
240                                                         if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
241                                                                 $update = true;
242                                                         }
243                                                         break;
244                                                 case 0:
245                                                 default:
246                                                         if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
247                                                                 $update = true;
248                                                         }
249                                                         break;
250                                         }
251                                         if (!$update) {
252                                                 continue;
253                                         }
254                                 }
255
256                                 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
257
258                                 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
259                                         $priority = PRIORITY_MEDIUM;
260                                 } else {
261                                         $priority = PRIORITY_LOW;
262                                 }
263                                 Worker::add(array('priority' => $priority, 'dont_fork' => true), 'OnePoll', (int)$contact['id']);
264                         }
265                 }
266         }
267 }