Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 118576...18bf9c )
by Sebastian
14:55
created

tx_dlf_indexing::solrConnect()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
/**
13
 * Indexing class 'tx_dlf_indexing' for the 'dlf' extension.
14
 *
15
 * @author	Sebastian Meyer <[email protected]>
16
 * @package	TYPO3
17
 * @subpackage	tx_dlf
18
 * @access	public
19
 */
20
class tx_dlf_indexing {
21
22
    /**
23
     * The extension key
24
     *
25
     * @var	string
26
     * @access public
27
     */
28
    public static $extKey = 'dlf';
29
30
    /**
31
     * Array of metadata fields' configuration
32
     * @see loadIndexConf()
33
     *
34
     * @var	array
35
     * @access protected
36
     */
37
    protected static $fields = array (
38
        'autocomplete' => array (),
39
        'facets' => array (),
40
        'sortables' => array (),
41
        'indexed' => array (),
42
        'stored' => array (),
43
        'tokenized' => array (),
44
        'fieldboost' => array ()
45
    );
46
47
    /**
48
     * Is the index configuration loaded?
49
     * @see $fields
50
     *
51
     * @var	boolean
52
     * @access protected
53
     */
54
    protected static $fieldsLoaded = FALSE;
55
56
    /**
57
     * List of already processed documents
58
     *
59
     * @var	array
60
     * @access protected
61
     */
62
    protected static $processedDocs = array ();
63
64
    /**
65
     * Instance of Apache_Solr_Service class
0 ignored issues
show
Bug introduced by
The type Apache_Solr_Service was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
     *
67
     * @var	Apache_Solr_Service
68
     * @access protected
69
     */
70
    protected static $solr;
71
72
    /**
73
     * Array of toplevel structure elements
74
     * @see loadIndexConf()
75
     *
76
     * @var	array
77
     * @access protected
78
     */
79
    protected static $toplevel = array ();
80
81
    /**
82
     * Insert given document into Solr index
83
     *
84
     * @access	public
85
     *
86
     * @param	tx_dlf_document		&$doc: The document to add
87
     * @param	integer		$core: UID of the Solr core to use
88
     *
89
     * @return	integer		0 on success or 1 on failure
90
     */
91
    public static function add(tx_dlf_document &$doc, $core = 0) {
92
93
        if (in_array($doc->uid, self::$processedDocs)) {
0 ignored issues
show
Bug Best Practice introduced by
The property $uid is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
94
95
            return 0;
96
97
        } elseif (self::solrConnect($core, $doc->pid)) {
0 ignored issues
show
Bug Best Practice introduced by
The property $pid is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
98
99
            $errors = 0;
100
101
            // Handle multi-volume documents.
102
            if ($doc->parentId) {
0 ignored issues
show
Bug Best Practice introduced by
The property $parentId is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
103
104
                $parent = & tx_dlf_document::getInstance($doc->parentId, 0, TRUE);
105
106
                if ($parent->ready) {
0 ignored issues
show
Bug Best Practice introduced by
The property $ready is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
107
108
                    $errors = self::add($parent, $core);
109
110
                } else {
111
112
                    if (TYPO3_DLOG) {
113
114
                        \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->add(['.$doc->uid.'], '.$core.')] Could not load parent document with UID "'.$doc->parentId.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
115
116
                    }
117
118
                    return 1;
119
120
                }
121
122
            }
123
124
            try {
125
126
                // Add document to list of processed documents.
127
                self::$processedDocs[] = $doc->uid;
128
129
                // Delete old Solr documents.
130
                self::$solr->service->deleteByQuery('uid:'.$doc->uid);
131
132
                // Index every logical unit as separate Solr document.
133
                foreach ($doc->tableOfContents as $logicalUnit) {
0 ignored issues
show
Bug Best Practice introduced by
The property $tableOfContents is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
134
135
                    if (!$errors) {
136
137
                        $errors = self::processLogical($doc, $logicalUnit);
138
139
                    } else {
140
141
                        break;
142
143
                    }
144
145
                }
146
147
                // Index fulltext files if available.
148
                if ($doc->hasFulltext) {
149
150
                    foreach ($doc->physicalStructure as $pageNumber => $xmlId) {
151
152
                        if (!$errors) {
153
154
                            $errors = self::processPhysical($doc, $pageNumber, $doc->physicalStructureInfo[$xmlId]);
155
156
                        } else {
157
158
                            break;
159
160
                        }
161
162
                    }
163
164
                }
165
166
                self::$solr->service->commit();
167
168
                // Get document title from database.
169
                $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
170
                    'tx_dlf_documents.title AS title',
171
                    'tx_dlf_documents',
172
                    'tx_dlf_documents.uid='.intval($doc->uid).tx_dlf_helper::whereClause('tx_dlf_documents'),
173
                    '',
174
                    '',
175
                    '1'
176
                );
177
178
                $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
179
180
                if (!defined('TYPO3_cliMode')) {
181
182
                    if (!$errors) {
183
184
                        $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
185
                            'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
186
                            htmlspecialchars(sprintf(tx_dlf_helper::getLL('flash.documentIndexed'), $resArray['title'], $doc->uid)),
187
                            tx_dlf_helper::getLL('flash.done', TRUE),
188
                            \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
189
                            TRUE
190
                        );
191
192
                    } else {
193
194
                        $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
195
                            'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
196
                            htmlspecialchars(sprintf(tx_dlf_helper::getLL('flash.documentNotIndexed'), $resArray['title'], $doc->uid)),
197
                            tx_dlf_helper::getLL('flash.error', TRUE),
198
                            \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
199
                            TRUE
200
                        );
201
202
                    }
203
204
                    tx_dlf_helper::addMessage($message);
205
206
                }
207
208
                return $errors;
209
210
            } catch (Exception $e) {
211
212
                if (!defined('TYPO3_cliMode')) {
213
214
                    $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
215
                        'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
216
                        tx_dlf_helper::getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()),
217
                        tx_dlf_helper::getLL('flash.error', TRUE),
218
                        \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
219
                        TRUE
220
                    );
221
222
                    tx_dlf_helper::addMessage($message);
223
224
                }
225
226
                if (TYPO3_DLOG) {
227
228
                    \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->add(['.$doc->uid.'], '.$core.')] Apache Solr threw exception: "'.$e->getMessage().'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
229
230
                }
231
232
                return 1;
233
234
            }
235
236
        } else {
237
238
            if (!defined('TYPO3_cliMode')) {
239
240
                $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
241
                    'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
242
                    tx_dlf_helper::getLL('flash.solrNoConnection', TRUE),
243
                    tx_dlf_helper::getLL('flash.warning', TRUE),
244
                    \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING,
245
                    TRUE
246
                );
247
248
                tx_dlf_helper::addMessage($message);
249
250
            }
251
252
            if (TYPO3_DLOG) {
253
254
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->add(['.$doc->uid.'], '.$core.')] Could not connect to Apache Solr server', self::$extKey, SYSLOG_SEVERITY_ERROR);
255
256
            }
257
258
            return 1;
259
260
        }
261
262
    }
263
264
    /**
265
     * Delete document from Solr index
266
     *
267
     * @access	public
268
     *
269
     * @param	integer		$uid: UID of the document to delete
270
     *
271
     * @return	integer		0 on success or 1 on failure
272
     */
273
    public static function delete($uid) {
274
275
        // Save parameter for logging purposes.
276
        $_uid = $uid;
277
278
        // Sanitize input.
279
        $uid = max(intval($uid), 0);
280
281
        // Get Solr core for document.
282
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
283
            'tx_dlf_solrcores.uid AS uid,tx_dlf_documents.title AS title',
284
            'tx_dlf_solrcores,tx_dlf_documents',
285
            'tx_dlf_solrcores.uid=tx_dlf_documents.solrcore AND tx_dlf_documents.uid='.$uid.tx_dlf_helper::whereClause('tx_dlf_solrcores'),
286
            '',
287
            '',
288
            '1'
289
        );
290
291
        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
292
293
            list ($core, $title) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result);
294
295
            // Establish Solr connection.
296
            if (self::solrConnect($core)) {
297
298
                try {
299
300
                    // Delete Solr document.
301
                    self::$solr->service->deleteByQuery('uid:'.$uid);
302
303
                    self::$solr->service->commit();
304
305
                } catch (Exception $e) {
306
307
                    if (!defined('TYPO3_cliMode')) {
308
309
                        $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
310
                            'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
311
                            tx_dlf_helper::getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()),
312
                            tx_dlf_helper::getLL('flash.error', TRUE),
313
                            \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
314
                            TRUE
315
                        );
316
317
                        tx_dlf_helper::addMessage($message);
318
319
                    }
320
321
                    if (TYPO3_DLOG) {
322
323
                        \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->delete('.$_uid.')] Apache Solr threw exception: "'.$e->getMessage().'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
324
325
                    }
326
327
                    return 1;
328
329
                }
330
331
            } else {
332
333
                if (!defined('TYPO3_cliMode')) {
334
335
                    $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
336
                        'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
337
                        tx_dlf_helper::getLL('flash.solrNoConnection', TRUE),
338
                        tx_dlf_helper::getLL('flash.error', TRUE),
339
                        \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
340
                        TRUE
341
                    );
342
343
                    tx_dlf_helper::addMessage($message);
344
345
                }
346
347
                if (TYPO3_DLOG) {
348
349
                    \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->delete('.$_uid.')] Could not connect to Apache Solr server', self::$extKey, SYSLOG_SEVERITY_ERROR);
350
351
                }
352
353
                return 1;
354
355
            }
356
357
            if (!defined('TYPO3_cliMode')) {
358
359
                $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
360
                    'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
361
                    htmlspecialchars(sprintf(tx_dlf_helper::getLL('flash.documentDeleted'), $title, $uid)),
362
                    tx_dlf_helper::getLL('flash.done', TRUE),
363
                    \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
364
                    TRUE
365
                );
366
367
                tx_dlf_helper::addMessage($message);
368
369
            }
370
371
            return 0;
372
373
        } else {
374
375
            if (TYPO3_DLOG) {
376
377
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->delete('.$_uid.')] Invalid UID "'.$uid.'" for document deletion', self::$extKey, SYSLOG_SEVERITY_ERROR);
378
379
            }
380
381
            return 1;
382
383
        }
384
385
    }
386
387
    /**
388
     * Returns the dynamic index field name for the given metadata field.
389
     *
390
     * @access	public
391
     *
392
     * @param	string		$index_name: The metadata field's name in database
393
     * @param	integer		$pid: UID of the configuration page
394
     *
395
     * @return	string		The field's dynamic index name
396
     */
397
    public static function getIndexFieldName($index_name, $pid = 0) {
398
399
        // Save parameter for logging purposes.
400
        $_pid = $pid;
401
402
        // Sanitize input.
403
        $pid = max(intval($pid), 0);
404
405
        if (!$pid) {
406
407
            if (TYPO3_DLOG) {
408
409
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->getIndexFieldName('.$index_name.', '.$_pid.')] Invalid PID "'.$pid.'" for metadata configuration', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
410
411
            }
412
413
            return '';
414
415
        }
416
417
        // Load metadata configuration.
418
        self::loadIndexConf($pid);
419
420
        // Build field's suffix.
421
        $suffix = (in_array($index_name, self::$fields['tokenized']) ? 't' : 'u');
422
423
        $suffix .= (in_array($index_name, self::$fields['stored']) ? 's' : 'u');
424
425
        $suffix .= (in_array($index_name, self::$fields['indexed']) ? 'i' : 'u');
426
427
        $index_name .= '_'.$suffix;
428
429
        return $index_name;
430
431
    }
432
433
    /**
434
     * Load indexing configuration
435
     *
436
     * @access	protected
437
     *
438
     * @param	integer		$pid: The configuration page's UID
439
     *
440
     * @return	void
441
     */
442
    protected static function loadIndexConf($pid) {
443
444
        if (!self::$fieldsLoaded) {
445
446
            // Get the list of toplevel structures.
447
            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
448
                'tx_dlf_structures.index_name AS index_name',
449
                'tx_dlf_structures',
450
                'tx_dlf_structures.toplevel=1 AND tx_dlf_structures.pid='.intval($pid).tx_dlf_helper::whereClause('tx_dlf_structures'),
451
                '',
452
                '',
453
                ''
454
            );
455
456
            while ($toplevel = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
457
458
                self::$toplevel[] = $toplevel['index_name'];
459
460
            }
461
462
            // Get the metadata indexing options.
463
            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
464
                'tx_dlf_metadata.index_name AS index_name,tx_dlf_metadata.index_tokenized AS index_tokenized,tx_dlf_metadata.index_stored AS index_stored,tx_dlf_metadata.index_indexed AS index_indexed,tx_dlf_metadata.is_sortable AS is_sortable,tx_dlf_metadata.is_facet AS is_facet,tx_dlf_metadata.is_listed AS is_listed,tx_dlf_metadata.index_autocomplete AS index_autocomplete,tx_dlf_metadata.index_boost AS index_boost',
465
                'tx_dlf_metadata',
466
                'tx_dlf_metadata.pid='.intval($pid).tx_dlf_helper::whereClause('tx_dlf_metadata'),
467
                '',
468
                '',
469
                ''
470
            );
471
472
            while ($indexing = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
473
474
                if ($indexing['index_tokenized']) {
475
476
                    self::$fields['tokenized'][] = $indexing['index_name'];
477
478
                }
479
480
                if ($indexing['index_stored'] || $indexing['is_listed']) {
481
482
                    self::$fields['stored'][] = $indexing['index_name'];
483
484
                }
485
486
                if ($indexing['index_indexed'] || $indexing['index_autocomplete']) {
487
488
                    self::$fields['indexed'][] = $indexing['index_name'];
489
490
                }
491
492
                if ($indexing['is_sortable']) {
493
494
                    self::$fields['sortables'][] = $indexing['index_name'];
495
496
                }
497
498
                if ($indexing['is_facet']) {
499
500
                    self::$fields['facets'][] = $indexing['index_name'];
501
502
                }
503
504
                if ($indexing['index_autocomplete']) {
505
506
                    self::$fields['autocomplete'][] = $indexing['index_name'];
507
508
                }
509
510
                if ($indexing['index_boost'] > 0.0) {
511
512
                    self::$fields['fieldboost'][$indexing['index_name']] = floatval($indexing['index_boost']);
513
514
                } else {
515
516
                    self::$fields['fieldboost'][$indexing['index_name']] = FALSE;
517
518
                }
519
520
            }
521
522
            self::$fieldsLoaded = TRUE;
523
524
        }
525
526
    }
527
528
    /**
529
     * Processes a logical unit (and its children) for the Solr index
530
     *
531
     * @access	protected
532
     *
533
     * @param	tx_dlf_document		&$doc: The METS document
534
     * @param	array		$logicalUnit: Array of the logical unit to process
535
     *
536
     * @return	integer		0 on success or 1 on failure
537
     */
538
    protected static function processLogical(tx_dlf_document &$doc, array $logicalUnit) {
539
540
        $errors = 0;
541
542
        // Get metadata for logical unit.
543
        $metadata = $doc->metadataArray[$logicalUnit['id']];
0 ignored issues
show
Bug Best Practice introduced by
The property $metadataArray is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
544
545
        if (!empty($metadata)) {
546
547
            // Load class.
548
            if (!class_exists('Apache_Solr_Document')) {
549
550
                require_once(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:'.self::$extKey.'/lib/SolrPhpClient/Apache/Solr/Document.php'));
551
552
            }
553
554
            // Create new Solr document.
555
            $solrDoc = new Apache_Solr_Document();
0 ignored issues
show
Bug introduced by
The type Apache_Solr_Document was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
556
557
            // Create unique identifier from document's UID and unit's XML ID.
558
            $solrDoc->setField('id', $doc->uid.$logicalUnit['id']);
0 ignored issues
show
Bug Best Practice introduced by
The property $uid is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
559
560
            $solrDoc->setField('uid', $doc->uid);
561
562
            $solrDoc->setField('pid', $doc->pid);
0 ignored issues
show
Bug Best Practice introduced by
The property $pid is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
563
564
            if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($logicalUnit['points'])) {
565
566
                $solrDoc->setField('page', $logicalUnit['points']);
567
568
            }
569
570
            if ($logicalUnit['id'] == $doc->toplevelId) {
0 ignored issues
show
Bug Best Practice introduced by
The property $toplevelId is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
571
572
                $solrDoc->setField('thumbnail', $doc->thumbnail);
0 ignored issues
show
Bug Best Practice introduced by
The property $thumbnail is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
573
574
            } elseif (!empty($logicalUnit['thumbnailId'])) {
575
576
                $solrDoc->setField('thumbnail', $doc->getFileLocation($logicalUnit['thumbnailId']));
577
578
            }
579
580
            $solrDoc->setField('partof', $doc->parentId);
0 ignored issues
show
Bug Best Practice introduced by
The property $parentId is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
581
582
            $solrDoc->setField('root', $doc->rootId);
0 ignored issues
show
Bug Best Practice introduced by
The property $rootId is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
583
584
            $solrDoc->setField('sid', $logicalUnit['id']);
585
586
            $solrDoc->setField('toplevel', in_array($logicalUnit['type'], self::$toplevel));
587
588
            $solrDoc->setField('type', $logicalUnit['type'], self::$fields['fieldboost']['type']);
589
590
            $solrDoc->setField('title', $metadata['title'][0], self::$fields['fieldboost']['title']);
591
592
            $solrDoc->setField('volume', $metadata['volume'][0], self::$fields['fieldboost']['volume']);
593
594
            $solrDoc->setField('record_id', $metadata['record_id'][0]);
595
596
            $solrDoc->setField('purl', $metadata['purl'][0]);
597
598
            $solrDoc->setField('location', $doc->location);
0 ignored issues
show
Bug Best Practice introduced by
The property $location is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
599
600
            $solrDoc->setField('urn', $metadata['urn']);
601
602
            $solrDoc->setField('collection', $doc->metadataArray[$doc->toplevelId]['collection']);
603
604
            $autocomplete = array ();
605
606
            foreach ($metadata as $index_name => $data) {
607
608
                if (!empty($data) && substr($index_name, -8) !== '_sorting') {
609
610
                    $solrDoc->setField(self::getIndexFieldName($index_name, $doc->pid), $data, self::$fields['fieldboost'][$index_name]);
611
612
                    if (in_array($index_name, self::$fields['sortables'])) {
613
614
                        // Add sortable fields to index.
615
                        $solrDoc->setField($index_name.'_sorting', $metadata[$index_name.'_sorting'][0]);
616
617
                    }
618
619
                    if (in_array($index_name, self::$fields['facets'])) {
620
621
                        // Add facets to index.
622
                        $solrDoc->setField($index_name.'_faceting', $data);
623
624
                    }
625
626
                    if (in_array($index_name, self::$fields['autocomplete'])) {
627
628
                        $autocomplete = array_merge($autocomplete, $data);
629
630
                    }
631
632
                }
633
634
            }
635
636
            // Add autocomplete values to index.
637
            if (!empty($autocomplete)) {
638
639
                $solrDoc->setField('autocomplete', $autocomplete);
640
641
            }
642
643
            // Add collection information to logical sub-elements if applicable.
644
            if (in_array('collection', self::$fields['facets'])
645
                && empty($metadata['collection'])
646
                && !empty($doc->metadataArray[$doc->toplevelId]['collection'])) {
647
648
                $solrDoc->setField('collection_faceting', $doc->metadataArray[$doc->toplevelId]['collection']);
649
650
            }
651
652
            try {
653
654
                self::$solr->service->addDocument($solrDoc);
655
656
            } catch (Exception $e) {
657
658
                if (!defined('TYPO3_cliMode')) {
659
660
                    $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
661
                        'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
662
                        tx_dlf_helper::getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()),
663
                        tx_dlf_helper::getLL('flash.error', TRUE),
664
                        \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
665
                        TRUE
666
                    );
667
668
                    tx_dlf_helper::addMessage($message);
669
670
                }
671
672
                return 1;
673
674
            }
675
676
        }
677
678
        // Check for child elements...
679
        if (!empty($logicalUnit['children'])) {
680
681
            foreach ($logicalUnit['children'] as $child) {
682
683
                if (!$errors) {
684
685
                    // ...and process them, too.
686
                    $errors = self::processLogical($doc, $child);
687
688
                } else {
689
690
                    break;
691
692
                }
693
694
            }
695
696
        }
697
698
        return $errors;
699
700
    }
701
702
    /**
703
     * Processes a physical unit for the Solr index
704
     *
705
     * @access	protected
706
     *
707
     * @param	tx_dlf_document		&$doc: The METS document
708
     * @param	integer		$page: The page number
709
     * @param	array		$physicalUnit: Array of the physical unit to process
710
     *
711
     * @return	integer		0 on success or 1 on failure
712
     */
713
    protected static function processPhysical(tx_dlf_document &$doc, $page, array $physicalUnit) {
714
715
        $errors = 0;
716
717
        // Read extension configuration.
718
        $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]);
719
720
        if (!empty($physicalUnit['files'][$extConf['fileGrpFulltext']])) {
721
722
            $file = $doc->getFileLocation($physicalUnit['files'][$extConf['fileGrpFulltext']]);
723
724
            // Load XML file.
725
            if (\TYPO3\CMS\Core\Utility\GeneralUtility::isValidUrl($file)) {
726
727
                // Set user-agent to identify self when fetching XML data.
728
                if (!empty($extConf['useragent'])) {
729
730
                    @ini_set('user_agent', $extConf['useragent']);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition for ini_set(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

730
                    /** @scrutinizer ignore-unhandled */ @ini_set('user_agent', $extConf['useragent']);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
731
732
                }
733
734
                // Turn off libxml's error logging.
735
                $libxmlErrors = libxml_use_internal_errors(TRUE);
736
737
                // disable entity loading
738
                $previousValueOfEntityLoader = libxml_disable_entity_loader(TRUE);
739
740
                // Load XML from file.
741
                $xml = simplexml_load_string(\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($file));
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Core\Utility\G...lUtility::getUrl($file) can also be of type mixed and false; however, parameter $data of simplexml_load_string() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

741
                $xml = simplexml_load_string(/** @scrutinizer ignore-type */ \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($file));
Loading history...
742
743
                // reset entity loader setting
744
                libxml_disable_entity_loader($previousValueOfEntityLoader);
745
746
                // Reset libxml's error logging.
747
                libxml_use_internal_errors($libxmlErrors);
748
749
                if ($xml === FALSE) {
750
751
                    return 1;
752
753
                }
754
755
            } else {
756
757
                return 1;
758
759
            }
760
761
            // Load class.
762
            if (!class_exists('Apache_Solr_Document')) {
763
764
                require_once(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:'.self::$extKey.'/lib/SolrPhpClient/Apache/Solr/Document.php'));
765
766
            }
767
768
            // Create new Solr document.
769
            $solrDoc = new Apache_Solr_Document();
770
771
            // Create unique identifier from document's UID and unit's XML ID.
772
            $solrDoc->setField('id', $doc->uid.$physicalUnit['id']);
0 ignored issues
show
Bug Best Practice introduced by
The property $uid is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
773
774
            $solrDoc->setField('uid', $doc->uid);
775
776
            $solrDoc->setField('pid', $doc->pid);
0 ignored issues
show
Bug Best Practice introduced by
The property $pid is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
777
778
            $solrDoc->setField('page', $page);
779
780
            if (!empty($physicalUnit['files'][$extConf['fileGrpThumbs']])) {
781
782
                $solrDoc->setField('thumbnail', $doc->getFileLocation($physicalUnit['files'][$extConf['fileGrpThumbs']]));
783
784
            }
785
786
            $solrDoc->setField('partof', $doc->parentId);
0 ignored issues
show
Bug Best Practice introduced by
The property $parentId is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
787
788
            $solrDoc->setField('root', $doc->rootId);
0 ignored issues
show
Bug Best Practice introduced by
The property $rootId is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
789
790
            $solrDoc->setField('sid', $physicalUnit['id']);
791
792
            $solrDoc->setField('toplevel', FALSE);
793
794
            $solrDoc->setField('type', $physicalUnit['type'], self::$fields['fieldboost']['type']);
795
796
            $solrDoc->setField('fulltext', tx_dlf_alto::getRawText($xml));
797
798
            // Add faceting information to physical sub-elements if applicable.
799
            foreach ($doc->metadataArray[$doc->toplevelId] as $index_name => $data) {
0 ignored issues
show
Bug Best Practice introduced by
The property $toplevelId is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
Bug Best Practice introduced by
The property $metadataArray is declared protected in tx_dlf_document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
800
801
                if (!empty($data) && substr($index_name, -8) !== '_sorting') {
802
803
                    if (in_array($index_name, self::$fields['facets'])) {
804
805
                        // Add facets to index.
806
                        $solrDoc->setField($index_name.'_faceting', $data);
807
808
                    }
809
810
                }
811
812
            }
813
814
            try {
815
816
                self::$solr->service->addDocument($solrDoc);
817
818
            } catch (Exception $e) {
819
820
                if (!defined('TYPO3_cliMode')) {
821
822
                    $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
823
                        'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
824
                        tx_dlf_helper::getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()),
825
                        tx_dlf_helper::getLL('flash.error', TRUE),
826
                        \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
827
                        TRUE
828
                    );
829
830
                    tx_dlf_helper::addMessage($message);
831
832
                }
833
834
                return 1;
835
836
            }
837
838
        }
839
840
        return $errors;
841
842
    }
843
844
    /**
845
     * Connects to Solr server.
846
     *
847
     * @access	protected
848
     *
849
     * @param	integer		$core: UID of the Solr core
850
     * @param	integer		$pid: UID of the configuration page
851
     *
852
     * @return	boolean		TRUE on success or FALSE on failure
853
     */
854
    protected static function solrConnect($core, $pid = 0) {
855
856
        // Get Solr instance.
857
        if (!self::$solr) {
858
859
            // Connect to Solr server.
860
            if (self::$solr = tx_dlf_solr::getInstance($core)) {
0 ignored issues
show
Documentation Bug introduced by
It seems like tx_dlf_solr::getInstance($core) of type tx_dlf_solr is incompatible with the declared type Apache_Solr_Service of property $solr.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

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

Loading history...
861
862
                // Load indexing configuration if needed.
863
                if ($pid) {
864
865
                    self::loadIndexConf($pid);
866
867
                }
868
869
            } else {
870
871
                return FALSE;
872
873
            }
874
875
        }
876
877
        return TRUE;
878
879
    }
880
881
    /**
882
     * This is a static class, thus no instances should be created
883
     *
884
     * @access private
885
     */
886
    private function __construct() {}
887
888
}
889