3 use Friendica\Core\Config;
5 function cronjobs_run(&$argv, &$argc){
8 require_once('include/datetime.php');
9 require_once('include/ostatus.php');
10 require_once('include/post_update.php');
11 require_once('mod/nodeinfo.php');
12 require_once('include/photos.php');
13 require_once('include/user.php');
14 require_once('include/socgraph.php');
15 require_once('include/Probe.php');
17 // No parameter set? So return
22 logger("Starting cronjob ".$argv[1], LOGGER_DEBUG);
24 // Check OStatus conversations
25 // Check only conversations with mentions (for a longer time)
26 if ($argv[1] == 'ostatus_mentions') {
27 ostatus::check_conversations(true);
31 // Check every conversation
32 if ($argv[1] == 'ostatus_conversations') {
33 ostatus::check_conversations(false);
37 // Call possible post update functions
38 // see include/post_update.php for more details
39 if ($argv[1] == 'post_update') {
44 // update nodeinfo data
45 if ($argv[1] == 'nodeinfo') {
50 // Expire and remove user entries
51 if ($argv[1] == 'expire_and_remove_users') {
52 cron_expire_and_remove_users();
56 if ($argv[1] == 'update_contact_birthdays') {
57 update_contact_birthdays();
61 if ($argv[1] == 'update_photo_albums') {
62 cron_update_photo_albums();
66 // Clear cache entries
67 if ($argv[1] == 'clear_cache') {
72 // Repair missing Diaspora values in contacts
73 if ($argv[1] == 'repair_diaspora') {
74 cron_repair_diaspora($a);
78 // Repair entries in the database
79 if ($argv[1] == 'repair_database') {
80 cron_repair_database();
84 logger("Xronjob ".$argv[1]." is unknown.", LOGGER_DEBUG);
90 * @brief Update the cached values for the number of photo albums per user
92 function cron_update_photo_albums() {
93 $r = q("SELECT `uid` FROM `user` WHERE NOT `account_expired` AND NOT `account_removed`");
94 if (!dbm::is_result($r)) {
98 foreach ($r AS $user) {
99 photo_albums($user['uid'], true);
104 * @brief Expire and remove user entries
106 function cron_expire_and_remove_users() {
107 // expire any expired accounts
108 q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
109 AND `account_expires_on` > '%s'
110 AND `account_expires_on` < UTC_TIMESTAMP()", dbesc(NULL_DATE));
112 // delete user and contact records for recently removed accounts
113 $r = q("SELECT * FROM `user` WHERE `account_removed` AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
114 if (dbm::is_result($r)) {
115 foreach ($r as $user) {
116 q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
117 q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
123 * @brief Clear cache entries
127 function cron_clear_cache(App $a) {
129 $last = get_config('system','cache_last_cleared');
132 $next = $last + (3600); // Once per hour
133 $clear_cache = ($next <= time());
145 // clear old item cache files
148 // clear cache for photos
149 clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
151 // clear smarty cache
152 clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
154 // clear cache for image proxy
155 if (!get_config("system", "proxy_disabled")) {
156 clear_cache($a->get_basepath(), $a->get_basepath()."/proxy");
158 $cachetime = get_config('system','proxy_cache_time');
160 $cachetime = PROXY_DEFAULT_TIME;
162 q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
165 // Delete the cached OEmbed entries that are older than one year
166 q("DELETE FROM `oembed` WHERE `created` < NOW() - INTERVAL 3 MONTH");
168 // Delete the cached "parse_url" entries that are older than one year
169 q("DELETE FROM `parsed_url` WHERE `created` < NOW() - INTERVAL 3 MONTH");
171 // Maximum table size in megabyte
172 $max_tablesize = intval(get_config('system','optimize_max_tablesize')) * 1000000;
173 if ($max_tablesize == 0) {
174 $max_tablesize = 100 * 1000000; // Default are 100 MB
176 if ($max_tablesize > 0) {
177 // Minimum fragmentation level in percent
178 $fragmentation_level = intval(get_config('system','optimize_fragmentation')) / 100;
179 if ($fragmentation_level == 0) {
180 $fragmentation_level = 0.3; // Default value is 30%
183 // Optimize some tables that need to be optimized
184 $r = q("SHOW TABLE STATUS");
185 foreach ($r as $table) {
187 // Don't optimize tables that are too large
188 if ($table["Data_length"] > $max_tablesize) {
192 // Don't optimize empty tables
193 if ($table["Data_length"] == 0) {
197 // Calculate fragmentation
198 $fragmentation = $table["Data_free"] / ($table["Data_length"] + $table["Index_length"]);
200 logger("Table ".$table["Name"]." - Fragmentation level: ".round($fragmentation * 100, 2), LOGGER_DEBUG);
202 // Don't optimize tables that needn't to be optimized
203 if ($fragmentation < $fragmentation_level) {
208 logger("Optimize Table ".$table["Name"], LOGGER_DEBUG);
209 q("OPTIMIZE TABLE `%s`", dbesc($table["Name"]));
213 set_config('system','cache_last_cleared', time());
217 * @brief Repair missing values in Diaspora contacts
221 function cron_repair_diaspora(App $a) {
225 $r = q("SELECT `id`, `url` FROM `contact`
226 WHERE `network` = '%s' AND (`batch` = '' OR `notify` = '' OR `poll` = '' OR pubkey = '')
227 ORDER BY RAND() LIMIT 50", dbesc(NETWORK_DIASPORA));
228 if (!dbm::is_result($r)) {
232 foreach ($r AS $contact) {
233 // Quit the loop after 3 minutes
234 if (time() > ($starttime + 180)) {
238 if (!poco_reachable($contact["url"])) {
242 $data = Probe::uri($contact["url"]);
243 if ($data["network"] != NETWORK_DIASPORA) {
247 logger("Repair contact ".$contact["id"]." ".$contact["url"], LOGGER_DEBUG);
248 q("UPDATE `contact` SET `batch` = '%s', `notify` = '%s', `poll` = '%s', pubkey = '%s' WHERE `id` = %d",
249 dbesc($data["batch"]), dbesc($data["notify"]), dbesc($data["poll"]), dbesc($data["pubkey"]),
250 intval($contact["id"]));
255 * @brief Do some repairs in database entries
258 function cron_repair_database() {
260 // Sometimes there seem to be issues where the "self" contact vanishes.
261 // We haven't found the origin of the problem by now.
262 $r = q("SELECT `uid` FROM `user` WHERE NOT EXISTS (SELECT `uid` FROM `contact` WHERE `contact`.`uid` = `user`.`uid` AND `contact`.`self`)");
263 if (dbm::is_result($r)) {
264 foreach ($r AS $user) {
265 logger('Create missing self contact for user '.$user['uid']);
266 user_create_self_contact($user['uid']);
270 // Set the parent if it wasn't set. (Shouldn't happen - but does sometimes)
271 // This call is very "cheap" so we can do it at any time without a problem
272 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");
274 // There was an issue where the nick vanishes from the contact table
275 q("UPDATE `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid` SET `nick` = `nickname` WHERE `self` AND `nick`=''");
277 // Update the global contacts for local users
278 $r = q("SELECT `uid` FROM `user` WHERE `verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired`");
279 if (dbm::is_result($r)) {
280 foreach ($r AS $user) {
281 update_gcontact_for_user($user["uid"]);
286 /// - remove thread entries without item
287 /// - remove sign entries without item
288 /// - remove children when parent got lost
289 /// - set contact-id in item when not present