Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.3 KiB

  1. <?php
  2. /**
  3. * @file src/Worker/Expire.php
  4. * @brief Expires old item entries
  5. */
  6. namespace Friendica\Worker;
  7. use Friendica\BaseObject;
  8. use Friendica\Core\Addon;
  9. use Friendica\Core\Config;
  10. use Friendica\Core\Worker;
  11. use Friendica\Database\DBA;
  12. use Friendica\Model\Item;
  13. require_once 'include/dba.php';
  14. class Expire
  15. {
  16. public static function execute($param = '', $hook_name = '')
  17. {
  18. $a = BaseObject::getApp();
  19. require_once 'include/items.php';
  20. Addon::loadHooks();
  21. if ($param == 'delete') {
  22. logger('Delete expired items', LOGGER_DEBUG);
  23. // physically remove anything that has been deleted for more than two months
  24. $condition = ["`deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY"];
  25. $rows = DBA::select('item', ['id', 'iaid', 'icid', 'psid'], $condition);
  26. while ($row = DBA::fetch($rows)) {
  27. DBA::delete('item', ['id' => $row['id']]);
  28. if (!empty($row['iaid']) && !DBA::exists('item', ['iaid' => $row['iaid']])) {
  29. DBA::delete('item-activity', ['id' => $row['iaid']]);
  30. }
  31. if (!empty($row['icid']) && !DBA::exists('item', ['icid' => $row['icid']])) {
  32. DBA::delete('item-content', ['id' => $row['icid']]);
  33. }
  34. // When the permission set will be used in photo and events as well.
  35. // this query here needs to be extended.
  36. if (!empty($row['psid']) && !DBA::exists('item', ['psid' => $row['psid']])) {
  37. DBA::delete('permissionset', ['id' => $row['psid']]);
  38. }
  39. // When the permission set will be used in photo and events as well.
  40. // this query here needs to be extended.
  41. if (!empty($row['psid']) && !dba::exists('item', ['psid' => $row['psid']])) {
  42. dba::delete('permissionset', ['id' => $row['psid']]);
  43. }
  44. }
  45. DBA::close($rows);
  46. // Normally we shouldn't have orphaned data at all.
  47. // If we do have some, then we have to check why.
  48. logger('Deleting orphaned item activities - start', LOGGER_DEBUG);
  49. $condition = ["NOT EXISTS (SELECT `iaid` FROM `item` WHERE `item`.`iaid` = `item-activity`.`id`)"];
  50. DBA::delete('item-activity', $condition);
  51. logger('Orphaned item activities deleted: ' . DBA::affectedRows(), LOGGER_DEBUG);
  52. logger('Deleting orphaned item content - start', LOGGER_DEBUG);
  53. $condition = ["NOT EXISTS (SELECT `icid` FROM `item` WHERE `item`.`icid` = `item-content`.`id`)"];
  54. DBA::delete('item-content', $condition);
  55. logger('Orphaned item content deleted: ' . DBA::affectedRows(), LOGGER_DEBUG);
  56. // make this optional as it could have a performance impact on large sites
  57. if (intval(Config::get('system', 'optimize_items'))) {
  58. DBA::e("OPTIMIZE TABLE `item`");
  59. }
  60. logger('Delete expired items - done', LOGGER_DEBUG);
  61. return;
  62. } elseif (intval($param) > 0) {
  63. $user = DBA::selectFirst('user', ['uid', 'username', 'expire'], ['uid' => $param]);
  64. if (DBA::isResult($user)) {
  65. logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], LOGGER_DEBUG);
  66. Item::expire($user['uid'], $user['expire']);
  67. logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', LOGGER_DEBUG);
  68. }
  69. return;
  70. } elseif (!empty($hook_name) && ($param == 'hook') && is_array($a->hooks) && array_key_exists("expire", $a->hooks)) {
  71. foreach ($a->hooks["expire"] as $hook) {
  72. if ($hook[1] == $hook_name) {
  73. logger("Calling expire hook '" . $hook[1] . "'", LOGGER_DEBUG);
  74. Addon::callSingleHook($a, $hook_name, $hook, $data);
  75. }
  76. }
  77. return;
  78. }
  79. logger('expire: start');
  80. Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
  81. 'Expire', 'delete');
  82. $r = DBA::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
  83. while ($row = DBA::fetch($r)) {
  84. logger('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', LOGGER_DEBUG);
  85. Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
  86. 'Expire', (int)$row['uid']);
  87. }
  88. DBA::close($r);
  89. logger('expire: calling hooks');
  90. if (is_array($a->hooks) && array_key_exists('expire', $a->hooks)) {
  91. foreach ($a->hooks['expire'] as $hook) {
  92. logger("Calling expire hook for '" . $hook[1] . "'", LOGGER_DEBUG);
  93. Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
  94. 'Expire', 'hook', $hook[1]);
  95. }
  96. }
  97. logger('expire: end');
  98. return;
  99. }
  100. }