Passed
Pull Request — master (#1228)
by Timo
18:39
created

Item::setChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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