]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
Use short form array syntax everywhere
[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 require_once 'include/dba.php';
13
14 Class Cron {
15         public static function execute($parameter = '', $generation = 0) {
16                 global $a;
17
18                 require_once 'include/datetime.php';
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                 // run queue delivery process in the background
44                 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
45
46                 // run the process to discover global contacts in the background
47                 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
48
49                 // run the process to update locally stored global contacts in the background
50                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
51
52                 // Expire and remove user entries
53                 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
54
55                 // Call possible post update functions
56                 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
57
58                 // update nodeinfo data
59                 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
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(datetime_convert('UTC', 'UTC', 'now', 'd'));
73
74                 if ($d2 != intval($d1)) {
75
76                         Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
77
78                         Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
79
80                         Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
81
82                         Config::set('system', 'last_expire_day', $d2);
83
84                         Worker::add(PRIORITY_LOW, 'Expire');
85
86                         Worker::add(PRIORITY_MEDIUM, 'DBClean');
87
88                         Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
89
90                         // Delete all done workerqueue entries
91                         dba::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR']);
92
93                         // check upstream version?
94                         Worker::add(PRIORITY_LOW, 'CheckVersion');
95                 }
96
97                 // Poll contacts
98                 self::pollContacts($parameter, $generation);
99
100                 logger('cron: end');
101
102                 Config::set('system', 'last_cron', time());
103
104                 return;
105         }
106
107         /**
108          * @brief Poll contacts for unreceived messages
109          *
110          * @todo Currently it seems as if the following parameter aren't used at all ...
111          *
112          * @param string $parameter Parameter (force, restart, ...) for the contact polling
113          * @param integer $generation
114          */
115         private static function pollContacts($parameter, $generation) {
116                 $manual_id  = 0;
117                 $generation = 0;
118                 $force      = false;
119                 $restart    = false;
120
121                 if ($parameter == 'force') {
122                         $force = true;
123                 }
124                 if ($parameter == 'restart') {
125                         $restart = true;
126                         $generation = intval($generation);
127                         if (!$generation) {
128                                 killme();
129                         }
130                 }
131
132                 if (intval($parameter)) {
133                         $manual_id = intval($parameter);
134                         $force     = true;
135                 }
136
137                 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
138
139                 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
140
141                 reload_plugins();
142
143                 $d = datetime_convert();
144
145                 // Only poll from those with suitable relationships,
146                 // and which have a polling address and ignore Diaspora since
147                 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
148
149                 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
150                 if ($abandon_days < 1) {
151                         $abandon_days = 0;
152                 }
153                 $abandon_sql = (($abandon_days)
154                         ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
155                         : ''
156                 );
157
158                 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
159                                         `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
160                                 FROM `user`
161                                 STRAIGHT_JOIN `contact`
162                                 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
163                                         AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
164                                         AND NOT `contact`.`self` AND NOT `contact`.`blocked`
165                                 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
166                         dbesc(NETWORK_DFRN),
167                         dbesc(NETWORK_OSTATUS),
168                         dbesc(NETWORK_DIASPORA),
169                         dbesc(NETWORK_FEED),
170                         dbesc(NETWORK_MAIL)
171                 );
172
173                 if (!DBM::is_result($contacts)) {
174                         return;
175                 }
176
177                 foreach ($contacts as $contact) {
178
179                         if ($manual_id) {
180                                 $contact['last-update'] = NULL_DATE;
181                         }
182
183                         // Friendica and OStatus are checked once a day
184                         if (in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
185                                 $contact['priority'] = 2;
186                         }
187
188                         if ($contact['subhub'] && in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
189                                 /*
190                                  * We should be getting everything via a hub. But just to be sure, let's check once a day.
191                                  * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
192                                  * This also lets us update our subscription to the hub, and add or replace hubs in case it
193                                  * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
194                                  */
195                                 $poll_interval = Config::get('system', 'pushpoll_frequency');
196                                 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
197                         }
198
199                         // Check Diaspora contacts or followers once a week
200                         if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
201                                 $contact['priority'] = 4;
202                         }
203
204                         // Check archived contacts once a month
205                         if ($contact['archive']) {
206                                 $contact['priority'] = 5;
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                         if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
256                                 $priority = PRIORITY_MEDIUM;
257                         } elseif ($contact['archive']) {
258                                 $priority = PRIORITY_NEGLIGIBLE;
259                         } else {
260                                 $priority = PRIORITY_LOW;
261                         }
262
263                         logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
264
265                         Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);
266                 }
267         }
268 }