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