Completed
Pull Request — master (#8)
by Joas
02:06
created

AnnouncementsGroupsLinks::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
23
namespace OCA\AnnouncementCenter\Migration;
24
25
26
use OCP\DB\QueryBuilder\IQueryBuilder;
27
use OCP\IDBConnection;
28
use OCP\Migration\IOutput;
29
use OCP\Migration\IRepairStep;
30
31
class AnnouncementsGroupsLinks implements IRepairStep {
32
33
	/** @var IDBConnection */
34
	protected $connection;
35
36
	/**
37
	 * @param IDBConnection $connection
38
	 */
39
	public function __construct(IDBConnection $connection) {
40
		$this->connection = $connection;
41
	}
42
43
	/**
44
	 * Returns the step's name
45
	 *
46
	 * @return string
47
	 * @since 9.1.0
48
	 */
49
	public function getName() {
50
		return 'Add read permissions for existing announcements';
51
	}
52
53
	/**
54
	 * Run repair step.
55
	 * Must throw exception on error.
56
	 *
57
	 * @since 9.1.0
58
	 * @param IOutput $output
59
	 * @throws \Exception in case of failure
60
	 */
61
	public function run(IOutput $output) {
62
		$queryInsert = $this->connection->getQueryBuilder();
63
		$queryInsert->insert('announcements_groups')
64
			->values([
65
				'announcement_id' => $queryInsert->createParameter('aid'),
66
				'gid' => $queryInsert->createNamedParameter('everyone'),
67
			]);
68
69
		$query = $this->connection->getQueryBuilder();
70
		$query->select(['a.announcement_id', 'a.announcement_subject'])
71
			->from('announcements', 'a')
72
			->leftJoin('a', 'announcements_groups', 'ag', $query->expr()->eq(
73
				'a.announcement_id', 'ag.announcement_id'
74
			))
75
			->where($query->expr()->isNull('ag.gid'));
76
		$result = $query->execute();
77
78
		$output->startProgress();
79
		while ($row = $result->fetch()) {
80
			$output->advance(1, $row['announcement_subject']);
81
			$queryInsert->setParameter('aid', (int) $row['announcement_id'])
82
				->execute();
83
		}
84
		$output->finishProgress();
85
86
		$result->closeCursor();
87
	}
88
}