1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Search\Common; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
13
|
|
|
use PDO; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Base class for the Search Engine Indexer Service aimed to recreate Search Engine Index. |
17
|
|
|
* Each Search Engine has to extend it on its own. |
18
|
|
|
* |
19
|
|
|
* Extends indexer to allow for reindexing your install while it is in production by splitting indexing into tree tasks: |
20
|
|
|
* - Remove items in index no longer valid in database |
21
|
|
|
* - Making purge of index optional |
22
|
|
|
* - indexing by specifying id's, for purpose of supporting paralell indexing |
23
|
|
|
*/ |
24
|
|
|
abstract class IterativelyIndexer extends Indexer |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @deprecated Kept for compatibility with consumers of Indexer, performs purge first & recreate of index second. |
28
|
|
|
*/ |
29
|
|
|
final public function createSearchIndex(OutputInterface $output, $iterationCount, $commit) |
30
|
|
|
{ |
31
|
|
|
$output->writeln('Purging Index..'); |
32
|
|
|
$this->searchHandler->purgeIndex(); |
33
|
|
|
|
34
|
|
|
$stmt = $this->getContentDbFieldsStmt(['count(id)']); |
35
|
|
|
$totalCount = intval($stmt->fetchColumn()); |
36
|
|
|
$stmt = $this->getContentDbFieldsStmt(['id']); |
37
|
|
|
|
38
|
|
|
$output->writeln("Re-Creating Search Engine Index for {$totalCount} content items.."); |
39
|
|
|
$progress = new ProgressBar($output); |
40
|
|
|
$progress->start($totalCount); |
41
|
|
|
|
42
|
|
|
$i = 0; |
43
|
|
|
do { |
44
|
|
|
$contentIds = []; |
45
|
|
|
for ($k = 0; $k <= $iterationCount; ++$k) { |
46
|
|
|
if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
47
|
|
|
break; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$contentIds[] = $row['id']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->updateSearchIndex($contentIds); |
54
|
|
|
if ($commit && method_exists($this->searchHandler, 'commit')) { |
55
|
|
|
$this->searchHandler->commit(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$progress->advance($k); |
59
|
|
|
} while (($i += $iterationCount) < $totalCount); |
60
|
|
|
|
61
|
|
|
$progress->finish(); |
62
|
|
|
$output->writeln(''); |
63
|
|
|
//$output->writeln('Finished creating Search Engine Index'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Updates search engine index based on Content id's. |
68
|
|
|
* |
69
|
|
|
* If content is: |
70
|
|
|
* - deleted (NotFoundException) |
71
|
|
|
* - not published (draft or trashed) |
72
|
|
|
* Then item is removed from index, if not it is added/updated. |
73
|
|
|
* |
74
|
|
|
* @param int[] $contentIds |
75
|
|
|
*/ |
76
|
|
|
abstract public function updateSearchIndex(array $contentIds); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: