Completed
Pull Request — master (#64)
by Joas
02:32
created

ImportOwncloudCustomGroups::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Circles\Migration;
25
26
use OCA\Circles\Model\Circle;
27
use OCA\Circles\Model\Member;
28
use OCP\IConfig;
29
use OCP\IDBConnection;
30
use OCP\Migration\IOutput;
31
use OCP\Migration\IRepairStep;
32
33
/**
34
 * Class ImportOwncloudCustomGroups
35
 *
36
 * @package OCA\Circles\Migration
37
 */
38
class ImportOwncloudCustomGroups implements IRepairStep {
39
40
	/** @var IDBConnection */
41
	protected $connection;
42
43
	/** @var  IConfig */
44
	protected $config;
45
46
	/** @var array */
47
	protected $circles = [];
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 'Fix the share type of guest shares when migrating from ownCloud';
62
	}
63
64
	/**
65
	 * @param IOutput $output
66
	 */
67
	public function run(IOutput $output) {
68
		if (!$this->shouldRun()) {
69
			return;
70
		}
71
72
		$this->createCircles($output);
73
		$this->createMemberships($output);
74
75
		$this->config->setAppValue('circles', 'imported_custom_groups', 'true');
76
	}
77
78
	/**
79
	 * @param IOutput $output
80
	 */
81
	public function createCircles(IOutput $output) {
82
		$output->info('Creating circles');
83
84
		$select = $this->connection->getQueryBuilder();
85
		$select->select('*')
86
			->from('custom_group')
87
			->orderBy('group_id');
88
89
		$insert = $this->connection->getQueryBuilder();
90
		$insert->insert('circles_circles')
91
			->values([
92
				'name' => $insert->createParameter('name'),
93
				'type' => $insert->createParameter('type'),
94
				'creation' => $insert->createFunction('NOW()'),
95
			]);
96
97
		$output->startProgress();
98
		$result = $select->execute();
99
100
		while ($row = $result->fetch()) {
101
			$insert->setParameter('name', $row['display_name'])
102
				->setParameter('type', Circle::CIRCLES_PERSONAL);
103
104
			$insert->execute();
105
			$output->advance();
106
107
			$this->circles[$row['groud_id']] = $insert->getLastInsertId();
108
		}
109
110
		$result->closeCursor();
111
		$output->finishProgress();
112
	}
113
114
	/**
115
	 * @param IOutput $output
116
	 */
117
	public function createMemberships(IOutput $output) {
118
		$output->info('Creating memberships');
119
120
		$select = $this->connection->getQueryBuilder();
121
		$select->select('*')
122
			->from('custom_group_member')
123
			->orderBy('group_id');
124
125
		$insert = $this->connection->getQueryBuilder();
126
		$insert->insert('circles_members')
127
			->values([
128
				'circle_id' => $insert->createParameter('circle_id'),
129
				'user_id' => $insert->createParameter('user_id'),
130
				'level' => $insert->createParameter('level'),
131
				'status' => $insert->createParameter('status'),
132
				'joined' => $insert->createFunction('NOW()'),
133
			]);
134
135
		$output->startProgress();
136
		$result = $select->execute();
137
138
		while ($row = $result->fetch()) {
139
			if (!isset($this->circles[$row['group_id']])) {
140
				// Stray membership
141
				continue;
142
			}
143
144
			$insert->setParameter('circle_id', $this->circles[$row['group_id']])
145
				->setParameter('user_id', $row['user_id'])
146
				->setParameter('level', (int) $row['role'] === 1 ? Member::LEVEL_OWNER : Member::LEVEL_MEMBER)
147
				->setParameter('status', 'Member');
148
149
			$insert->execute();
150
			$output->advance();
151
		}
152
153
		$result->closeCursor();
154
		$output->finishProgress();
155
	}
156
157
	protected function shouldRun() {
158
		$alreadyImported = $this->config->getAppValue('circles', 'imported_custom_groups', 'false');
159
		return !$alreadyImported && $this->connection->tableExists('custom_group') && $this->connection->tableExists('custom_group_member');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
160
	}
161
162
}
163