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