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

BuildSocialSearchIndexBackgroundJob::buildIndex()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 28
rs 9.7333
1
<?php
2
/**
3
 * @copyright 2020 Matthias Heinisch <[email protected]>
4
 *
5
 * @author Matthias Heinisch <[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\DAV\Migration;
25
26
use OC\BackgroundJob\QueuedJob;
27
use OCA\DAV\CardDAV\CardDavBackend;
28
use OCP\AppFramework\Utility\ITimeFactory;
29
use OCP\BackgroundJob\IJobList;
30
use OCP\IDBConnection;
31
use OCP\ILogger;
32
33
class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
34
35
	/** @var IDBConnection */
36
	private $db;
37
38
	/** @var CardDavBackend */
39
	private $davBackend;
40
41
	/** @var ILogger */
42
	private $logger;
43
44
	/** @var IJobList */
45
	private $jobList;
46
47
	/** @var ITimeFactory */
48
	private $timeFactory;
49
50
	/**
51
	 * @param IDBConnection $db
52
	 * @param CardDavBackend $davBackend
53
	 * @param ILogger $logger
54
	 * @param IJobList $jobList
55
	 * @param ITimeFactory $timeFactory
56
	 */
57
	public function __construct(IDBConnection $db,
58
								CardDavBackend $davBackend,
59
								ILogger $logger,
60
								IJobList $jobList,
61
								ITimeFactory $timeFactory) {
62
		$this->db = $db;
63
		$this->davBackend = $davBackend;
64
		$this->logger = $logger;
65
		$this->jobList = $jobList;
66
		$this->timeFactory = $timeFactory;
67
	}
68
69
	public function run($arguments) {
70
		$offset = $arguments['offset'];
71
		$stopAt = $arguments['stopAt'];
72
73
		$this->logger->info('Indexing social profile data (' . $offset .'/' . $stopAt . ')');
74
75
		$offset = $this->buildIndex($offset, $stopAt);
76
77
		if ($offset >= $stopAt) {
78
			$this->logger->info('All contacts with social profiles indexed');
79
		} else {
80
			$this->jobList->add(self::class, [
81
				'offset' => $offset,
82
				'stopAt' => $stopAt
83
			]);
84
			$this->logger->info('New social profile indexing job scheduled with offset ' . $offset);
85
		}
86
	}
87
88
	/**
89
	 * @param int $offset
90
	 * @param int $stopAt
91
	 * @return int
92
	 */
93
	private function buildIndex($offset, $stopAt) {
94
		$startTime = $this->timeFactory->getTime();
95
96
		// get contacts with social profiles
97
		$query = $this->db->getQueryBuilder();
98
		$query->select('id', 'addressbookid', 'uri', 'carddata')
99
			->from('cards', 'c')
100
			->orderBy('id', 'ASC')
101
			->where($query->expr()->like('carddata', $query->createNamedParameter('%SOCIALPROFILE%')))
102
			->setMaxResults(100);
103
		$social_cards = $query->execute()->fetchAll();
104
105
		if (empty($social_cards)) {
106
			return $stopAt;
107
		}
108
109
		// refresh identified contacts in order to re-index
110
		foreach ($social_cards as $contact) {
111
			$offset = $contact['id'];
112
			$this->davBackend->updateCard($contact['addressbookid'], $contact['uri'], $contact['carddata']);
113
114
			// stop after 15sec (to be continued with next chunk)
115
			if (($this->timeFactory->getTime() - $startTime) > 15) {
116
				break;
117
			}
118
		}
119
120
		return $offset;
121
	}
122
}
123