]> git.mxchange.org Git - friendica.git/blob - include/cronjobs.php
Fixed settings for test mysql database and updated documentation
[friendica.git] / include / cronjobs.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Network\Probe;
6
7 function cronjobs_run(&$argv, &$argc){
8         global $a;
9
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';
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         // Check OStatus conversations
26         // Check only conversations with mentions (for a longer time)
27         if ($argv[1] == 'ostatus_mentions') {
28                 ostatus::check_conversations(true);
29                 return;
30         }
31
32         // Check every conversation
33         if ($argv[1] == 'ostatus_conversations') {
34                 ostatus::check_conversations(false);
35                 return;
36         }
37
38         // Call possible post update functions
39         // see include/post_update.php for more details
40         if ($argv[1] == 'post_update') {
41                 post_update();
42                 return;
43         }
44
45         // update nodeinfo data
46         if ($argv[1] == 'nodeinfo') {
47                 nodeinfo_cron();
48                 return;
49         }
50
51         // Expire and remove user entries
52         if ($argv[1] == 'expire_and_remove_users') {
53                 cron_expire_and_remove_users();
54                 return;
55         }
56
57         if ($argv[1] == 'update_contact_birthdays') {
58                 update_contact_birthdays();
59                 return;
60         }
61
62         if ($argv[1] == 'update_photo_albums') {
63                 cron_update_photo_albums();
64                 return;
65         }
66
67         // Clear cache entries
68         if ($argv[1] == 'clear_cache') {
69                 cron_clear_cache($a);
70                 return;
71         }
72
73         // Repair missing Diaspora values in contacts
74         if ($argv[1] == 'repair_diaspora') {
75                 cron_repair_diaspora($a);
76                 return;
77         }
78
79         // Repair entries in the database
80         if ($argv[1] == 'repair_database') {
81                 cron_repair_database();
82                 return;
83         }
84
85         logger("Xronjob ".$argv[1]." is unknown.", LOGGER_DEBUG);
86
87         return;
88 }
89
90 /**
91  * @brief Update the cached values for the number of photo albums per user
92  */
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)) {
96                 return;
97         }
98
99         foreach ($r AS $user) {
100                 photo_albums($user['uid'], true);
101         }
102 }
103
104 /**
105  * @brief Expire and remove user entries
106  */
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));
112
113         // delete user 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                         dba::delete('user', array('uid' => $user['uid']));
118                 }
119         }
120 }
121
122 /**
123  * @brief Clear cache entries
124  *
125  * @param App $a
126  */
127 function cron_clear_cache(App $a) {
128
129         $last = get_config('system','cache_last_cleared');
130
131         if ($last) {
132                 $next = $last + (3600); // Once per hour
133                 $clear_cache = ($next <= time());
134         } else {
135                 $clear_cache = true;
136         }
137
138         if (!$clear_cache) {
139                 return;
140         }
141
142         // clear old cache
143         Cache::clear();
144
145         // clear old item cache files
146         clear_cache();
147
148         // clear cache for photos
149         clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
150
151         // clear smarty cache
152         clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
153
154         // clear cache for image proxy
155         if (!get_config("system", "proxy_disabled")) {
156                 clear_cache($a->get_basepath(), $a->get_basepath()."/proxy");
157
158                 $cachetime = get_config('system','proxy_cache_time');
159                 if (!$cachetime) {
160                         $cachetime = PROXY_DEFAULT_TIME;
161                 }
162                 q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
163         }
164
165         // Delete the cached OEmbed entries that are older than one year
166         q("DELETE FROM `oembed` WHERE `created` < NOW() - INTERVAL 3 MONTH");
167
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");
170
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
175         }
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%
181                 }
182
183                 // Optimize some tables that need to be optimized
184                 $r = q("SHOW TABLE STATUS");
185                 foreach ($r as $table) {
186
187                         // Don't optimize tables that are too large
188                         if ($table["Data_length"] > $max_tablesize) {
189                                 continue;
190                         }
191
192                         // Don't optimize empty tables
193                         if ($table["Data_length"] == 0) {
194                                 continue;
195                         }
196
197                         // Calculate fragmentation
198                         $fragmentation = $table["Data_free"] / ($table["Data_length"] + $table["Index_length"]);
199
200                         logger("Table ".$table["Name"]." - Fragmentation level: ".round($fragmentation * 100, 2), LOGGER_DEBUG);
201
202                         // Don't optimize tables that needn't to be optimized
203                         if ($fragmentation < $fragmentation_level) {
204                                 continue;
205                         }
206
207                         // So optimize it
208                         logger("Optimize Table ".$table["Name"], LOGGER_DEBUG);
209                         q("OPTIMIZE TABLE `%s`", dbesc($table["Name"]));
210                 }
211         }
212
213         set_config('system','cache_last_cleared', time());
214 }
215
216 /**
217  * @brief Repair missing values in Diaspora contacts
218  *
219  * @param App $a
220  */
221 function cron_repair_diaspora(App $a) {
222
223         $starttime = time();
224
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)) {
229                 return;
230         }
231
232         foreach ($r AS $contact) {
233                 // Quit the loop after 3 minutes
234                 if (time() > ($starttime + 180)) {
235                         return;
236                 }
237
238                 if (!poco_reachable($contact["url"])) {
239                         continue;
240                 }
241
242                 $data = Probe::uri($contact["url"]);
243                 if ($data["network"] != NETWORK_DIASPORA) {
244                         continue;
245                 }
246
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"]));
251         }
252 }
253
254 /**
255  * @brief Do some repairs in database entries
256  *
257  */
258 function cron_repair_database() {
259
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']);
267                 }
268         }
269
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");
273
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`=''");
276
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"]);
282                 }
283         }
284
285         /// @todo
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
290 }