]> git.mxchange.org Git - friendica.git/blob - include/cronjobs.php
453db6d0144961da47200bd583fa954ccb3c9c26
[friendica.git] / include / cronjobs.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Database\DBM;
6 use Friendica\Network\Probe;
7
8 function cronjobs_run(&$argv, &$argc){
9         global $a;
10
11         require_once 'include/datetime.php';
12         require_once 'include/post_update.php';
13         require_once 'mod/nodeinfo.php';
14         require_once 'include/photos.php';
15         require_once 'include/user.php';
16         require_once 'include/socgraph.php';
17
18         // No parameter set? So return
19         if ($argc <= 1) {
20                 return;
21         }
22
23         logger("Starting cronjob ".$argv[1], LOGGER_DEBUG);
24
25         // Call possible post update functions
26         // see include/post_update.php for more details
27         if ($argv[1] == 'post_update') {
28                 post_update();
29                 return;
30         }
31
32         // update nodeinfo data
33         if ($argv[1] == 'nodeinfo') {
34                 nodeinfo_cron();
35                 return;
36         }
37
38         // Expire and remove user entries
39         if ($argv[1] == 'expire_and_remove_users') {
40                 cron_expire_and_remove_users();
41                 return;
42         }
43
44         if ($argv[1] == 'update_contact_birthdays') {
45                 update_contact_birthdays();
46                 return;
47         }
48
49         if ($argv[1] == 'update_photo_albums') {
50                 cron_update_photo_albums();
51                 return;
52         }
53
54         // Clear cache entries
55         if ($argv[1] == 'clear_cache') {
56                 cron_clear_cache($a);
57                 return;
58         }
59
60         // Repair missing Diaspora values in contacts
61         if ($argv[1] == 'repair_diaspora') {
62                 cron_repair_diaspora($a);
63                 return;
64         }
65
66         // Repair entries in the database
67         if ($argv[1] == 'repair_database') {
68                 cron_repair_database();
69                 return;
70         }
71
72         logger("Xronjob ".$argv[1]." is unknown.", LOGGER_DEBUG);
73
74         return;
75 }
76
77 /**
78  * @brief Update the cached values for the number of photo albums per user
79  */
80 function cron_update_photo_albums() {
81         $r = q("SELECT `uid` FROM `user` WHERE NOT `account_expired` AND NOT `account_removed`");
82         if (!DBM::is_result($r)) {
83                 return;
84         }
85
86         foreach ($r AS $user) {
87                 photo_albums($user['uid'], true);
88         }
89 }
90
91 /**
92  * @brief Expire and remove user entries
93  */
94 function cron_expire_and_remove_users() {
95         // expire any expired accounts
96         q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
97                 AND `account_expires_on` > '%s'
98                 AND `account_expires_on` < UTC_TIMESTAMP()", dbesc(NULL_DATE));
99
100         // delete user records for recently removed accounts
101         $r = q("SELECT * FROM `user` WHERE `account_removed` AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
102         if (DBM::is_result($r)) {
103                 foreach ($r as $user) {
104                         dba::delete('user', array('uid' => $user['uid']));
105                 }
106         }
107 }
108
109 /**
110  * @brief Clear cache entries
111  *
112  * @param App $a
113  */
114 function cron_clear_cache(App $a) {
115
116         $last = Config::get('system','cache_last_cleared');
117
118         if ($last) {
119                 $next = $last + (3600); // Once per hour
120                 $clear_cache = ($next <= time());
121         } else {
122                 $clear_cache = true;
123         }
124
125         if (!$clear_cache) {
126                 return;
127         }
128
129         // clear old cache
130         Cache::clear();
131
132         // clear old item cache files
133         clear_cache();
134
135         // clear cache for photos
136         clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
137
138         // clear smarty cache
139         clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
140
141         // clear cache for image proxy
142         if (!Config::get("system", "proxy_disabled")) {
143                 clear_cache($a->get_basepath(), $a->get_basepath()."/proxy");
144
145                 $cachetime = Config::get('system','proxy_cache_time');
146                 if (!$cachetime) {
147                         $cachetime = PROXY_DEFAULT_TIME;
148                 }
149                 q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
150         }
151
152         // Delete the cached OEmbed entries that are older than one year
153         q("DELETE FROM `oembed` WHERE `created` < NOW() - INTERVAL 3 MONTH");
154
155         // Delete the cached "parse_url" entries that are older than one year
156         q("DELETE FROM `parsed_url` WHERE `created` < NOW() - INTERVAL 3 MONTH");
157
158         // Maximum table size in megabyte
159         $max_tablesize = intval(Config::get('system','optimize_max_tablesize')) * 1000000;
160         if ($max_tablesize == 0) {
161                 $max_tablesize = 100 * 1000000; // Default are 100 MB
162         }
163         if ($max_tablesize > 0) {
164                 // Minimum fragmentation level in percent
165                 $fragmentation_level = intval(Config::get('system','optimize_fragmentation')) / 100;
166                 if ($fragmentation_level == 0) {
167                         $fragmentation_level = 0.3; // Default value is 30%
168                 }
169
170                 // Optimize some tables that need to be optimized
171                 $r = q("SHOW TABLE STATUS");
172                 foreach ($r as $table) {
173
174                         // Don't optimize tables that are too large
175                         if ($table["Data_length"] > $max_tablesize) {
176                                 continue;
177                         }
178
179                         // Don't optimize empty tables
180                         if ($table["Data_length"] == 0) {
181                                 continue;
182                         }
183
184                         // Calculate fragmentation
185                         $fragmentation = $table["Data_free"] / ($table["Data_length"] + $table["Index_length"]);
186
187                         logger("Table ".$table["Name"]." - Fragmentation level: ".round($fragmentation * 100, 2), LOGGER_DEBUG);
188
189                         // Don't optimize tables that needn't to be optimized
190                         if ($fragmentation < $fragmentation_level) {
191                                 continue;
192                         }
193
194                         // So optimize it
195                         logger("Optimize Table ".$table["Name"], LOGGER_DEBUG);
196                         q("OPTIMIZE TABLE `%s`", dbesc($table["Name"]));
197                 }
198         }
199
200         Config::set('system','cache_last_cleared', time());
201 }
202
203 /**
204  * @brief Repair missing values in Diaspora contacts
205  *
206  * @param App $a
207  */
208 function cron_repair_diaspora(App $a) {
209
210         $starttime = time();
211
212         $r = q("SELECT `id`, `url` FROM `contact`
213                 WHERE `network` = '%s' AND (`batch` = '' OR `notify` = '' OR `poll` = '' OR pubkey = '')
214                         ORDER BY RAND() LIMIT 50", dbesc(NETWORK_DIASPORA));
215         if (!DBM::is_result($r)) {
216                 return;
217         }
218
219         foreach ($r AS $contact) {
220                 // Quit the loop after 3 minutes
221                 if (time() > ($starttime + 180)) {
222                         return;
223                 }
224
225                 if (!poco_reachable($contact["url"])) {
226                         continue;
227                 }
228
229                 $data = Probe::uri($contact["url"]);
230                 if ($data["network"] != NETWORK_DIASPORA) {
231                         continue;
232                 }
233
234                 logger("Repair contact ".$contact["id"]." ".$contact["url"], LOGGER_DEBUG);
235                 q("UPDATE `contact` SET `batch` = '%s', `notify` = '%s', `poll` = '%s', pubkey = '%s' WHERE `id` = %d",
236                         dbesc($data["batch"]), dbesc($data["notify"]), dbesc($data["poll"]), dbesc($data["pubkey"]),
237                         intval($contact["id"]));
238         }
239 }
240
241 /**
242  * @brief Do some repairs in database entries
243  *
244  */
245 function cron_repair_database() {
246
247         // Sometimes there seem to be issues where the "self" contact vanishes.
248         // We haven't found the origin of the problem by now.
249         $r = q("SELECT `uid` FROM `user` WHERE NOT EXISTS (SELECT `uid` FROM `contact` WHERE `contact`.`uid` = `user`.`uid` AND `contact`.`self`)");
250         if (DBM::is_result($r)) {
251                 foreach ($r AS $user) {
252                         logger('Create missing self contact for user '.$user['uid']);
253                         user_create_self_contact($user['uid']);
254                 }
255         }
256
257         // Set the parent if it wasn't set. (Shouldn't happen - but does sometimes)
258         // This call is very "cheap" so we can do it at any time without a problem
259         q("UPDATE `item` INNER JOIN `item` AS `parent` ON `parent`.`uri` = `item`.`parent-uri` AND `parent`.`uid` = `item`.`uid` SET `item`.`parent` = `parent`.`id` WHERE `item`.`parent` = 0");
260
261         // There was an issue where the nick vanishes from the contact table
262         q("UPDATE `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid` SET `nick` = `nickname` WHERE `self` AND `nick`=''");
263
264         // Update the global contacts for local users
265         $r = q("SELECT `uid` FROM `user` WHERE `verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired`");
266         if (DBM::is_result($r)) {
267                 foreach ($r AS $user) {
268                         update_gcontact_for_user($user["uid"]);
269                 }
270         }
271
272         /// @todo
273         /// - remove thread entries without item
274         /// - remove sign entries without item
275         /// - remove children when parent got lost
276         /// - set contact-id in item when not present
277 }