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 Victor Dubiniuk <dubiniuk@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_Versions\BackgroundJob;
  25. use OCP\IUser;
  26. use OCP\IUserManager;
  27. use OCA\Files_Versions\AppInfo\Application;
  28. use OCA\Files_Versions\Storage;
  29. use OCA\Files_Versions\Expiration;
  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. }