We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 55 |
| Total Lines | 420 |
| Duplicated Lines | 0 % |
| Changes | 16 | ||
| 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 |
||
| 24 | class ext_update |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * This holds the output ready to return |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | * @access protected |
||
| 31 | */ |
||
| 32 | protected $content = ''; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Triggers the update option in the extension manager |
||
| 36 | * |
||
| 37 | * @access public |
||
| 38 | * |
||
| 39 | * @return bool Should the update option be shown? |
||
| 40 | */ |
||
| 41 | public function access() |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get all outdated metadata configuration records |
||
| 59 | * |
||
| 60 | * @access protected |
||
| 61 | * |
||
| 62 | * @return array Array of UIDs of outdated records |
||
| 63 | */ |
||
| 64 | protected function getMetadataConfig() |
||
| 65 | { |
||
| 66 | $uids = []; |
||
| 67 | // check if tx_dlf_metadata.xpath exists anyhow |
||
| 68 | $fieldsInDatabase = $GLOBALS['TYPO3_DB']->admin_get_fields('tx_dlf_metadata'); |
||
| 69 | if (!in_array('xpath', array_keys($fieldsInDatabase))) { |
||
| 70 | return $uids; |
||
| 71 | } |
||
| 72 | // Get all records with outdated configuration. |
||
| 73 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 74 | 'tx_dlf_metadata.uid AS uid', |
||
| 75 | 'tx_dlf_metadata', |
||
| 76 | 'tx_dlf_metadata.format=0' |
||
| 77 | . ' AND NOT tx_dlf_metadata.xpath=""' |
||
| 78 | . Helper::whereClause('tx_dlf_metadata') |
||
| 79 | ); |
||
| 80 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) { |
||
| 81 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 82 | $uids[] = intval($resArray['uid']); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | return $uids; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The main method of the class |
||
| 90 | * |
||
| 91 | * @access public |
||
| 92 | * |
||
| 93 | * @return string The content that is displayed on the website |
||
| 94 | */ |
||
| 95 | public function main() |
||
| 96 | { |
||
| 97 | // Load localization file. |
||
| 98 | $GLOBALS['LANG']->includeLLFile('EXT:dlf/Resources/Private/Language/FlashMessages.xml'); |
||
| 99 | // Update the metadata configuration. |
||
| 100 | if (count($this->getMetadataConfig())) { |
||
| 101 | $this->updateMetadataConfig(); |
||
| 102 | } |
||
| 103 | if ($this->oldIndexRelatedTableNames()) { |
||
| 104 | $this->renameIndexRelatedColumns(); |
||
| 105 | } |
||
| 106 | if ($this->solariumSolrUpdateRequired()) { |
||
| 107 | $this->doSolariumSolrUpdate(); |
||
| 108 | } |
||
| 109 | if (count($this->oldFormatClasses())) { |
||
| 110 | $this->updateFormatClasses(); |
||
| 111 | } |
||
| 112 | // Set tx_dlf_documents.document_format to distinguish between METS and IIIF. |
||
| 113 | if ($this->hasNoFormatForDocument()) { |
||
| 114 | $this->updateDocumentAddFormat(); |
||
| 115 | } |
||
| 116 | return $this->content; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Check for old format classes |
||
| 121 | * |
||
| 122 | * @access protected |
||
| 123 | * |
||
| 124 | * @return array containing old format classes |
||
| 125 | */ |
||
| 126 | protected function oldFormatClasses() |
||
| 127 | { |
||
| 128 | $oldRecords = []; |
||
| 129 | // Get all records with outdated configuration. |
||
| 130 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 131 | 'tx_dlf_formats.uid AS uid,tx_dlf_formats.type AS type', |
||
| 132 | 'tx_dlf_formats', |
||
| 133 | 'tx_dlf_formats.class IS NOT NULL AND tx_dlf_formats.class != "" AND tx_dlf_formats.class NOT LIKE "%\\\\\\\\%"' // We are looking for a single backslash... |
||
| 134 | . Helper::whereClause('tx_dlf_formats') |
||
| 135 | ); |
||
| 136 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 137 | $oldRecords[$resArray['uid']] = $resArray['type']; |
||
| 138 | } |
||
| 139 | return $oldRecords; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Check for old index related columns |
||
| 144 | * |
||
| 145 | * @access protected |
||
| 146 | * |
||
| 147 | * @return bool true if old index related columns exist |
||
| 148 | */ |
||
| 149 | protected function oldIndexRelatedTableNames() |
||
| 150 | { |
||
| 151 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 152 | 'column_name', |
||
| 153 | 'INFORMATION_SCHEMA.COLUMNS', |
||
| 154 | 'TABLE_NAME = "tx_dlf_metadata"' |
||
| 155 | ); |
||
| 156 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 157 | if ( |
||
| 158 | $resArray['column_name'] == 'tokenized' |
||
| 159 | || $resArray['column_name'] == 'stored' |
||
| 160 | || $resArray['column_name'] == 'indexed' |
||
| 161 | || $resArray['column_name'] == 'boost' |
||
| 162 | || $resArray['column_name'] == 'autocomplete' |
||
| 163 | ) { |
||
| 164 | return true; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Copy the data of the old index related columns to the new columns |
||
| 171 | * |
||
| 172 | * @access protected |
||
| 173 | * |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | protected function renameIndexRelatedColumns() |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Update all outdated format records |
||
| 204 | * |
||
| 205 | * @access protected |
||
| 206 | * |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | protected function updateFormatClasses() |
||
| 210 | { |
||
| 211 | $oldRecords = $this->oldFormatClasses(); |
||
| 212 | $newValues = [ |
||
| 213 | 'ALTO' => 'Kitodo\\\\Dlf\\\\Format\\\\Alto', // Those are effectively single backslashes |
||
| 214 | 'MODS' => 'Kitodo\\\\Dlf\\\\Format\\\\Mods', |
||
| 215 | 'TEIHDR' => 'Kitodo\\\\Dlf\\\\Format\\\\TeiHeader' |
||
| 216 | ]; |
||
| 217 | foreach ($oldRecords as $uid => $type) { |
||
| 218 | $sqlQuery = 'UPDATE tx_dlf_formats SET class="' . $newValues[$type] . '" WHERE uid=' . $uid; |
||
| 219 | $GLOBALS['TYPO3_DB']->sql_query($sqlQuery); |
||
| 220 | } |
||
| 221 | Helper::addMessage( |
||
| 222 | $GLOBALS['LANG']->getLL('update.FormatClassesOkay', true), |
||
| 223 | $GLOBALS['LANG']->getLL('update.FormatClasses', true), |
||
| 224 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK |
||
| 225 | ); |
||
| 226 | $this->content .= Helper::renderFlashMessages(); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Update all outdated metadata configuration records |
||
| 231 | * |
||
| 232 | * @access protected |
||
| 233 | * |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | protected function updateMetadataConfig() |
||
| 237 | { |
||
| 238 | $metadataUids = $this->getMetadataConfig(); |
||
| 239 | if (!empty($metadataUids)) { |
||
| 240 | $data = []; |
||
| 241 | // Get all old metadata configuration records. |
||
| 242 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 243 | 'tx_dlf_metadata.uid AS uid,tx_dlf_metadata.pid AS pid,tx_dlf_metadata.cruser_id AS cruser_id,tx_dlf_metadata.encoded AS encoded,tx_dlf_metadata.xpath AS xpath,tx_dlf_metadata.xpath_sorting AS xpath_sorting', |
||
| 244 | 'tx_dlf_metadata', |
||
| 245 | 'tx_dlf_metadata.uid IN (' . implode(',', $metadataUids) . ')' |
||
| 246 | . Helper::whereClause('tx_dlf_metadata') |
||
| 247 | ); |
||
| 248 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 249 | $newId = uniqid('NEW'); |
||
| 250 | // Copy record to new table. |
||
| 251 | $data['tx_dlf_metadataformat'][$newId] = [ |
||
| 252 | 'pid' => $resArray['pid'], |
||
| 253 | 'cruser_id' => $resArray['cruser_id'], |
||
| 254 | 'parent_id' => $resArray['uid'], |
||
| 255 | 'encoded' => $resArray['encoded'], |
||
| 256 | 'xpath' => $resArray['xpath'], |
||
| 257 | 'xpath_sorting' => $resArray['xpath_sorting'] |
||
| 258 | ]; |
||
| 259 | // Add reference to old table. |
||
| 260 | $data['tx_dlf_metadata'][$resArray['uid']]['format'] = $newId; |
||
| 261 | } |
||
| 262 | if (!empty($data)) { |
||
| 263 | // Process datamap. |
||
| 264 | $substUids = Helper::processDBasAdmin($data); |
||
| 265 | unset($data); |
||
| 266 | if (!empty($substUids)) { |
||
| 267 | Helper::addMessage( |
||
| 268 | $GLOBALS['LANG']->getLL('update.metadataConfigOkay', true), |
||
| 269 | $GLOBALS['LANG']->getLL('update.metadataConfig', true), |
||
| 270 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK |
||
| 271 | ); |
||
| 272 | } else { |
||
| 273 | Helper::addMessage( |
||
| 274 | $GLOBALS['LANG']->getLL('update.metadataConfigNotOkay', true), |
||
| 275 | $GLOBALS['LANG']->getLL('update.metadataConfig', true), |
||
| 276 | \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING |
||
| 277 | ); |
||
| 278 | } |
||
| 279 | $this->content .= Helper::renderFlashMessages(); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Check all configured Solr cores |
||
| 286 | * |
||
| 287 | * @access protected |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | protected function solariumSolrUpdateRequired() |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Create all configured Solr cores |
||
| 312 | * |
||
| 313 | * @access protected |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | protected function doSolariumSolrUpdate() |
||
| 376 | } |
||
| 377 | |||
| 378 | protected function hasNoFormatForDocument($checkStructureOnly = false) |
||
| 379 | { |
||
| 380 | // Check if column "document_format" exists. |
||
| 381 | $database = $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['dbname']; |
||
| 382 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 383 | 'COLUMN_NAME', |
||
| 384 | 'INFORMATION_SCHEMA.COLUMNS', |
||
| 385 | 'TABLE_NAME="tx_dlf_documents" AND TABLE_SCHEMA="' . $database . '" AND COLUMN_NAME="document_format"' |
||
| 386 | ); |
||
| 387 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 388 | if ($resArray['COLUMN_NAME'] == 'document_format') { |
||
| 389 | if ($checkStructureOnly) { |
||
| 390 | return false; |
||
| 391 | } |
||
| 392 | // Check if column has empty fields. |
||
| 393 | $result2 = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 394 | 'uid', |
||
| 395 | 'tx_dlf_documents', |
||
| 396 | 'document_format="" OR document_format IS NULL' |
||
| 397 | ); |
||
| 398 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result2) == 0) { |
||
| 399 | return false; |
||
| 400 | } |
||
| 401 | } |
||
| 402 | } |
||
| 403 | return true; |
||
| 404 | } |
||
| 405 | |||
| 406 | protected function updateDocumentAddFormat() |
||
| 444 | } |
||
| 445 | } |
||
| 446 |