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