4 use Friendica\Core\Config;
5 use Friendica\Network\Probe;
7 function cronjobs_run(&$argv, &$argc){
10 require_once 'include/datetime.php';
11 require_once 'include/ostatus.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';
18 // No parameter set? So return
23 logger("Starting cronjob ".$argv[1], LOGGER_DEBUG);
25 // Check OStatus conversations
26 // Check only conversations with mentions (for a longer time)
27 if ($argv[1] == 'ostatus_mentions') {
28 ostatus::check_conversations(true);
32 // Check every conversation
33 if ($argv[1] == 'ostatus_conversations') {
34 ostatus::check_conversations(false);
38 // Call possible post update functions
39 // see include/post_update.php for more details
40 if ($argv[1] == 'post_update') {
45 // update nodeinfo data
46 if ($argv[1] == 'nodeinfo') {
51 // Expire and remove user entries
52 if ($argv[1] == 'expire_and_remove_users') {
53 cron_expire_and_remove_users();
57 if ($argv[1] == 'update_contact_birthdays') {
58 update_contact_birthdays();
62 if ($argv[1] == 'update_photo_albums') {
63 cron_update_photo_albums();
67 // Clear cache entries
68 if ($argv[1] == 'clear_cache') {
73 // Repair missing Diaspora values in contacts
74 if ($argv[1] == 'repair_diaspora') {
75 cron_repair_diaspora($a);
79 // Repair entries in the database
80 if ($argv[1] == 'repair_database') {
81 cron_repair_database();
85 logger("Xronjob ".$argv[1]." is unknown.", LOGGER_DEBUG);
91 * @brief Update the cached values for the number of photo albums per user
93 function cron_update_photo_albums() {
94 $r = q("SELECT `uid` FROM `user` WHERE NOT `account_expired` AND NOT `account_removed`");
95 if (!dbm::is_result($r)) {
99 foreach ($r AS $user) {
100 photo_albums($user['uid'], true);
105 * @brief Expire and remove user entries
107 function cron_expire_and_remove_users() {
108 // expire any expired accounts
109 q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
110 AND `account_expires_on` > '%s'
111 AND `account_expires_on` < UTC_TIMESTAMP()", dbesc(NULL_DATE));
113 // delete user and contact records for recently removed accounts
114 $r = q("SELECT * FROM `user` WHERE `account_removed` AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
115 if (dbm::is_result($r)) {
116 foreach ($r as $user) {
117 q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
118 q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
124 * @brief Clear cache entries
128 function cron_clear_cache(App $a) {
130 $last = get_config('system','cache_last_cleared');
133 $next = $last + (3600); // Once per hour
134 $clear_cache = ($next <= time());
146 // clear old item cache files
149 // clear cache for photos
150 clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
152 // clear smarty cache
153 clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
155 // clear cache for image proxy
156 if (!get_config("system", "proxy_disabled")) {
157 clear_cache($a->get_basepath(), $a->get_basepath()."/proxy");
159 $cachetime = get_config('system','proxy_cache_time');
161 $cachetime = PROXY_DEFAULT_TIME;
163 q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
166 // Delete the cached OEmbed entries that are older than one year
167 q("DELETE FROM `oembed` WHERE `created` < NOW() - INTERVAL 3 MONTH");
169 // Delete the cached "parse_url" entries that are older than one year
170 q("DELETE FROM `parsed_url` WHERE `created` < NOW() - INTERVAL 3 MONTH");
172 // Maximum table size in megabyte
173 $max_tablesize = intval(get_config('system','optimize_max_tablesize')) * 1000000;
174 if ($max_tablesize == 0) {
175 $max_tablesize = 100 * 1000000; // Default are 100 MB
177 if ($max_tablesize > 0) {
178 // Minimum fragmentation level in percent
179 $fragmentation_level = intval(get_config('system','optimize_fragmentation')) / 100;
180 if ($fragmentation_level == 0) {
181 $fragmentation_level = 0.3; // Default value is 30%
184 // Optimize some tables that need to be optimized
185 $r = q("SHOW TABLE STATUS");
186 foreach ($r as $table) {
188 // Don't optimize tables that are too large
189 if ($table["Data_length"] > $max_tablesize) {
193 // Don't optimize empty tables
194 if ($table["Data_length"] == 0) {
198 // Calculate fragmentation
199 $fragmentation = $table["Data_free"] / ($table["Data_length"] + $table["Index_length"]);
201 logger("Table ".$table["Name"]." - Fragmentation level: ".round($fragmentation * 100, 2), LOGGER_DEBUG);
203 // Don't optimize tables that needn't to be optimized
204 if ($fragmentation < $fragmentation_level) {
209 logger("Optimize Table ".$table["Name"], LOGGER_DEBUG);
210 q("OPTIMIZE TABLE `%s`", dbesc($table["Name"]));
214 set_config('system','cache_last_cleared', time());
218 * @brief Repair missing values in Diaspora contacts
222 function cron_repair_diaspora(App $a) {
226 $r = q("SELECT `id`, `url` FROM `contact`
227 WHERE `network` = '%s' AND (`batch` = '' OR `notify` = '' OR `poll` = '' OR pubkey = '')
228 ORDER BY RAND() LIMIT 50", dbesc(NETWORK_DIASPORA));
229 if (!dbm::is_result($r)) {
233 foreach ($r AS $contact) {
234 // Quit the loop after 3 minutes
235 if (time() > ($starttime + 180)) {
239 if (!poco_reachable($contact["url"])) {
243 $data = Probe::uri($contact["url"]);
244 if ($data["network"] != NETWORK_DIASPORA) {
248 logger("Repair contact ".$contact["id"]." ".$contact["url"], LOGGER_DEBUG);
249 q("UPDATE `contact` SET `batch` = '%s', `notify` = '%s', `poll` = '%s', pubkey = '%s' WHERE `id` = %d",
250 dbesc($data["batch"]), dbesc($data["notify"]), dbesc($data["poll"]), dbesc($data["pubkey"]),
251 intval($contact["id"]));
256 * @brief Do some repairs in database entries
259 function cron_repair_database() {
261 // Sometimes there seem to be issues where the "self" contact vanishes.
262 // We haven't found the origin of the problem by now.
263 $r = q("SELECT `uid` FROM `user` WHERE NOT EXISTS (SELECT `uid` FROM `contact` WHERE `contact`.`uid` = `user`.`uid` AND `contact`.`self`)");
264 if (dbm::is_result($r)) {
265 foreach ($r AS $user) {
266 logger('Create missing self contact for user '.$user['uid']);
267 user_create_self_contact($user['uid']);
271 // Set the parent if it wasn't set. (Shouldn't happen - but does sometimes)
272 // This call is very "cheap" so we can do it at any time without a problem
273 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");
275 // There was an issue where the nick vanishes from the contact table
276 q("UPDATE `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid` SET `nick` = `nickname` WHERE `self` AND `nick`=''");
278 // Update the global contacts for local users
279 $r = q("SELECT `uid` FROM `user` WHERE `verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired`");
280 if (dbm::is_result($r)) {
281 foreach ($r AS $user) {
282 update_gcontact_for_user($user["uid"]);
287 /// - remove thread entries without item
288 /// - remove sign entries without item
289 /// - remove children when parent got lost
290 /// - set contact-id in item when not present