Completed
Push — master ( 424d62...2a7733 )
by Morris
11:53
created

BuildCalendarSearchIndex::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 13
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 21
rs 9.3142
1
<?php
2
/**
3
 * @copyright 2017 Georg Ehrke <[email protected]>
4
 *
5
 * @author Georg Ehrke <[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
namespace OCA\DAV\Migration;
24
25
use OCP\BackgroundJob\IJobList;
26
use OCP\IConfig;
27
use OCP\IDBConnection;
28
use OCP\Migration\IOutput;
29
use OCP\Migration\IRepairStep;
30
31
class BuildCalendarSearchIndex implements IRepairStep {
32
33
	/** @var IDBConnection */
34
	private $db;
35
36
	/** @var IJobList */
37
	private $jobList;
38
39
	/** @var IConfig */
40
	private $config;
41
42
	/**
43
	 * @param IDBConnection $db
44
	 * @param IJobList $jobList
45
	 * @param IConfig $config
46
	 */
47
	public function __construct(IDBConnection $db,
48
								IJobList $jobList,
49
								IConfig $config) {
50
		$this->db = $db;
51
		$this->jobList = $jobList;
52
		$this->config = $config;
53
	}
54
55
	/**
56
	 * @return string
57
	 */
58
	public function getName() {
59
		return 'Registering building of calendar search index as background job';
60
	}
61
62
	/**
63
	 * @param IOutput $output
64
	 */
65
	public function run(IOutput $output) {
66
		// only run once
67
		if ($this->config->getAppValue('dav', 'buildCalendarSearchIndex') === 'yes') {
68
			$output->info('Repair step already executed');
69
			return;
70
		}
71
72
		$query = $this->db->getQueryBuilder();
73
		$query->select($query->createFunction('MAX(id)'))
74
			->from('calendarobjects');
75
		$maxId = (int)$query->execute()->fetchColumn();
76
77
		$output->info('Add background job');
78
		$this->jobList->add(BuildCalendarSearchIndexBackgroundJob::class, [
79
			'offset' => 0,
80
			'stopAt' => $maxId
81
		]);
82
83
		// if all were done, no need to redo the repair during next upgrade
84
		$this->config->setAppValue('dav', 'buildCalendarSearchIndex', 'yes');
85
	}
86
}