| Total Complexity | 62 |
| Total Lines | 400 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SystemMaintenance 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 SystemMaintenance, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class SystemMaintenance |
||
| 27 | { |
||
| 28 | public $db; |
||
| 29 | public $prefix; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Constructor |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Display Tables |
||
| 44 | * |
||
| 45 | * @param bool $array |
||
| 46 | * |
||
| 47 | * @internal param $array |
||
| 48 | * @return array|string |
||
| 49 | */ |
||
| 50 | public function displayTables($array = true) |
||
| 51 | { |
||
| 52 | $tables = array(); |
||
| 53 | $sql = 'SHOW TABLES'; |
||
| 54 | $result = $this->db->queryF($sql); |
||
| 55 | if (!$this->db->isResultSet($result)) { |
||
| 56 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 57 | } |
||
| 58 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
|
|
|||
| 59 | $value = array_values($myrow); |
||
| 60 | $value = substr($value[0], strlen(XOOPS_DB_PREFIX) + 1); |
||
| 61 | $tables[$value] = $value; |
||
| 62 | } |
||
| 63 | if (true === (bool) $array) { |
||
| 64 | return $tables; |
||
| 65 | } else { |
||
| 66 | return implode(',', $tables); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Clear sessions |
||
| 72 | * |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | public function CleanSession() |
||
| 76 | { |
||
| 77 | $result = $this->db->queryF('TRUNCATE TABLE ' . $this->db->prefix('session')); |
||
| 78 | |||
| 79 | return true; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * CleanAvatar |
||
| 84 | * |
||
| 85 | * Clean up orphaned custom avatars left when a user is deleted. |
||
| 86 | * |
||
| 87 | * @author slider84 of Team FrXoops |
||
| 88 | * |
||
| 89 | * @return boolean |
||
| 90 | */ |
||
| 91 | public function CleanAvatar() |
||
| 92 | { |
||
| 93 | $sql = 'SELECT avatar_id, avatar_file FROM ' . $this->db->prefix('avatar') . " WHERE avatar_type='C' AND avatar_id IN (" . 'SELECT t1.avatar_id FROM ' . $this->db->prefix('avatar_user_link') . ' AS t1 ' . 'LEFT JOIN ' . $this->db->prefix('users') . ' AS t2 ON t2.uid=t1.user_id ' . 'WHERE t2.uid IS NULL)'; |
||
| 94 | $result = $this->db->queryF($sql); |
||
| 95 | if (!$this->db->isResultSet($result)) { |
||
| 96 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 97 | } |
||
| 98 | |||
| 99 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 100 | //delete file |
||
| 101 | @unlink(XOOPS_UPLOAD_PATH . '/' . $myrow['avatar_file']); |
||
| 102 | //clean avatar table |
||
| 103 | $result1 = $this->db->queryF('DELETE FROM ' . $this->db->prefix('avatar') . ' WHERE avatar_id=' . $myrow['avatar_id']); |
||
| 104 | } |
||
| 105 | //clean any deleted users from avatar_user_link table |
||
| 106 | $result2 = $this->db->queryF('DELETE FROM ' . $this->db->prefix('avatar_user_link') . ' WHERE user_id NOT IN (SELECT uid FROM ' . $this->db->prefix('users') . ')'); |
||
| 107 | |||
| 108 | return true; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Delete all files in a directory |
||
| 113 | * |
||
| 114 | * @param string $dir directory to clear |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | protected function clearDirectory($dir) |
||
| 119 | { |
||
| 120 | if (is_dir($dir)) { |
||
| 121 | if ($dirHandle = opendir($dir)) { |
||
| 122 | while (($file = readdir($dirHandle)) !== false) { |
||
| 123 | if (filetype($dir . $file) === 'file') { |
||
| 124 | unlink($dir . $file); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | closedir($dirHandle); |
||
| 128 | } |
||
| 129 | file_put_contents($dir . 'index.php', '<?php' . PHP_EOL . 'header("HTTP/1.0 404 Not Found");' . PHP_EOL); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Clean cache 'xoops_data/caches/smarty_cache' |
||
| 135 | * |
||
| 136 | * @param array $cacheList int[] of cache "ids" |
||
| 137 | * 1 = smarty cache |
||
| 138 | * 2 = smarty compile |
||
| 139 | * 3 = xoops cache |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function CleanCache($cacheList) |
||
| 143 | { |
||
| 144 | foreach ($cacheList as $cache) { |
||
| 145 | switch ($cache) { |
||
| 146 | case 1: |
||
| 147 | $this->clearDirectory(XOOPS_VAR_PATH . '/caches/smarty_cache/'); |
||
| 148 | break; |
||
| 149 | case 2: |
||
| 150 | $this->clearDirectory(XOOPS_VAR_PATH . '/caches/smarty_compile/'); |
||
| 151 | break; |
||
| 152 | case 3: |
||
| 153 | $this->clearDirectory(XOOPS_VAR_PATH . '/caches/xoops_cache/'); |
||
| 154 | break; |
||
| 155 | default: |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return true; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Maintenance database |
||
| 165 | * |
||
| 166 | * @param array tables 'list of tables' |
||
| 167 | * @param array maintenance 'optimize, check, repair, analyze' |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function CheckRepairAnalyzeOptimizeQueries($tables, $maintenance) |
||
| 171 | { |
||
| 172 | $ret = '<table class="outer"><th>' . _AM_SYSTEM_MAINTENANCE_TABLES1 . '</th><th>' . _AM_SYSTEM_MAINTENANCE_TABLES_OPTIMIZE . '</th><th>' . _AM_SYSTEM_MAINTENANCE_TABLES_CHECK . '</th><th>' . _AM_SYSTEM_MAINTENANCE_TABLES_REPAIR . '</th><th>' . _AM_SYSTEM_MAINTENANCE_TABLES_ANALYZE . '</th>'; |
||
| 173 | $tab = array(); |
||
| 174 | for ($i = 0; $i < 4; ++$i) { |
||
| 175 | $tab[$i] = $i + 1; |
||
| 176 | } |
||
| 177 | $tab1 = array(); |
||
| 178 | for ($i = 0; $i < 4; ++$i) { |
||
| 179 | if (in_array($tab[$i], $maintenance)) { |
||
| 180 | $tab1[$i] = $tab[$i]; |
||
| 181 | } else { |
||
| 182 | $tab1[$i] = '0'; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | unset($tab); |
||
| 186 | $class = 'odd'; |
||
| 187 | $tablesCount = count($tables); |
||
| 188 | for ($i = 0; $i < $tablesCount; ++$i) { |
||
| 189 | $ret .= '<tr class="' . $class . '"><td align="center">' . $this->prefix . $tables[$i] . '</td>'; |
||
| 190 | for ($j = 0; $j < 4; ++$j) { |
||
| 191 | if ($tab1[$j] == 1) { |
||
| 192 | // Optimize |
||
| 193 | $result = $this->db->queryF('OPTIMIZE TABLE ' . $this->prefix . $tables[$i]); |
||
| 194 | if ($result) { |
||
| 195 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>'; |
||
| 196 | } else { |
||
| 197 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>'; |
||
| 198 | } |
||
| 199 | } elseif ($tab1[$j] == 2) { |
||
| 200 | // Check tables |
||
| 201 | $result = $this->db->queryF('CHECK TABLE ' . $this->prefix . $tables[$i]); |
||
| 202 | if ($result) { |
||
| 203 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>'; |
||
| 204 | } else { |
||
| 205 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>'; |
||
| 206 | } |
||
| 207 | } elseif ($tab1[$j] == 3) { |
||
| 208 | // Repair |
||
| 209 | $result = $this->db->queryF('REPAIR TABLE ' . $this->prefix . $tables[$i]); |
||
| 210 | if ($result) { |
||
| 211 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>'; |
||
| 212 | } else { |
||
| 213 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>'; |
||
| 214 | } |
||
| 215 | } elseif ($tab1[$j] == 4) { |
||
| 216 | // Analyze |
||
| 217 | $result = $this->db->queryF('ANALYZE TABLE ' . $this->prefix . $tables[$i]); |
||
| 218 | if ($result) { |
||
| 219 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>'; |
||
| 220 | } else { |
||
| 221 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>'; |
||
| 222 | } |
||
| 223 | } else { |
||
| 224 | $ret .= '<td> </td>'; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | $ret .= '</tr>'; |
||
| 228 | $class = ($class === 'even') ? 'odd' : 'even'; |
||
| 229 | } |
||
| 230 | $ret .= '</table>'; |
||
| 231 | |||
| 232 | return $ret; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Dump by tables |
||
| 237 | * |
||
| 238 | * @param array tables 'list of tables' |
||
| 239 | * @param int drop |
||
| 240 | * @return array 'ret[0] = dump, ret[1] = display result |
||
| 241 | */ |
||
| 242 | public function dump_tables($tables, $drop) |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Dump by modules |
||
| 266 | * |
||
| 267 | * @param array modules 'list of modules' |
||
| 268 | * @param int drop |
||
| 269 | * @return array 'ret[0] = dump, ret[1] = display result |
||
| 270 | */ |
||
| 271 | public function dump_modules($modules, $drop) |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Dump table structure |
||
| 307 | * |
||
| 308 | * @param array |
||
| 309 | * @param string table |
||
| 310 | * @param int drop |
||
| 311 | * @param string class |
||
| 312 | * @return array 'ret[0] = dump, ret[1] = display result |
||
| 313 | */ |
||
| 314 | public function dump_table_structure($ret, $table, $drop, $class) |
||
| 315 | { |
||
| 316 | $verif = false; |
||
| 317 | $sql = 'SHOW create table `' . $table . '`;'; |
||
| 318 | $result = $this->db->queryF($sql); |
||
| 319 | if ($this->db->isResultSet($result)) { |
||
| 320 | if ($row = $this->db->fetchArray($result)) { |
||
| 321 | $ret[0] .= '# Table structure for table `' . $table . "` \n\n"; |
||
| 322 | if ($drop == 1) { |
||
| 323 | $ret[0] .= 'DROP TABLE IF EXISTS `' . $table . "`;\n\n"; |
||
| 324 | } |
||
| 325 | $verif = true; |
||
| 326 | $ret[0] .= $row['Create Table'] . ";\n\n"; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | $ret[1] .= '<tr class="' . $class . '"><td align="center">' . $table . '</td><td class="xo-actions txtcenter">'; |
||
| 331 | $ret[1] .= ($verif === true) ? '<img src="' . system_AdminIcons('success.png') . '" />' : '<img src="' . system_AdminIcons('cancel.png') . '" />'; |
||
| 332 | $ret[1] .= '</td>'; |
||
| 333 | if ($this->db->isResultSet($result)) { |
||
| 334 | $this->db->freeRecordSet($result); |
||
| 335 | } |
||
| 336 | |||
| 337 | return $ret; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Dump table data |
||
| 342 | * |
||
| 343 | * @param array |
||
| 344 | * @param string table |
||
| 345 | * @return array 'ret[0] = dump, ret[1] = display result |
||
| 346 | */ |
||
| 347 | public function dump_table_datas($ret, $table) |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Dump Write |
||
| 411 | * |
||
| 412 | * @param array |
||
| 413 | * @return array 'ret[0] = dump, ret[1] = display result |
||
| 414 | */ |
||
| 415 | public function dump_write($ret) |
||
| 426 | } |
||
| 427 | } |
||
| 428 |