Total Complexity | 46 |
Total Lines | 475 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like upgrade_220 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 upgrade_220, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class upgrade_220 extends xoopsUpgrade |
||
20 | { |
||
21 | var $tasks = array('config', 'profile', 'block' /*, 'pm', 'module'*/); |
||
22 | |||
23 | function upgrade_220() |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Check if config category already removed |
||
30 | |||
31 | */ |
||
32 | function check_config() |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Check if user profile table already converted |
||
48 | |||
49 | */ |
||
50 | function check_profile() |
||
51 | { |
||
52 | $xoops = Xoops::getInstance(); |
||
53 | $module_handler = $xoops->getHandlerModule(); |
||
54 | if (!$profile_module = $module_handler->getByDirname('profile')) { |
||
55 | return true; |
||
56 | } |
||
57 | $sql = "SHOW COLUMNS FROM " . $xoops->db()->prefix("users") . " LIKE 'posts'"; |
||
58 | $result = $xoops->db()->queryF($sql); |
||
59 | if (!$result) { |
||
60 | return false; |
||
61 | } |
||
62 | if ($xoops->db()->getRowsNum($result) == 0) { |
||
63 | return false; |
||
64 | } |
||
65 | return true; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Check if block table already converted |
||
70 | |||
71 | */ |
||
72 | function check_block() |
||
73 | { |
||
74 | $xoops = Xoops::getInstance(); |
||
75 | $sql = "SHOW TABLES LIKE '" . $xoops->db()->prefix("block_instance") . "'"; |
||
76 | $result = $xoops->db()->queryF($sql); |
||
77 | if (!$result) { |
||
78 | return true; |
||
79 | } |
||
80 | if ($xoops->db()->getRowsNum($result) > 0) { |
||
81 | return false; |
||
82 | } |
||
83 | return true; |
||
84 | } |
||
85 | |||
86 | function apply() |
||
95 | } |
||
96 | |||
97 | function apply_config() |
||
98 | { |
||
99 | $xoops = Xoops::getInstance(); |
||
100 | |||
101 | $result = true; |
||
102 | |||
103 | //Set core configuration back to zero for system module |
||
104 | $xoops->db()->queryF("UPDATE `" . $xoops->db()->prefix('config') . "` SET conf_modid = 0 WHERE conf_modid = 1"); |
||
105 | |||
106 | //Change debug modes so there can only be one active at any one time |
||
107 | $xoops->db()->queryF("UPDATE `" . $xoops->db()->prefix('config') . "` SET conf_formtype = 'select', conf_valuetype = 'int' WHERE conf_name = 'debug_mode'"); |
||
108 | |||
109 | //Reset category ID for non-system configs |
||
110 | $xoops->db()->queryF("UPDATE `" . $xoops->db()->prefix('config') . "` SET conf_catid = 0 WHERE conf_modid > 1 AND conf_catid > 0"); |
||
111 | |||
112 | // remove admin theme configuration item |
||
113 | $xoops->db()->queryF("DELETE FROM `" . $xoops->db()->prefix('config') . "` WHERE conf_name='theme_set_admin'"); |
||
114 | |||
115 | //Drop non-System config categories |
||
116 | $xoops->db()->queryF("DELETE FROM `" . $xoops->db()->prefix('configcategory') . "` WHERE confcat_modid > 1"); |
||
117 | |||
118 | //Drop category information fields added in 2.2 |
||
119 | $xoops->db()->queryF("ALTER TABLE `" . $xoops->db()->prefix("configcategory") . "` DROP `confcat_nameid`, DROP `confcat_description`, DROP `confcat_modid`"); |
||
120 | |||
121 | // Re-add user configuration category |
||
122 | $xoops->db()->queryF("INSERT INTO `" . $xoops->db()->prefix("configcategory") . "` (confcat_id, confcat_name, confcat_order) VALUES (2, '_MD_AM_USERSETTINGS', 2)"); |
||
123 | |||
124 | //Rebuild user configuration items |
||
125 | //Get values from Profile module |
||
126 | $profile_config_arr = array(); |
||
127 | $profile_config_arr['minpass'] = 5; |
||
128 | $profile_config_arr['minuname'] = 3; |
||
129 | $profile_config_arr['new_user_notify'] = 1; |
||
130 | $profile_config_arr['new_user_notify_group'] = XOOPS_GROUP_ADMIN; |
||
131 | $profile_config_arr['activation_type'] = 0; |
||
132 | $profile_config_arr['activation_group'] = XOOPS_GROUP_ADMIN; |
||
133 | $profile_config_arr['uname_test_level'] = 0; |
||
134 | $profile_config_arr['avatar_allow_upload'] = 0; |
||
135 | $profile_config_arr['avatar_width'] = 80; |
||
136 | $profile_config_arr['avatar_height'] = 80; |
||
137 | $profile_config_arr['avatar_maxsize'] = 35000; |
||
138 | $profile_config_arr['self_delete'] = 0; |
||
139 | $profile_config_arr['bad_unames'] = serialize(array('webmaster', '^xoops', '^admin')); |
||
140 | $profile_config_arr['bad_emails'] = serialize(array('xoops.org$')); |
||
141 | $profile_config_arr['maxuname'] = 10; |
||
142 | $profile_config_arr['avatar_minposts'] = 0; |
||
143 | $profile_config_arr['allow_chgmail'] = 0; |
||
144 | $profile_config_arr['reg_dispdsclmr'] = 0; |
||
145 | $profile_config_arr['reg_disclaimer'] = ""; |
||
146 | $profile_config_arr['allow_register'] = 1; |
||
147 | |||
148 | $module_handler = $xoops->getHandlerModule(); |
||
149 | $config_handler = $xoops->getHandlerConfig(); |
||
150 | $profile_module = $module_handler->getByDirname('profile'); |
||
151 | if (is_object($profile_module)) { |
||
152 | $profile_config = $config_handler->getConfigs(new Criteria('conf_modid', $profile_module->getVar('mid'))); |
||
153 | foreach (array_keys($profile_config) as $i) { |
||
154 | $profile_config_arr[$profile_config[$i]->getVar('conf_name')] = $profile_config[$i]->getVar('conf_value', 'n'); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | $xoops->db()->queryF("INSERT INTO `" . |
||
159 | $xoops->db()->prefix("config") . |
||
160 | "` (conf_modid, conf_catid, conf_name, conf_title, conf_value, conf_desc, conf_formtype, conf_valuetype, conf_order) VALUES " . |
||
161 | " (0, 2, 'minpass', '_MD_AM_MINPASS', " . |
||
162 | $xoops->db()->quote($profile_config_arr['minpass']) . ", '_MD_AM_MINPASSDSC', 'textbox', 'int', 1)," . |
||
163 | " (0, 2, 'minuname', '_MD_AM_MINUNAME', " . |
||
164 | $xoops->db()->quote($profile_config_arr['minuname']) . ", '_MD_AM_MINUNAMEDSC', 'textbox', 'int', 2)," . " (0, 2, 'new_user_notify', '_MD_AM_NEWUNOTIFY', " . |
||
165 | $xoops->db()->quote($profile_config_arr['new_user_notify']) . ", '_MD_AM_NEWUNOTIFYDSC', 'yesno', 'int', 4)," . " (0, 2, 'new_user_notify_group', '_MD_AM_NOTIFYTO', " . |
||
166 | $xoops->db()->quote($profile_config_arr['new_user_notify_group']) . ", '_MD_AM_NOTIFYTODSC', 'group', 'int', 6)," . " (0, 2, 'activation_type', '_MD_AM_ACTVTYPE', " . |
||
167 | $xoops->db()->quote($profile_config_arr['activation_type']) . ", '_MD_AM_ACTVTYPEDSC', 'select', 'int', 8)," . " (0, 2, 'activation_group', '_MD_AM_ACTVGROUP', ". |
||
168 | $xoops->db()->quote($profile_config_arr['activation_group']) . ", '_MD_AM_ACTVGROUPDSC', 'group', 'int', 10)," . " (0, 2, 'uname_test_level', '_MD_AM_UNAMELVL', " . |
||
169 | $xoops->db()->quote($profile_config_arr['uname_test_level']) . ", '_MD_AM_UNAMELVLDSC', 'select', 'int', 12)," . " (0, 2, 'avatar_allow_upload', '_MD_AM_AVATARALLOW', " . |
||
170 | $xoops->db()->quote($profile_config_arr['avatar_allow_upload']) . ", '_MD_AM_AVATARALWDSC', 'yesno', 'int', 14)," . " (0, 2, 'avatar_width', '_MD_AM_AVATARW', " . |
||
171 | $xoops->db()->quote($profile_config_arr['avatar_width']) . ", '_MD_AM_AVATARWDSC', 'textbox', 'int', 16)," . " (0, 2, 'avatar_height', '_MD_AM_AVATARH', " . |
||
172 | $xoops->db()->quote($profile_config_arr['avatar_height']) . ", '_MD_AM_AVATARHDSC', 'textbox', 'int', 18)," . " (0, 2, 'avatar_maxsize', '_MD_AM_AVATARMAX', " . |
||
173 | $xoops->db()->quote($profile_config_arr['avatar_maxsize']) . ", '_MD_AM_AVATARMAXDSC', 'textbox', 'int', 20)," . " (0, 2, 'self_delete', '_MD_AM_SELFDELETE', " . |
||
174 | $xoops->db()->quote($profile_config_arr['self_delete']) . ", '_MD_AM_SELFDELETEDSC', 'yesno', 'int', 22)," . " (0, 2, 'bad_unames', '_MD_AM_BADUNAMES', " . |
||
175 | $xoops->db()->quote($profile_config_arr['bad_unames']) . ", '_MD_AM_BADUNAMESDSC', 'textarea', 'array', 24)," . " (0, 2, 'bad_emails', '_MD_AM_BADEMAILS', " . |
||
176 | $xoops->db()->quote($profile_config_arr['bad_emails']) . ", '_MD_AM_BADEMAILSDSC', 'textarea', 'array', 26)," . " (0, 2, 'maxuname', '_MD_AM_MAXUNAME', " . |
||
177 | $xoops->db()->quote($profile_config_arr['maxuname']) . ", '_MD_AM_MAXUNAMEDSC', 'textbox', 'int', 3)," . " (0, 2, 'avatar_minposts', '_MD_AM_AVATARMP', " . |
||
178 | $xoops->db()->quote($profile_config_arr['avatar_minposts']) . ", '_MD_AM_AVATARMPDSC', 'textbox', 'int', 15)," . " (0, 2, 'allow_chgmail', '_MD_AM_ALLWCHGMAIL', " . |
||
179 | $xoops->db()->quote($profile_config_arr['allow_chgmail']) . ", '_MD_AM_ALLWCHGMAILDSC', 'yesno', 'int', 3)," . " (0, 2, 'reg_dispdsclmr', '_MD_AM_DSPDSCLMR', " . |
||
180 | $xoops->db()->quote($profile_config_arr['reg_dispdsclmr']) . ", '_MD_AM_DSPDSCLMRDSC', 'yesno', 'int', 30)," . " (0, 2, 'reg_disclaimer', '_MD_AM_REGDSCLMR', " . |
||
181 | $xoops->db()->quote($profile_config_arr['reg_disclaimer']) . ", '_MD_AM_REGDSCLMRDSC', 'textarea', 'text', 32)," . " (0, 2, 'allow_register', '_MD_AM_ALLOWREG', " . |
||
182 | $xoops->db()->quote($profile_config_arr['allow_register']) . ", '_MD_AM_ALLOWREGDSC', 'yesno', 'int', 0)"); |
||
183 | |||
184 | //Rebuild user configuration options |
||
185 | $criteria = new CriteriaCompo(new Criteria('conf_name', "('activation_type', 'uname_test_level')", "IN")); |
||
186 | $criteria->add(new Criteria('conf_modid', 0)); |
||
187 | $criteria->setSort('conf_name'); |
||
188 | $criteria->setOrder('ASC'); |
||
189 | $configs = $config_handler->getConfigs($criteria); |
||
190 | $id_activation_type = $configs[0]->getVar("conf_id"); |
||
191 | $id_uname_test_level = $configs[1]->getVar("conf_id"); |
||
192 | $xoops->db()->queryF("INSERT INTO `" . |
||
193 | $xoops->db()->prefix("configoption") . |
||
194 | "` (confop_name, confop_value, conf_id) VALUES " . |
||
195 | " ('_MD_AM_USERACTV', '0', {$id_activation_type})," . |
||
196 | " ('_MD_AM_AUTOACTV', '1', {$id_activation_type})," . |
||
197 | " ('_MD_AM_ADMINACTV', '2', {$id_activation_type})," . |
||
198 | " ('_MD_AM_STRICT', '0', {$id_uname_test_level})," . |
||
199 | " ('_MD_AM_MEDIUM', '1', {$id_uname_test_level})," . |
||
200 | " ('_MD_AM_LIGHT', '2', {$id_uname_test_level})"); |
||
201 | |||
202 | return $result; |
||
203 | } |
||
204 | |||
205 | function apply_profile() |
||
206 | { |
||
207 | $xoops = Xoops::getInstance(); |
||
208 | $xoops->db(); |
||
209 | // Restore users table |
||
210 | $xoops->db()->queryF("ALTER TABLE `" . $xoops->db()->prefix("users") . "` |
||
211 | ADD url varchar(100) NOT NULL default '', |
||
212 | ADD user_regdate int(10) unsigned NOT NULL default '0', |
||
213 | ADD user_icq varchar(15) NOT NULL default '', |
||
214 | ADD user_from varchar(100) NOT NULL default '', |
||
215 | ADD user_sig tinytext, |
||
216 | ADD user_viewemail tinyint(1) unsigned NOT NULL default '0', |
||
217 | ADD actkey varchar(8) NOT NULL default '', |
||
218 | ADD user_aim varchar(18) NOT NULL default '', |
||
219 | ADD user_yim varchar(25) NOT NULL default '', |
||
220 | ADD user_msnm varchar(100) NOT NULL default '', |
||
221 | ADD posts mediumint(8) unsigned NOT NULL default '0', |
||
222 | ADD attachsig tinyint(1) unsigned NOT NULL default '0', |
||
223 | ADD theme varchar(100) NOT NULL default '', |
||
224 | ADD timezone_offset float(3,1) NOT NULL default '0.0', |
||
225 | ADD last_login int(10) unsigned NOT NULL default '0', |
||
226 | ADD umode varchar(10) NOT NULL default '', |
||
227 | ADD uorder tinyint(1) unsigned NOT NULL default '0', |
||
228 | ADD notify_method tinyint(1) NOT NULL default '1', |
||
229 | ADD notify_mode tinyint(1) NOT NULL default '0', |
||
230 | ADD user_occ varchar(100) NOT NULL default '', |
||
231 | ADD bio tinytext, |
||
232 | ADD user_intrest varchar(150) NOT NULL default '', |
||
233 | ADD user_mailok tinyint(1) unsigned NOT NULL default '1' |
||
234 | "); |
||
235 | |||
236 | // Copy data from profile table |
||
237 | $profile_fields = array( |
||
238 | "url", "user_regdate", "user_icq", "user_from", "user_sig", "user_viewemail", "actkey", "user_aim", |
||
239 | "user_yim", "user_msnm", "posts", "attachsig", "theme", "timezone_offset", "last_login", "umode", "uorder", |
||
240 | "notify_method", "notify_mode", "user_occ", "bio", "user_intrest", "user_mailok" |
||
241 | ); |
||
242 | foreach ($profile_fields as $field) { |
||
243 | $xoops->db()->queryF("UPDATE `" . |
||
244 | $xoops->db()->prefix("users") . "` u, `" . |
||
245 | $xoops->db()->prefix("user_profile") . |
||
246 | "` p SET u.{$field} = p.{$field} WHERE u.uid=p.profileid"); |
||
247 | } |
||
248 | |||
249 | //Set display name as real name |
||
250 | $xoops->db()->queryF("UPDATE `" . $xoops->db()->prefix("users") . "` SET name=uname WHERE name=''"); |
||
251 | //Set loginname as uname |
||
252 | $xoops->db()->queryF("UPDATE `" . $xoops->db()->prefix("users") . "` SET uname=loginname"); |
||
253 | //Drop loginname |
||
254 | $xoops->db()->queryF("ALTER TABLE `" . $xoops->db()->prefix("users") . "` DROP loginname"); |
||
255 | |||
256 | return true; |
||
257 | } |
||
258 | |||
259 | function _block_lookup($block, $blocks) |
||
260 | { |
||
261 | if ($block['show_func'] == 'b_system_custom_show') { |
||
262 | return 0; |
||
263 | } |
||
264 | |||
265 | foreach ($blocks as $key => $bk) { |
||
266 | if ($block['show_func'] == $bk['show_func'] && $block['edit_func'] == $bk['edit_func'] && $block['template'] == $bk['template'] |
||
267 | ) { |
||
268 | return $key; |
||
269 | } |
||
270 | } |
||
271 | return null; |
||
272 | } |
||
273 | |||
274 | function apply_block() |
||
494 | } |
||
495 | } |
||
496 | |||
497 | $upg = new upgrade_220(); |
||
501 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.