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