Total Complexity | 62 |
Total Lines | 404 |
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 | throw new \RuntimeException( |
||
57 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR |
||
58 | ); |
||
59 | } |
||
60 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
|
|||
61 | $value = array_values($myrow); |
||
62 | $value = substr($value[0], strlen(XOOPS_DB_PREFIX) + 1); |
||
63 | $tables[$value] = $value; |
||
64 | } |
||
65 | if (true === (bool)$array) { |
||
66 | return $tables; |
||
67 | } else { |
||
68 | return implode(',', $tables); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Clear sessions |
||
74 | * |
||
75 | * @return bool |
||
76 | */ |
||
77 | public function CleanSession() |
||
78 | { |
||
79 | $result = $this->db->queryF('TRUNCATE TABLE ' . $this->db->prefix('session')); |
||
80 | |||
81 | return true; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * CleanAvatar |
||
86 | * |
||
87 | * Clean up orphaned custom avatars left when a user is deleted. |
||
88 | * |
||
89 | * @author slider84 of Team FrXoops |
||
90 | * |
||
91 | * @return boolean |
||
92 | */ |
||
93 | public function CleanAvatar() |
||
94 | { |
||
95 | $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)'; |
||
96 | $result = $this->db->queryF($sql); |
||
97 | if (!$this->db->isResultSet($result)) { |
||
98 | throw new \RuntimeException( |
||
99 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
104 | //delete file |
||
105 | @unlink(XOOPS_UPLOAD_PATH . '/' . $myrow['avatar_file']); |
||
106 | //clean avatar table |
||
107 | $result1 = $this->db->queryF('DELETE FROM ' . $this->db->prefix('avatar') . ' WHERE avatar_id=' . $myrow['avatar_id']); |
||
108 | } |
||
109 | //clean any deleted users from avatar_user_link table |
||
110 | $result2 = $this->db->queryF('DELETE FROM ' . $this->db->prefix('avatar_user_link') . ' WHERE user_id NOT IN (SELECT uid FROM ' . $this->db->prefix('users') . ')'); |
||
111 | |||
112 | return true; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Delete all files in a directory |
||
117 | * |
||
118 | * @param string $dir directory to clear |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | protected function clearDirectory($dir) |
||
123 | { |
||
124 | if (is_dir($dir)) { |
||
125 | if ($dirHandle = opendir($dir)) { |
||
126 | while (($file = readdir($dirHandle)) !== false) { |
||
127 | if (filetype($dir . $file) === 'file') { |
||
128 | unlink($dir . $file); |
||
129 | } |
||
130 | } |
||
131 | closedir($dirHandle); |
||
132 | } |
||
133 | file_put_contents($dir . 'index.php', '<?php' . PHP_EOL . 'header("HTTP/1.0 404 Not Found");' . PHP_EOL); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Clean cache 'xoops_data/caches/smarty_cache' |
||
139 | * |
||
140 | * @param array $cacheList int[] of cache "ids" |
||
141 | * 1 = smarty cache |
||
142 | * 2 = smarty compile |
||
143 | * 3 = xoops cache |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function CleanCache($cacheList) |
||
147 | { |
||
148 | foreach ($cacheList as $cache) { |
||
149 | switch ($cache) { |
||
150 | case 1: |
||
151 | $this->clearDirectory(XOOPS_VAR_PATH . '/caches/smarty_cache/'); |
||
152 | break; |
||
153 | case 2: |
||
154 | $this->clearDirectory(XOOPS_VAR_PATH . '/caches/smarty_compile/'); |
||
155 | break; |
||
156 | case 3: |
||
157 | $this->clearDirectory(XOOPS_VAR_PATH . '/caches/xoops_cache/'); |
||
158 | break; |
||
159 | default: |
||
160 | return false; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | return true; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Maintenance database |
||
169 | * |
||
170 | * @param array tables 'list of tables' |
||
171 | * @param array maintenance 'optimize, check, repair, analyze' |
||
172 | * @return array |
||
173 | */ |
||
174 | public function CheckRepairAnalyzeOptimizeQueries($tables, $maintenance) |
||
175 | { |
||
176 | $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>'; |
||
177 | $tab = array(); |
||
178 | for ($i = 0; $i < 4; ++$i) { |
||
179 | $tab[$i] = $i + 1; |
||
180 | } |
||
181 | $tab1 = array(); |
||
182 | for ($i = 0; $i < 4; ++$i) { |
||
183 | if (in_array($tab[$i], $maintenance)) { |
||
184 | $tab1[$i] = $tab[$i]; |
||
185 | } else { |
||
186 | $tab1[$i] = '0'; |
||
187 | } |
||
188 | } |
||
189 | unset($tab); |
||
190 | $class = 'odd'; |
||
191 | $tablesCount = count($tables); |
||
192 | for ($i = 0; $i < $tablesCount; ++$i) { |
||
193 | $ret .= '<tr class="' . $class . '"><td align="center">' . $this->prefix . $tables[$i] . '</td>'; |
||
194 | for ($j = 0; $j < 4; ++$j) { |
||
195 | if ($tab1[$j] == 1) { |
||
196 | // Optimize |
||
197 | $result = $this->db->queryF('OPTIMIZE TABLE ' . $this->prefix . $tables[$i]); |
||
198 | if ($result) { |
||
199 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>'; |
||
200 | } else { |
||
201 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>'; |
||
202 | } |
||
203 | } elseif ($tab1[$j] == 2) { |
||
204 | // Check tables |
||
205 | $result = $this->db->queryF('CHECK TABLE ' . $this->prefix . $tables[$i]); |
||
206 | if ($result) { |
||
207 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>'; |
||
208 | } else { |
||
209 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>'; |
||
210 | } |
||
211 | } elseif ($tab1[$j] == 3) { |
||
212 | // Repair |
||
213 | $result = $this->db->queryF('REPAIR TABLE ' . $this->prefix . $tables[$i]); |
||
214 | if ($result) { |
||
215 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>'; |
||
216 | } else { |
||
217 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>'; |
||
218 | } |
||
219 | } elseif ($tab1[$j] == 4) { |
||
220 | // Analyze |
||
221 | $result = $this->db->queryF('ANALYZE TABLE ' . $this->prefix . $tables[$i]); |
||
222 | if ($result) { |
||
223 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>'; |
||
224 | } else { |
||
225 | $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>'; |
||
226 | } |
||
227 | } else { |
||
228 | $ret .= '<td> </td>'; |
||
229 | } |
||
230 | } |
||
231 | $ret .= '</tr>'; |
||
232 | $class = ($class === 'even') ? 'odd' : 'even'; |
||
233 | } |
||
234 | $ret .= '</table>'; |
||
235 | |||
236 | return $ret; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Dump by tables |
||
241 | * |
||
242 | * @param array tables 'list of tables' |
||
243 | * @param int drop |
||
244 | * @return array 'ret[0] = dump, ret[1] = display result |
||
245 | */ |
||
246 | public function dump_tables($tables, $drop) |
||
247 | { |
||
248 | $ret = array(); |
||
249 | $ret[0] = "# \n"; |
||
250 | $ret[0] .= "# Dump SQL, Generate by Xoops \n"; |
||
251 | $ret[0] .= '# Date : ' . date('d-m-Y - H:i') . " \n"; |
||
252 | $ret[1] = '<table class="outer"><tr><th width="30%">' . _AM_SYSTEM_MAINTENANCE_DUMP_TABLES . '</th><th width="35%">' . _AM_SYSTEM_MAINTENANCE_DUMP_STRUCTURES . '</th><th width="35%">' . _AM_SYSTEM_MAINTENANCE_DUMP_NB_RECORDS . '</th></tr>'; |
||
253 | $class = 'odd'; |
||
254 | $tablesCount = count($tables); |
||
255 | for ($i = 0; $i < $tablesCount; ++$i) { |
||
256 | //structure |
||
257 | $ret = $this->dump_table_structure($ret, $this->prefix . $tables[$i], $drop, $class); |
||
258 | //data |
||
259 | $ret = $this->dump_table_datas($ret, $this->prefix . $tables[$i]); |
||
260 | $class = ($class === 'even') ? 'odd' : 'even'; |
||
261 | } |
||
262 | $ret = $this->dump_write($ret); |
||
263 | $ret[1] .= '</table>'; |
||
264 | |||
265 | return $ret; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Dump by modules |
||
270 | * |
||
271 | * @param array modules 'list of modules' |
||
272 | * @param int drop |
||
273 | * @return array 'ret[0] = dump, ret[1] = display result |
||
274 | */ |
||
275 | public function dump_modules($modules, $drop) |
||
276 | { |
||
277 | $ret = array(); |
||
278 | $ret[0] = "# \n"; |
||
279 | $ret[0] .= "# Dump SQL, Generate by Xoops \n"; |
||
280 | $ret[0] .= '# Date : ' . date('d-m-Y - H:i') . " \n"; |
||
281 | $ret[0] .= "# \n\n"; |
||
282 | $ret[1] = '<table class="outer"><tr><th width="30%">' . _AM_SYSTEM_MAINTENANCE_DUMP_TABLES . '</th><th width="35%">' . _AM_SYSTEM_MAINTENANCE_DUMP_STRUCTURES . '</th><th width="35%">' . _AM_SYSTEM_MAINTENANCE_DUMP_NB_RECORDS . '</th></tr>'; |
||
283 | $class = 'odd'; |
||
284 | $modulesCount = count($modules); |
||
285 | for ($i = 0; $i < $modulesCount; ++$i) { |
||
286 | /* @var XoopsModuleHandler $module_handler */ |
||
287 | $module_handler = xoops_getHandler('module'); |
||
288 | $module = $module_handler->getByDirname($modules[$i]); |
||
289 | $ret[1] .= '<tr><th colspan="3" align="left">' . ucfirst($modules[$i]) . '</th></tr>'; |
||
290 | $modtables = $module->getInfo('tables'); |
||
291 | if ($modtables !== false && is_array($modtables)) { |
||
292 | foreach ($modtables as $table) { |
||
293 | //structure |
||
294 | $ret = $this->dump_table_structure($ret, $this->prefix . $table, $drop, $class); |
||
295 | //data |
||
296 | $ret = $this->dump_table_datas($ret, $this->prefix . $table); |
||
297 | $class = ($class === 'even') ? 'odd' : 'even'; |
||
298 | } |
||
299 | } else { |
||
300 | $ret[1] .= '<tr><td colspan="3" align="center">' . _AM_SYSTEM_MAINTENANCE_DUMP_NO_TABLES . '</td></tr>'; |
||
301 | } |
||
302 | } |
||
303 | $ret[1] .= '</table>'; |
||
304 | $ret = $this->dump_write($ret); |
||
305 | |||
306 | return $ret; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Dump table structure |
||
311 | * |
||
312 | * @param array |
||
313 | * @param string table |
||
314 | * @param int drop |
||
315 | * @param string class |
||
316 | * @return array 'ret[0] = dump, ret[1] = display result |
||
317 | */ |
||
318 | public function dump_table_structure($ret, $table, $drop, $class) |
||
319 | { |
||
320 | $verif = false; |
||
321 | $sql = 'SHOW create table `' . $table . '`;'; |
||
322 | $result = $this->db->queryF($sql); |
||
323 | if ($this->db->isResultSet($result)) { |
||
324 | if ($row = $this->db->fetchArray($result)) { |
||
325 | $ret[0] .= '# Table structure for table `' . $table . "` \n\n"; |
||
326 | if ($drop == 1) { |
||
327 | $ret[0] .= 'DROP TABLE IF EXISTS `' . $table . "`;\n\n"; |
||
328 | } |
||
329 | $verif = true; |
||
330 | $ret[0] .= $row['Create Table'] . ";\n\n"; |
||
331 | } |
||
332 | } |
||
333 | |||
334 | $ret[1] .= '<tr class="' . $class . '"><td align="center">' . $table . '</td><td class="xo-actions txtcenter">'; |
||
335 | $ret[1] .= ($verif === true) ? '<img src="' . system_AdminIcons('success.png') . '" />' : '<img src="' . system_AdminIcons('cancel.png') . '" />'; |
||
336 | $ret[1] .= '</td>'; |
||
337 | if ($this->db->isResultSet($result)) { |
||
338 | $this->db->freeRecordSet($result); |
||
339 | } |
||
340 | |||
341 | return $ret; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Dump table data |
||
346 | * |
||
347 | * @param array |
||
348 | * @param string table |
||
349 | * @return array 'ret[0] = dump, ret[1] = display result |
||
350 | */ |
||
351 | public function dump_table_datas($ret, $table) |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Dump Write |
||
415 | * |
||
416 | * @param array |
||
417 | * @return array 'ret[0] = dump, ret[1] = display result |
||
418 | */ |
||
419 | public function dump_write($ret) |
||
430 | } |
||
431 | } |
||
432 |