Completed
Pull Request — master (#105)
by Maxence
02:19
created

swapToShortenUniqueIdInShares()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
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 OCA\Circles\Db\CoreRequestBuilder;
30
use OCA\Circles\Model\Circle;
31
use OCP\IConfig;
32
use OCP\IDBConnection;
33
use OCP\Migration\IOutput;
34
use OCP\Migration\IRepairStep;
35
36
/**
37
 * Class UsingShortenUniqueIdInsteadOfCircleId
38
 *
39
 * @package OCA\Circles\Migration
40
 */
41
class UsingShortenUniqueIdInsteadOfCircleId implements IRepairStep {
42
43
	/** @var IDBConnection */
44
	protected $connection;
45
46
	/** @var  IConfig */
47
	protected $config;
48
49
	public function __construct(IDBConnection $connection, IConfig $config) {
50
		$this->connection = $connection;
51
		$this->config = $config;
52
	}
53
54
	/**
55
	 * Returns the step's name
56
	 *
57
	 * @return string
58
	 * @since 9.1.0
59
	 */
60
	public function getName() {
61
		return 'Using shorten unique id instead of circle id';
62
	}
63
64
	/**
65
	 * @param IOutput $output
66
	 */
67
	public function run(IOutput $output) {
68
		$oldVersion = explode(
69
			'.', \OC::$server->getConfig()
70
							 ->getAppValue('circles', 'installed_version', '')
71
		);
72
73
		if ((int)$oldVersion[0] === 0 && $oldVersion[1] < 12) {
74
			$this->swapToShortenUniqueId();
75
		}
76
	}
77
78
79
	private function swapToShortenUniqueId() {
80
81
		$qb = $this->connection->getQueryBuilder();
82
83
		/** @noinspection PhpMethodParametersCountMismatchInspection */
84
		$qb->select('id', 'unique_id')
85
		   ->from(CoreRequestBuilder::TABLE_CIRCLES);
86
87
		$cursor = $qb->execute();
88
		while ($data = $cursor->fetch()) {
89
			$circleId = $data['id'];
90
			$shortenUniqueId = substr($data['unique_id'], 0, Circle::UNIQUEID_SHORT_LENGTH);
91
92
			$this->swapToShortenUniqueIdInTable(
93
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_GROUPS
94
			);
95
			$this->swapToShortenUniqueIdInTable(
96
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS
97
			);
98
			$this->swapToShortenUniqueIdInTable(
99
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_MEMBERS
100
			);
101
			$this->swapToShortenUniqueIdInTable(
102
				$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS
103
			);
104
			$this->swapToShortenUniqueIdInShares($circleId, $shortenUniqueId);
105
		}
106
		$cursor->closeCursor();
107
	}
108
109
110
	private function swapToShortenUniqueIdInTable($circleId, $shortenUniqueId, $table) {
111
		$qb = $this->connection->getQueryBuilder();
112
		$qb->update($table)
113
		   ->where(
114
			   $qb->expr()
115
				  ->eq('circle_id', $qb->createNamedParameter($circleId))
116
		   );
117
118
		$qb->set('circle_id', $qb->createNamedParameter($shortenUniqueId));
119
		$qb->execute();
120
	}
121
122
123
	private function swapToShortenUniqueIdInShares($circleId, $shortenUniqueId) {
124
		$qb = $this->connection->getQueryBuilder();
125
		$expr = $qb->expr();
126
127
		/** @noinspection PhpMethodParametersCountMismatchInspection */
128
		$qb->update('share')
129
		   ->where(
130
			   $expr->andX(
131
				   $expr->eq('share_type', $qb->createNamedParameter(\OC\Share\Share::SHARE_TYPE_CIRCLE)),
132
				   $expr->eq('share_with', $qb->createNamedParameter($circleId))
133
			   )
134
		   );
135
136
		$qb->set('share_with', $qb->createNamedParameter($shortenUniqueId));
137
		$qb->execute();
138
	}
139
140
}
141
142
143