ExpireVersions.php 2.1 KB

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