1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Thomas Müller <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016, ownCloud GmbH. |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Files_Versions\Command; |
23
|
|
|
|
24
|
|
|
use OCA\Files_Versions\Expiration; |
25
|
|
|
use OCA\Files_Versions\Storage; |
26
|
|
|
use OCP\IUser; |
27
|
|
|
use OCP\IUserManager; |
28
|
|
|
use Symfony\Component\Console\Command\Command; |
29
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
30
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
31
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
32
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
33
|
|
|
|
34
|
|
|
class ExpireVersions extends Command { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Expiration |
38
|
|
|
*/ |
39
|
|
|
private $expiration; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var IUserManager |
43
|
|
|
*/ |
44
|
|
|
private $userManager; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param IUserManager|null $userManager |
48
|
|
|
* @param Expiration|null $expiration |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function __construct(IUserManager $userManager = null, |
51
|
|
|
Expiration $expiration = null) { |
52
|
|
|
parent::__construct(); |
53
|
|
|
|
54
|
|
|
$this->userManager = $userManager; |
55
|
|
|
$this->expiration = $expiration; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function configure() { |
59
|
|
|
$this |
60
|
|
|
->setName('versions:expire') |
61
|
|
|
->setDescription('Expires the users file versions') |
62
|
|
|
->addArgument( |
63
|
|
|
'user_id', |
64
|
|
|
InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
65
|
|
|
'expire file versions of the given user(s), if no user is given file versions for all users will be expired.' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) { |
70
|
|
|
|
71
|
|
|
$maxAge = $this->expiration->getMaxAgeAsTimestamp(); |
72
|
|
|
if (!$maxAge) { |
|
|
|
|
73
|
|
|
$output->writeln("No expiry configured."); |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$users = $input->getArgument('user_id'); |
78
|
|
|
if (!empty($users)) { |
79
|
|
|
foreach ($users as $user) { |
80
|
|
|
if ($this->userManager->userExists($user)) { |
81
|
|
|
$output->writeln("Remove deleted files of <info>$user</info>"); |
82
|
|
|
$userObject = $this->userManager->get($user); |
83
|
|
|
$this->expireVersionsForUser($userObject); |
84
|
|
|
} else { |
85
|
|
|
$output->writeln("<error>Unknown user $user</error>"); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} else { |
89
|
|
|
$p = new ProgressBar($output); |
90
|
|
|
$p->start(); |
91
|
|
|
$this->callForAllUsers(function(IUser $user) use ($p) { |
92
|
|
|
$p->advance(); |
93
|
|
|
$this->expireVersionsForUser($user); |
94
|
|
|
}); |
95
|
|
|
$p->finish(); |
96
|
|
|
$output->writeln(''); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
function expireVersionsForUser(IUser $user) { |
101
|
|
|
$uid = $user->getUID(); |
102
|
|
|
if ($user->getLastLogin() === 0 || !$this->setupFS($uid)) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
Storage::expireOlderThanMaxForUser($uid); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Act on behalf on versions item owner |
110
|
|
|
* @param string $user |
111
|
|
|
* @return boolean |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
protected function setupFS($user) { |
114
|
|
|
\OC_Util::tearDownFS(); |
115
|
|
|
\OC_Util::setupFS($user); |
116
|
|
|
|
117
|
|
|
// Check if this user has a version directory |
118
|
|
|
$view = new \OC\Files\View('/' . $user); |
119
|
|
|
if (!$view->is_dir('/files_versions')) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
private function callForAllUsers(\Closure $callback, $search = '') { |
127
|
|
|
foreach($this->userManager->getBackends() as $backend) { |
128
|
|
|
$limit = 500; |
129
|
|
|
$offset = 0; |
130
|
|
|
do { |
131
|
|
|
$users = $backend->getUsers($search, $limit, $offset); |
132
|
|
|
foreach ($users as $uid) { |
133
|
|
|
if (!$backend->userExists($uid)) { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
$user = $this->userManager->get($uid); |
137
|
|
|
$return = $callback($user); |
138
|
|
|
if ($return === false) { |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
$offset += $limit; |
143
|
|
|
} while (count($users) >= $limit); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: