Passed
Push — master ( 3b900f...c009f5 )
by Timo
52s
created

Item::loadIndexingProperties()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

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