]> git.mxchange.org Git - friendica.git/blob - include/cron.php
It is better this way
[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         // Check OStatus conversations
46         proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "ostatus_mentions");
47
48         // Check every conversation
49         proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "ostatus_conversations");
50
51         // Call possible post update functions
52         proc_run(PRIORITY_LOW, "include/cronjobs.php", "post_update");
53
54         // update nodeinfo data
55         proc_run(PRIORITY_LOW, "include/cronjobs.php", "nodeinfo");
56
57         // Clear cache entries
58         proc_run(PRIORITY_LOW, "include/cronjobs.php", "clear_cache");
59
60         // Repair missing Diaspora values in contacts
61         proc_run(PRIORITY_LOW, "include/cronjobs.php", "repair_diaspora");
62
63         // Repair entries in the database
64         proc_run(PRIORITY_LOW, "include/cronjobs.php", "repair_database");
65
66         // once daily run birthday_updates and then expire in background
67         $d1 = get_config('system', 'last_expire_day');
68         $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
69
70         if ($d2 != intval($d1)) {
71
72                 proc_run(PRIORITY_LOW, "include/cronjobs.php", "update_contact_birthdays");
73
74                 proc_run(PRIORITY_LOW, "include/discover_poco.php", "update_server");
75
76                 proc_run(PRIORITY_LOW, "include/discover_poco.php", "suggestions");
77
78                 set_config('system', 'last_expire_day', $d2);
79
80                 proc_run(PRIORITY_LOW, 'include/expire.php');
81
82                 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
83
84                 proc_run(PRIORITY_LOW, "include/cronjobs.php", "update_photo_albums");
85         }
86
87         // Poll contacts
88         cron_poll_contacts($argc, $argv);
89
90         logger('cron: end');
91
92         set_config('system', 'last_cron', time());
93
94         return;
95 }
96
97 /**
98  * @brief Poll contacts for unreceived messages
99  *
100  * @param Integer $argc Number of command line arguments
101  * @param Array $argv Array of command line arguments
102  */
103 function cron_poll_contacts($argc, $argv) {
104         $manual_id  = 0;
105         $generation = 0;
106         $force      = false;
107         $restart    = false;
108
109         if (($argc > 1) && ($argv[1] == 'force')) {
110                 $force = true;
111         }
112         if (($argc > 1) && ($argv[1] == 'restart')) {
113                 $restart = true;
114                 $generation = intval($argv[2]);
115                 if (!$generation) {
116                         killme();
117                 }
118         }
119
120         if (($argc > 1) && intval($argv[1])) {
121                 $manual_id = intval($argv[1]);
122                 $force     = true;
123         }
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'] AND 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'] AND !$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                                         default:
229                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
230                                                         $update = true;
231                                                 }
232                                                 break;
233                                 }
234                                 if (!$update) {
235                                         continue;
236                                 }
237                         }
238
239                         logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
240
241                         if (($contact['network'] == NETWORK_FEED) AND ($contact['priority'] <= 3)) {
242                                 proc_run(PRIORITY_MEDIUM, 'include/onepoll.php', intval($contact['id']));
243                         } else {
244                                 proc_run(PRIORITY_LOW, 'include/onepoll.php', intval($contact['id']));
245                         }
246                 }
247         }
248 }