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