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 SystemModule 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 SystemModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class SystemModule |
||
| 34 | { |
||
| 35 | public $error = array(); |
||
| 36 | public $trace = array(); |
||
| 37 | protected $modulesList = array(); |
||
| 38 | protected $modulesDirnames = array(); |
||
| 39 | protected $config_delng = array(); |
||
| 40 | protected $template_delng = array(); |
||
| 41 | protected $config_old = array(); |
||
| 42 | protected $reservedTables = array( |
||
| 43 | 'system_blockmodule', |
||
| 44 | 'system_config', |
||
| 45 | 'system_configoption', |
||
| 46 | 'system_group', |
||
| 47 | 'system_usergroup', |
||
| 48 | 'system_permission', |
||
| 49 | 'system_module', |
||
| 50 | 'system_block', |
||
| 51 | 'system_online', |
||
| 52 | 'system_privatemessage', |
||
| 53 | 'system_session', |
||
| 54 | 'system_tplfile', |
||
| 55 | 'system_tplset', |
||
| 56 | 'system_tplsource', |
||
| 57 | 'system_user', |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor |
||
| 62 | */ |
||
| 63 | public function __construct() |
||
| 64 | { |
||
| 65 | // Get main instance |
||
| 66 | $xoops = Xoops::getInstance(); |
||
| 67 | $module_handler = $xoops->getHandlerModule(); |
||
| 68 | |||
| 69 | $this->modulesList = XoopsLists::getModulesList(); |
||
| 70 | |||
| 71 | $modules = $module_handler->getObjectsArray(); |
||
| 72 | /* @var $module XoopsModule */ |
||
| 73 | foreach ($modules as $module) { |
||
| 74 | $this->modulesDirnames[] = $module->getInfo('dirname'); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * getModuleList |
||
| 80 | * |
||
| 81 | * @return array of modules |
||
| 82 | */ |
||
| 83 | public function getModuleList() |
||
| 84 | { |
||
| 85 | // Get main instance |
||
| 86 | $xoops = Xoops::getInstance(); |
||
| 87 | $module_handler = $xoops->getHandlerModule(); |
||
| 88 | $moduleperm_handler = $xoops->getHandlerGroupPermission(); |
||
| 89 | |||
| 90 | $criteria = new CriteriaCompo(); |
||
| 91 | $criteria->setSort('weight'); |
||
| 92 | // Get all installed modules |
||
| 93 | $modules = $module_handler->getObjects($criteria, true); |
||
| 94 | $list = array(); |
||
| 95 | /* @var $module XoopsModule */ |
||
| 96 | foreach ($modules as $module) { |
||
| 97 | if (!$module->getInfo('extension')) { |
||
| 98 | if ($module->getInfo('dirname') === 'system') { |
||
| 99 | $module->setInfo('can_delete', false); |
||
| 100 | $module->setInfo('can_disable', false); |
||
| 101 | } else { |
||
| 102 | $module->setInfo('can_delete', true); |
||
| 103 | $module->setInfo('can_disable', true); |
||
| 104 | } |
||
| 105 | View Code Duplication | if (round($module->getInfo('version'), 2) != $module->getVar('version')) { |
|
| 106 | $module->setInfo('warning_update', true); |
||
| 107 | } |
||
| 108 | View Code Duplication | if (XoopsLoad::fileExists( |
|
| 109 | \XoopsBaseConfig::get('root-path') . '/modules/' . $module->getVar('dirname') . '/icons/logo_small.png' |
||
| 110 | )) { |
||
| 111 | $module->setInfo( |
||
| 112 | 'logo_small', |
||
| 113 | \XoopsBaseConfig::get('url') . '/modules/' . $module->getVar('dirname') . '/icons/logo_small.png' |
||
| 114 | ); |
||
| 115 | } else { |
||
| 116 | $module->setInfo('logo_small', \XoopsBaseConfig::get('url') . '/media/xoops/images/icons/16/default.png'); |
||
| 117 | } |
||
| 118 | $module->setInfo('version', round($module->getVar('version') / 100, 2)); |
||
| 119 | $module->setInfo('update', XoopsLocale::formatTimestamp($module->getVar('last_update'), 's')); |
||
| 120 | $module->setInfo( |
||
| 121 | 'link_admin', |
||
| 122 | \XoopsBaseConfig::get('url') . '/modules/' . $module->getVar('dirname') . '/' . $module->getInfo('adminindex') |
||
| 123 | ); |
||
| 124 | |||
| 125 | if ($module->getVar('isactive')) { |
||
| 126 | $module->setInfo('options', $module->getAdminMenu()); |
||
| 127 | } |
||
| 128 | |||
| 129 | $groups = array(); |
||
| 130 | if (is_object($xoops->user)) { |
||
| 131 | $groups = $xoops->user->getGroups(); |
||
| 132 | } |
||
| 133 | |||
| 134 | $sadmin = $moduleperm_handler->checkRight( |
||
| 135 | 'module_admin', |
||
| 136 | $module->getVar('mid'), |
||
| 137 | $groups |
||
| 138 | ); |
||
| 139 | View Code Duplication | if ($sadmin && ($module->getVar('hasnotification') |
|
| 140 | || is_array($module->getInfo('config')) || is_array($module->getInfo('comments'))) |
||
| 141 | ) { |
||
| 142 | $module->setInfo( |
||
| 143 | 'link_pref', |
||
| 144 | \XoopsBaseConfig::get('url') . '/modules/system/admin.php?fct=preferences&op=showmod&mod=' |
||
| 145 | . $module->getVar('mid') |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | $list[] = $module; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | return $list; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * getInstalledModules |
||
| 157 | * |
||
| 158 | * @return array of installed modules |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function getInstalledModules() |
|
| 161 | { |
||
| 162 | // Get main instance |
||
| 163 | $xoops = Xoops::getInstance(); |
||
| 164 | $module_handler = $xoops->getHandlerModule(); |
||
| 165 | |||
| 166 | $ret = array(); |
||
| 167 | $i = 0; |
||
| 168 | foreach ($this->modulesList as $file) { |
||
| 169 | if (XoopsLoad::fileExists(\XoopsBaseConfig::get('root-path') . '/modules/' . $file . '/xoops_version.php')) { |
||
| 170 | clearstatcache(); |
||
| 171 | $file = trim($file); |
||
| 172 | if (!in_array($file, $this->modulesDirnames)) { |
||
| 173 | /* @var $module XoopsModule */ |
||
| 174 | $module = $module_handler->create(); |
||
| 175 | $module->loadInfo($file); |
||
| 176 | if (!$module->getInfo('extension')) { |
||
| 177 | $module->setInfo('mid', $i); |
||
| 178 | $module->setInfo('version', round($module->getInfo('version'), 2)); |
||
| 179 | $ret[] = $module; |
||
| 180 | unset($module); |
||
| 181 | ++$i; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | return $ret; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * install a module |
||
| 191 | * |
||
| 192 | * @param string $mod module dirname |
||
| 193 | * @param boolean $force force query |
||
| 194 | * |
||
| 195 | * @return bool|XoopsModule|XoopsObject |
||
| 196 | */ |
||
| 197 | public function install($mod = '', $force = false) |
||
| 198 | { |
||
| 199 | $xoops = Xoops::getInstance(); |
||
| 200 | $module_handler = $xoops->getHandlerModule(); |
||
| 201 | $mod = trim($mod); |
||
| 202 | try { |
||
| 203 | $cnt = $module_handler->getCount(new Criteria('dirname', $mod)); |
||
| 204 | } catch (DBALException $e) { |
||
| 205 | $cnt = 0; |
||
| 206 | } |
||
| 207 | if ($cnt == 0) { |
||
| 208 | /* @var $module XoopsModule */ |
||
| 209 | $module = $module_handler->create(); |
||
| 210 | $module->loadInfoAsVar($mod); |
||
| 211 | $module->setVar('weight', 1); |
||
| 212 | $module->setVar('isactive', 1); |
||
| 213 | $module->setVar('last_update', time()); |
||
| 214 | $install_script = $module->getInfo('onInstall'); |
||
| 215 | View Code Duplication | if ($install_script && trim($install_script) != '') { |
|
| 216 | XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($install_script))); |
||
| 217 | } |
||
| 218 | $func = "xoops_module_pre_install_{$mod}"; |
||
| 219 | // If pre install function is defined, execute |
||
| 220 | if (function_exists($func)) { |
||
| 221 | $result = $func($module); |
||
| 222 | if (!$result) { |
||
| 223 | $this->error[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 224 | $this->error = array_merge($this->error, $module->getErrors()); |
||
| 225 | return false; |
||
| 226 | } else { |
||
| 227 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 228 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | // Create tables |
||
| 232 | $created_tables = array(); |
||
| 233 | if (count($this->error) == 0) { |
||
| 234 | $schema_file = $module->getInfo('schema'); |
||
| 235 | $sql_file = $module->getInfo('sqlfile'); |
||
| 236 | if (!empty($schema_file)) { |
||
| 237 | $schema_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $schema_file; |
||
| 238 | View Code Duplication | if (!XoopsLoad::fileExists($schema_file_path)) { |
|
| 239 | $this->error[] = |
||
| 240 | sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$schema_file}</strong>"); |
||
| 241 | return false; |
||
| 242 | } |
||
| 243 | $importer = new ImportSchema; |
||
| 244 | $importSchema = $importer->importSchemaArray(Yaml::read($schema_file_path)); |
||
| 245 | $synchronizer = new SingleDatabaseSynchronizer($xoops->db()); |
||
| 246 | $synchronizer->updateSchema($importSchema, true); |
||
| 247 | } elseif (is_array($sql_file) && !empty($sql_file[\XoopsBaseConfig::get('db-type')])) { |
||
| 248 | $xoops->deprecated('Install SQL files are deprecated since 2.6.0. Convert to portable Schemas'); |
||
| 249 | |||
| 250 | $sql_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $sql_file[\XoopsBaseConfig::get('db-type')]; |
||
| 251 | if (!XoopsLoad::fileExists($sql_file_path)) { |
||
| 252 | $this->error[] = |
||
| 253 | sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$sql_file_path}</strong>"); |
||
| 254 | return false; |
||
| 255 | } else { |
||
| 256 | $this->trace[] = sprintf(SystemLocale::SF_SQL_FILE_FOUND, "<strong>{$sql_file_path}</strong>"); |
||
| 257 | $this->trace[] = SystemLocale::MANAGING_TABLES; |
||
| 258 | |||
| 259 | $sql_query = fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)); |
||
| 260 | $sql_query = trim($sql_query); |
||
| 261 | SqlUtility::splitMySqlFile($pieces, $sql_query); |
||
| 262 | foreach ($pieces as $piece) { |
||
| 263 | // [0] contains the prefixed query |
||
| 264 | // [4] contains unprefixed table name |
||
| 265 | $prefixed_query = SqlUtility::prefixQuery($piece, $xoops->db()->prefix()); |
||
| 266 | if (!$prefixed_query) { |
||
| 267 | $this->error[]['sub'] = '<span class="red">' . sprintf( |
||
| 268 | XoopsLocale::EF_INVALID_SQL, |
||
| 269 | '<strong>' . $piece . '</strong>' |
||
| 270 | ) . '</span>'; |
||
| 271 | break; |
||
| 272 | } |
||
| 273 | // check if the table name is reserved |
||
| 274 | if (!in_array($prefixed_query[4], $this->reservedTables) || $mod === 'system') { |
||
| 275 | // not reserved, so try to create one |
||
| 276 | try { |
||
| 277 | $result = $xoops->db()->query($prefixed_query[0]); |
||
| 278 | } catch (Exception $e) { |
||
| 279 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 280 | $result=false; |
||
| 281 | } |
||
| 282 | |||
| 283 | if (!$result) { |
||
| 284 | $this->error[] = $xoops->db()->errorInfo(); |
||
| 285 | break; |
||
| 286 | } else { |
||
| 287 | if (!in_array($prefixed_query[4], $created_tables)) { |
||
| 288 | $this->trace[]['sub'] = sprintf( |
||
| 289 | XoopsLocale::SF_TABLE_CREATED, |
||
| 290 | '<strong>' . $xoops->db()->prefix($prefixed_query[4]) . '</strong>' |
||
| 291 | ); |
||
| 292 | $created_tables[] = $prefixed_query[4]; |
||
| 293 | View Code Duplication | } else { |
|
| 294 | $this->trace[]['sub'] = sprintf( |
||
| 295 | XoopsLocale::SF_DATA_INSERTED_TO_TABLE, |
||
| 296 | '<strong>' . $xoops->db()->prefix($prefixed_query[4]) . '</strong>' |
||
| 297 | ); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } else { |
||
| 301 | // the table name is reserved, so halt the installation |
||
| 302 | $this->error[]['sub'] = sprintf( |
||
| 303 | SystemLocale::EF_TABLE_IS_RESERVED, |
||
| 304 | '<strong>' . $prefixed_query[4] . '</strong>' |
||
| 305 | ); |
||
| 306 | break; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | // if there was an error, delete the tables created so far, |
||
| 310 | // so the next installation will not fail |
||
| 311 | if (count($this->error) > 0) { |
||
| 312 | View Code Duplication | foreach ($created_tables as $table) { |
|
| 313 | try { |
||
| 314 | $xoops->db()->query('DROP TABLE ' . $xoops->db()->prefix($table)); |
||
| 315 | } catch (Exception $e) { |
||
| 316 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | return false; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } |
||
| 324 | // Save module info, blocks, templates and perms |
||
| 325 | if (count($this->error) == 0) { |
||
| 326 | if (!$module_handler->insertModule($module)) { |
||
| 327 | $this->error[] = sprintf( |
||
| 328 | XoopsLocale::EF_NOT_INSERTED_TO_DATABASE, |
||
| 329 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 330 | ); |
||
| 331 | View Code Duplication | foreach ($created_tables as $ct) { |
|
| 332 | try { |
||
| 333 | $xoops->db()->query('DROP TABLE ' . $xoops->db()->prefix($ct)); |
||
| 334 | } catch (Exception $e) { |
||
| 335 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | $this->error[] = sprintf(XoopsLocale::EF_NOT_INSTALLED, "<strong>" . $module->name() . "</strong>"); |
||
| 339 | $this->error[] = XoopsLocale::C_ERRORS; |
||
| 340 | unset($module); |
||
| 341 | unset($created_tables); |
||
| 342 | return false; |
||
| 343 | } |
||
| 344 | unset($created_tables); |
||
| 345 | $this->trace[] = XoopsLocale::S_DATA_INSERTED . sprintf( |
||
| 346 | SystemLocale::F_MODULE_ID, |
||
| 347 | '<strong>' . $module->getVar('mid') . '</strong>' |
||
| 348 | ); |
||
| 349 | $xoops->db()->beginTransaction(); |
||
| 350 | // install Templates |
||
| 351 | $this->installTemplates($module); |
||
| 352 | |||
| 353 | $xoops->templateClearModuleCache($module->getVar('mid')); |
||
| 354 | |||
| 355 | // install blocks |
||
| 356 | $this->installBlocks($module); |
||
| 357 | |||
| 358 | // Install Configs |
||
| 359 | $this->installConfigs($module); |
||
| 360 | |||
| 361 | if ($module->getInfo('hasMain')) { |
||
| 362 | $groups = array(FixedGroups::ADMIN, FixedGroups::USERS, FixedGroups::ANONYMOUS); |
||
| 363 | } else { |
||
| 364 | $groups = array(FixedGroups::ADMIN); |
||
| 365 | } |
||
| 366 | // retrieve all block ids for this module |
||
| 367 | $block_handler = $xoops->getHandlerBlock(); |
||
| 368 | $blocks = $block_handler->getByModule($module->getVar('mid'), false); |
||
| 369 | $this->trace[] = SystemLocale::MANAGING_PERMISSIONS; |
||
| 370 | $gperm_handler = $xoops->getHandlerGroupPermission(); |
||
| 371 | foreach ($groups as $mygroup) { |
||
| 372 | if ($gperm_handler->checkRight('module_admin', 0, $mygroup)) { |
||
| 373 | $mperm = $gperm_handler->create(); |
||
| 374 | $mperm->setVar('gperm_groupid', $mygroup); |
||
| 375 | $mperm->setVar('gperm_itemid', $module->getVar('mid')); |
||
| 376 | $mperm->setVar('gperm_name', 'module_admin'); |
||
| 377 | $mperm->setVar('gperm_modid', 1); |
||
| 378 | View Code Duplication | if (!$gperm_handler->insert($mperm)) { |
|
| 379 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 380 | SystemLocale::EF_GROUP_ID_ADMIN_ACCESS_RIGHT_NOT_ADDED, |
||
| 381 | '<strong>' . $mygroup . '</strong>' |
||
| 382 | ) . '</span>'; |
||
| 383 | } else { |
||
| 384 | $this->trace[]['sub'] = sprintf( |
||
| 385 | SystemLocale::SF_GROUP_ID_ADMIN_ACCESS_RIGHT_ADDED, |
||
| 386 | '<strong>' . $mygroup . '</strong>' |
||
| 387 | ); |
||
| 388 | } |
||
| 389 | unset($mperm); |
||
| 390 | } |
||
| 391 | $mperm = $gperm_handler->create(); |
||
| 392 | $mperm->setVar('gperm_groupid', $mygroup); |
||
| 393 | $mperm->setVar('gperm_itemid', $module->getVar('mid')); |
||
| 394 | $mperm->setVar('gperm_name', 'module_read'); |
||
| 395 | $mperm->setVar('gperm_modid', 1); |
||
| 396 | View Code Duplication | if (!$gperm_handler->insert($mperm)) { |
|
| 397 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 398 | SystemLocale::EF_GROUP_ID_USER_ACCESS_RIGHT_NOT_ADDED, |
||
| 399 | '<strong>' . $mygroup . '</strong>' |
||
| 400 | ) . '</span>'; |
||
| 401 | } else { |
||
| 402 | $this->trace[]['sub'] = sprintf( |
||
| 403 | SystemLocale::SF_GROUP_ID_USER_ACCESS_RIGHT_ADDED, |
||
| 404 | '<strong>' . $mygroup . '</strong>' |
||
| 405 | ); |
||
| 406 | } |
||
| 407 | unset($mperm); |
||
| 408 | foreach ($blocks as $blc) { |
||
| 409 | $bperm = $gperm_handler->create(); |
||
| 410 | $bperm->setVar('gperm_groupid', $mygroup); |
||
| 411 | $bperm->setVar('gperm_itemid', $blc); |
||
| 412 | $bperm->setVar('gperm_name', 'block_read'); |
||
| 413 | $bperm->setVar('gperm_modid', 1); |
||
| 414 | if (!$gperm_handler->insert($bperm)) { |
||
| 415 | $this->trace[]['sub'] = '<span class="red">' |
||
| 416 | . SystemLocale::E_BLOCK_ACCESS_NOT_ADDED . ' Block ID: <strong>' |
||
| 417 | . $blc . '</strong> Group ID: <strong>' . $mygroup . '</strong></span>'; |
||
| 418 | } else { |
||
| 419 | $this->trace[]['sub'] = SystemLocale::S_BLOCK_ACCESS_ADDED |
||
| 420 | . sprintf(SystemLocale::F_BLOCK_ID, "<strong>" . $blc . "</strong>") |
||
| 421 | . sprintf(SystemLocale::F_GROUP_ID, "<strong>" . $mygroup . "</strong>"); |
||
| 422 | } |
||
| 423 | unset($bperm); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | unset($blocks); |
||
| 427 | unset($groups); |
||
| 428 | |||
| 429 | // execute module specific install script if any |
||
| 430 | // If pre install function is defined, execute |
||
| 431 | $func = "xoops_module_install_{$mod}"; |
||
| 432 | if (function_exists($func)) { |
||
| 433 | $result = $func($module); |
||
| 434 | if (!$result) { |
||
| 435 | $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 436 | $this->trace = array_merge($this->trace, $module->getErrors()); |
||
| 437 | } else { |
||
| 438 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 439 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | $this->trace[] = sprintf( |
||
| 444 | XoopsLocale::SF_INSTALLED, |
||
| 445 | '<strong>' . $module->getVar('name', 's') . '</strong>' |
||
| 446 | ); |
||
| 447 | unset($blocks); |
||
| 448 | |||
| 449 | $xoops->db()->commit(); |
||
| 450 | |||
| 451 | $xoops->events()->triggerEvent('system.module.install', $module); |
||
| 452 | return $module; |
||
| 453 | } |
||
| 454 | } else { |
||
| 455 | $this->error[] = sprintf( |
||
| 456 | XoopsLocale::EF_NOT_INSTALLED, |
||
| 457 | '<strong>' . $mod . '</strong>' |
||
| 458 | ) . " " . XoopsLocale::C_ERRORS; |
||
| 459 | return false; |
||
| 460 | } |
||
| 461 | return false; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * uninstall |
||
| 466 | * |
||
| 467 | * @param string $mod module dirname |
||
| 468 | * |
||
| 469 | * @return bool|XoopsModule false on failure, module context on success |
||
| 470 | */ |
||
| 471 | public function uninstall($mod = '') |
||
| 472 | { |
||
| 473 | $xoops = Xoops::getInstance(); |
||
| 474 | $module_handler = $xoops->getHandlerModule(); |
||
| 475 | $module = $module_handler->getByDirname($mod); |
||
| 476 | $xoops->templateClearModuleCache($module->getVar('mid')); |
||
| 477 | |||
| 478 | if ($module->getVar('dirname') === 'system') { |
||
| 479 | $this->error[] = sprintf( |
||
| 480 | XoopsLocale::EF_NOT_UNINSTALLED, |
||
| 481 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 482 | ) . " " . XoopsLocale::C_ERRORS; |
||
| 483 | $this->error[] = " - " . SystemLocale::E_SYSTEM_MODULE_CANNOT_BE_DEACTIVATED; |
||
| 484 | return false; |
||
| 485 | } elseif ($module->getVar('dirname') == $xoops->getConfig('startpage')) { |
||
| 486 | $this->error[] = sprintf( |
||
| 487 | XoopsLocale::EF_NOT_UNINSTALLED, |
||
| 488 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 489 | ) . " " . XoopsLocale::C_ERRORS; |
||
| 490 | $this->error[] = " - " . SystemLocale::E_THIS_MODULE_IS_SET_AS_DEFAULT_START_PAGE; |
||
| 491 | return false; |
||
| 492 | } else { |
||
| 493 | // Load module specific install script if any |
||
| 494 | $uninstall_script = $module->getInfo('onUninstall'); |
||
| 495 | View Code Duplication | if ($uninstall_script && trim($uninstall_script) != '') { |
|
| 496 | XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($uninstall_script))); |
||
| 497 | } |
||
| 498 | $func = "xoops_module_pre_uninstall_{$mod}"; |
||
| 499 | // If pre uninstall function is defined, execute |
||
| 500 | if (function_exists($func)) { |
||
| 501 | $result = $func($module); |
||
| 502 | if (!$result) { |
||
| 503 | $this->error[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 504 | $this->error[] = sprintf( |
||
| 505 | XoopsLocale::EF_NOT_UNINSTALLED, |
||
| 506 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 507 | ) . " " . XoopsLocale::C_ERRORS; |
||
| 508 | $this->error = array_merge($this->error, $module->getErrors()); |
||
| 509 | return false; |
||
| 510 | } else { |
||
| 511 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 512 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 513 | } |
||
| 514 | } |
||
| 515 | |||
| 516 | if (false === $module_handler->deleteModule($module)) { |
||
| 517 | $this->error[] = sprintf(XoopsLocale::EF_NOT_DELETED, $module->getVar('name')); |
||
| 518 | return false; |
||
| 519 | } else { |
||
| 520 | // delete templates |
||
| 521 | $this->deleteTemplates($module); |
||
| 522 | |||
| 523 | // Delete blocks and block template files |
||
| 524 | $this->deleteBlocks($module); |
||
| 525 | |||
| 526 | // Delete tables used by this module |
||
| 527 | $modtables = $module->getInfo('tables'); |
||
| 528 | if ($modtables != false && is_array($modtables)) { |
||
| 529 | // get a schema manager |
||
| 530 | $schemaManager = $xoops->db()->getSchemaManager(); |
||
| 531 | // create schema from the current database |
||
| 532 | $toSchema = $schemaManager->createSchema(); |
||
| 533 | |||
| 534 | $this->trace[] = SystemLocale::MANAGING_TABLES; |
||
| 535 | foreach ($modtables as $table) { |
||
| 536 | // prevent deletion of reserved core tables! |
||
| 537 | if (!in_array($table, $this->reservedTables)) { |
||
| 538 | $toSchema->dropTable($xoops->db()->prefix($table)); |
||
| 539 | $this->trace[]['sub'] = sprintf( |
||
| 540 | XoopsLocale::SF_TABLE_DROPPED, |
||
| 541 | '<strong>' . $xoops->db()->prefix($table) . '</strong>' |
||
| 542 | ); |
||
| 543 | View Code Duplication | } else { |
|
| 544 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 545 | XoopsLocale::EF_TABLE_DROP_NOT_ALLOWED, |
||
| 546 | '<strong>' . $xoops->db()->prefix($table) . '</strong>' |
||
| 547 | ) . '</span>'; |
||
| 548 | } |
||
| 549 | } |
||
| 550 | $synchronizer = new SingleDatabaseSynchronizer($xoops->db()); |
||
| 551 | $synchronizer->updateSchema($toSchema, false); |
||
| 552 | } |
||
| 553 | |||
| 554 | // delete permissions if any |
||
| 555 | $gperm_handler = $xoops->getHandlerGroupPermission(); |
||
| 556 | if (false === $gperm_handler->deleteByModule($module->getVar('mid'))) { |
||
| 557 | $this->trace[] = '<span class="red">' . SystemLocale::E_GROUP_PERMISSIONS_NOT_DELETED . '</span>'; |
||
| 558 | } else { |
||
| 559 | $this->trace[] = SystemLocale::S_GROUP_PERMISSIONS_DELETED; |
||
| 560 | } |
||
| 561 | |||
| 562 | // delete module config options if any |
||
| 563 | $this->deleteConfigs($module); |
||
| 564 | |||
| 565 | // execute module specific install script if any |
||
| 566 | $func = 'xoops_module_uninstall_' . $mod; |
||
| 567 | if (function_exists($func)) { |
||
| 568 | $result = $func($module); |
||
| 569 | if (!$result) { |
||
| 570 | $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 571 | $this->trace = array_merge($this->error, $module->getErrors()); |
||
| 572 | } else { |
||
| 573 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 574 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 575 | } |
||
| 576 | } |
||
| 577 | $this->trace[] = sprintf( |
||
| 578 | XoopsLocale::SF_UNINSTALLED, |
||
| 579 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 580 | ); |
||
| 581 | $xoops->events()->triggerEvent('system.module.uninstall', $module); |
||
| 582 | return $module; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * update |
||
| 589 | * |
||
| 590 | * @param string $mod module dirname |
||
| 591 | * |
||
| 592 | * @return mixed boolean false if failed, XoopsModule if success |
||
| 593 | */ |
||
| 594 | public function update($mod = '') |
||
| 682 | |||
| 683 | /** |
||
| 684 | * getTemplate |
||
| 685 | * |
||
| 686 | * @param string $dirname module directory |
||
| 687 | * @param string $template template name |
||
| 688 | * @param string $type template type - blocks, admin |
||
| 689 | * |
||
| 690 | * @return string |
||
| 691 | */ |
||
| 692 | public function getTemplate($dirname, $template, $type = '') |
||
| 693 | { |
||
| 694 | $xoops = Xoops::getInstance(); |
||
| 695 | $ret = ''; |
||
| 696 | switch ($type) { |
||
| 697 | case 'blocks': |
||
| 698 | case 'admin': |
||
| 699 | $path = $xoops->path('modules/' . $dirname . '/templates/' . $type . '/' . $template); |
||
| 700 | break; |
||
| 701 | default: |
||
| 702 | $path = $xoops->path('modules/' . $dirname . '/templates/' . $template); |
||
| 703 | break; |
||
| 704 | } |
||
| 705 | if (!XoopsLoad::fileExists($path)) { |
||
| 706 | return $ret; |
||
| 707 | } else { |
||
| 708 | $lines = file($path); |
||
| 709 | } |
||
| 710 | if (!$lines) { |
||
| 711 | return $ret; |
||
| 712 | } |
||
| 713 | $count = count($lines); |
||
| 714 | for ($i = 0; $i < $count; ++$i) { |
||
| 715 | $ret .= str_replace("\n", "\r\n", str_replace("\r\n", "\n", $lines[$i])); |
||
| 716 | } |
||
| 717 | return $ret; |
||
| 718 | } |
||
| 719 | |||
| 720 | /** |
||
| 721 | * installTemplates |
||
| 722 | * |
||
| 723 | * @param XoopsModule $module module context |
||
| 724 | * |
||
| 725 | * @return void |
||
| 726 | */ |
||
| 727 | public function installTemplates(XoopsModule $module) |
||
| 803 | |||
| 804 | /** |
||
| 805 | * deleteTemplates |
||
| 806 | * |
||
| 807 | * @param XoopsModule $module module context |
||
| 808 | * |
||
| 809 | * @return void |
||
| 810 | */ |
||
| 811 | public function deleteTemplates(XoopsModule $module) |
||
| 812 | { |
||
| 813 | $xoops = Xoops::getInstance(); |
||
| 814 | $tplfile_handler = $xoops->getHandlerTplFile(); |
||
| 815 | $templates = $tplfile_handler->find('default', 'module', $module->getVar('mid')); |
||
| 816 | if (is_array($templates) && count($templates) > 0) { |
||
| 817 | $this->trace[] = SystemLocale::MANAGING_TEMPLATES; |
||
| 818 | // delete template file entry in db |
||
| 819 | /* @var $template XoopsTplFile */ |
||
| 820 | foreach ($templates as $template) { |
||
| 821 | if (!$tplfile_handler->deleteTpl($template)) { |
||
| 822 | $this->template_delng[] = $template->getVar('tpl_file'); |
||
| 823 | } |
||
| 824 | } |
||
| 825 | } |
||
| 826 | } |
||
| 827 | |||
| 828 | /** |
||
| 829 | * installBlocks |
||
| 830 | * |
||
| 831 | * @param XoopsModule $module module context |
||
| 832 | * |
||
| 833 | * @return void |
||
| 834 | */ |
||
| 835 | public function installBlocks(XoopsModule $module) |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * deleteBlocks |
||
| 1015 | * |
||
| 1016 | * @param XoopsModule $module module |
||
| 1017 | * |
||
| 1018 | * @return void |
||
| 1019 | */ |
||
| 1020 | public function deleteBlocks(XoopsModule $module) |
||
| 1021 | { |
||
| 1022 | $xoops = Xoops::getInstance(); |
||
| 1023 | $block_handler = $xoops->getHandlerBlock(); |
||
| 1024 | $blocks = $block_handler->getByModule($module->getVar('mid')); |
||
| 1025 | if (is_array($blocks) && count($blocks) > 0) { |
||
| 1026 | $tplfile_handler = $xoops->getHandlerTplFile(); |
||
| 1027 | $this->trace[] = SystemLocale::MANAGING_BLOCKS; |
||
| 1028 | /* @var $block XoopsBlock */ |
||
| 1029 | foreach ($blocks as $block) { |
||
| 1030 | if (false === $block_handler->deleteBlock($block)) { |
||
| 1031 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 1032 | SystemLocale::EF_BLOCK_NOT_DELETED, |
||
| 1033 | "<strong>" . $block->getVar('name') . "</strong>" |
||
| 1034 | ) . sprintf( |
||
| 1035 | SystemLocale::F_BLOCK_ID, |
||
| 1036 | "<strong>" . $block->getVar('bid') . "</strong>" |
||
| 1037 | ) . '</span>'; |
||
| 1038 | View Code Duplication | } else { |
|
| 1039 | $this->trace[]['sub'] = sprintf( |
||
| 1040 | SystemLocale::SF_BLOCK_DELETED, |
||
| 1041 | "<strong>" . $block->getVar('name') . "</strong>" |
||
| 1042 | ) . sprintf( |
||
| 1043 | SystemLocale::F_BLOCK_ID, |
||
| 1044 | "<strong>" . $block->getVar('bid') . "</strong>" |
||
| 1045 | ); |
||
| 1046 | } |
||
| 1047 | if ($block->getVar('template') != '') { |
||
| 1048 | $templates = $tplfile_handler->find(null, 'block', $block->getVar('bid')); |
||
| 1049 | /* @var $template XoopsTplFile */ |
||
| 1050 | foreach ($templates as $template) { |
||
| 1051 | if (!$tplfile_handler->delete($template)) { |
||
| 1052 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 1053 | SystemLocale::EF_BLOCK_TEMPLATE_NOT_DELETED, |
||
| 1054 | $template->getVar('tpl_file') |
||
| 1055 | ) . sprintf( |
||
| 1056 | SystemLocale::F_TEMPLATE_ID, |
||
| 1057 | "<strong>" . $template->getVar('tpl_id') . "</strong>" |
||
| 1058 | ) . '</span>'; |
||
| 1059 | View Code Duplication | } else { |
|
| 1060 | $this->trace[]['sub'] = sprintf( |
||
| 1061 | SystemLocale::SF_BLOCK_TEMPLATE_DELETED, |
||
| 1062 | "<strong>" . $template->getVar('tpl_file') . "</strong>" |
||
| 1063 | ) . sprintf( |
||
| 1064 | SystemLocale::F_TEMPLATE_ID, |
||
| 1065 | "<strong>" . $template->getVar('tpl_id') . "</strong>" |
||
| 1066 | ); |
||
| 1067 | } |
||
| 1068 | } |
||
| 1069 | unset($templates); |
||
| 1070 | } |
||
| 1071 | } |
||
| 1072 | } |
||
| 1073 | } |
||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * deleteConfigs |
||
| 1077 | * |
||
| 1078 | * @param XoopsModule $module module |
||
| 1079 | * |
||
| 1080 | * @return void |
||
| 1081 | */ |
||
| 1082 | public function deleteConfigs(XoopsModule $module) |
||
| 1083 | { |
||
| 1084 | $xoops = Xoops::getInstance(); |
||
| 1085 | |||
| 1086 | $config_handler = $xoops->getHandlerConfig(); |
||
| 1087 | $configs = $config_handler->getConfigs(new Criteria('conf_modid', $module->getVar('mid'))); |
||
| 1088 | if (is_array($configs) && count($configs) > 0) { |
||
| 1089 | $this->trace[] = SystemLocale::MANAGING_PREFERENCES; |
||
| 1090 | /* @var $config XoopsConfigItem */ |
||
| 1091 | foreach ($configs as $config) { |
||
| 1092 | if (!$config_handler->deleteConfig($config)) { |
||
| 1093 | $this->trace[]['sub'] = '<span class="red">' |
||
| 1094 | . SystemLocale::E_CONFIG_DATA_NOT_DELETED |
||
| 1095 | . sprintf(SystemLocale::F_CONFIG_ID, "<strong>" . $config->getVar('conf_id') . "</strong>") |
||
| 1096 | . '</span>'; |
||
| 1097 | // save the name of config failed to delete for later use |
||
| 1098 | $this->config_delng[] = $config->getVar('conf_name'); |
||
| 1099 | } else { |
||
| 1100 | $this->config_old[$config->getVar('conf_name')]['value'] = $config->getVar('conf_value', 'N'); |
||
| 1101 | $this->config_old[$config->getVar('conf_name')]['formtype'] = $config->getVar('conf_formtype'); |
||
| 1102 | $this->config_old[$config->getVar('conf_name')]['valuetype'] = $config->getVar('conf_valuetype'); |
||
| 1103 | $this->trace[]['sub'] = SystemLocale::S_CONFIG_DATA_DELETED |
||
| 1104 | . sprintf(SystemLocale::F_CONFIG_ID, "<strong>" . $config->getVar('conf_id') . "</strong>"); |
||
| 1105 | } |
||
| 1106 | } |
||
| 1107 | } |
||
| 1108 | } |
||
| 1109 | |||
| 1110 | /** |
||
| 1111 | * installconfigs |
||
| 1112 | * |
||
| 1113 | * @param XoopsModule $module module being installed |
||
| 1114 | * |
||
| 1115 | * @return void |
||
| 1116 | */ |
||
| 1117 | public function installConfigs(XoopsModule $module) |
||
| 1191 | } |
||
| 1192 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: