Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Upgrade_230 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Upgrade_230, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Upgrade_230 extends XoopsUpgrade |
||
| 31 | { |
||
| 32 | /* |
||
| 33 | * __construct() |
||
| 34 | */ |
||
| 35 | public function __construct() |
||
| 36 | { |
||
| 37 | parent::__construct(basename(__DIR__)); |
||
| 38 | $this->usedFiles = array('mainfile.php'); |
||
| 39 | $this->tasks = array('config', 'cache', 'path', 'db', 'bmlink'); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Check if cpanel config already exists |
||
| 44 | * |
||
| 45 | */ |
||
| 46 | public function check_config() |
||
| 47 | { |
||
| 48 | $sql = 'SELECT COUNT(*) FROM `' . $GLOBALS['xoopsDB']->prefix('config') . "` WHERE `conf_name` IN ('welcome_type', 'cpanel')"; |
||
| 49 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 50 | return false; |
||
| 51 | } |
||
| 52 | list($count) = $GLOBALS['xoopsDB']->fetchRow($result); |
||
| 53 | |||
| 54 | return ($count == 2); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Check if cache_model table already exists |
||
| 59 | * |
||
| 60 | */ |
||
| 61 | public function check_cache() |
||
| 62 | { |
||
| 63 | $sql = "SHOW TABLES LIKE '" . $GLOBALS['xoopsDB']->prefix('cache_model') . "'"; |
||
| 64 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 65 | if (!$result) { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | return $GLOBALS['xoopsDB']->getRowsNum($result) > 0; |
||
| 70 | |||
| 71 | /* |
||
| 72 | $sql = "SELECT COUNT(*) FROM `" . $GLOBALS['xoopsDB']->prefix('cache_model') . "`"; |
||
| 73 | if ( !$result = $GLOBALS['xoopsDB']->queryF( $sql ) ) { |
||
| 74 | return false; |
||
| 75 | } |
||
| 76 | |||
| 77 | return true; |
||
| 78 | */ |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Check if primary key for `block_module_link` is already set |
||
| 83 | * |
||
| 84 | */ |
||
| 85 | public function check_bmlink() |
||
| 86 | { |
||
| 87 | // MySQL 5.0+ |
||
| 88 | //$sql = "SHOW KEYS FROM `" . $GLOBALS['xoopsDB']->prefix('block_module_link') . "` WHERE `KEY_NAME` LIKE 'PRIMARY'"; |
||
| 89 | $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . '`'; |
||
| 90 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 91 | return false; |
||
| 92 | } |
||
| 93 | while ($row = $GLOBALS['xoopsDB']->fetchArray($result)) { |
||
| 94 | if ($row['Key_name'] === 'PRIMARY') { |
||
| 95 | return true; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public function apply_bmlink() |
||
| 106 | { |
||
| 107 | $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . '`'; |
||
| 108 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | $keys_drop = array(); |
||
| 112 | $primary_add = true; |
||
| 113 | while ($row = $GLOBALS['xoopsDB']->fetchArray($result)) { |
||
| 114 | if ($row['Key_name'] === 'PRIMARY') { |
||
| 115 | $primary_add = false; |
||
| 116 | } |
||
| 117 | if (in_array($row['Key_name'], array('block_id', 'module_id'))) { |
||
| 118 | $keys_drop[] = $row['Key_name']; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | foreach ($keys_drop as $drop) { |
||
| 122 | $sql = 'ALTER TABLE `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . "` DROP KEY `{$drop}`"; |
||
| 123 | $GLOBALS['xoopsDB']->queryF($sql); |
||
| 124 | } |
||
| 125 | if ($primary_add) { |
||
| 126 | $sql = 'ALTER IGNORE TABLE `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . '` ADD PRIMARY KEY (`block_id`, `module_id`)'; |
||
| 127 | |||
| 128 | return $GLOBALS['xoopsDB']->queryF($sql); |
||
| 129 | } |
||
| 130 | |||
| 131 | return true; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function apply_config() |
||
| 138 | { |
||
| 139 | $result = true; |
||
| 140 | if (!isset($GLOBALS['xoopsConfig']['cpanel'])) { |
||
| 141 | $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('config') . ' (conf_id, conf_modid, conf_catid, conf_name, conf_title, conf_value, conf_desc, conf_formtype, conf_valuetype, conf_order) ' . ' VALUES ' . " (NULL, 0, 1, 'cpanel', '_MD_AM_CPANEL', 'default', '_MD_AM_CPANELDSC', 'cpanel', 'other', 11)"; |
||
| 142 | |||
| 143 | $result *= $GLOBALS['xoopsDB']->queryF($sql); |
||
| 144 | } |
||
| 145 | |||
| 146 | $welcometype_installed = false; |
||
| 147 | $sql = 'SELECT COUNT(*) FROM `' . $GLOBALS['xoopsDB']->prefix('config') . "` WHERE `conf_name` = 'welcome_type'"; |
||
| 148 | if ($result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 149 | list($count) = $GLOBALS['xoopsDB']->fetchRow($result); |
||
| 150 | if ($count == 1) { |
||
| 151 | $welcometype_installed = true; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | if (!$welcometype_installed) { |
||
| 155 | $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('config') . ' (conf_id, conf_modid, conf_catid, conf_name, conf_title, conf_value, conf_desc, conf_formtype, conf_valuetype, conf_order) ' . ' VALUES ' . " (NULL, 0, 2, 'welcome_type', '_MD_AM_WELCOMETYPE', '1', '_MD_AM_WELCOMETYPE_DESC', 'select', 'int', 3)"; |
||
| 156 | |||
| 157 | if (!$GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | $config_id = $GLOBALS['xoopsDB']->getInsertId(); |
||
| 161 | |||
| 162 | $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('configoption') . ' (confop_id, confop_name, confop_value, conf_id)' . ' VALUES' . " (NULL, '_NO', '0', {$config_id})," . " (NULL, '_MD_AM_WELCOMETYPE_EMAIL', '1', {$config_id})," . " (NULL, '_MD_AM_WELCOMETYPE_PM', '2', {$config_id})," . " (NULL, '_MD_AM_WELCOMETYPE_BOTH', '3', {$config_id})"; |
||
| 163 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | return $result; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function apply_cache() |
||
| 172 | { |
||
| 173 | $allowWebChanges = $GLOBALS['xoopsDB']->allowWebChanges; |
||
| 174 | $GLOBALS['xoopsDB']->allowWebChanges = true; |
||
| 175 | $result = $GLOBALS['xoopsDB']->queryFromFile(__DIR__ . '/mysql.structure.sql'); |
||
| 176 | $GLOBALS['xoopsDB']->allowWebChanges = $allowWebChanges; |
||
| 177 | |||
| 178 | return $result; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function check_path() |
||
| 185 | { |
||
| 186 | if (!(defined('XOOPS_PATH') && defined('XOOPS_VAR_PATH') && defined('XOOPS_TRUST_PATH'))) { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | $ctrl = new PathStuffController(); |
||
| 190 | if (!$ctrl->checkPath()) { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | if (!$ctrl->checkPermissions()) { |
||
| 194 | return false; |
||
| 195 | } |
||
| 196 | |||
| 197 | return true; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function apply_path() |
||
| 204 | { |
||
| 205 | return $this->update_configs('path'); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public function check_db() |
||
| 212 | { |
||
| 213 | // mainfile was included to get here, just check for definition |
||
| 214 | if (defined('XOOPS_DB_CHARSET')) { |
||
| 215 | return true; |
||
| 216 | } |
||
| 217 | /* |
||
| 218 | $lines = file(XOOPS_ROOT_PATH . '/mainfile.php'); |
||
| 219 | foreach ($lines as $line) { |
||
| 220 | if (preg_match("/(define\(\s*)([\"'])(XOOPS_DB_CHARSET)\\2,\s*([\"'])([^\"']*?)\\4\s*\);/", $line)) { |
||
| 221 | return true; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | */ |
||
| 225 | return false; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function apply_db() |
||
| 232 | { |
||
| 233 | return $this->update_configs('db'); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param $task |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function update_configs($task) |
||
| 242 | { |
||
| 243 | if (!$vars = $this->set_configs($task)) { |
||
| 244 | return false; |
||
| 245 | } |
||
| 246 | if ($task === 'db' && !empty($vars['XOOPS_DB_COLLATION'])) { |
||
| 247 | if ($pos = strpos($vars['XOOPS_DB_COLLATION'], '_')) { |
||
| 248 | $vars['XOOPS_DB_CHARSET'] = substr($vars['XOOPS_DB_COLLATION'], 0, $pos); |
||
| 249 | $this->convert_db($vars['XOOPS_DB_CHARSET'], $vars['XOOPS_DB_COLLATION']); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | return $this->write_mainfile($vars); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param $charset |
||
| 258 | * @param $collation |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function convert_db($charset, $collation) |
||
| 263 | { |
||
| 264 | $sql = 'ALTER DATABASE `' . XOOPS_DB_NAME . '` DEFAULT CHARACTER SET ' . $GLOBALS['xoopsDB']->quote($charset) . ' COLLATE ' . $GLOBALS['xoopsDB']->quote($collation); |
||
| 265 | if (!$GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 266 | return false; |
||
| 267 | } |
||
| 268 | if (!$result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '" . XOOPS_DB_PREFIX . "\_%'")) { |
||
| 269 | return false; |
||
| 270 | } |
||
| 271 | $tables = array(); |
||
| 272 | while (list($table) = $GLOBALS['xoopsDB']->fetchRow($result)) { |
||
| 273 | $tables[] = $table; |
||
| 274 | //$GLOBALS["xoopsDB"]->queryF( "ALTER TABLE `{$table}` DEFAULT CHARACTER SET " . $GLOBALS["xoopsDB"]->quote($charset) . " COLLATE " . $GLOBALS["xoopsDB"]->quote($collation) ); |
||
| 275 | //$GLOBALS["xoopsDB"]->queryF( "ALTER TABLE `{$table}` CONVERT TO CHARACTER SET " . $GLOBALS["xoopsDB"]->quote($charset) . " COLLATE " . $GLOBALS["xoopsDB"]->quote($collation) ); |
||
| 276 | } |
||
| 277 | $this->convert_table($tables, $charset, $collation); |
||
| 278 | return null; |
||
| 279 | } |
||
| 280 | |||
| 281 | // Some code not ready to use |
||
| 282 | /** |
||
| 283 | * @param $tables |
||
| 284 | * @param $charset |
||
| 285 | * @param $collation |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function convert_table($tables, $charset, $collation) |
||
| 290 | { |
||
| 291 | // Initialize vars. |
||
| 292 | $string_querys = array(); |
||
| 293 | $binary_querys = array(); |
||
| 294 | $gen_index_querys = array(); |
||
| 295 | $drop_index_querys = array(); |
||
| 296 | $tables_querys = array(); |
||
| 297 | $optimize_querys = array(); |
||
| 298 | $final_querys = array(); |
||
| 299 | |||
| 300 | // Begin Converter Core |
||
| 301 | if (!empty($tables)) { |
||
| 302 | foreach ((array)$tables as $table) { |
||
| 303 | // Analyze tables for string types columns and generate his binary and string correctness sql sentences. |
||
| 304 | $resource = $GLOBALS['xoopsDB']->queryF("DESCRIBE $table"); |
||
| 305 | while ($result = $GLOBALS['xoopsDB']->fetchArray($resource)) { |
||
| 306 | if (preg_match('/(char)|(text)|(enum)|(set)/', $result['Type'])) { |
||
| 307 | // String Type SQL Sentence. |
||
| 308 | $string_querys[] = "ALTER TABLE `$table` MODIFY `" . $result['Field'] . '` ' . $result['Type'] . " CHARACTER SET $charset COLLATE $collation " . (((!empty($result['Default'])) || ($result['Default'] === '0') || ($result['Default'] === 0)) ? "DEFAULT '" . $result['Default'] . "' " : '') . ('YES' === $result['Null'] ? '' : 'NOT ') . 'NULL'; |
||
| 309 | |||
| 310 | // Binary String Type SQL Sentence. |
||
| 311 | if (preg_match('/(enum)|(set)/', $result['Type'])) { |
||
| 312 | $binary_querys[] = "ALTER TABLE `$table` MODIFY `" . $result['Field'] . '` ' . $result['Type'] . ' CHARACTER SET binary ' . (((!empty($result['Default'])) || ($result['Default'] === '0') || ($result['Default'] === 0)) ? "DEFAULT '" . $result['Default'] . "' " : '') . ('YES' === $result['Null'] ? '' : 'NOT ') . 'NULL'; |
||
| 313 | } else { |
||
| 314 | $result['Type'] = str_replace('char', 'binary', $result['Type']); |
||
| 315 | $result['Type'] = str_replace('text', 'blob', $result['Type']); |
||
| 316 | $binary_querys[] = "ALTER TABLE `$table` MODIFY `" . $result['Field'] . '` ' . $result['Type'] . ' ' . (((!empty($result['Default'])) || ($result['Default'] === '0') || ($result['Default'] === 0)) ? "DEFAULT '" . $result['Default'] . "' " : '') . ('YES' === $result['Null'] ? '' : 'NOT ') . 'NULL'; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | // Analyze table indexs for any FULLTEXT-Type of index in the table. |
||
| 322 | $fulltext_indexes = array(); |
||
| 323 | $resource = $GLOBALS['xoopsDB']->queryF("SHOW INDEX FROM `$table`"); |
||
| 324 | while ($result = $GLOBALS['xoopsDB']->fetchArray($resource)) { |
||
| 325 | if (preg_match('/FULLTEXT/', $result['Index_type'])) { |
||
| 326 | $fulltext_indexes[$result['Key_name']][$result['Column_name']] = 1; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | // Generate the SQL Sentence for drop and add every FULLTEXT index we found previously. |
||
| 331 | if (!empty($fulltext_indexes)) { |
||
| 332 | foreach ((array)$fulltext_indexes as $key_name => $column) { |
||
| 333 | $drop_index_querys[] = "ALTER TABLE `$table` DROP INDEX `$key_name`"; |
||
| 334 | $tmp_gen_index_query = "ALTER TABLE `$table` ADD FULLTEXT `$key_name`("; |
||
| 335 | $fields_names = array_keys($column); |
||
| 336 | for ($i = 1; $i <= count($column); ++$i) { |
||
| 337 | $tmp_gen_index_query .= $fields_names[$i - 1] . (($i == count($column)) ? '' : ', '); |
||
| 338 | } |
||
| 339 | $gen_index_querys[] = $tmp_gen_index_query . ')'; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | // Generate the SQL Sentence for change default table character set. |
||
| 344 | $tables_querys[] = "ALTER TABLE `$table` DEFAULT CHARACTER SET $charset COLLATE $collation"; |
||
| 345 | |||
| 346 | // Generate the SQL Sentence for Optimize Table. |
||
| 347 | $optimize_querys[] = "OPTIMIZE TABLE `$table`"; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | // End Converter Core |
||
| 351 | |||
| 352 | // Merge all SQL Sentences that we temporary store in arrays. |
||
| 353 | $final_querys = array_merge((array)$drop_index_querys, (array)$binary_querys, (array)$tables_querys, (array)$string_querys, (array)$gen_index_querys, (array)$optimize_querys); |
||
| 354 | |||
| 355 | foreach ($final_querys as $sql) { |
||
| 356 | $GLOBALS['xoopsDB']->queryF($sql); |
||
| 357 | } |
||
| 358 | |||
| 359 | // Time to return. |
||
| 360 | return $final_querys; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param $vars |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public function write_mainfile($vars) |
||
| 369 | { |
||
| 370 | if (empty($vars)) { |
||
| 371 | return false; |
||
| 372 | } |
||
| 373 | |||
| 374 | $file = __DIR__ . '/mainfile.dist.php'; |
||
| 375 | |||
| 376 | $lines = file($file); |
||
| 377 | foreach (array_keys($lines) as $ln) { |
||
| 378 | if (preg_match("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([0-9]+)\s*\)/", $lines[$ln], $matches)) { |
||
| 379 | $val = isset($vars[$matches[3]]) ? (string)constant($matches[3]) : (defined($matches[3]) ? (string)constant($matches[3]) : '0'); |
||
| 380 | $lines[$ln] = preg_replace("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([0-9]+)\s*\)/", "define('" . $matches[3] . "', " . $val . ' )', $lines[$ln]); |
||
| 381 | } elseif (preg_match("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([\"'])([^\"']*?)\\4\s*\)/", $lines[$ln], $matches)) { |
||
| 382 | $val = isset($vars[$matches[3]]) ? (string)$vars[$matches[3]] : (defined($matches[3]) ? (string)constant($matches[3]) : ''); |
||
| 383 | $lines[$ln] = preg_replace("/(define\()([\"'])(XOOPS_[^\"']+)\\2,\s*([\"'])(.*?)\\4\s*\)/", "define('" . $matches[3] . "', '" . $val . "' )", $lines[$ln]); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | $fp = fopen(XOOPS_ROOT_PATH . '/mainfile.php', 'wt'); |
||
| 388 | if (!$fp) { |
||
| 389 | echo ERR_COULD_NOT_WRITE_MAINFILE; |
||
| 390 | echo "<pre style='border: 1px solid black; width: 80%; overflow: auto;'><div style='color: #ff0000; font-weight: bold;'><div>" . implode('</div><div>', array_map('htmlspecialchars', $lines)) . '</div></div></pre>'; |
||
| 391 | |||
| 392 | return false; |
||
| 393 | } else { |
||
| 394 | $newline = defined(PHP_EOL) ? PHP_EOL : (strpos(php_uname(), 'Windows') ? "\r\n" : "\n"); |
||
| 395 | $content = str_replace(array("\r\n", "\n"), $newline, implode('', $lines)); |
||
| 396 | |||
| 397 | fwrite($fp, $content); |
||
| 398 | fclose($fp); |
||
| 399 | |||
| 400 | return true; |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param $task |
||
| 406 | * |
||
| 407 | * @return array|bool |
||
| 408 | */ |
||
| 409 | public function set_configs($task) |
||
| 410 | { |
||
| 411 | $ret = array(); |
||
| 412 | $configs = include __DIR__ . "/settings_{$task}.php"; |
||
| 413 | if (!$configs || !is_array($configs)) { |
||
| 414 | return $ret; |
||
| 415 | } |
||
| 416 | if (empty($_POST['task']) || $_POST['task'] != $task) { |
||
| 417 | return false; |
||
| 418 | } |
||
| 419 | |||
| 420 | foreach ($configs as $key => $val) { |
||
| 421 | $ret['XOOPS_' . $key] = $val; |
||
| 422 | } |
||
| 423 | |||
| 424 | return $ret; |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 430 |