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\Legacy\Content; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
12
|
|
|
use eZ\Publish\Core\Search\Common\Indexer as SearchIndexer; |
13
|
|
|
use eZ\Publish\Core\Search\Legacy\Content\Handler as SearchHandler; |
14
|
|
|
use PDO; |
15
|
|
|
use RuntimeException; |
16
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
class Indexer extends SearchIndexer |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \eZ\Publish\Core\Search\Legacy\Content\Handler |
23
|
|
|
*/ |
24
|
|
|
protected $searchHandler; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create search engine index. |
28
|
|
|
* |
29
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
30
|
|
|
* @param int $iterationCount |
31
|
|
|
* @param bool $commit commit changes after each iteration |
32
|
|
|
*/ |
33
|
|
|
public function createSearchIndex(OutputInterface $output, $iterationCount, $commit) |
34
|
|
|
{ |
35
|
|
|
$output->writeln('Creating Legacy Search Engine Index...'); |
36
|
|
|
|
37
|
|
|
if (!$this->searchHandler instanceof SearchHandler) { |
38
|
|
|
throw new RuntimeException(sprintf('Expected to find an instance of %s, but found %s', SearchHandler::class, get_class($this->searchHandler))); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$stmt = $this->getContentDbFieldsStmt(['count(id)']); |
42
|
|
|
$totalCount = intval($stmt->fetchColumn()); |
43
|
|
|
$stmt = $this->getContentDbFieldsStmt(['id', 'current_version']); |
44
|
|
|
|
45
|
|
|
$this->searchHandler->purgeIndex(); |
46
|
|
|
|
47
|
|
|
$progress = new ProgressBar($output); |
48
|
|
|
$progress->start($totalCount); |
49
|
|
|
|
50
|
|
|
$i = 0; |
51
|
|
View Code Duplication |
do { |
|
|
|
|
52
|
|
|
$contentObjects = []; |
53
|
|
|
for ($k = 0; $k <= $iterationCount; ++$k) { |
54
|
|
|
if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
try { |
58
|
|
|
$contentObjects[] = $this->persistenceHandler->contentHandler()->load( |
59
|
|
|
$row['id'], |
60
|
|
|
$row['current_version'] |
61
|
|
|
); |
62
|
|
|
} catch (NotFoundException $e) { |
63
|
|
|
$this->logWarning($progress, "Could not load current version of Content with id ${row['id']}, so skipped for indexing. Full exception: " . $e->getMessage()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($contentObjects as $content) { |
68
|
|
|
try { |
69
|
|
|
$this->searchHandler->indexContent($content); |
70
|
|
|
} catch (NotFoundException $e) { |
71
|
|
|
// Ignore content objects that have some sort of missing data on it |
72
|
|
|
$this->logWarning($progress, 'Content with id ' . $content->versionInfo->id . ' has missing data, so skipped for indexing. Full exception: ' . $e->getMessage()); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$progress->advance($k); |
77
|
|
|
} while (($i += $iterationCount) < $totalCount); |
78
|
|
|
$progress->finish(); |
79
|
|
|
$output->writeln(''); |
80
|
|
|
|
81
|
|
|
$output->writeln('Finished creating Legacy Search Engine Index'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.