|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeSearchBundle\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Elastica\Index; |
|
7
|
|
|
use Elastica\Type\Mapping; |
|
8
|
|
|
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface; |
|
9
|
|
|
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\MaskBuilder; |
|
10
|
|
|
use Kunstmaan\NodeBundle\Entity\HasNodeInterface; |
|
11
|
|
|
use Kunstmaan\NodeBundle\Entity\Node; |
|
12
|
|
|
use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
|
13
|
|
|
use Kunstmaan\NodeBundle\Entity\NodeVersion; |
|
14
|
|
|
use Kunstmaan\NodeBundle\Entity\PageInterface; |
|
15
|
|
|
use Kunstmaan\NodeBundle\Helper\RenderContext; |
|
16
|
|
|
use Kunstmaan\NodeSearchBundle\Event\IndexNodeEvent; |
|
17
|
|
|
use Kunstmaan\NodeSearchBundle\Helper\IndexablePagePartsService; |
|
18
|
|
|
use Kunstmaan\NodeSearchBundle\Helper\SearchViewTemplateInterface; |
|
19
|
|
|
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface; |
|
20
|
|
|
use Kunstmaan\SearchBundle\Configuration\SearchConfigurationInterface; |
|
21
|
|
|
use Kunstmaan\SearchBundle\Provider\SearchProviderInterface; |
|
22
|
|
|
use Kunstmaan\SearchBundle\Search\AnalysisFactoryInterface; |
|
23
|
|
|
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup; |
|
24
|
|
|
use Psr\Log\LoggerInterface; |
|
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
26
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
27
|
|
|
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
29
|
|
|
use Symfony\Component\Security\Acl\Domain\ObjectIdentity; |
|
30
|
|
|
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; |
|
31
|
|
|
use Symfony\Component\Security\Acl\Exception\AclNotFoundException; |
|
32
|
|
|
use Symfony\Component\Security\Acl\Model\AclInterface; |
|
33
|
|
|
use Symfony\Component\Security\Acl\Model\AclProviderInterface; |
|
34
|
|
|
use Symfony\Component\Security\Acl\Model\AuditableEntryInterface; |
|
35
|
|
|
use Symfony\Component\Templating\EngineInterface; |
|
36
|
|
|
|
|
37
|
|
|
class NodePagesConfiguration implements SearchConfigurationInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var string */ |
|
40
|
|
|
protected $indexName; |
|
41
|
|
|
|
|
42
|
|
|
/** @var string */ |
|
43
|
|
|
protected $indexType; |
|
44
|
|
|
|
|
45
|
|
|
/** @var SearchProviderInterface */ |
|
46
|
|
|
protected $searchProvider; |
|
47
|
|
|
|
|
48
|
|
|
/** @var array */ |
|
49
|
|
|
protected $locales = []; |
|
50
|
|
|
|
|
51
|
|
|
/** @var array */ |
|
52
|
|
|
protected $analyzerLanguages; |
|
53
|
|
|
|
|
54
|
|
|
/** @var EntityManager */ |
|
55
|
|
|
protected $em; |
|
56
|
|
|
|
|
57
|
|
|
/** @var array */ |
|
58
|
|
|
protected $documents = []; |
|
59
|
|
|
|
|
60
|
|
|
/** @var ContainerInterface */ |
|
61
|
|
|
protected $container; |
|
62
|
|
|
|
|
63
|
|
|
/** @var AclProviderInterface */ |
|
64
|
|
|
protected $aclProvider = null; |
|
65
|
|
|
|
|
66
|
|
|
/** @var LoggerInterface */ |
|
67
|
|
|
protected $logger = null; |
|
68
|
|
|
|
|
69
|
|
|
/** @var IndexablePagePartsService */ |
|
70
|
|
|
protected $indexablePagePartsService; |
|
71
|
|
|
|
|
72
|
|
|
/** @var DomainConfigurationInterface */ |
|
73
|
|
|
protected $domainConfiguration; |
|
74
|
|
|
|
|
75
|
|
|
/** @var array */ |
|
76
|
|
|
protected $properties = []; |
|
77
|
|
|
|
|
78
|
|
|
/** @var int */ |
|
79
|
|
|
protected $numberOfShards; |
|
80
|
|
|
|
|
81
|
|
|
/** @var int */ |
|
82
|
|
|
protected $numberOfReplicas; |
|
83
|
|
|
|
|
84
|
|
|
/** @var Node */ |
|
85
|
|
|
protected $currentTopNode = null; |
|
86
|
|
|
|
|
87
|
|
|
/** @var array */ |
|
88
|
|
|
protected $nodeRefs = []; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param ContainerInterface $container |
|
92
|
|
|
* @param SearchProviderInterface $searchProvider |
|
93
|
|
|
* @param string $name |
|
94
|
|
|
* @param string $type |
|
95
|
|
|
*/ |
|
96
|
|
|
public function __construct($container, $searchProvider, $name, $type, $numberOfShards = 1, $numberOfReplicas = 0) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->container = $container; |
|
99
|
|
|
$this->indexName = $name; |
|
100
|
|
|
$this->indexType = $type; |
|
101
|
|
|
$this->searchProvider = $searchProvider; |
|
102
|
|
|
$this->domainConfiguration = $this->container->get('kunstmaan_admin.domain_configuration'); |
|
103
|
|
|
$this->locales = $this->domainConfiguration->getBackendLocales(); |
|
104
|
|
|
$this->analyzerLanguages = $this->container->getParameter('analyzer_languages'); |
|
|
|
|
|
|
105
|
|
|
$this->em = $this->container->get('doctrine')->getManager(); |
|
106
|
|
|
$this->numberOfShards = $numberOfShards; |
|
107
|
|
|
$this->numberOfReplicas = $numberOfReplicas; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param AclProviderInterface $aclProvider |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setAclProvider(AclProviderInterface $aclProvider) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->aclProvider = $aclProvider; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param IndexablePagePartsService $indexablePagePartsService |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setIndexablePagePartsService(IndexablePagePartsService $indexablePagePartsService) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->indexablePagePartsService = $indexablePagePartsService; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param array $properties |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setDefaultProperties(array $properties) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->properties = array_merge($this->properties, $properties); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param LoggerInterface $logger |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setLogger(LoggerInterface $logger) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->logger = $logger; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getLanguagesNotAnalyzed() |
|
146
|
|
|
{ |
|
147
|
|
|
$notAnalyzed = []; |
|
148
|
|
|
foreach ($this->locales as $locale) { |
|
149
|
|
|
if (preg_match('/[a-z]{2}_?+[a-zA-Z]{2}/', $locale)) { |
|
150
|
|
|
$locale = strtolower($locale); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if (false === \array_key_exists($locale, $this->analyzerLanguages)) { |
|
154
|
|
|
$notAnalyzed[] = $locale; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $notAnalyzed; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Create node index |
|
163
|
|
|
*/ |
|
164
|
|
|
public function createIndex() |
|
165
|
|
|
{ |
|
166
|
|
|
//create analysis |
|
167
|
|
|
$analysis = $this->container->get( |
|
168
|
|
|
'kunstmaan_search.search.factory.analysis' |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
foreach ($this->locales as $locale) { |
|
172
|
|
|
// Multilanguage check |
|
173
|
|
|
if (preg_match('/[a-z]{2}_?+[a-zA-Z]{2}/', $locale)) { |
|
174
|
|
|
$locale = strtolower($locale); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// Build new index |
|
178
|
|
|
$index = $this->searchProvider->createIndex($this->indexName . '_' . $locale); |
|
179
|
|
|
|
|
180
|
|
|
if (\array_key_exists($locale, $this->analyzerLanguages)) { |
|
181
|
|
|
$localeAnalysis = clone $analysis; |
|
182
|
|
|
$language = $this->analyzerLanguages[$locale]['analyzer']; |
|
183
|
|
|
|
|
184
|
|
|
// Create index with analysis |
|
185
|
|
|
$this->setAnalysis($index, $localeAnalysis->setupLanguage($language)); |
|
186
|
|
|
} else { |
|
187
|
|
|
$index->create(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$this->setMapping($index, $locale); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Populate node index |
|
196
|
|
|
*/ |
|
197
|
|
|
public function populateIndex() |
|
198
|
|
|
{ |
|
199
|
|
|
$nodeRepository = $this->em->getRepository(Node::class); |
|
200
|
|
|
$nodes = $nodeRepository->getAllTopNodes(); |
|
201
|
|
|
|
|
202
|
|
|
foreach ($nodes as $node) { |
|
203
|
|
|
$this->currentTopNode = $node; |
|
204
|
|
|
foreach ($this->locales as $lang) { |
|
205
|
|
|
$this->createNodeDocuments($node, $lang); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
if (!empty($this->documents)) { |
|
210
|
|
|
$this->searchProvider->addDocuments($this->documents); |
|
211
|
|
|
$this->documents = []; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Index a node (including its children) - for the specified language only |
|
217
|
|
|
* |
|
218
|
|
|
* @param Node $node |
|
219
|
|
|
* @param string $lang |
|
220
|
|
|
*/ |
|
221
|
|
|
public function indexNode(Node $node, $lang) |
|
222
|
|
|
{ |
|
223
|
|
|
$this->createNodeDocuments($node, $lang); |
|
224
|
|
|
|
|
225
|
|
|
if (!empty($this->documents)) { |
|
226
|
|
|
$this->searchProvider->addDocuments($this->documents); |
|
227
|
|
|
$this->documents = []; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Add documents for the node translation (and children) to the index |
|
233
|
|
|
* |
|
234
|
|
|
* @param Node $node |
|
235
|
|
|
* @param string $lang |
|
236
|
|
|
*/ |
|
237
|
|
|
public function createNodeDocuments(Node $node, $lang) |
|
238
|
|
|
{ |
|
239
|
|
|
$nodeTranslation = $node->getNodeTranslation($lang, true); |
|
240
|
|
|
if ($nodeTranslation && $this->indexNodeTranslation($nodeTranslation)) { |
|
241
|
|
|
$this->indexChildren($node, $lang); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Index all children of the specified node (only for the specified |
|
247
|
|
|
* language) |
|
248
|
|
|
* |
|
249
|
|
|
* @param Node $node |
|
250
|
|
|
* @param string $lang |
|
251
|
|
|
*/ |
|
252
|
|
|
public function indexChildren(Node $node, $lang) |
|
253
|
|
|
{ |
|
254
|
|
|
foreach ($node->getChildren() as $childNode) { |
|
255
|
|
|
$this->indexNode($childNode, $lang); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Index a node translation |
|
261
|
|
|
* |
|
262
|
|
|
* @param NodeTranslation $nodeTranslation |
|
263
|
|
|
* @param bool $add Add node immediately to index? |
|
264
|
|
|
* |
|
265
|
|
|
* @return bool Return true if the document has been indexed |
|
266
|
|
|
*/ |
|
267
|
|
|
public function indexNodeTranslation(NodeTranslation $nodeTranslation, $add = false) |
|
268
|
|
|
{ |
|
269
|
|
|
// Retrieve the public NodeVersion |
|
270
|
|
|
$publicNodeVersion = $nodeTranslation->getPublicNodeVersion(); |
|
271
|
|
|
if (\is_null($publicNodeVersion)) { |
|
272
|
|
|
return false; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
$refPage = $this->getNodeRefPage($publicNodeVersion); |
|
276
|
|
|
if ($refPage->isStructureNode()) { |
|
277
|
|
|
return true; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
// Only index online NodeTranslations |
|
281
|
|
|
if (!$nodeTranslation->isOnline()) { |
|
282
|
|
|
return false; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
$node = $nodeTranslation->getNode(); |
|
286
|
|
|
if ($this->isIndexable($refPage)) { |
|
287
|
|
|
// Retrieve the referenced entity from the public NodeVersion |
|
288
|
|
|
$page = $publicNodeVersion->getRef($this->em); |
|
289
|
|
|
|
|
290
|
|
|
$this->addPageToIndex($nodeTranslation, $node, $publicNodeVersion, $page); |
|
|
|
|
|
|
291
|
|
|
if ($add) { |
|
292
|
|
|
$this->searchProvider->addDocuments($this->documents); |
|
293
|
|
|
$this->documents = []; |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return true; // return true even if the page itself should not be indexed. This makes sure its children are being processed (i.e. structured nodes) |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Return if the page is indexable - by default all pages are indexable, |
|
302
|
|
|
* you can override this by implementing the IndexableInterface on your |
|
303
|
|
|
* page entity and returning false in the isIndexable method. |
|
304
|
|
|
* |
|
305
|
|
|
* @param HasNodeInterface $page |
|
306
|
|
|
* |
|
307
|
|
|
* @return bool |
|
308
|
|
|
*/ |
|
309
|
|
|
protected function isIndexable(HasNodeInterface $page) |
|
310
|
|
|
{ |
|
311
|
|
|
return $this->container->get('kunstmaan_node.pages_configuration')->isIndexable($page); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Remove the specified node translation from the index |
|
316
|
|
|
* |
|
317
|
|
|
* @param NodeTranslation $nodeTranslation |
|
318
|
|
|
*/ |
|
319
|
|
|
public function deleteNodeTranslation(NodeTranslation $nodeTranslation) |
|
320
|
|
|
{ |
|
321
|
|
|
$uid = 'nodetranslation_' . $nodeTranslation->getId(); |
|
322
|
|
|
$indexName = $this->indexName . '_' . $nodeTranslation->getLang(); |
|
323
|
|
|
$this->searchProvider->deleteDocument($indexName, $this->indexType, $uid); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Delete the specified index |
|
328
|
|
|
*/ |
|
329
|
|
|
public function deleteIndex() |
|
330
|
|
|
{ |
|
331
|
|
|
foreach ($this->locales as $locale) { |
|
332
|
|
|
$this->searchProvider->deleteIndex($this->indexName . '_' . $locale); |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* Apply the analysis factory to the index |
|
338
|
|
|
* |
|
339
|
|
|
* @param Index $index |
|
340
|
|
|
* @param AnalysisFactoryInterface $analysis |
|
341
|
|
|
*/ |
|
342
|
|
|
public function setAnalysis(Index $index, AnalysisFactoryInterface $analysis) |
|
343
|
|
|
{ |
|
344
|
|
|
$analysers = $analysis->build(); |
|
345
|
|
|
$args = [ |
|
346
|
|
|
'number_of_shards' => $this->numberOfShards, |
|
347
|
|
|
'number_of_replicas' => $this->numberOfReplicas, |
|
348
|
|
|
'analysis' => $analysers, |
|
349
|
|
|
]; |
|
350
|
|
|
|
|
351
|
|
|
if (class_exists(\Elastica\Mapping::class)) { |
|
352
|
|
|
$args = [ |
|
353
|
|
|
'settings' => [ |
|
354
|
|
|
'number_of_shards' => $this->numberOfShards, |
|
355
|
|
|
'number_of_replicas' => $this->numberOfReplicas, |
|
356
|
|
|
'analysis' => $analysers, |
|
357
|
|
|
], |
|
358
|
|
|
]; |
|
359
|
|
|
|
|
360
|
|
|
$ngramDiff = 1; |
|
361
|
|
|
if (isset($analysers['tokenizer']) && count($analysers['tokenizer']) > 0) { |
|
362
|
|
|
foreach ($analysers['tokenizer'] as $tokenizer) { |
|
363
|
|
|
if ($tokenizer['type'] === 'nGram') { |
|
364
|
|
|
$diff = $tokenizer['max_gram'] - $tokenizer['min_gram']; |
|
365
|
|
|
|
|
366
|
|
|
$ngramDiff = $diff > $ngramDiff ? $diff : $ngramDiff; |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
if ($ngramDiff > 1) { |
|
372
|
|
|
$args['settings']['max_ngram_diff'] = $ngramDiff; |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
$index->create($args); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Return default search fields mapping for node translations |
|
381
|
|
|
* |
|
382
|
|
|
* @param Index $index |
|
383
|
|
|
* @param string $lang |
|
384
|
|
|
* |
|
385
|
|
|
* @return Mapping|\Elastica\Mapping |
|
386
|
|
|
*/ |
|
387
|
|
|
protected function createDefaultSearchFieldsMapping(Index $index, $lang = 'en') |
|
388
|
|
|
{ |
|
389
|
|
|
if (class_exists(\Elastica\Type\Mapping::class)) { |
|
390
|
|
|
$mapping = new Mapping(); |
|
391
|
|
|
$mapping->setType($index->getType($this->indexType)); |
|
|
|
|
|
|
392
|
|
|
} else { |
|
393
|
|
|
$mapping = new \Elastica\Mapping(); |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
$mapping->setProperties($this->properties); |
|
397
|
|
|
|
|
398
|
|
|
return $mapping; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* Initialize the index with the default search fields mapping |
|
403
|
|
|
* |
|
404
|
|
|
* @param Index $index |
|
405
|
|
|
* @param string $lang |
|
406
|
|
|
*/ |
|
407
|
|
|
protected function setMapping(Index $index, $lang = 'en') |
|
408
|
|
|
{ |
|
409
|
|
|
$mapping = $this->createDefaultSearchFieldsMapping($index, $lang); |
|
410
|
|
|
if (class_exists(\Elastica\Mapping::class)) { |
|
411
|
|
|
$mapping->send($index); |
|
412
|
|
|
} else { |
|
413
|
|
|
$mapping->send(); |
|
|
|
|
|
|
414
|
|
|
} |
|
415
|
|
|
$index->refresh(); |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Create a search document for a page |
|
420
|
|
|
* |
|
421
|
|
|
* @param NodeTranslation $nodeTranslation |
|
422
|
|
|
* @param Node $node |
|
423
|
|
|
* @param NodeVersion $publicNodeVersion |
|
424
|
|
|
* @param HasNodeInterface $page |
|
425
|
|
|
*/ |
|
426
|
|
|
protected function addPageToIndex( |
|
427
|
|
|
NodeTranslation $nodeTranslation, |
|
428
|
|
|
Node $node, |
|
429
|
|
|
NodeVersion $publicNodeVersion, |
|
430
|
|
|
HasNodeInterface $page |
|
431
|
|
|
) { |
|
432
|
|
|
$rootNode = $this->currentTopNode; |
|
433
|
|
|
if (!$rootNode) { |
|
434
|
|
|
// Fetch main parent of current node... |
|
435
|
|
|
$rootNode = $this->em->getRepository(Node::class)->getRootNodeFor( |
|
|
|
|
|
|
436
|
|
|
$node, |
|
437
|
|
|
$nodeTranslation->getLang() |
|
438
|
|
|
); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
$doc = array( |
|
442
|
|
|
'root_id' => $rootNode->getId(), |
|
443
|
|
|
'node_id' => $node->getId(), |
|
444
|
|
|
'node_translation_id' => $nodeTranslation->getId(), |
|
445
|
|
|
'node_version_id' => $publicNodeVersion->getId(), |
|
446
|
|
|
'title' => $nodeTranslation->getTitle(), |
|
447
|
|
|
'slug' => $nodeTranslation->getFullSlug(), |
|
448
|
|
|
'page_class' => ClassLookup::getClass($page), |
|
449
|
|
|
'created' => $this->getUTCDateTime( |
|
450
|
|
|
$nodeTranslation->getCreated() |
|
451
|
|
|
)->format(\DateTime::ISO8601), |
|
452
|
|
|
'updated' => $this->getUTCDateTime( |
|
453
|
|
|
$nodeTranslation->getUpdated() |
|
454
|
|
|
)->format(\DateTime::ISO8601), |
|
455
|
|
|
); |
|
456
|
|
|
if ($this->logger) { |
|
457
|
|
|
$this->logger->info('Indexing document : ' . implode(', ', $doc)); |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
// Permissions |
|
461
|
|
|
$this->addPermissions($node, $doc); |
|
462
|
|
|
|
|
463
|
|
|
// Search type |
|
464
|
|
|
$this->addSearchType($page, $doc); |
|
465
|
|
|
|
|
466
|
|
|
// Parent and Ancestors |
|
467
|
|
|
$this->addParentAndAncestors($node, $doc); |
|
468
|
|
|
|
|
469
|
|
|
// Content |
|
470
|
|
|
$this->addPageContent($nodeTranslation, $page, $doc); |
|
471
|
|
|
|
|
472
|
|
|
// Add document to index |
|
473
|
|
|
$uid = 'nodetranslation_' . $nodeTranslation->getId(); |
|
474
|
|
|
|
|
475
|
|
|
$this->addCustomData($page, $doc); |
|
476
|
|
|
|
|
477
|
|
|
$this->documents[] = $this->searchProvider->createDocument( |
|
478
|
|
|
$uid, |
|
479
|
|
|
$doc, |
|
|
|
|
|
|
480
|
|
|
$this->indexName . '_' . $nodeTranslation->getLang(), |
|
481
|
|
|
$this->indexType |
|
482
|
|
|
); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* Add view permissions to the index document |
|
487
|
|
|
* |
|
488
|
|
|
* @param Node $node |
|
489
|
|
|
* @param array $doc |
|
490
|
|
|
* |
|
491
|
|
|
* @return array |
|
|
|
|
|
|
492
|
|
|
*/ |
|
493
|
|
|
protected function addPermissions(Node $node, &$doc) |
|
494
|
|
|
{ |
|
495
|
|
|
if (!\is_null($this->aclProvider)) { |
|
496
|
|
|
$roles = $this->getAclPermissions($node); |
|
497
|
|
|
} else { |
|
498
|
|
|
// Fallback when no ACL available / assume everything is accessible... |
|
499
|
|
|
$roles = array('IS_AUTHENTICATED_ANONYMOUSLY'); |
|
500
|
|
|
} |
|
501
|
|
|
$doc['view_roles'] = $roles; |
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* Add type to the index document |
|
506
|
|
|
* |
|
507
|
|
|
* @param object $page |
|
508
|
|
|
* @param array $doc |
|
509
|
|
|
* |
|
510
|
|
|
* @return array |
|
|
|
|
|
|
511
|
|
|
*/ |
|
512
|
|
|
protected function addSearchType($page, &$doc) |
|
513
|
|
|
{ |
|
514
|
|
|
$doc['type'] = $this->container->get('kunstmaan_node.pages_configuration')->getSearchType($page); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
/** |
|
518
|
|
|
* Add parent nodes to the index document |
|
519
|
|
|
* |
|
520
|
|
|
* @param Node $node |
|
521
|
|
|
* @param array $doc |
|
522
|
|
|
* |
|
523
|
|
|
* @return array |
|
|
|
|
|
|
524
|
|
|
*/ |
|
525
|
|
|
protected function addParentAndAncestors($node, &$doc) |
|
526
|
|
|
{ |
|
527
|
|
|
$parent = $node->getParent(); |
|
528
|
|
|
|
|
529
|
|
|
if ($parent) { |
|
530
|
|
|
$doc['parent'] = $parent->getId(); |
|
531
|
|
|
$ancestors = []; |
|
532
|
|
|
do { |
|
533
|
|
|
$ancestors[] = $parent->getId(); |
|
534
|
|
|
$parent = $parent->getParent(); |
|
535
|
|
|
} while ($parent); |
|
536
|
|
|
$doc['ancestors'] = $ancestors; |
|
537
|
|
|
} |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
/** |
|
541
|
|
|
* Add page content to the index document |
|
542
|
|
|
* |
|
543
|
|
|
* @param NodeTranslation $nodeTranslation |
|
544
|
|
|
* @param HasNodeInterface $page |
|
545
|
|
|
* @param array $doc |
|
546
|
|
|
*/ |
|
547
|
|
|
protected function addPageContent(NodeTranslation $nodeTranslation, $page, &$doc) |
|
548
|
|
|
{ |
|
549
|
|
|
$this->enterRequestScope($nodeTranslation->getLang()); |
|
550
|
|
|
if ($this->logger) { |
|
551
|
|
|
$this->logger->debug( |
|
552
|
|
|
sprintf( |
|
553
|
|
|
'Indexing page "%s" / lang : %s / type : %s / id : %d / node id : %d', |
|
554
|
|
|
$page->getTitle(), |
|
555
|
|
|
$nodeTranslation->getLang(), |
|
556
|
|
|
\get_class($page), |
|
557
|
|
|
$page->getId(), |
|
558
|
|
|
$nodeTranslation->getNode()->getId() |
|
559
|
|
|
) |
|
560
|
|
|
); |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
$renderer = $this->container->get('templating'); |
|
564
|
|
|
$doc['content'] = ''; |
|
565
|
|
|
|
|
566
|
|
|
if ($page instanceof SearchViewTemplateInterface) { |
|
567
|
|
|
$doc['content'] = $this->renderCustomSearchView($nodeTranslation, $page, $renderer); |
|
|
|
|
|
|
568
|
|
|
|
|
569
|
|
|
return null; |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
if ($page instanceof HasPagePartsInterface) { |
|
573
|
|
|
$doc['content'] = $this->renderDefaultSearchView($nodeTranslation, $page, $renderer); |
|
|
|
|
|
|
574
|
|
|
|
|
575
|
|
|
return null; |
|
576
|
|
|
} |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
/** |
|
580
|
|
|
* Enter request scope if it is not active yet... |
|
581
|
|
|
* |
|
582
|
|
|
* @param string $lang |
|
583
|
|
|
*/ |
|
584
|
|
|
protected function enterRequestScope($lang) |
|
585
|
|
|
{ |
|
586
|
|
|
$locale = null; |
|
587
|
|
|
$requestStack = $this->container->get('request_stack'); |
|
588
|
|
|
// If there already is a request, get the locale from it. |
|
589
|
|
|
if ($requestStack->getCurrentRequest()) { |
|
590
|
|
|
$locale = $requestStack->getCurrentRequest()->getLocale(); |
|
591
|
|
|
} |
|
592
|
|
|
// If we don't have a request or the current request locale is different from the node langauge |
|
593
|
|
|
if (!$requestStack->getCurrentRequest() || ($locale && $locale !== $lang)) { |
|
594
|
|
|
$request = new Request(); |
|
595
|
|
|
$request->setLocale($lang); |
|
596
|
|
|
|
|
597
|
|
|
$context = $this->container->get('router')->getContext(); |
|
598
|
|
|
$context->setParameter('_locale', $lang); |
|
599
|
|
|
|
|
600
|
|
|
$requestStack->push($request); |
|
601
|
|
|
} |
|
602
|
|
|
} |
|
603
|
|
|
|
|
604
|
|
|
/** |
|
605
|
|
|
* Render a custom search view |
|
606
|
|
|
* |
|
607
|
|
|
* @param NodeTranslation $nodeTranslation |
|
608
|
|
|
* @param SearchViewTemplateInterface $page |
|
609
|
|
|
* @param EngineInterface $renderer |
|
610
|
|
|
* |
|
611
|
|
|
* @return string |
|
612
|
|
|
*/ |
|
613
|
|
|
protected function renderCustomSearchView( |
|
614
|
|
|
NodeTranslation $nodeTranslation, |
|
615
|
|
|
SearchViewTemplateInterface $page, |
|
616
|
|
|
EngineInterface $renderer |
|
617
|
|
|
) { |
|
618
|
|
|
$view = $page->getSearchView(); |
|
619
|
|
|
$renderContext = new RenderContext([ |
|
620
|
|
|
'locale' => $nodeTranslation->getLang(), |
|
621
|
|
|
'page' => $page, |
|
622
|
|
|
'indexMode' => true, |
|
623
|
|
|
'nodetranslation' => $nodeTranslation, |
|
624
|
|
|
]); |
|
625
|
|
|
|
|
626
|
|
|
if ($page instanceof PageInterface) { |
|
627
|
|
|
$request = $this->container->get('request_stack')->getCurrentRequest(); |
|
628
|
|
|
$page->service($this->container, $request, $renderContext); |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
$content = $this->removeHtml( |
|
632
|
|
|
$renderer->render( |
|
633
|
|
|
$view, |
|
634
|
|
|
$renderContext->getArrayCopy() |
|
635
|
|
|
) |
|
636
|
|
|
); |
|
637
|
|
|
|
|
638
|
|
|
return $content; |
|
639
|
|
|
} |
|
640
|
|
|
|
|
641
|
|
|
/** |
|
642
|
|
|
* Render default search view (all indexable pageparts in the main context |
|
643
|
|
|
* of the page) |
|
644
|
|
|
* |
|
645
|
|
|
* @param NodeTranslation $nodeTranslation |
|
646
|
|
|
* @param HasPagePartsInterface $page |
|
647
|
|
|
* @param EngineInterface $renderer |
|
648
|
|
|
* |
|
649
|
|
|
* @return string |
|
650
|
|
|
*/ |
|
651
|
|
|
protected function renderDefaultSearchView( |
|
652
|
|
|
NodeTranslation $nodeTranslation, |
|
653
|
|
|
HasPagePartsInterface $page, |
|
654
|
|
|
EngineInterface $renderer |
|
655
|
|
|
) { |
|
656
|
|
|
$pageparts = $this->indexablePagePartsService->getIndexablePageParts($page); |
|
657
|
|
|
$view = '@KunstmaanNodeSearch/PagePart/view.html.twig'; |
|
658
|
|
|
$content = $this->removeHtml( |
|
659
|
|
|
$renderer->render( |
|
660
|
|
|
$view, |
|
661
|
|
|
array( |
|
662
|
|
|
'locale' => $nodeTranslation->getLang(), |
|
663
|
|
|
'page' => $page, |
|
664
|
|
|
'pageparts' => $pageparts, |
|
665
|
|
|
'indexMode' => true, |
|
666
|
|
|
) |
|
667
|
|
|
) |
|
668
|
|
|
); |
|
669
|
|
|
|
|
670
|
|
|
return $content; |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
/** |
|
674
|
|
|
* Add custom data to index document (you can override to add custom fields |
|
675
|
|
|
* to the search index) |
|
676
|
|
|
* |
|
677
|
|
|
* @param HasNodeInterface $page |
|
678
|
|
|
* @param array $doc |
|
679
|
|
|
*/ |
|
680
|
|
|
protected function addCustomData(HasNodeInterface $page, &$doc) |
|
681
|
|
|
{ |
|
682
|
|
|
$event = new IndexNodeEvent($page, $doc); |
|
683
|
|
|
$this->dispatch($event, IndexNodeEvent::EVENT_INDEX_NODE); |
|
684
|
|
|
|
|
685
|
|
|
$doc = $event->doc; |
|
686
|
|
|
|
|
687
|
|
|
if ($page instanceof HasCustomSearchDataInterface) { |
|
688
|
|
|
$doc += $page->getCustomSearchData($doc); |
|
689
|
|
|
} |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
/** |
|
693
|
|
|
* Convert a DateTime to UTC equivalent... |
|
694
|
|
|
* |
|
695
|
|
|
* @param \DateTime $dateTime |
|
696
|
|
|
* |
|
697
|
|
|
* @return \DateTime |
|
698
|
|
|
*/ |
|
699
|
|
|
protected function getUTCDateTime(\DateTime $dateTime) |
|
700
|
|
|
{ |
|
701
|
|
|
$result = clone $dateTime; |
|
702
|
|
|
$result->setTimezone(new \DateTimeZone('UTC')); |
|
703
|
|
|
|
|
704
|
|
|
return $result; |
|
705
|
|
|
} |
|
706
|
|
|
|
|
707
|
|
|
/** |
|
708
|
|
|
* Removes all HTML markup & decode HTML entities |
|
709
|
|
|
* |
|
710
|
|
|
* @param $text |
|
711
|
|
|
* |
|
712
|
|
|
* @return string |
|
713
|
|
|
*/ |
|
714
|
|
|
protected function removeHtml($text) |
|
715
|
|
|
{ |
|
716
|
|
|
if (!trim($text)) { |
|
717
|
|
|
return ''; |
|
718
|
|
|
} |
|
719
|
|
|
|
|
720
|
|
|
// Remove Styles and Scripts |
|
721
|
|
|
$crawler = new Crawler(); |
|
722
|
|
|
$crawler->addHtmlContent($text); |
|
723
|
|
|
$crawler->filter('style, script')->each(function (Crawler $crawler) { |
|
724
|
|
|
foreach ($crawler as $node) { |
|
725
|
|
|
$node->parentNode->removeChild($node); |
|
726
|
|
|
} |
|
727
|
|
|
}); |
|
728
|
|
|
$text = $crawler->html(); |
|
729
|
|
|
|
|
730
|
|
|
// Remove HTML markup |
|
731
|
|
|
$result = strip_tags($text); |
|
732
|
|
|
|
|
733
|
|
|
// Decode HTML entities |
|
734
|
|
|
$result = trim(html_entity_decode($result, ENT_QUOTES)); |
|
735
|
|
|
|
|
736
|
|
|
return $result; |
|
737
|
|
|
} |
|
738
|
|
|
|
|
739
|
|
|
/** |
|
740
|
|
|
* Fetch ACL permissions for the specified entity |
|
741
|
|
|
* |
|
742
|
|
|
* @param object $object |
|
743
|
|
|
* |
|
744
|
|
|
* @return array |
|
745
|
|
|
*/ |
|
746
|
|
|
protected function getAclPermissions($object) |
|
747
|
|
|
{ |
|
748
|
|
|
$roles = []; |
|
749
|
|
|
|
|
750
|
|
|
try { |
|
751
|
|
|
$objectIdentity = ObjectIdentity::fromDomainObject($object); |
|
752
|
|
|
|
|
753
|
|
|
/* @var AclInterface $acl */ |
|
754
|
|
|
$acl = $this->aclProvider->findAcl($objectIdentity); |
|
755
|
|
|
$objectAces = $acl->getObjectAces(); |
|
756
|
|
|
|
|
757
|
|
|
/* @var AuditableEntryInterface $ace */ |
|
758
|
|
View Code Duplication |
foreach ($objectAces as $ace) { |
|
|
|
|
|
|
759
|
|
|
$securityIdentity = $ace->getSecurityIdentity(); |
|
760
|
|
|
if ( |
|
761
|
|
|
$securityIdentity instanceof RoleSecurityIdentity && |
|
762
|
|
|
($ace->getMask() & MaskBuilder::MASK_VIEW != 0) |
|
763
|
|
|
) { |
|
764
|
|
|
$roles[] = $securityIdentity->getRole(); |
|
765
|
|
|
} |
|
766
|
|
|
} |
|
767
|
|
|
} catch (AclNotFoundException $e) { |
|
768
|
|
|
// No ACL found... assume default |
|
769
|
|
|
$roles = array('IS_AUTHENTICATED_ANONYMOUSLY'); |
|
770
|
|
|
} |
|
771
|
|
|
|
|
772
|
|
|
return $roles; |
|
773
|
|
|
} |
|
774
|
|
|
|
|
775
|
|
|
/** |
|
776
|
|
|
* @param $publicNodeVersion |
|
777
|
|
|
* |
|
778
|
|
|
* @return mixed |
|
779
|
|
|
*/ |
|
780
|
|
|
private function getNodeRefPage(NodeVersion $publicNodeVersion) |
|
781
|
|
|
{ |
|
782
|
|
|
$refEntityName = $publicNodeVersion->getRefEntityName(); |
|
783
|
|
|
|
|
784
|
|
|
if (!isset($this->nodeRefs[$refEntityName])) { |
|
785
|
|
|
$this->nodeRefs[$refEntityName] = new $refEntityName(); |
|
786
|
|
|
} |
|
787
|
|
|
|
|
788
|
|
|
return $this->nodeRefs[$refEntityName]; |
|
789
|
|
|
} |
|
790
|
|
|
|
|
791
|
|
|
/** |
|
792
|
|
|
* @param object $event |
|
793
|
|
|
* @param string $eventName |
|
794
|
|
|
* |
|
795
|
|
|
* @return object |
|
796
|
|
|
*/ |
|
797
|
|
View Code Duplication |
private function dispatch($event, string $eventName) |
|
|
|
|
|
|
798
|
|
|
{ |
|
799
|
|
|
$eventDispatcher = $this->container->get('event_dispatcher'); |
|
800
|
|
|
if (class_exists(LegacyEventDispatcherProxy::class)) { |
|
801
|
|
|
$eventDispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher); |
|
802
|
|
|
|
|
803
|
|
|
return $eventDispatcher->dispatch($event, $eventName); |
|
804
|
|
|
} |
|
805
|
|
|
|
|
806
|
|
|
return $eventDispatcher->dispatch($eventName, $event); |
|
807
|
|
|
} |
|
808
|
|
|
} |
|
809
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..