1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) STMicroelectronics, 2012. All Rights Reserved. |
4
|
|
|
* Copyright (c) Enalean, 2014. All Rights Reserved. |
5
|
|
|
* |
6
|
|
|
* This file is a part of Tuleap. |
7
|
|
|
* |
8
|
|
|
* Tuleap is free software; you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Tuleap is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Tuleap. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class responsible to send indexation requests for tracker changesets to an indexation server |
24
|
|
|
*/ |
25
|
|
|
class FullTextSearchTrackerActions { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Logger |
29
|
|
|
*/ |
30
|
|
|
private $logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ElasticSearch_1_2_RequestTrackerDataFactory |
34
|
|
|
*/ |
35
|
|
|
private $tracker_data_factory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var FullTextSearch_IIndexDocuments |
39
|
|
|
*/ |
40
|
|
|
private $client; |
41
|
|
|
|
42
|
|
|
/** Constructor |
43
|
|
|
* |
44
|
|
|
* @param FullTextSearch_IIndexDocuments $client Search client |
45
|
|
|
* |
46
|
|
|
* @return Void |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function __construct(FullTextSearch_IIndexDocuments $client, ElasticSearch_1_2_RequestTrackerDataFactory $tracker_data_factory, Logger $logger) { |
49
|
|
|
$this->client = $client; |
50
|
|
|
$this->tracker_data_factory = $tracker_data_factory; |
51
|
|
|
$this->logger = $logger; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Index an artifact |
56
|
|
|
* |
57
|
|
|
* @param Tracker_Artifact $artifact The artifact to index |
58
|
|
|
*/ |
59
|
|
|
public function indexArtifactUpdate(Tracker_Artifact $artifact) { |
60
|
|
|
$this->initializeMapping($artifact->getTracker()); |
61
|
|
|
$this->logger->debug('[Tracker] Elasticsearch index artifact #' . $artifact->getId() . ' in tracker #' . $artifact->getTrackerId()); |
62
|
|
|
$this->client->index( |
63
|
|
|
$artifact->getTrackerId(), |
64
|
|
|
$artifact->getId(), |
65
|
|
|
$this->tracker_data_factory->getFormattedArtifact($artifact) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function reIndexProjectArtifacts(array $trackers) { |
70
|
|
|
foreach ($trackers as $tracker) { |
71
|
|
|
$this->reIndexTracker($tracker); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function reIndexTracker(Tracker $tracker) { |
76
|
|
|
$this->deleteTracker($tracker); |
77
|
|
|
$this->indexAllArtifacts($tracker); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function deleteTracker($tracker) { |
81
|
|
|
$tracker_id = $tracker->getId(); |
82
|
|
|
|
83
|
|
|
$this->logger->debug('[Tracker] ElasticSearch: deleting all artifacts of tracker #' . $tracker_id); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$this->client->getIndexedType($tracker_id); |
87
|
|
|
$this->client->deleteType($tracker_id); |
|
|
|
|
88
|
|
|
|
89
|
|
|
} catch (ElasticSearch_TypeNotIndexed $e) { |
90
|
|
|
$this->logger->debug('[Tracker] ElasticSearch: tracker #' . $tracker_id . ' has not previously been indexed, nothing to delete'); |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function indexAllArtifacts(Tracker $tracker) { |
96
|
|
|
$tracker_id = $tracker->getId(); |
97
|
|
|
$tracker_artifact_factory = Tracker_ArtifactFactory::instance(); |
98
|
|
|
$tracker_artifact_iterator = new Tracker_Artifact_BatchIterator($tracker_artifact_factory, $tracker_id); |
99
|
|
|
|
100
|
|
|
$this->logger->debug('[Tracker] ElasticSearch: indexing all artifacts of tracker #' . $tracker_id); |
101
|
|
|
|
102
|
|
|
$tracker_artifact_iterator->rewind(); |
103
|
|
|
while ($batch = $tracker_artifact_iterator->next()) { |
104
|
|
|
foreach ($batch as $artifact) { |
105
|
|
|
$this->indexArtifactUpdate($artifact); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function initializeMapping(Tracker $tracker) { |
111
|
|
|
if (! $this->mappingExists($tracker)) { |
112
|
|
|
$this->logger->debug('[Tracker] Elasticsearch set mapping for tracker #'.$tracker->getId()); |
113
|
|
|
$this->client->setMapping((string) $tracker->getId(), $this->tracker_data_factory->getTrackerMapping($tracker)); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function mappingExists(Tracker $tracker) { |
118
|
|
|
return count($this->client->getMapping((string) $tracker->getId())) > 0; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function deleteArtifactIndex($artifact_id, $tracker_id) { |
122
|
|
|
$this->client->delete( |
123
|
|
|
$tracker_id, |
124
|
|
|
$artifact_id |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function deleteTrackerIndex($tracker_id) { |
129
|
|
|
$this->client->deleteType( |
|
|
|
|
130
|
|
|
$tracker_id |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.