Passed
Push — master ( adc4f1...ee57ef )
by Roeland
14:15 queued 10s
created

BuildSocialSearchIndex::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * @copyright 2017 Georg Ehrke <[email protected]>
4
 *
5
 * @author Georg Ehrke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\DAV\Migration;
27
28
use OCP\BackgroundJob\IJobList;
29
use OCP\IConfig;
30
use OCP\IDBConnection;
31
use OCP\Migration\IOutput;
32
use OCP\Migration\IRepairStep;
33
34
class BuildSocialSearchIndex implements IRepairStep {
35
36
	/** @var IDBConnection */
37
	private $db;
38
39
	/** @var IJobList */
40
	private $jobList;
41
42
	/** @var IConfig */
43
	private $config;
44
45
	/**
46
	 * @param IDBConnection $db
47
	 * @param IJobList $jobList
48
	 * @param IConfig $config
49
	 */
50
	public function __construct(IDBConnection $db,
51
								IJobList $jobList,
52
								IConfig $config) {
53
		$this->db = $db;
54
		$this->jobList = $jobList;
55
		$this->config = $config;
56
	}
57
58
	/**
59
	 * @return string
60
	 */
61
	public function getName() {
62
		return 'Register building of social profile search index as background job';
63
	}
64
65
	/**
66
	 * @param IOutput $output
67
	 */
68
	public function run(IOutput $output) {
69
		// only run once
70
		if ($this->config->getAppValue('dav', 'builtSocialSearchIndex') === 'yes') {
71
			$output->info('Repair step already executed');
72
			return;
73
		}
74
75
		$query = $this->db->getQueryBuilder();
76
		$query->select($query->func()->max('cardid'))
77
			->from('cards_properties')
78
			->where($query->expr()->eq('name', $query->createNamedParameter('X-SOCIALPROFILE')));
79
		$maxId = (int)$query->execute()->fetchColumn();
80
		
81
		if ($maxId === 0) {
82
			return;
83
		}
84
85
		$output->info('Add background job');
86
		$this->jobList->add(BuildSocialSearchIndexBackgroundJob::class, [
87
			'offset' => 0,
88
			'stopAt' => $maxId
89
		]);
90
91
		// no need to redo the repair during next upgrade
92
		$this->config->setAppValue('dav', 'builtSocialSearchIndex', 'yes');
93
	}
94
}
95