Issues (202)

Classes/IndexQueue/Item.php (1 issue)

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