Passed
Pull Request — master (#1257)
by Timo
38:37 queued 34:16
created

Item::updateIndexedTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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