Failed Conditions
Push — master ( 5f60a5...9b80eb )
by Rafael
21:42
created

Classes/IndexQueue/Item.php (1 issue)

Labels
Severity
1
<?php
2
namespace ApacheSolrForTypo3\Solr\IndexQueue;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\IndexQueueIndexingPropertyRepository;
28
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\QueueItemRepository;
29
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
30
use ApacheSolrForTypo3\Solr\Site;
31
use TYPO3\CMS\Backend\Utility\BackendUtility;
0 ignored issues
show
The type TYPO3\CMS\Backend\Utility\BackendUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
/**
35
 * Representation of an index queue item, carrying meta data and the record to be
36
 * indexed.
37
 *
38
 * @todo: Loose coupling from Repos
39
 *
40
 * @author Ingo Renner <[email protected]>
41
 */
42
class Item
43
{
44
    /**
45
     * The item's uid in the index queue (tx_solr_indexqueue_item.uid)
46
     *
47
     * @var int
48
     */
49
    protected $indexQueueUid;
50
51
    /**
52
     * The root page uid of the tree the item is located in (tx_solr_indexqueue_item.root)
53
     *
54
     * @var int
55
     */
56
    protected $rootPageUid;
57
58
    /**
59
     * The record's type, usually a table name, but could also be a file type (tx_solr_indexqueue_item.item_type)
60
     *
61
     * @var string
62
     */
63
    protected $type;
64
65
    /**
66
     * The name of the indexing configuration that should be used when indexing (tx_solr_indexqueue_item.indexing_configuration)
67
     * the item.
68
     *
69
     * @var string
70
     */
71
    protected $indexingConfigurationName;
72
73
    /**
74
     * The unix timestamp when the record was last changed (tx_solr_indexqueue_item.changed)
75
     *
76
     * @var int
77
     */
78
    protected $changed;
79
80
    /**
81
     * Indexing properties to provide additional information for the item's
82
     * indexer / how to index the item.
83
     *
84
     * @var array
85
     */
86
    protected $indexingProperties = [];
87
88
    /**
89
     * Flag for lazy loading indexing properties.
90
     *
91
     * @var bool
92
     */
93
    protected $indexingPropertiesLoaded = false;
94
95
    /**
96
     * Flag, whether indexing properties exits for this item.
97
     *
98
     * @var bool
99
     */
100
    protected $hasIndexingProperties = false;
101
102
    /**
103
     * The record's uid.
104
     *
105
     * @var int
106
     */
107
    protected $recordUid = 0;
108
109
    /**
110
     * The record itself
111
     *
112
     * @var array
113
     */
114
    protected $record;
115
116
    /**
117
     * Moint point identifier.
118
     *
119
     * @var string
120
     */
121
    protected $mountPointIdentifier;
122
123
    /**
124
     * @var string
125
     */
126
    protected $errors = '';
127
128
    /**
129
     * @var IndexQueueIndexingPropertyRepository
130
     */
131
    protected $indexQueueIndexingPropertyRepository;
132
133
    /**
134
     * @var QueueItemRepository
135
     */
136
    protected $queueItemRepository;
137
138
    /**
139
     * Constructor, takes item meta data information and resolves that to the full record.
140
     *
141
     * @param array $itemMetaData Metadata describing the item to index using the index queue. Is expected to contain a record from table tx_solr_indexqueue_item
142
     * @param array $fullRecord Optional full record for the item. If provided, can save some SQL queries.
143
     * @param IndexQueueIndexingPropertyRepository|null $indexQueueIndexingPropertyRepository
144
     */
145 69
    public function __construct(array $itemMetaData, array $fullRecord = [], IndexQueueIndexingPropertyRepository $indexQueueIndexingPropertyRepository = null, QueueItemRepository $queueItemRepository = null)
146
    {
147 69
        $this->indexQueueUid = $itemMetaData['uid'];
148 69
        $this->rootPageUid = $itemMetaData['root'];
149 69
        $this->type = $itemMetaData['item_type'];
150 69
        $this->recordUid = $itemMetaData['item_uid'];
151 69
        $this->mountPointIdentifier = (string) empty($itemMetaData['pages_mountidentifier']) ? '' : $itemMetaData['pages_mountidentifier'];
152 69
        $this->changed = $itemMetaData['changed'];
153 69
        $this->errors = (string) empty($itemMetaData['errors']) ? '' : $itemMetaData['errors'];
154
155 69
        $this->indexingConfigurationName = $itemMetaData['indexing_configuration'];
156 69
        $this->hasIndexingProperties = (boolean)$itemMetaData['has_indexing_properties'];
157
158 69
        if (!empty($fullRecord)) {
159 37
            $this->record = $fullRecord;
160
        }
161
162 69
        $this->indexQueueIndexingPropertyRepository = isset($indexQueueIndexingPropertyRepository) ? $indexQueueIndexingPropertyRepository : GeneralUtility::makeInstance(IndexQueueIndexingPropertyRepository::class);
163 69
        $this->queueItemRepository = isset($queueItemRepository) ? $queueItemRepository : GeneralUtility::makeInstance(QueueItemRepository::class);
164 69
    }
165
166
    /**
167
     * Getter for Index Queue UID
168
     *
169
     * @return integer
170
     */
171 8
    public function getIndexQueueUid()
172
    {
173 8
        return $this->indexQueueUid;
174
    }
175
176
    /**
177
     * Gets the item's root page ID (uid)
178
     *
179
     * @return int root page ID
180
     */
181 31
    public function getRootPageUid()
182
    {
183 31
        return $this->rootPageUid;
184
    }
185
186
    /**
187
     * Returns mount point identifier
188
     *
189
     * @return string
190
     */
191 24
    public function getMountPointIdentifier()
192
    {
193 24
        return $this->mountPointIdentifier;
194
    }
195
196
    /**
197
     * @param integer $uid
198
     */
199
    public function setRootPageUid($uid)
200
    {
201
        $this->rootPageUid = intval($uid);
202
    }
203
204
    /**
205
     * @return string
206
     */
207 5
    public function getErrors()
208
    {
209 5
        return $this->errors;
210
    }
211
212
    /**
213
     * Gets the site the item belongs to.
214
     *
215
     * @return Site Site instance the item belongs to.
216
     */
217 30
    public function getSite()
218
    {
219 30
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
220 30
        return $siteRepository->getSiteByRootPageId($this->rootPageUid);
221
    }
222
223 26
    public function getType()
224
    {
225 26
        return $this->type;
226
    }
227
228
    public function setType($type)
229
    {
230
        $this->type = $type;
231
    }
232
233 36
    public function getIndexingConfigurationName()
234
    {
235 36
        return $this->indexingConfigurationName;
236
    }
237
238
    public function setIndexingConfigurationName($indexingConfigurationName)
239
    {
240
        $this->indexingConfigurationName = $indexingConfigurationName;
241
    }
242
243
    public function getChanged()
244
    {
245
        return $this->changed;
246
    }
247
248
    public function setChanged($changed)
249
    {
250
        $this->changed = intval($changed);
251
    }
252
253 5
    public function getRecordUid()
254
    {
255 5
        $this->getRecord();
256
257 5
        return $this->record['uid'];
258
    }
259
260
    /**
261
     * Gets the item's full record.
262
     *
263
     * Uses lazy loading.
264
     *
265
     * @return array The item's DB record.
266
     */
267 23
    public function getRecord()
268
    {
269 23
        if (empty($this->record)) {
270 1
            $this->record = (array)BackendUtility::getRecord(
271 1
                $this->type,
272 1
                $this->recordUid,
273 1
                '*',
274 1
                '',
275 1
                false
276
            );
277
        }
278
279 23
        return $this->record;
280
    }
281
282
    public function setRecord(array $record)
283
    {
284
        $this->record = $record;
285
    }
286
287
    /**
288
     * @return int
289
     */
290 20
    public function getRecordPageId()
291
    {
292 20
        $this->getRecord();
293
294 20
        return $this->record['pid'];
295
    }
296
297
    /**
298
     * Stores the indexing properties.
299
     *
300
     */
301 7
    public function storeIndexingProperties()
302
    {
303 7
        $this->indexQueueIndexingPropertyRepository->removeByRootPidAndIndexQueueUid(intval($this->rootPageUid), intval($this->indexQueueUid));
304
305 7
        if ($this->hasIndexingProperties()) {
306 7
            $this->writeIndexingProperties();
307
        }
308
309 7
        $this->queueItemRepository->updateHasIndexingPropertiesFlagByItemUid($this->indexQueueUid, $this->hasIndexingProperties);
310 7
    }
311
312 8
    public function hasIndexingProperties()
313
    {
314 8
        return $this->hasIndexingProperties;
315
    }
316
317
    /**
318
     * Writes all indexing properties.
319
     */
320 7
    protected function writeIndexingProperties()
321
    {
322 7
        $properties = [];
323 7
        foreach ($this->indexingProperties as $propertyKey => $propertyValue) {
324 7
            $properties[] = [
325 7
                'root' => $this->rootPageUid,
326 7
                'item_id' => $this->indexQueueUid,
327 7
                'property_key' => $propertyKey,
328 7
                'property_value' => $propertyValue
329
            ];
330
        }
331 7
        if (empty($properties)) {
332
            return;
333
        }
334 7
        $this->indexQueueIndexingPropertyRepository->bulkInsert($properties);
335 7
    }
336
337
    /**
338
     * @param string $key
339
     * @return bool
340
     */
341 1
    public function hasIndexingProperty($key)
342
    {
343 1
        $this->loadIndexingProperties();
344
345 1
        return array_key_exists($key, $this->indexingProperties);
346
    }
347
348
    /**
349
     * Loads the indexing properties for the item - if not already loaded.
350
     */
351 8
    public function loadIndexingProperties()
352
    {
353 8
        if ($this->indexingPropertiesLoaded) {
354 8
            return;
355
        }
356
357 8
        $indexingProperties = $this->indexQueueIndexingPropertyRepository->findAllByIndexQueueUid(intval($this->indexQueueUid));
358 8
        $this->indexingPropertiesLoaded = true;
359 8
        if (empty($indexingProperties)) {
360 8
            return;
361
        }
362
363
        foreach ($indexingProperties as $indexingProperty) {
364
            $this->indexingProperties[$indexingProperty['property_key']] = $indexingProperty['property_value'];
365
        }
366
    }
367
368
    /**
369
     * Sets an indexing property for the item.
370
     *
371
     * @param string $key Indexing property name
372
     * @param string|int|float $value Indexing property value
373
     * @throws \InvalidArgumentException when $value is not string, integer or float
374
     */
375 7
    public function setIndexingProperty($key, $value)
376
    {
377
        // make sure to not interfere with existing indexing properties
378 7
        $this->loadIndexingProperties();
379
380 7
        $key = (string)$key; // Scalar typehints now!
381
382 7
        if (!is_string($value) && !is_int($value) && !is_float($value)) {
383
            throw new \InvalidArgumentException(
384
                'Cannot set indexing property "' . $key
385
                . '", its value must be string, integer or float, '
386
                . 'type given was "' . gettype($value) . '"',
387
                1323173209
388
            );
389
        }
390
391 7
        $this->indexingProperties[$key] = $value;
392 7
        $this->hasIndexingProperties = true;
393 7
    }
394
395
    /**
396
     * Gets a specific indexing property by its name/key.
397
     *
398
     * @param string $key Indexing property name/key.
399
     * @throws \InvalidArgumentException when the given $key does not exist.
400
     * @return string
401
     */
402
    public function getIndexingProperty($key)
403
    {
404
        $this->loadIndexingProperties();
405
406
        if (!array_key_exists($key, $this->indexingProperties)) {
407
            throw new \InvalidArgumentException(
408
                'No indexing property "' . $key . '".',
409
                1323174143
410
            );
411
        }
412
413
        return $this->indexingProperties[$key];
414
    }
415
416
    /**
417
     * Gets all indexing properties set for this item.
418
     *
419
     * @return array Array of indexing properties.
420
     */
421
    public function getIndexingProperties()
422
    {
423
        $this->loadIndexingProperties();
424
425
        return $this->indexingProperties;
426
    }
427
428
    /**
429
     * Gets the names/keys of the item's indexing properties.
430
     *
431
     * @return array Array of indexing property names/keys
432
     */
433
    public function getIndexingPropertyKeys()
434
    {
435
        $this->loadIndexingProperties();
436
437
        return array_keys($this->indexingProperties);
438
    }
439
}
440