Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

291 lignes
7.7KB

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