ExpireVersions.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_Versions\BackgroundJob;
  24. use OCP\IUser;
  25. use OCP\IUserManager;
  26. use OCA\Files_Versions\AppInfo\Application;
  27. use OCA\Files_Versions\Storage;
  28. use OCA\Files_Versions\Expiration;
  29. class ExpireVersions extends \OC\BackgroundJob\TimedJob {
  30. const ITEMS_PER_SESSION = 1000;
  31. /**
  32. * @var Expiration
  33. */
  34. private $expiration;
  35. /**
  36. * @var IUserManager
  37. */
  38. private $userManager;
  39. public function __construct(IUserManager $userManager = null, Expiration $expiration = null) {
  40. // Run once per 30 minutes
  41. $this->setInterval(60 * 30);
  42. if (is_null($expiration) || is_null($userManager)) {
  43. $this->fixDIForJobs();
  44. } else {
  45. $this->expiration = $expiration;
  46. $this->userManager = $userManager;
  47. }
  48. }
  49. protected function fixDIForJobs() {
  50. $application = new Application();
  51. $this->expiration = $application->getContainer()->query('Expiration');
  52. $this->userManager = \OC::$server->getUserManager();
  53. }
  54. protected function run($argument) {
  55. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  56. if (!$maxAge) {
  57. return;
  58. }
  59. $this->userManager->callForSeenUsers(function(IUser $user) {
  60. $uid = $user->getUID();
  61. if (!$this->setupFS($uid)) {
  62. return;
  63. }
  64. Storage::expireOlderThanMaxForUser($uid);
  65. });
  66. }
  67. /**
  68. * Act on behalf on trash item owner
  69. * @param string $user
  70. * @return boolean
  71. */
  72. protected function setupFS($user) {
  73. \OC_Util::tearDownFS();
  74. \OC_Util::setupFS($user);
  75. // Check if this user has a versions directory
  76. $view = new \OC\Files\View('/' . $user);
  77. if (!$view->is_dir('/files_versions')) {
  78. return false;
  79. }
  80. return true;
  81. }
  82. }