We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 50 |
Total Lines | 437 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 2 | Features | 0 |
Complex classes like ext_update often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ext_update, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ext_update |
||
27 | { |
||
28 | /** |
||
29 | * This holds the output ready to return |
||
30 | * |
||
31 | * @var string |
||
32 | * @access protected |
||
33 | */ |
||
34 | protected $content = ''; |
||
35 | |||
36 | /** |
||
37 | * Triggers the update option in the extension manager |
||
38 | * |
||
39 | * @access public |
||
40 | * |
||
41 | * @return bool Should the update option be shown? |
||
42 | */ |
||
43 | public function access(): bool |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * The main method of the class |
||
65 | * |
||
66 | * @access public |
||
67 | * |
||
68 | * @return string The content that is displayed on the website |
||
69 | */ |
||
70 | public function main(): string |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get all outdated metadata configuration records |
||
96 | * |
||
97 | * @access protected |
||
98 | * |
||
99 | * @return array Array of UIDs of outdated records |
||
100 | */ |
||
101 | protected function getMetadataConfig(): array |
||
102 | { |
||
103 | $uids = []; |
||
104 | // check if tx_dlf_metadata.xpath exists anyhow |
||
105 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_dlf_metadata'); |
||
106 | |||
107 | $result = $queryBuilder |
||
108 | ->select('*') |
||
109 | ->from('tx_dlf_metadata') |
||
110 | ->execute(); |
||
111 | |||
112 | $rows = $result->fetchAll(); |
||
113 | |||
114 | if ((count($rows) === 0) || !array_key_exists('xpath', $rows[0])) { |
||
115 | return $uids; |
||
116 | } |
||
117 | foreach ($rows as $row) { |
||
118 | if ($row['format'] === 0 && $row['xpath']) { |
||
119 | $uids[] = (int)$row['uid']; |
||
120 | } |
||
121 | } |
||
122 | return $uids; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Check all configured Solr cores |
||
127 | * |
||
128 | * @access protected |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | protected function solariumSolrUpdateRequired(): bool |
||
133 | { |
||
134 | // Get all Solr cores that were not deleted. |
||
135 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_dlf_solrcores'); |
||
136 | $result = $queryBuilder |
||
137 | ->select('index_name') |
||
138 | ->from('tx_dlf_solrcores') |
||
139 | ->execute(); |
||
140 | |||
141 | while ($resArray = $result->fetch()) { |
||
142 | // Instantiate search object. |
||
143 | $solr = Solr::getInstance($resArray['index_name']); |
||
144 | if (!$solr->ready) { |
||
145 | return true; |
||
146 | } |
||
147 | } |
||
148 | return false; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Check for old format classes |
||
153 | * |
||
154 | * @access protected |
||
155 | * |
||
156 | * @return array containing old format classes |
||
157 | */ |
||
158 | protected function oldFormatClasses(): array |
||
159 | { |
||
160 | $oldRecords = []; |
||
161 | // Get all records with outdated configuration. |
||
162 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_dlf_formats'); |
||
163 | |||
164 | $result = $queryBuilder |
||
165 | ->select('tx_dlf_formats.uid AS uid', 'tx_dlf_formats.type AS type') |
||
166 | ->from('tx_dlf_formats') |
||
167 | ->where( |
||
168 | $queryBuilder->expr()->isNotNull('tx_dlf_formats.class'), |
||
169 | $queryBuilder->expr()->neq('tx_dlf_formats.class', $queryBuilder->createNamedParameter('')), |
||
170 | $queryBuilder->expr()->like('tx_dlf_formats.class', $queryBuilder->createNamedParameter('%tx_dlf_%')) |
||
171 | ) |
||
172 | ->execute(); |
||
173 | while ($resArray = $result->fetch()) { |
||
174 | $oldRecords[$resArray['uid']] = $resArray['type']; |
||
175 | } |
||
176 | |||
177 | return $oldRecords; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Check for old index related columns |
||
182 | * |
||
183 | * @access protected |
||
184 | * |
||
185 | * @return bool true if old index related columns exist |
||
186 | */ |
||
187 | protected function oldIndexRelatedTableNames(): bool |
||
188 | { |
||
189 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('INFORMATION_SCHEMA.COLUMNS'); |
||
190 | |||
191 | $result = $queryBuilder |
||
192 | ->select('column_name') |
||
193 | ->from('INFORMATION_SCHEMA.COLUMNS') |
||
194 | ->where('TABLE_NAME = "tx_dlf_metadata"') |
||
195 | ->execute(); |
||
196 | while ($resArray = $result->fetch()) { |
||
197 | if ( |
||
198 | $resArray['column_name'] === 'tokenized' |
||
199 | || $resArray['column_name'] === 'stored' |
||
200 | || $resArray['column_name'] === 'indexed' |
||
201 | || $resArray['column_name'] === 'boost' |
||
202 | || $resArray['column_name'] === 'autocomplete' |
||
203 | ) { |
||
204 | return true; |
||
205 | } |
||
206 | } |
||
207 | return false; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * check if document has format |
||
212 | * |
||
213 | * @access protected |
||
214 | * @param bool $checkStructureOnly |
||
215 | * @return bool |
||
216 | */ |
||
217 | protected function hasNoFormatForDocument($checkStructureOnly = false): bool |
||
218 | { |
||
219 | // Check if column "document_format" exists. |
||
220 | $database = $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['dbname']; |
||
221 | |||
222 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('INFORMATION_SCHEMA.COLUMNS'); |
||
223 | |||
224 | $result = $queryBuilder |
||
225 | ->select('COLUMN_NAME') |
||
226 | ->from('INFORMATION_SCHEMA.COLUMNS') |
||
227 | ->where('TABLE_NAME="tx_dlf_documents" AND TABLE_SCHEMA="' . $database . '" AND COLUMN_NAME="document_format"') |
||
228 | ->execute(); |
||
229 | while ($resArray = $result->fetch()) { |
||
230 | if ($resArray['COLUMN_NAME'] === 'document_format') { |
||
231 | if ($checkStructureOnly) { |
||
232 | return false; |
||
233 | } |
||
234 | // Check if column has empty fields. |
||
235 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('INFORMATION_SCHEMA.COLUMNS'); |
||
236 | $count = $queryBuilder |
||
237 | ->count('uid') |
||
238 | ->from('tx_dlf_documents') |
||
239 | ->where('document_format="" OR document_format IS NULL') |
||
240 | ->execute() |
||
241 | ->fetchColumn(0); |
||
242 | |||
243 | if ($count === 0) { |
||
244 | return false; |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | return true; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Copy the data of the old index related columns to the new columns |
||
253 | * |
||
254 | * @access protected |
||
255 | * |
||
256 | * @return void |
||
257 | */ |
||
258 | protected function renameIndexRelatedColumns(): void |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Update all outdated format records |
||
290 | * |
||
291 | * @access protected |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | protected function updateFormatClasses(): void |
||
296 | { |
||
297 | $oldRecords = $this->oldFormatClasses(); |
||
298 | $newValues = [ |
||
299 | 'ALTO' => 'Kitodo\\\\Dlf\\\\Format\\\\Alto', // Those are effectively single backslashes |
||
300 | 'MODS' => 'Kitodo\\\\Dlf\\\\Format\\\\Mods', |
||
301 | 'TEIHDR' => 'Kitodo\\\\Dlf\\\\Format\\\\TeiHeader' |
||
302 | ]; |
||
303 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_dlf_formats'); |
||
304 | |||
305 | foreach ($oldRecords as $uid => $type) { |
||
306 | $queryBuilder |
||
307 | ->update('tx_dlf_formats') |
||
308 | ->where( |
||
309 | $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid)) |
||
310 | ) |
||
311 | ->set('class', $newValues[$type]) |
||
312 | ->execute(); |
||
313 | } |
||
314 | Helper::addMessage( |
||
315 | htmlspecialchars($GLOBALS['LANG']->getLL('update.FormatClassesOkay')), |
||
316 | htmlspecialchars($GLOBALS['LANG']->getLL('update.FormatClasses')), |
||
317 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK |
||
318 | ); |
||
319 | $this->content .= Helper::renderFlashMessages(); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Update all outdated metadata configuration records |
||
324 | * |
||
325 | * @access protected |
||
326 | * |
||
327 | * @return void |
||
328 | */ |
||
329 | protected function updateMetadataConfig(): void |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | |||
387 | |||
388 | |||
389 | /** |
||
390 | * Create all configured Solr cores |
||
391 | * |
||
392 | * @access protected |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | protected function doSolariumSolrUpdate(): void |
||
397 | { |
||
398 | $error = false; |
||
399 | // Get all Solr cores that were not deleted. |
||
400 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_dlf_solrcores'); |
||
401 | $result = $queryBuilder |
||
402 | ->select('index_name') |
||
403 | ->from('tx_dlf_solrcores') |
||
404 | ->execute(); |
||
405 | |||
406 | while ($resArray = $result->fetch()) { |
||
407 | // Create core if it doesn't exist. |
||
408 | if (Solr::createCore($resArray['index_name']) !== $resArray['index_name']) { |
||
409 | Helper::addMessage( |
||
410 | htmlspecialchars($GLOBALS['LANG']->getLL('update.solariumSolrUpdateNotOkay')), |
||
411 | htmlspecialchars(sprintf($GLOBALS['LANG']->getLL('update.solariumSolrUpdate'), $resArray['index_name'])), |
||
412 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR |
||
413 | ); |
||
414 | $this->content .= Helper::renderFlashMessages(); |
||
415 | $error = true; |
||
416 | } |
||
417 | } |
||
418 | if (!$error) { |
||
419 | Helper::addMessage( |
||
420 | htmlspecialchars($GLOBALS['LANG']->getLL('update.solariumSolrUpdateOkay')), |
||
421 | htmlspecialchars($GLOBALS['LANG']->getLL('update.solariumSolrUpdate')), |
||
422 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK |
||
423 | ); |
||
424 | $this->content .= Helper::renderFlashMessages(); |
||
425 | } |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Add format type to outdated tx_dlf_documents rows |
||
430 | * |
||
431 | * @return void |
||
432 | */ |
||
433 | protected function updateDocumentAddFormat(): void |
||
463 | } |
||
464 | } |
||
465 |