Completed
Push — 6.7 ( 730fb7...a124d5 )
by André
39:46 queued 26:19
created

IncrementalIndexer::createSearchIndex()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 34
Code Lines 23

Duplication

Lines 7
Ratio 20.59 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 3
nop 3
dl 7
loc 34
rs 8.5806
c 0
b 0
f 0
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 parallel indexing
23
 *
24
 * @api
25
 */
26
abstract class IncrementalIndexer extends Indexer
27
{
28
    /**
29
     * @deprecated Kept for compatibility with consumers of Indexer, performs purge first & recreate of index second.
30
     */
31
    final public function createSearchIndex(OutputInterface $output, $iterationCount, $commit)
32
    {
33
        $output->writeln('Re-creating Search index for: ' . $this->getName());
34
        $output->writeln('Purging Index...');
35
        $this->searchHandler->purgeIndex();
36
37
        $stmt = $this->getContentDbFieldsStmt(['count(id)']);
38
        $totalCount = (int) ($stmt->fetchColumn());
39
        $stmt = $this->getContentDbFieldsStmt(['id']);
40
41
        $output->writeln("Re-Creating Search Engine Index for {$totalCount} content items...");
42
        $progress = new ProgressBar($output);
43
        $progress->start($totalCount);
44
45
        $i = 0;
46
        do {
47
            $contentIds = [];
48 View Code Duplication
            for ($k = 0; $k <= $iterationCount; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
49
                if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) {
50
                    break;
51
                }
52
53
                $contentIds[] = $row['id'];
54
            }
55
56
            $this->updateSearchIndex($contentIds, $commit);
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
     * @param bool $commit
76
     */
77
    abstract public function updateSearchIndex(array $contentIds, $commit);
78
79
    /**
80
     * Purges whole index, should only be done if user asked for it.
81
     */
82
    abstract public function purge();
83
84
    /**
85
     * Return human readable name of given search engine (and if custom indexer you can append that to).
86
     *
87
     * @return string
88
     */
89
    abstract public function getName();
90
}
91