Completed
Pull Request — master (#186)
by Lukas
10:10
created

RemoveRootShares   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 122
rs 10
wmc 9
lcom 1
cbo 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getName() 0 3 1
A run() 0 5 2
B removeRootShares() 0 25 1
A countUsers() 0 10 2
B rootSharesExist() 0 27 2
1
<?php
2
/**
3
 * @author Roeland Jago Douma <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
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
namespace OC\Repair;
22
23
use OCP\Files\IRootFolder;
24
use OCP\IDBConnection;
25
use OCP\IUser;
26
use OCP\IUserManager;
27
use OCP\Migration\IOutput;
28
use OCP\Migration\IRepairStep;
29
30
/**
31
 * Class RemoveRootShares
32
 *
33
 * @package OC\Repair
34
 */
35
class RemoveRootShares implements IRepairStep {
36
37
	/** @var IDBConnection */
38
	protected $connection;
39
40
	/** @var IUserManager */
41
	protected $userManager;
42
43
	/** @var IRootFolder */
44
	protected $rootFolder;
45
46
	/**
47
	 * RemoveRootShares constructor.
48
	 *
49
	 * @param IDBConnection $connection
50
	 * @param IUserManager $userManager
51
	 * @param IRootFolder $rootFolder
52
	 */
53
	public function __construct(IDBConnection $connection,
54
								IUserManager $userManager,
55
								IRootFolder $rootFolder) {
56
		$this->connection = $connection;
57
		$this->userManager = $userManager;
58
		$this->rootFolder = $rootFolder;
59
	}
60
61
	/**
62
	 * @return string
63
	 */
64
	public function getName() {
65
		return 'Remove shares of a users root folder';
66
	}
67
68
	/**
69
	 * @param IOutput $output
70
	 */
71
	public function run(IOutput $output) {
72
		if ($this->rootSharesExist()) {
73
			$this->removeRootShares($output);
74
		}
75
	}
76
77
	/**
78
	 * @param IOutput $output
79
	 */
80
	private function removeRootShares(IOutput $output) {
81
		$function = function(IUser $user) use ($output) {
82
			$userFolder = $this->rootFolder->getUserFolder($user->getUID());
83
			$fileId = $userFolder->getId();
84
85
			$qb = $this->connection->getQueryBuilder();
86
			$qb->delete('share')
87
				->where($qb->expr()->eq('file_source', $qb->createNamedParameter($fileId)))
88
				->andWhere($qb->expr()->orX(
89
					$qb->expr()->eq('item_type', $qb->expr()->literal('file')),
90
					$qb->expr()->eq('item_type', $qb->expr()->literal('folder'))
0 ignored issues
show
Unused Code introduced by
The call to IExpressionBuilder::orX() has too many arguments starting with $qb->expr()->eq('item_ty...r()->literal('folder')).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
91
				));
92
93
			$qb->execute();
94
95
			$output->advance();
96
		};
97
98
		$userCount = $this->countUsers();
99
		$output->startProgress($userCount);
100
101
		$this->userManager->callForAllUsers($function);
102
103
		$output->finishProgress();
104
	}
105
106
	/**
107
	 * Count all the users
108
	 *
109
	 * @return int
110
	 */
111
	private function countUsers() {
112
		$allCount = $this->userManager->countUsers();
113
114
		$totalCount = 0;
115
		foreach ($allCount as $backend => $count) {
116
			$totalCount += $count;
117
		}
118
119
		return $totalCount;
120
	}
121
122
	/**
123
	 * Verify if this repair steps is required
124
	 * It *should* not be necessary in most cases and it can be very
125
	 * costly.
126
	 *
127
	 * @return bool
128
	 */
129
	private function rootSharesExist() {
130
		$qb = $this->connection->getQueryBuilder();
131
		$qb2 = $this->connection->getQueryBuilder();
132
133
		$qb->select('fileid')
134
			->from('filecache')
135
			->where($qb->expr()->eq('path', $qb->expr()->literal('files')));
136
137
		$qb2->select('id')
138
			->from('share')
139
			->where($qb2->expr()->in('file_source', $qb2->createFunction($qb->getSQL())))
140
			->andWhere($qb2->expr()->orX(
141
				$qb2->expr()->eq('item_type', $qb->expr()->literal('file')),
142
				$qb2->expr()->eq('item_type', $qb->expr()->literal('folder'))
0 ignored issues
show
Unused Code introduced by
The call to IExpressionBuilder::orX() has too many arguments starting with $qb2->expr()->eq('item_t...r()->literal('folder')).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
143
			))
144
			->setMaxResults(1);
145
146
		$cursor = $qb2->execute();
147
		$data = $cursor->fetch();
148
		$cursor->closeCursor();
149
150
		if ($data === false) {
151
			return false;
152
		}
153
154
		return true;
155
	}
156
}
157
158