Completed
Push — master ( 2079de...41fdc1 )
by Maxence
02:55
created

cleanBuggyDuplicateEntries()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 3
eloc 20
nc 3
nop 4
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Migration;
28
29
use OC\Share\Share;
30
use OCA\Circles\AppInfo\Application;
31
use OCA\Circles\Db\CoreRequestBuilder;
32
use OCA\Circles\Model\Circle;
33
use OCP\IConfig;
34
use OCP\IDBConnection;
35
use OCP\Migration\IOutput;
36
use OCP\Migration\IRepairStep;
37
38
/**
39
 * Class UsingShortenUniqueIdInsteadOfCircleId
40
 *
41
 * @package OCA\Circles\Migration
42
 */
43
class UsingShortenUniqueIdInsteadOfCircleId implements IRepairStep {
44
45
	/** @var IDBConnection */
46
	protected $connection;
47
48
	/** @var  IConfig */
49
	protected $config;
50
51
	public function __construct(IDBConnection $connection, IConfig $config) {
52
		$this->connection = $connection;
53
		$this->config = $config;
54
	}
55
56
	/**
57
	 * Returns the step's name
58
	 *
59
	 * @return string
60
	 * @since 9.1.0
61
	 */
62
	public function getName() {
63
		return 'Using shorten unique id instead of circle id';
64
	}
65
66
	/**
67
	 * @param IOutput $output
68
	 */
69
	public function run(IOutput $output) {
70
		$oldVersion = explode(
71
			'.', \OC::$server->getConfig()
72
							 ->getAppValue(Application::APP_NAME, 'installed_version', '')
73
		);
74
75
		if ((int)$oldVersion[0] === 0
76
			&& (int)$oldVersion[1] < 13) {
77
			$this->swapToShortenUniqueId();
78
		}
79
	}
80
81
82
	private function swapToShortenUniqueId() {
83
84
		$qb = $this->connection->getQueryBuilder();
85
86
		/** @noinspection PhpMethodParametersCountMismatchInspection */
87
		$qb->select('id', 'unique_id')
88
		   ->from(CoreRequestBuilder::TABLE_CIRCLES);
89
90
		$cursor = $qb->execute();
91
		while ($data = $cursor->fetch()) {
92
			$circleId = $data['id'];
93
			$shortenUniqueId = substr($data['unique_id'], 0, Circle::UNIQUEID_SHORT_LENGTH);
94
95
			$this->swapToShortenUniqueIdInTable(
96
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_GROUPS
97
			);
98
			$this->swapToShortenUniqueIdInTable(
99
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS
100
			);
101
102
//			$this->cleanBuggyDuplicateEntries(
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
//				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_MEMBERS, 'user_id'
104
//			);
105
			$this->swapToShortenUniqueIdInTable(
106
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_MEMBERS
107
			);
108
109
			$this->swapToShortenUniqueIdInTable(
110
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS
111
			);
112
			$this->swapToShortenUniqueIdInShares($circleId, $shortenUniqueId);
113
		}
114
		$cursor->closeCursor();
115
	}
116
117
118
	private function swapToShortenUniqueIdInTable($circleId, $shortenUniqueId, $table) {
119
120
		$qb = $this->connection->getQueryBuilder();
121
		$qb->update($table)
122
		   ->where(
123
			   $qb->expr()
124
				  ->eq('circle_id', $qb->createNamedParameter($circleId))
125
		   );
126
127
		$qb->set('circle_id', $qb->createNamedParameter($shortenUniqueId));
128
		$qb->execute();
129
	}
130
131
132 View Code Duplication
	private function swapToShortenUniqueIdInShares($circleId, $shortenUniqueId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
		$qb = $this->connection->getQueryBuilder();
134
		$expr = $qb->expr();
135
136
		/** @noinspection PhpMethodParametersCountMismatchInspection */
137
		$qb->update('share')
138
		   ->where(
139
			   $expr->andX(
140
				   $expr->eq(
141
					   'share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE)
142
				   ),
143
				   $expr->eq('share_with', $qb->createNamedParameter($circleId))
144
			   )
145
		   );
146
147
		$qb->set('share_with', $qb->createNamedParameter($shortenUniqueId));
148
		$qb->execute();
149
	}
150
151
152
//	private function cleanBuggyDuplicateEntries($circleId, $shortenUniqueId, $table, $field) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
153
//
154
//		$qb = $this->connection->getQueryBuilder();
155
//		$expr = $qb->expr();
156
//
157
//		$qb->select($field)
158
//		   ->from($table)
159
//		   ->where(
160
//			   $expr->eq('circle_id', $qb->createNamedParameter($circleId))
161
//		   );
162
//
163
//		$cursor = $qb->execute();
164
//		while ($data = $cursor->fetch()) {
165
//			$val = $data[$field];
166
//			if ($val !== '') {
167
//				$qb2 = $this->connection->getQueryBuilder();
168
//				$expr2 = $qb2->expr();
169
//				/** @noinspection PhpMethodParametersCountMismatchInspection */
170
//				$qb2->delete($table)
171
//					->where(
172
//						$expr2->andX(
173
//							$expr2->eq('circle_id', $qb2->createNamedParameter($shortenUniqueId)),
174
//							$expr2->eq($field, $qb2->createNamedParameter($val))
175
//						)
176
//					);
177
//				$qb2->execute();
178
//			}
179
//		}
180
//		$cursor->closeCursor();
181
//	}
182
183
}
184
185
186