Completed
Push — master ( 694264...44bc0a )
by Timo
11s
created

Item::setIndexingProperty()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.1576

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 7
cts 12
cp 0.5833
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 2
nop 2
crap 5.1576
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 = array();
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
     * Constructor, takes item meta data information and resolves that to the full record.
114
     *
115
     * @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
116
     * @param array $fullRecord Optional full record for the item. If provided, can save some SQL queries.
117
     */
118 30
    public function __construct(
119
        array $itemMetaData,
120
        array $fullRecord = array()
121
    ) {
122 30
        $this->indexQueueUid = $itemMetaData['uid'];
123 30
        $this->rootPageUid = $itemMetaData['root'];
124 30
        $this->type = $itemMetaData['item_type'];
125 30
        $this->recordUid = $itemMetaData['item_uid'];
126 30
        $this->changed = $itemMetaData['changed'];
127
128 30
        $this->indexingConfigurationName = $itemMetaData['indexing_configuration'];
129 30
        $this->hasIndexingProperties = (boolean)$itemMetaData['has_indexing_properties'];
130
131 30
        if (!empty($fullRecord)) {
132 17
            $this->record = $fullRecord;
133
        }
134 30
    }
135
136 1
    public function getIndexQueueUid()
137
    {
138 1
        return $this->indexQueueUid;
139
    }
140
141
    /**
142
     * Gets the item's root page ID (uid)
143
     *
144
     * @return int root page ID
145
     */
146 16
    public function getRootPageUid()
147
    {
148 16
        return $this->rootPageUid;
149
    }
150
151
    public function setRootPageUid($uid)
152
    {
153
        $this->rootPageUid = intval($uid);
154
    }
155
156
    /**
157
     * Gets the site the item belongs to.
158
     *
159
     * @return Site Site instance the item belongs to.
160
     */
161 14
    public function getSite()
162
    {
163 14
        return GeneralUtility::makeInstance(Site::class, $this->rootPageUid);
164
    }
165
166 15
    public function getType()
167
    {
168 15
        return $this->type;
169
    }
170
171
    public function setType($type)
172
    {
173
        $this->type = $type;
174
    }
175
176 22
    public function getIndexingConfigurationName()
177
    {
178 22
        return $this->indexingConfigurationName;
179
    }
180
181
    public function setIndexingConfigurationName($indexingConfigurationName)
182
    {
183
        $this->indexingConfigurationName = $indexingConfigurationName;
184
    }
185
186
    public function getChanged()
187
    {
188
        return $this->changed;
189
    }
190
191
    public function setChanged($changed)
192
    {
193
        $this->changed = intval($changed);
194
    }
195
196
    /**
197
     * Sets the timestamp of when an item has been indexed.
198
     *
199
     * @return void
200
     */
201 5
    public function updateIndexedTime()
202
    {
203 5
        $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
204 5
            'tx_solr_indexqueue_item',
205 5
            'uid = ' . (int)$this->indexQueueUid,
206 5
            array('indexed' => time())
207
        );
208 5
    }
209
210 2
    public function getRecordUid()
211
    {
212 2
        $this->getRecord();
213
214 2
        return $this->record['uid'];
215
    }
216
217
    /**
218
     * Gets the item's full record.
219
     *
220
     * Uses lazy loading.
221
     *
222
     * @return array The item's DB record.
223
     */
224 11
    public function getRecord()
225
    {
226 11
        if (empty($this->record)) {
227
            $this->record = BackendUtility::getRecord(
0 ignored issues
show
Documentation Bug introduced by
It seems like \TYPO3\CMS\Backend\Utili...ordUid, '*', '', false) can be null. However, the property $record is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
228
                $this->type,
229
                $this->recordUid,
230
                '*',
231
                '',
232
                false
233
            );
234
        }
235
236 11
        return $this->record;
237
    }
238
239
    public function setRecord(array $record)
240
    {
241
        $this->record = $record;
242
    }
243
244 1
    public function getRecordPageId()
245
    {
246 1
        $this->getRecord();
247
248 1
        return $this->record['pid'];
249
    }
250
251
    /**
252
     * Stores the indexing properties.
253
     *
254
     */
255 3
    public function storeIndexingProperties()
256
    {
257 3
        $this->removeIndexingProperties();
258
259 3
        if ($this->hasIndexingProperties()) {
260 3
            $this->writeIndexingProperties();
261
        }
262
263 3
        $this->updateHasIndexingPropertiesFlag();
264 3
    }
265
266
    /**
267
     * Removes existing indexing properties.
268
     *
269
     * @throws \RuntimeException when an SQL error occurs
270
     */
271 3
    protected function removeIndexingProperties()
272
    {
273 3
        $GLOBALS['TYPO3_DB']->exec_DELETEquery(
274 3
            'tx_solr_indexqueue_indexing_property',
275 3
            'root = ' . intval($this->rootPageUid)
276 3
            . ' AND item_id = ' . intval($this->indexQueueUid)
277
        );
278
279 3
        if ($GLOBALS['TYPO3_DB']->sql_error()) {
280
            throw new \RuntimeException(
281
                'Could not remove indexing properties for item ' . $this->indexQueueUid,
282
                1323802532
283
            );
284
        }
285 3
    }
286
287 3
    public function hasIndexingProperties()
288
    {
289 3
        return $this->hasIndexingProperties;
290
    }
291
292
    /**
293
     * Writes all indexing properties.
294
     *
295
     * @throws \RuntimeException when an SQL error occurs
296
     */
297 3
    protected function writeIndexingProperties()
298
    {
299 3
        $properties = array();
300 3
        foreach ($this->indexingProperties as $propertyKey => $propertyValue) {
301 3
            $properties[] = array(
302 3
                $this->rootPageUid,
303 3
                $this->indexQueueUid,
304 3
                $propertyKey,
305 3
                $propertyValue
306
            );
307
        }
308
309 3
        $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows(
310 3
            'tx_solr_indexqueue_indexing_property',
311 3
            array('root', 'item_id', 'property_key', 'property_value'),
312
            $properties
313
        );
314
315 3
        if ($GLOBALS['TYPO3_DB']->sql_error()) {
316
            throw new \RuntimeException(
317
                'Could not insert indexing properties for item ' . $this->indexQueueUid,
318
                1323802570
319
            );
320
        }
321 3
    }
322
323
    /**
324
     * Updates the "has_indexing_properties" flag in the Index Queue.
325
     *
326
     * @throws \RuntimeException when an SQL error occurs
327
     */
328 3
    protected function updateHasIndexingPropertiesFlag()
329
    {
330 3
        $hasIndexingProperties = '0';
331 3
        if ($this->hasIndexingProperties()) {
332 3
            $hasIndexingProperties = '1';
333
        }
334
335 3
        $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
336 3
            'tx_solr_indexqueue_item',
337 3
            'uid = ' . intval($this->indexQueueUid),
338 3
            array('has_indexing_properties' => $hasIndexingProperties)
339
        );
340
341 3
        if ($GLOBALS['TYPO3_DB']->sql_error()) {
342
            throw new \RuntimeException(
343
                'Could not update has_indexing_properties flag in Index Queue for item ' . $this->indexQueueUid,
344
                1323802610
345
            );
346
        }
347 3
    }
348
349
    /**
350
     * @param string $key
351
     * @return bool
352
     */
353 1
    public function hasIndexingProperty($key)
354
    {
355 1
        $this->loadIndexingProperties();
356
357 1
        return array_key_exists($key, $this->indexingProperties);
358
    }
359
360
    /**
361
     * Loads the indexing properties for the item - if not already loaded.
362
     *
363
     */
364 4
    public function loadIndexingProperties()
365
    {
366 4
        if (!$this->indexingPropertiesLoaded) {
367 4
            $indexingProperties = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
368 4
                'property_key, property_value',
369 4
                'tx_solr_indexqueue_indexing_property',
370 4
                'item_id = ' . intval($this->indexQueueUid)
371
            );
372
373 4
            if (!empty($indexingProperties)) {
374
                foreach ($indexingProperties as $indexingProperty) {
375
                    $this->indexingProperties[$indexingProperty['property_key']] = $indexingProperty['property_value'];
376
                }
377
            }
378
379 4
            $this->indexingPropertiesLoaded = true;
380
        }
381 4
    }
382
383
    /**
384
     * Sets an indexing property for the item.
385
     *
386
     * @param string $key Indexing property name
387
     * @param string|int|float $value Indexing property value
388
     * @throws \InvalidArgumentException when $value is not string, integer or float
389
     */
390 3
    public function setIndexingProperty($key, $value)
391
    {
392
393
        // make sure to not interfere with existing indexing properties
394 3
        $this->loadIndexingProperties();
395
396 3
        $key = (string)$key; // Scalar typehints now!
397
398 3
        if (!is_string($value) && !is_int($value) && !is_float($value)) {
399
            throw new \InvalidArgumentException(
400
                'Cannot set indexing property "' . $key
401
                . '", its value must be string, integer or float, '
402
                . 'type given was "' . gettype($value) . '"',
403
                1323173209
404
            );
405
        }
406
407 3
        $this->indexingProperties[$key] = $value;
408 3
        $this->hasIndexingProperties = true;
409 3
    }
410
411
    /**
412
     * Gets a specific indexing property by its name/key.
413
     *
414
     * @param string $key Indexing property name/key.
415
     * @throws \InvalidArgumentException when the given $key does not exist.
416
     * @return string
417
     */
418
    public function getIndexingProperty($key)
419
    {
420
        $this->loadIndexingProperties();
421
422
        if (!array_key_exists($key, $this->indexingProperties)) {
423
            throw new \InvalidArgumentException(
424
                'No indexing property "' . $key . '".',
425
                1323174143
426
            );
427
        }
428
429
        return $this->indexingProperties[$key];
430
    }
431
432
    /**
433
     * Gets all indexing properties set for this item.
434
     *
435
     * @return array Array of indexing properties.
436
     */
437
    public function getIndexingProperties()
438
    {
439
        $this->loadIndexingProperties();
440
441
        return $this->indexingProperties;
442
    }
443
444
    /**
445
     * Gets the names/keys of the item's indexing properties.
446
     *
447
     * @return array Array of indexing property names/keys
448
     */
449
    public function getIndexingPropertyKeys()
450
    {
451
        $this->loadIndexingProperties();
452
453
        return array_keys($this->indexingProperties);
454
    }
455
}
456