Passed
Pull Request — master (#2)
by
unknown
04:59
created

RefIndex::setSelectedTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
namespace Aoe\UpdateRefindex\Typo3;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Core\Database\DatabaseConnection;
29
use TYPO3\CMS\Core\Database\ReferenceIndex;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * scheduler-task to update refindex of TYPO3
34
 *
35
 * @package update_refindex
36
 * @subpackage Typo3
37
 */
38
class RefIndex
39
{
40
    /**
41
     * @var array
42
     */
43
    private $existingTables;
44
45
    /**
46
     * @var array
47
     */
48
    private $selectedTables = array();
49
50
    /**
51
     * @param array $selectedTables
52
     * @return RefIndex
53
     */
54 1
    public function setSelectedTables(array $selectedTables)
55
    {
56 1
        $this->selectedTables = $selectedTables;
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * @return array
63
     */
64 1
    public function getSelectedTables()
65
    {
66 1
        return $this->selectedTables;
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function getExistingTables()
73
    {
74
        if ($this->existingTables === null) {
75
            $this->existingTables = array_keys($GLOBALS['TCA']);
76
            sort($this->existingTables);
77
        }
78
79
        return $this->existingTables;
80
    }
81
82
    /**
83
     * update refIndex of selected tables
84
     */
85 1
    public function update()
86
    {
87
        // update index of selected tables
88 1
        foreach ($this->getSelectedTables() as $selectedTable) {
89 1
            if (array_search($selectedTable, $this->getExistingTables()) !== false) {
90 1
                $this->updateTable($selectedTable);
91 1
            }
92 1
        }
93
94
        // delete lost indexes ONLY if index of ALL tables where updated
95 1
        if (count($this->getExistingTables()) === count($this->getSelectedTables())) {
96 1
            $this->deleteLostIndexes();
97 1
        }
98 1
    }
99
100
    /**
101
     * @return ReferenceIndex
102
     */
103
    protected function getReferenceIndex()
104
    {
105
        return GeneralUtility::makeInstance(ReferenceIndex::class);
106
    }
107
108
    /**
109
     * Searching lost indexes for non-existing tables
110
     * this code is inspired by the code of method 'updateIndex' in class '\TYPO3\CMS\Core\Database\ReferenceIndex'
111
     */
112 1
    protected function deleteLostIndexes()
113
    {
114 1
        $where = 'tablename NOT IN (' . implode(',',
115 1
                $this->getDatabaseConnection()->fullQuoteArray($this->getExistingTables(), 'sys_refindex')) . ')';
116 1
        $this->getDatabaseConnection()->exec_DELETEquery('sys_refindex', $where);
117 1
    }
118
119
    /**
120
     * @return DatabaseConnection
121
     */
122
    protected function getDatabaseConnection()
123
    {
124
        return $GLOBALS['TYPO3_DB'];
125
    }
126
127
    /**
128
     * update table
129
     * this code is inspired by the code of method 'updateIndex' in class '\TYPO3\CMS\Core\Database\ReferenceIndex'
130
     *
131
     * @param string $tableName
132
     */
133 1
    protected function updateTable($tableName)
134
    {
135
        // Traverse all records in table, including deleted records:
136 1
        $allRecs = $this->getDatabaseConnection()
137 1
            ->exec_SELECTgetRows('uid', $tableName, '1=1'); //.TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tableName)
138 1
        $uidList = array(0);
139 1
        foreach ($allRecs as $recdat) {
140 1
            $this->getReferenceIndex()->updateRefIndexTable($tableName, $recdat['uid']);
141 1
            $uidList[] = $recdat['uid'];
142 1
        }
143
144
        // Searching lost indexes for this table:
145 1
        $where = 'tablename=' . $this->getDatabaseConnection()
146 1
                ->fullQuoteStr($tableName, 'sys_refindex') . ' AND recuid NOT IN (' . implode(',', $uidList) . ')';
147 1
        $this->getDatabaseConnection()->exec_DELETEquery('sys_refindex', $where);
148 1
    }
149
}
150