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