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

BuildCalendarSearchIndexBackgroundJob   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 89
rs 10
wmc 7
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A run() 0 18 2
B buildIndex() 0 28 4
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 OC\BackgroundJob\QueuedJob;
26
use OCA\DAV\CalDAV\CalDavBackend;
27
use OCP\AppFramework\Utility\ITimeFactory;
28
use OCP\BackgroundJob\IJobList;
29
use OCP\IDBConnection;
30
use OCP\ILogger;
31
32
class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
33
34
	/** @var IDBConnection */
35
	private $db;
36
37
	/** @var CalDavBackend */
38
	private $calDavBackend;
39
40
	/** @var ILogger */
41
	private $logger;
42
43
	/** @var IJobList */
44
	private $jobList;
45
46
	/** @var ITimeFactory */
47
	private $timeFactory;
48
49
	/**
50
	 * @param IDBConnection $db
51
	 * @param CalDavBackend $calDavBackend
52
	 * @param ILogger $logger
53
	 * @param IJobList $jobList
54
	 * @param ITimeFactory $timeFactory
55
	 */
56
	public function __construct(IDBConnection $db,
57
								CalDavBackend $calDavBackend,
58
								ILogger $logger,
59
								IJobList $jobList,
60
								ITimeFactory $timeFactory) {
61
		$this->db = $db;
62
		$this->calDavBackend = $calDavBackend;
63
		$this->logger = $logger;
64
		$this->jobList = $jobList;
65
		$this->timeFactory = $timeFactory;
66
	}
67
68
	public function run($arguments) {
69
		$offset = $arguments['offset'];
70
		$stopAt = $arguments['stopAt'];
71
72
		$this->logger->info('Building calendar index (' . $offset .'/' . $stopAt . ')');
73
74
		$offset = $this->buildIndex($offset, $stopAt);
75
76
		if ($offset >= $stopAt) {
77
			$this->logger->info('Building calendar index done');
78
		} else {
79
			$this->jobList->add(self::class, [
80
				'offset' => $offset,
81
				'stopAt' => $stopAt
82
			]);
83
			$this->logger->info('New building calendar index job scheduled with offset ' . $offset);
84
		}
85
	}
86
87
	/**
88
	 * @param int $offset
89
	 * @param int $stopAt
90
	 * @return int
91
	 */
92
	private function buildIndex($offset, $stopAt) {
93
		$startTime = $this->timeFactory->getTime();
94
95
		$query = $this->db->getQueryBuilder();
96
		$query->select(['id', 'calendarid', 'uri', 'calendardata'])
97
			->from('calendarobjects')
98
			->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
99
			->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
100
			->orderBy('id', 'ASC');
101
102
		$stmt = $query->execute();
103
		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
104
			$offset = $row['id'];
105
106
			$calendarData = $row['calendardata'];
107
			if (is_resource($calendarData)) {
108
				$calendarData = stream_get_contents($calendarData);
109
			}
110
111
			$this->calDavBackend->updateProperties($row['calendarid'], $row['uri'], $calendarData);
112
113
			if (($this->timeFactory->getTime() - $startTime) > 15) {
114
				return $offset;
115
			}
116
		}
117
118
		return $stopAt;
119
	}
120
}
121