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