]> git.mxchange.org Git - friendica.git/blob - include/cron.php
d4d19d8c86dc7e6f6a81418e2fd7f85e4a67826d
[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                 // check upstream version?
85                 Worker::add(PRIORITY_LOW, 'checkversion');
86         }
87
88         // Poll contacts
89         cron_poll_contacts($argc, $argv);
90
91         logger('cron: end');
92
93         set_config('system', 'last_cron', time());
94
95         return;
96 }
97
98 /**
99  * @brief Poll contacts for unreceived messages
100  *
101  * @param Integer $argc Number of command line arguments
102  * @param Array $argv Array of command line arguments
103  */
104 function cron_poll_contacts($argc, $argv) {
105         $manual_id  = 0;
106         $generation = 0;
107         $force      = false;
108         $restart    = false;
109
110         if (($argc > 1) && ($argv[1] == 'force')) {
111                 $force = true;
112         }
113         if (($argc > 1) && ($argv[1] == 'restart')) {
114                 $restart = true;
115                 $generation = intval($argv[2]);
116                 if (!$generation) {
117                         killme();
118                 }
119         }
120
121         if (($argc > 1) && intval($argv[1])) {
122                 $manual_id = intval($argv[1]);
123                 $force     = true;
124         }
125
126         $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
127
128         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
129
130         reload_plugins();
131
132         $d = datetime_convert();
133
134         // Only poll from those with suitable relationships,
135         // and which have a polling address and ignore Diaspora since
136         // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
137
138         $abandon_days = intval(get_config('system', 'account_abandon_days'));
139         if ($abandon_days < 1) {
140                 $abandon_days = 0;
141         }
142         $abandon_sql = (($abandon_days)
143                 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
144                 : ''
145         );
146
147         $contacts = q("SELECT `contact`.`id` FROM `user`
148                         STRAIGHT_JOIN `contact`
149                         ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
150                                 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
151                                 AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
152                                 AND NOT `contact`.`archive`
153                         WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
154                 intval(CONTACT_IS_SHARING),
155                 intval(CONTACT_IS_FRIEND),
156                 dbesc(NETWORK_DFRN),
157                 dbesc(NETWORK_ZOT),
158                 dbesc(NETWORK_OSTATUS),
159                 dbesc(NETWORK_FEED),
160                 dbesc(NETWORK_MAIL),
161                 dbesc(NETWORK_MAIL2)
162         );
163
164         if (!dbm::is_result($contacts)) {
165                 return;
166         }
167
168         foreach ($contacts as $c) {
169
170                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
171                         intval($c['id'])
172                 );
173
174                 if (!dbm::is_result($res)) {
175                         continue;
176                 }
177
178                 foreach ($res as $contact) {
179
180                         $xml = false;
181
182                         if ($manual_id) {
183                                 $contact['last-update'] = NULL_DATE;
184                         }
185
186                         if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
187                                 $contact['priority'] = 2;
188                         }
189
190                         if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, 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 = get_config('system', 'pushpoll_frequency');
198                                 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
199                         }
200
201                         if (($contact['priority'] >= 0) && !$force) {
202                                 $update = false;
203
204                                 $t = $contact['last-update'];
205
206                                 /*
207                                  * Based on $contact['priority'], should we poll this site now? Or later?
208                                  */
209                                 switch ($contact['priority']) {
210                                         case 5:
211                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
212                                                         $update = true;
213                                                 }
214                                                 break;
215                                         case 4:
216                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
217                                                         $update = true;
218                                                 }
219                                                 break;
220                                         case 3:
221                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
222                                                         $update = true;
223                                                 }
224                                                 break;
225                                         case 2:
226                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
227                                                         $update = true;
228                                                 }
229                                                 break;
230                                         case 1:
231                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
232                                                         $update = true;
233                                                 }
234                                                 break;
235                                         case 0:
236                                         default:
237                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
238                                                         $update = true;
239                                                 }
240                                                 break;
241                                 }
242                                 if (!$update) {
243                                         continue;
244                                 }
245                         }
246
247                         logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
248
249                         if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
250                                 $priority = PRIORITY_MEDIUM;
251                         } else {
252                                 $priority = PRIORITY_LOW;
253                         }
254                         Worker::add(array('priority' => $priority, 'dont_fork' => true), 'onepoll', (int)$contact['id']);
255                 }
256         }
257 }