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