Passed
Push — master ( 771259...180e9a )
by Timo
25:15 queued 04:34
created

Item::hasIndexingProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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