Completed
Pull Request — stable8.2 (#25917)
by Thomas
42:02
created

ExpireTrash::setupFS()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 12
loc 12
rs 9.4285
c 1
b 0
f 1
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_Trashbin\Command;
23
24
use OCP\IUser;
25
use OCP\IUserManager;
26
use OCA\Files_Trashbin\Expiration;
27
use OCA\Files_Trashbin\Helper;
28
use OCA\Files_Trashbin\Trashbin;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Helper\ProgressBar;
31
use Symfony\Component\Console\Input\InputArgument;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class ExpireTrash extends Command {
36
37
	/**
38
	 * @var Expiration
39
	 */
40
	private $expiration;
41
	
42
	/**
43
	 * @var IUserManager
44
	 */
45
	private $userManager;
46
47
	/**
48
	 * @param IUserManager|null $userManager
49
	 * @param Expiration|null $expiration
50
	 */
51 View Code Duplication
	public function __construct(IUserManager $userManager = null,
52
								Expiration $expiration = null) {
53
		parent::__construct();
54
55
		$this->userManager = $userManager;
56
		$this->expiration = $expiration;
57
	}
58
59
	protected function configure() {
60
		$this
61
			->setName('trashbin:expire')
62
			->setDescription('Expires the users trashbin')
63
			->addArgument(
64
				'user_id',
65
				InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
66
				'expires the trashbin of the given user(s), if no user is given the trash for all users will be expired'
67
			);
68
	}
69
70 View Code Duplication
	protected function execute(InputInterface $input, OutputInterface $output) {
71
72
		$maxAge = $this->expiration->getMaxAgeAsTimestamp();
73
		if (!$maxAge) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxAge of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
74
			$output->writeln("No expiry configured.");
75
			return;
76
		}
77
78
		$users = $input->getArgument('user_id');
79
		if (!empty($users)) {
80
			foreach ($users as $user) {
81
				if ($this->userManager->userExists($user)) {
82
					$output->writeln("Remove deleted files of   <info>$user</info>");
83
					$userObject = $this->userManager->get($user);
84
					$this->expireTrashForUser($userObject);
85
				} else {
86
					$output->writeln("<error>Unknown user $user</error>");
87
				}
88
			}
89
		} else {
90
			$p = new ProgressBar($output);
91
			$p->start();
92
			$this->callForAllUsers(function(IUser $user) use ($p) {
93
				$p->advance();
94
				$this->expireTrashForUser($user);
95
			});
96
			$p->finish();
97
			$output->writeln('');
98
		}
99
	}
100
101 View Code Duplication
	function expireTrashForUser(IUser $user) {
102
		$uid = $user->getUID();
103
		if ($user->getLastLogin() === 0 || !$this->setupFS($uid)) {
104
			return;
105
		}
106
		$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
107
		Trashbin::deleteExpiredFiles($dirContent, $uid);
108
	}
109
110
	/**
111
	 * Act on behalf on trash item owner
112
	 * @param string $user
113
	 * @return boolean
114
	 */
115 View Code Duplication
	protected function setupFS($user) {
116
		\OC_Util::tearDownFS();
117
		\OC_Util::setupFS($user);
118
119
		// Check if this user has a trashbin directory
120
		$view = new \OC\Files\View('/' . $user);
121
		if (!$view->is_dir('/files_trashbin/files')) {
122
			return false;
123
		}
124
125
		return true;
126
	}
127
128 View Code Duplication
	private function callForAllUsers(\Closure $callback, $search = '') {
129
		foreach($this->userManager->getBackends() as $backend) {
130
			$limit = 500;
131
			$offset = 0;
132
			do {
133
				$users = $backend->getUsers($search, $limit, $offset);
134
				foreach ($users as $uid) {
135
					if (!$backend->userExists($uid)) {
136
						continue;
137
					}
138
					$user = $this->userManager->get($uid);
139
					$return = $callback($user);
140
					if ($return === false) {
141
						break;
142
					}
143
				}
144
				$offset += $limit;
145
			} while (count($users) >= $limit);
146
		}
147
	}
148
149
}
150