Completed
Push — ezp26175-exception_on_non_defa... ( 77d2f3...ca5fc8 )
by
unknown
39:33
created

SearchIndex::incrementWordObjectCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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\Legacy\Content\WordIndexer\Repository;
10
11
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
12
use eZ\Publish\Core\Persistence\Database\SelectQuery;
13
use PDO;
14
15
/**
16
 * A service encapsulating database operations on ezsearch* tables.
17
 */
18
class SearchIndex
19
{
20
    /**
21
     * Database handler.
22
     *
23
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
24
     */
25
    protected $dbHandler;
26
27
    /**
28
     * SearchIndex constructor.
29
     *
30
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
31
     */
32
    public function __construct(
33
        DatabaseHandler $dbHandler
34
    ) {
35
        $this->dbHandler = $dbHandler;
36
    }
37
38
    /**
39
     * Fetch already indexed words from database (legacy db table: ezsearch_word).
40
     *
41
     * @param array $words
42
     *
43
     * @return array
44
     */
45
    public function getWords(array $words)
46
    {
47
        $query = $this->dbHandler->createSelectQuery();
48
49
        // use array_map as some DBMS-es do not cast integers to strings by default
50
        $query->select('*')
51
            ->from('ezsearch_word')
52
            ->where($query->expr->in('word', array_map('strval', $words)));
53
54
        $stmt = $query->prepare();
55
        $stmt->execute();
56
57
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
58
    }
59
60
    /**
61
     * Increase the object count of the given words by one.
62
     *
63
     * @param array $wordId
64
     */
65
    public function incrementWordObjectCount(array $wordId)
66
    {
67
        $this->updateWordObjectCount($wordId, ['object_count' => 'object_count + 1']);
68
    }
69
70
    /**
71
     * Decrease the object count of the given words by one.
72
     *
73
     * @param array $wordId
74
     */
75
    public function decrementWordObjectCount(array $wordId)
76
    {
77
        $this->updateWordObjectCount($wordId, ['object_count' => 'object_count - 1']);
78
    }
79
80
    /**
81
     * Insert new words (legacy db table: ezsearch_word).
82
     *
83
     * @param array $words
84
     */
85
    public function addWords(array $words)
86
    {
87
        $query = $this->dbHandler->createInsertQuery();
88
        $query->insertInto('ezsearch_word');
89
90
        $word = null;
91
92
        $query->set(
93
            'word',
94
            ':word'
95
        )->set(
96
            'object_count',
97
            '1'
98
        );
99
        $stmt = $query->prepare();
100
        foreach ($words as $word) {
101
            $stmt->execute(['word' => $word]);
102
        }
103
    }
104
105
    /**
106
     * Remove entire search index.
107
     */
108
    public function purge()
109
    {
110
        $this->dbHandler->beginTransaction();
111
        $query = $this->dbHandler->createDeleteQuery();
112
        $tables = [
113
            'ezsearch_object_word_link',
114
            'ezsearch_return_count',
115
            'ezsearch_search_phrase',
116
            'ezsearch_word',
117
        ];
118
        foreach ($tables as $tbl) {
119
            $query->deleteFrom($tbl);
120
            $stmt = $query->prepare();
121
            $stmt->execute();
122
        }
123
        $this->dbHandler->commit();
124
    }
125
126
    /**
127
     * Link word with specific content object (legacy db table: ezsearch_object_word_link).
128
     *
129
     * @param $wordId
130
     * @param $contentId
131
     * @param $frequency
132
     * @param $placement
133
     * @param $nextWordId
134
     * @param $prevWordId
135
     * @param $contentTypeId
136
     * @param $fieldTypeId
137
     * @param $published
138
     * @param $sectionId
139
     * @param $identifier
140
     * @param $integerValue
141
     */
142
    public function addObjectWordLink($wordId, $contentId, $frequency, $placement, $nextWordId, $prevWordId,
143
                                      $contentTypeId, $fieldTypeId, $published, $sectionId, $identifier,
144
                                      $integerValue)
145
    {
146
        $assoc = [
147
            'word_id' => $wordId,
148
            'contentobject_id' => $contentId,
149
            'frequency' => $frequency,
150
            'placement' => $placement,
151
            'next_word_id' => $nextWordId,
152
            'prev_word_id' => $prevWordId,
153
            'contentclass_id' => $contentTypeId,
154
            'contentclass_attribute_id' => $fieldTypeId,
155
            'published' => $published,
156
            'section_id' => $sectionId,
157
            'identifier' => $identifier,
158
            'integer_value' => $integerValue,
159
        ];
160
        $query = $this->dbHandler->createInsertQuery();
161
        $query->insertInto('ezsearch_object_word_link');
162
        foreach ($assoc as $column => $value) {
163
            $query->set($this->dbHandler->quoteColumn($column), $query->bindValue($value));
164
        }
165
        $stmt = $query->prepare();
166
        $stmt->execute();
167
    }
168
169
    /**
170
     * Get all words related to the content object (legacy db table: ezsearch_object_word_link).
171
     *
172
     * @param $contentId
173
     *
174
     * @return array
175
     */
176
    public function getContentObjectWords($contentId)
177
    {
178
        $query = $this->dbHandler->createSelectQuery();
179
180
        $this->setContentObjectWordsSelectQuery($query);
181
182
        $stmt = $query->prepare();
183
        $stmt->execute(['contentId' => $contentId]);
184
185
        $wordIDList = [];
186
187
        while (false !== ($row = $stmt->fetch(PDO::FETCH_NUM))) {
188
            $wordIDList[] = $row[0];
189
        }
190
191
        return $wordIDList;
192
    }
193
194
    /**
195
     * Delete words not related to any content object.
196
     */
197 View Code Duplication
    public function deleteWordsWithoutObjects()
198
    {
199
        $query = $this->dbHandler->createDeleteQuery();
200
201
        $query->deleteFrom($this->dbHandler->quoteTable('ezsearch_word'))
202
            ->where($query->expr->eq($this->dbHandler->quoteColumn('object_count'), ':c'));
203
204
        $stmt = $query->prepare();
205
        $stmt->execute(['c' => 0]);
206
    }
207
208
    /**
209
     * Delete relation between a word and a content object.
210
     *
211
     * @param $contentId
212
     */
213 View Code Duplication
    public function deleteObjectWordsLink($contentId)
214
    {
215
        $query = $this->dbHandler->createDeleteQuery();
216
        $query->deleteFrom($this->dbHandler->quoteTable('ezsearch_object_word_link'))
217
            ->where($query->expr->eq($this->dbHandler->quoteColumn('contentobject_id'), ':contentId'));
218
219
        $stmt = $query->prepare();
220
        $stmt->execute(['contentId' => $contentId]);
221
    }
222
223
    /**
224
     * Set query selecting word ids for content object (method was extracted to be reusable).
225
     *
226
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
227
     */
228
    private function setContentObjectWordsSelectQuery(SelectQuery $query)
229
    {
230
        $query->select('word_id')
231
            ->from($this->dbHandler->quoteTable('ezsearch_object_word_link'))
232
            ->where($query->expr->eq($this->dbHandler->quoteColumn('contentobject_id'), ':contentId'));
233
    }
234
235
    /**
236
     * Update object count for words (legacy db table: ezsearch_word).
237
     *
238
     * @param array $wordId list of word IDs
239
     * @param array $columns map of columns and values to be updated ([column => value])
240
     */
241
    private function updateWordObjectCount(array $wordId, array $columns)
242
    {
243
        $query = $this->dbHandler->createUpdateQuery();
244
        $query->update($this->dbHandler->quoteTable('ezsearch_word'));
245
246
        foreach ($columns as $column => $value) {
247
            $query->set($this->dbHandler->quoteColumn($column), $value);
248
        }
249
250
        $query->where($query->expr->in($this->dbHandler->quoteColumn('id'), $wordId));
251
252
        $stmt = $query->prepare();
253
        $stmt->execute();
254
    }
255
}
256