Completed
Pull Request — master (#133)
by Goffy
16:45
created

Upgrade_220   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 412
Duplicated Lines 7.28 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 30
loc 412
rs 8.3157
c 1
b 0
f 0
wmc 43
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
F apply_block() 30 164 22
A apply_profile() 0 68 2
A __construct() 0 4 1
A check_block() 0 10 2
B _block_lookup() 0 14 6
A apply_config() 0 75 3
A check_config() 0 9 2
A check_profile() 0 14 3
A apply() 0 11 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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. 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_220, and based on these observations, apply Extract Interface, too.

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 433.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Upgrader from 2.2.* to 2.3.0
5
 *
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 *
13
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
14
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package             upgrader
16
 * @since               2.3.0
17
 * @author              Taiwen Jiang <[email protected]>
18
 * @version             $Id: index.php 13082 2015-06-06 21:59:41Z beckmi $
19
 */
20
class Upgrade_220 extends XoopsUpgrade
21
{
22
    public $tasks = array('config', 'profile', 'block'/*, 'pm', 'module'*/);
23
24
    public function __construct()
25
    {
26
        parent::__construct(basename(__DIR__));
27
    }
28
29
    /**
30
     * Check if config category already removed
31
     *
32
     */
33
    public function check_config()
34
    {
35
        $sql    = 'SHOW COLUMNS FROM `' . $GLOBALS['xoopsDB']->prefix('configcategory') . "` LIKE 'confcat_modid'";
36
        $result = $GLOBALS['xoopsDB']->queryF($sql);
37
        if (!$result) {
38
            return true;
39
        }
40
        return !($GLOBALS['xoopsDB']->getRowsNum($result) > 0);
41
    }
42
43
    /**
44
     * Check if user profile table already converted
45
     *
46
     */
47
    public function check_profile()
48
    {
49
        $module_handler = xoops_getHandler('module');
50
        if (!$profile_module = $module_handler->getByDirname('profile')) {
51
            return true;
52
        }
53
        $sql    = 'SHOW COLUMNS FROM ' . $GLOBALS['xoopsDB']->prefix('users') . " LIKE 'posts'";
54
        $result = $GLOBALS['xoopsDB']->queryF($sql);
55
        if (!$result) {
56
            return false;
57
        }
58
59
        return !($GLOBALS['xoopsDB']->getRowsNum($result) == 0);
60
    }
61
62
    /**
63
     * Check if block table already converted
64
     *
65
     */
66
    public function check_block()
67
    {
68
        $sql    = "SHOW TABLES LIKE '" . $GLOBALS['xoopsDB']->prefix('block_instance') . "'";
69
        $result = $GLOBALS['xoopsDB']->queryF($sql);
70
        if (!$result) {
71
            return true;
72
        }
73
74
        return !($GLOBALS['xoopsDB']->getRowsNum($result) > 0);
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function apply()
81
    {
82
        if (empty($_GET['upd220'])) {
83
            $this->logs[] = _CONFIRM_UPGRADE_220;
84
            $res          = false;
85
        } else {
86
            $res = parent::apply();
87
        }
88
89
        return $res;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function apply_config()
96
    {
97
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
98
99
        $result = true;
100
101
        //Set core configuration back to zero for system module
102
        $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('config') . '` SET conf_modid = 0 WHERE conf_modid = 1');
103
104
        //Change debug modes so there can only be one active at any one time
105
        $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('config') . "` SET conf_formtype = 'select', conf_valuetype = 'int' WHERE conf_name = 'debug_mode'");
106
107
        //Reset category ID for non-system configs
108
        $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('config') . '` SET conf_catid = 0 WHERE conf_modid > 1 AND conf_catid > 0');
109
110
        // remove admin theme configuration item
111
        $xoopsDB->queryF('DELETE FROM `' . $xoopsDB->prefix('config') . "` WHERE conf_name='theme_set_admin'");
112
113
        //Drop non-System config categories
114
        $xoopsDB->queryF('DELETE FROM `' . $xoopsDB->prefix('configcategory') . '` WHERE confcat_modid > 1');
115
116
        //Drop category information fields added in 2.2
117
        $xoopsDB->queryF('ALTER TABLE `' . $xoopsDB->prefix('configcategory') . '` DROP `confcat_nameid`, DROP `confcat_description`, DROP `confcat_modid`');
118
119
        // Re-add user configuration category
120
        $xoopsDB->queryF('INSERT INTO `' . $xoopsDB->prefix('configcategory') . "` (confcat_id, confcat_name, confcat_order) VALUES (2, '_MD_AM_USERSETTINGS', 2)");
121
122
        //Rebuild user configuration items
123
        //Get values from Profile module
124
        $profile_config_arr                          = array();
125
        $profile_config_arr['minpass']               = 5;
126
        $profile_config_arr['minuname']              = 3;
127
        $profile_config_arr['new_user_notify']       = 1;
128
        $profile_config_arr['new_user_notify_group'] = XOOPS_GROUP_ADMIN;
129
        $profile_config_arr['activation_type']       = 0;
130
        $profile_config_arr['activation_group']      = XOOPS_GROUP_ADMIN;
131
        $profile_config_arr['uname_test_level']      = 0;
132
        $profile_config_arr['avatar_allow_upload']   = 0;
133
        $profile_config_arr['avatar_width']          = 80;
134
        $profile_config_arr['avatar_height']         = 80;
135
        $profile_config_arr['avatar_maxsize']        = 35000;
136
        $profile_config_arr['self_delete']           = 0;
137
        $profile_config_arr['bad_unames']            = serialize(array('webmaster', '^xoops', '^admin'));
138
        $profile_config_arr['bad_emails']            = serialize(array('xoops.org$'));
139
        $profile_config_arr['maxuname']              = 10;
140
        $profile_config_arr['avatar_minposts']       = 0;
141
        $profile_config_arr['allow_chgmail']         = 0;
142
        $profile_config_arr['reg_dispdsclmr']        = 0;
143
        $profile_config_arr['reg_disclaimer']        = '';
144
        $profile_config_arr['allow_register']        = 1;
145
146
        $module_handler = xoops_getHandler('module');
147
        $config_handler = xoops_getHandler('config');
148
        $profile_module = $module_handler->getByDirname('profile');
149
        if (is_object($profile_module)) {
150
            $profile_config = $config_handler->getConfigs(new Criteria('conf_modid', $profile_module->getVar('mid')));
151
            foreach (array_keys($profile_config) as $i) {
152
                $profile_config_arr[$profile_config[$i]->getVar('conf_name')] = $profile_config[$i]->getVar('conf_value', 'n');
153
            }
154
        }
155
156
        $xoopsDB->queryF('INSERT INTO `' . $xoopsDB->prefix('config') . '` (conf_modid, conf_catid, conf_name, conf_title, conf_value, conf_desc, conf_formtype, conf_valuetype, conf_order) VALUES ' . " (0, 2, 'minpass', '_MD_AM_MINPASS', " . $xoopsDB->quote($profile_config_arr['minpass']) . ", '_MD_AM_MINPASSDSC', 'textbox', 'int', 1)," . " (0, 2, 'minuname', '_MD_AM_MINUNAME', " . $xoopsDB->quote($profile_config_arr['minuname']) . ", '_MD_AM_MINUNAMEDSC', 'textbox', 'int', 2)," . " (0, 2, 'new_user_notify', '_MD_AM_NEWUNOTIFY', " . $xoopsDB->quote($profile_config_arr['new_user_notify']) . ", '_MD_AM_NEWUNOTIFYDSC', 'yesno', 'int', 4)," . " (0, 2, 'new_user_notify_group', '_MD_AM_NOTIFYTO', " . $xoopsDB->quote($profile_config_arr['new_user_notify_group']) . ", '_MD_AM_NOTIFYTODSC', 'group', 'int', 6)," . " (0, 2, 'activation_type', '_MD_AM_ACTVTYPE', " . $xoopsDB->quote($profile_config_arr['activation_type']) . ", '_MD_AM_ACTVTYPEDSC', 'select', 'int', 8)," . " (0, 2, 'activation_group', '_MD_AM_ACTVGROUP', " . $xoopsDB->quote($profile_config_arr['activation_group']) . ", '_MD_AM_ACTVGROUPDSC', 'group', 'int', 10)," . " (0, 2, 'uname_test_level', '_MD_AM_UNAMELVL', " . $xoopsDB->quote($profile_config_arr['uname_test_level']) . ", '_MD_AM_UNAMELVLDSC', 'select', 'int', 12)," . " (0, 2, 'avatar_allow_upload', '_MD_AM_AVATARALLOW', " . $xoopsDB->quote($profile_config_arr['avatar_allow_upload']) . ", '_MD_AM_AVATARALWDSC', 'yesno', 'int', 14)," . " (0, 2, 'avatar_width', '_MD_AM_AVATARW', " . $xoopsDB->quote($profile_config_arr['avatar_width']) . ", '_MD_AM_AVATARWDSC', 'textbox', 'int', 16)," . " (0, 2, 'avatar_height', '_MD_AM_AVATARH', " . $xoopsDB->quote($profile_config_arr['avatar_height']) . ", '_MD_AM_AVATARHDSC', 'textbox', 'int', 18)," . " (0, 2, 'avatar_maxsize', '_MD_AM_AVATARMAX', " . $xoopsDB->quote($profile_config_arr['avatar_maxsize']) . ", '_MD_AM_AVATARMAXDSC', 'textbox', 'int', 20)," . " (0, 2, 'self_delete', '_MD_AM_SELFDELETE', " . $xoopsDB->quote($profile_config_arr['self_delete']) . ", '_MD_AM_SELFDELETEDSC', 'yesno', 'int', 22)," . " (0, 2, 'bad_unames', '_MD_AM_BADUNAMES', " . $xoopsDB->quote($profile_config_arr['bad_unames']) . ", '_MD_AM_BADUNAMESDSC', 'textarea', 'array', 24)," . " (0, 2, 'bad_emails', '_MD_AM_BADEMAILS', " . $xoopsDB->quote($profile_config_arr['bad_emails']) . ", '_MD_AM_BADEMAILSDSC', 'textarea', 'array', 26)," . " (0, 2, 'maxuname', '_MD_AM_MAXUNAME', " . $xoopsDB->quote($profile_config_arr['maxuname']) . ", '_MD_AM_MAXUNAMEDSC', 'textbox', 'int', 3)," . " (0, 2, 'avatar_minposts', '_MD_AM_AVATARMP', " . $xoopsDB->quote($profile_config_arr['avatar_minposts']) . ", '_MD_AM_AVATARMPDSC', 'textbox', 'int', 15)," . " (0, 2, 'allow_chgmail', '_MD_AM_ALLWCHGMAIL', " . $xoopsDB->quote($profile_config_arr['allow_chgmail']) . ", '_MD_AM_ALLWCHGMAILDSC', 'yesno', 'int', 3)," . " (0, 2, 'reg_dispdsclmr', '_MD_AM_DSPDSCLMR', " . $xoopsDB->quote($profile_config_arr['reg_dispdsclmr']) . ", '_MD_AM_DSPDSCLMRDSC', 'yesno', 'int', 30)," . " (0, 2, 'reg_disclaimer', '_MD_AM_REGDSCLMR', " . $xoopsDB->quote($profile_config_arr['reg_disclaimer']) . ", '_MD_AM_REGDSCLMRDSC', 'textarea', 'text', 32)," . " (0, 2, 'allow_register', '_MD_AM_ALLOWREG', " . $xoopsDB->quote($profile_config_arr['allow_register']) . ", '_MD_AM_ALLOWREGDSC', 'yesno', 'int', 0)");
157
158
        //Rebuild user configuration options
159
        $criteria = new CriteriaCompo(new Criteria('conf_name', "('activation_type', 'uname_test_level')", 'IN'));
160
        $criteria->add(new Criteria('conf_modid', 0));
161
        $criteria->setSort('conf_name');
162
        $criteria->setOrder('ASC');
163
        $configs             = $config_handler->getConfigs($criteria);
164
        $id_activation_type  = $configs[0]->getVar('conf_id');
165
        $id_uname_test_level = $configs[1]->getVar('conf_id');
166
        $xoopsDB->queryF('INSERT INTO `' . $xoopsDB->prefix('configoption') . '` (confop_name, confop_value, conf_id) VALUES ' . " ('_MD_AM_USERACTV', '0', {$id_activation_type})," . " ('_MD_AM_AUTOACTV', '1', {$id_activation_type})," . " ('_MD_AM_ADMINACTV', '2', {$id_activation_type})," . " ('_MD_AM_STRICT', '0', {$id_uname_test_level})," . " ('_MD_AM_MEDIUM', '1', {$id_uname_test_level})," . " ('_MD_AM_LIGHT', '2', {$id_uname_test_level})");
167
168
        return $result;
169
    }
170
171
    /**
172
     * @return bool
173
     */
174
    public function apply_profile()
175
    {
176
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
177
        // Restore users table
178
        $xoopsDB->queryF('ALTER TABLE `' . $xoopsDB->prefix('users') . "`
179
              ADD url varchar(100) NOT NULL default '',
180
              ADD user_regdate int(10) unsigned NOT NULL default '0',
181
              ADD user_icq varchar(15) NOT NULL default '',
182
              ADD user_from varchar(100) NOT NULL default '',
183
              ADD user_sig tinytext,
184
              ADD user_viewemail tinyint(1) unsigned NOT NULL default '0',
185
              ADD actkey varchar(8) NOT NULL default '',
186
              ADD user_aim varchar(18) NOT NULL default '',
187
              ADD user_yim varchar(25) NOT NULL default '',
188
              ADD user_msnm varchar(100) NOT NULL default '',
189
              ADD posts mediumint(8) unsigned NOT NULL default '0',
190
              ADD attachsig tinyint(1) unsigned NOT NULL default '0',
191
              ADD theme varchar(100) NOT NULL default '',
192
              ADD timezone_offset float(3,1) NOT NULL default '0.0',
193
              ADD last_login int(10) unsigned NOT NULL default '0',
194
              ADD umode varchar(10) NOT NULL default '',
195
              ADD uorder tinyint(1) unsigned NOT NULL default '0',
196
              ADD notify_method tinyint(1) NOT NULL default '1',
197
              ADD notify_mode tinyint(1) NOT NULL default '0',
198
              ADD user_occ varchar(100) NOT NULL default '',
199
              ADD bio tinytext,
200
              ADD user_intrest varchar(150) NOT NULL default '',
201
              ADD user_mailok tinyint(1) unsigned NOT NULL default '1'
202
              ");
203
204
        // Copy data from profile table
205
        $profile_fields = array(
206
            'url',
207
            'user_regdate',
208
            'user_icq',
209
            'user_from',
210
            'user_sig',
211
            'user_viewemail',
212
            'actkey',
213
            'user_aim',
214
            'user_yim',
215
            'user_msnm',
216
            'posts',
217
            'attachsig',
218
            'theme',
219
            'timezone_offset',
220
            'last_login',
221
            'umode',
222
            'uorder',
223
            'notify_method',
224
            'notify_mode',
225
            'user_occ',
226
            'bio',
227
            'user_intrest',
228
            'user_mailok');
229
        foreach ($profile_fields as $field) {
230
            $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('users') . '` u, `' . $xoopsDB->prefix('user_profile') . "` p SET u.{$field} = p.{$field} WHERE u.uid=p.profileid");
231
        }
232
233
        //Set display name as real name
234
        $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('users') . "` SET name=uname WHERE name=''");
235
        //Set loginname as uname
236
        $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('users') . '` SET uname=loginname');
237
        //Drop loginname
238
        $xoopsDB->queryF('ALTER TABLE `' . $xoopsDB->prefix('users') . '` DROP loginname');
239
240
        return true;
241
    }
242
243
    /**
244
     * @param $block
245
     * @param $blocks
246
     *
247
     * @return int|null|string
248
     */
249
    public function _block_lookup($block, $blocks)
250
    {
251
        if ($block['show_func'] === 'b_system_custom_show') {
252
            return 0;
253
        }
254
255
        foreach ($blocks as $key => $bk) {
256
            if ($block['show_func'] == $bk['show_func'] && $block['edit_func'] == $bk['edit_func'] && $block['template'] == $bk['template']) {
257
                return $key;
258
            }
259
        }
260
261
        return null;
262
    }
263
264
    /**
265
     * @return bool
266
     */
267
    public function apply_block()
268
    {
269
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
270
        $xoopsDB->queryF('UPDATE ' . $xoopsDB->prefix('block_module_link') . ' SET module_id = -1, pageid = 0 WHERE module_id < 2 AND pageid = 1');
271
272
        //Change block module link to remove pages
273
        //Remove page links for module subpages
274
        $xoopsDB->queryF('DELETE FROM ' . $xoopsDB->prefix('block_module_link') . ' WHERE pageid > 0');
275
276
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP PRIMARY KEY';
277
        $xoopsDB->queryF($sql);
278
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP pageid';
279
        $xoopsDB->queryF($sql);
280
        $sql = 'ALTER IGNORE TABLE `' . $xoopsDB->prefix('block_module_link') . '` ADD PRIMARY KEY (`block_id` , `module_id`)';
281
        $xoopsDB->queryF($sql);
282
283
        $xoopsDB->queryF('RENAME TABLE `' . $xoopsDB->prefix('newblocks') . '` TO `' . $xoopsDB->prefix('newblocks_bak') . '`');
284
285
        // Create new block table
286
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('newblocks') . " (
287
              bid mediumint(8) unsigned NOT NULL auto_increment,
288
              mid smallint(5) unsigned NOT NULL default '0',
289
              func_num tinyint(3) unsigned NOT NULL default '0',
290
              options varchar(255) NOT NULL default '',
291
              name varchar(150) NOT NULL default '',
292
              title varchar(255) NOT NULL default '',
293
              content text,
294
              side tinyint(1) unsigned NOT NULL default '0',
295
              weight smallint(5) unsigned NOT NULL default '0',
296
              visible tinyint(1) unsigned NOT NULL default '0',
297
              block_type char(1) NOT NULL default '',
298
              c_type char(1) NOT NULL default '',
299
              isactive tinyint(1) unsigned NOT NULL default '0',
300
              dirname varchar(50) NOT NULL default '',
301
              func_file varchar(50) NOT NULL default '',
302
              show_func varchar(50) NOT NULL default '',
303
              edit_func varchar(50) NOT NULL default '',
304
              template varchar(50) NOT NULL default '',
305
              bcachetime int(10) unsigned NOT NULL default '0',
306
              last_modified int(10) unsigned NOT NULL default '0',
307
              PRIMARY KEY  (bid),
308
              KEY mid (mid),
309
              KEY visible (visible),
310
              KEY isactive_visible_mid (isactive,visible,mid),
311
              KEY mid_funcnum (mid,func_num)
312
            ) TYPE=MyISAM;
313
            ";
314
        $xoopsDB->queryF($sql);
315
316
        $sql    = '   SELECT MAX(instanceid) FROM ' . $xoopsDB->prefix('block_instance');
317
        $result = $xoopsDB->query($sql);
318
        list($MaxInstanceId) = $xoopsDB->fetchRow($result);
319
320
        // Change custom block mid from 1 to 0
321
        $sql    = 'UPDATE `' . $xoopsDB->prefix('newblocks_bak') . "` SET mid = 0 WHERE show_func = 'b_system_custom_show'";
322
        $result = $xoopsDB->queryF($sql);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
323
324
        $sql       = '   SELECT b.*, i.instanceid ' . '   FROM ' . $xoopsDB->prefix('block_instance') . ' AS i LEFT JOIN ' . $xoopsDB->prefix('newblocks_bak') . ' AS b ON b.bid = i.bid ' . '   GROUP BY b.dirname, b.bid, i.instanceid';
325
        $result    = $xoopsDB->query($sql);
326
        $dirname   = '';
327
        $bid       = 0;
328
        $block_key = null;
329
        while ($row = $xoopsDB->fetchArray($result)) {
330 View Code Duplication
            if ($row['dirname'] != $dirname) {
331
                $dirname    = $row['dirname'];
332
                $modversion = array();
333
                if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') {
334
                    continue;
335
                }
336
            }
337
            if (empty($modversion['blocks']) && $dirname !== 'system') {
338
                continue;
339
            }
340
341
            $isClone = true;
342 View Code Duplication
            if ($row['bid'] != $bid) {
343
                $bid       = $row['bid'];
344
                $isClone   = false;
345
                $block_key = null;
0 ignored issues
show
Unused Code introduced by
$block_key is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
346
                $block_key = @$this->_block_lookup($row, $modversion['blocks']);
347
            }
348
            if ($block_key === null) {
349
                continue;
350
            }
351
352
            // Copy data from block instance table and blocks table
353
            $sql = '    INSERT INTO ' . $xoopsDB->prefix('newblocks') . '        (bid, mid, options, name, title, side, weight, visible, ' . '            func_num, ' . '            block_type, ' . '           c_type, ' . '            isactive, dirname, func_file,' . '            show_func, edit_func, template, bcachetime, last_modified)' . '    SELECT ' . '        i.instanceid, c.mid, i.options, c.name, i.title, i.side, i.weight, i.visible, ' . "        {$block_key}, " . ($isClone ? " CASE WHEN c.show_func='b_system_custom_show' THEN 'C' ELSE 'D' END," : " CASE WHEN c.show_func='b_system_custom_show' THEN 'C' WHEN c.mid = 1 THEN 'S' ELSE 'M' END,") . "        CASE WHEN c.c_type='' THEN 'H' ELSE c.c_type END," . '        c.isactive, c.dirname, c.func_file,' . '        c.show_func, c.edit_func, c.template, i.bcachetime, c.last_modified' . '    FROM ' . $xoopsDB->prefix('block_instance') . ' AS i,' . '        ' . $xoopsDB->prefix('newblocks_bak') . ' AS c' . '    WHERE i.bid = c.bid' . '        AND i.instanceid = ' . $row['instanceid'];
354
            $xoopsDB->queryF($sql);
355
        }
356
357
        $sql = '   SELECT b.* ' . '   FROM ' . $xoopsDB->prefix('newblocks_bak') . ' AS b LEFT JOIN ' . $xoopsDB->prefix('block_instance') . ' AS i ON b.bid = i.bid ' . '   WHERE i.instanceid IS NULL';
358
        '   GROUP BY b.dirname, b.bid';
359
        $result    = $xoopsDB->query($sql);
360
        $dirname   = '';
361
        $bid       = 0;
362
        $block_key = null;
363
        while ($row = $xoopsDB->fetchArray($result)) {
364 View Code Duplication
            if ($row['dirname'] != $dirname) {
365
                $dirname    = $row['dirname'];
366
                $modversion = array();
367
                if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') {
368
                    continue;
369
                }
370
            }
371
            if (empty($modversion['blocks']) && $dirname !== 'system') {
372
                continue;
373
            }
374
375 View Code Duplication
            if ($row['bid'] != $bid) {
376
                $bid       = $row['bid'];
377
                $block_key = null;
0 ignored issues
show
Unused Code introduced by
$block_key is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
378
                $block_key = @$this->_block_lookup($row, $modversion['blocks']);
379
            }
380
            if ($block_key === null) {
381
                continue;
382
            }
383
384
            // Copy data from blocks table
385
            $sql = '    INSERT INTO ' . $xoopsDB->prefix('newblocks') . '        (bid, mid, options, name, title, side, weight, visible, ' . '            func_num, ' . '            block_type, ' . '           c_type, ' . '            isactive, dirname, func_file,' . '            show_func, edit_func, template, bcachetime, last_modified)' . '    SELECT ' . "        bid + {$MaxInstanceId}, mid, options, name, name, 0, 0, 0, " . "        {$block_key}, " . "        CASE WHEN show_func='b_system_custom_show' THEN 'C' WHEN mid = 1 THEN 'S' ELSE 'M' END," . "        CASE WHEN c_type='' THEN 'H' ELSE c_type END," . '        isactive, dirname, func_file,' . '        show_func, edit_func, template, 0, last_modified' . '    FROM ' . $xoopsDB->prefix('newblocks_bak') . '    WHERE bid = ' . $row['bid'];
386
            $xoopsDB->queryF($sql);
387
388
            // Build block-module link
389
            $sql = '    INSERT INTO ' . $xoopsDB->prefix('block_module_link') . '        (block_id, module_id)' . '    SELECT ' . "        bid + {$MaxInstanceId}, -1" . '    FROM ' . $xoopsDB->prefix('newblocks_bak') . '    WHERE bid = ' . $row['bid'];
390
            $xoopsDB->queryF($sql);
391
        }
392
393
        // Dealing with tables
394
        $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('block_instance') . '`;');
395
        $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('newblocks_bak') . '`;');
396
397
        // Deal with custom blocks, convert options to type and content
398
        $sql    = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func='b_system_custom_show'";
399
        $result = $xoopsDB->query($sql);
400
        while (list($bid, $options) = $xoopsDB->fetchRow($result)) {
401
            $_options = unserialize($options);
402
            $content  = $_options[0];
403
            $type     = $_options[1];
404
            $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('newblocks') . "` SET c_type = '{$type}', options = '', content = " . $xoopsDB->quote($content) . " WHERE bid = {$bid}");
405
        }
406
407
        // Deal with block options, convert array values to "," and "|" delimited
408
        $sql    = 'UPDATE `' . $xoopsDB->prefix('newblocks') . "` SET options = '' WHERE show_func <> 'b_system_custom_show' AND ( options = 'a:1:{i:0;s:0:\"\";}' OR options = 'a:0:{}' )";
409
        $result = $xoopsDB->queryF($sql);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
410
        $sql    = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func <> 'b_system_custom_show' AND options <> ''";
411
        $result = $xoopsDB->query($sql);
412
        while (list($bid, $_options) = $xoopsDB->fetchRow($result)) {
413
            $options = unserialize($_options);
414
            if (empty($options) || !is_array($options)) {
415
                $options = array();
416
            }
417
            $count = count($options);
418
            //Convert array values to comma-separated
419 View Code Duplication
            for ($i = 0; $i < $count; ++$i) {
420
                if (is_array($options[$i])) {
421
                    $options[$i] = implode(',', $options[$i]);
422
                }
423
            }
424
            $options = implode('|', $options);
425
            $sql     = 'UPDATE `' . $xoopsDB->prefix('newblocks') . '` SET options = ' . $xoopsDB->quote($options) . " WHERE bid = {$bid}";
426
            $xoopsDB->queryF($sql);
427
        }
428
429
        return true;
430
    }
431
}
432
433
$upg = new Upgrade_220();
434
return $upg;
435