Upgrade_220::apply_block()   F
last analyzed

Complexity

Conditions 27
Paths 3282

Size

Total Lines 189
Code Lines 104

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 27
eloc 104
nc 3282
nop 0
dl 0
loc 189
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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 (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package             upgrader
16
 * @since               2.3.0
17
 * @author              Taiwen Jiang <[email protected]>
18
 */
19
class Upgrade_220 extends XoopsUpgrade
20
{
21
    public function __construct()
22
    {
23
        parent::__construct(basename(__DIR__));
24
        $this->tasks = array('config', 'profile', 'block'/*, 'pm', 'module'*/);
25
    }
26
27
    /**
28
     * Check if config category already removed
29
     *
30
     */
31
    public function check_config()
32
    {
33
        $sql    = 'SHOW COLUMNS FROM `' . $GLOBALS['xoopsDB']->prefix('configcategory') . "` LIKE 'confcat_modid'";
34
        $result = $GLOBALS['xoopsDB']->queryF($sql);
35
        if (!$GLOBALS['xoopsDB']->isResultSet($result)) {
36
            return true;
37
        }
38
39
        return !($GLOBALS['xoopsDB']->getRowsNum($result) > 0);
40
    }
41
42
    /**
43
     * Check if user profile table already converted
44
     *
45
     */
46
    public function check_profile()
47
    {
48
        /** @var XoopsModuleHandler $module_handler */
49
        $module_handler = xoops_getHandler('module');
50
        if (!$profile_module = $module_handler->getByDirname('profile')) {
0 ignored issues
show
Unused Code introduced by
The assignment to $profile_module is dead and can be removed.
Loading history...
51
            return true;
52
        }
53
        $sql    = 'SHOW COLUMNS FROM ' . $GLOBALS['xoopsDB']->prefix('users') . " LIKE 'posts'";
54
        $result = $GLOBALS['xoopsDB']->queryF($sql);
55
        if (!$GLOBALS['xoopsDB']->isResultSet($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 (!$GLOBALS['xoopsDB']->isResultSet($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;
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
        /** @var XoopsModuleHandler $module_handler */
147
        $module_handler = xoops_getHandler('module');
148
        /** @var XoopsConfigHandler $config_handler */
149
        $config_handler = xoops_getHandler('config');
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')));
0 ignored issues
show
Bug introduced by
It seems like $profile_module->getVar('mid') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
            $profile_config = $config_handler->getConfigs(new Criteria('conf_modid', /** @scrutinizer ignore-type */ $profile_module->getVar('mid')));
Loading history...
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
        $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)");
159
160
        //Rebuild user configuration options
161
        $criteria = new CriteriaCompo(new Criteria('conf_name', "('activation_type', 'uname_test_level')", 'IN'));
162
        $criteria->add(new Criteria('conf_modid', 0));
163
        $criteria->setSort('conf_name');
164
        $criteria->setOrder('ASC');
165
        $configs             = $config_handler->getConfigs($criteria);
166
        $id_activation_type  = $configs[0]->getVar('conf_id');
167
        $id_uname_test_level = $configs[1]->getVar('conf_id');
168
        $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})");
169
170
        return $result;
171
    }
172
173
    /**
174
     * @return bool
175
     */
176
    public function apply_profile()
177
    {
178
        global $xoopsDB;
179
        // Restore users table
180
        $xoopsDB->queryF('ALTER TABLE `' . $xoopsDB->prefix('users') . "`
181
              ADD url varchar(100) NOT NULL default '',
182
              ADD user_regdate int(10) unsigned NOT NULL default '0',
183
              ADD user_icq varchar(15) NOT NULL default '',
184
              ADD user_from varchar(100) NOT NULL default '',
185
              ADD user_sig tinytext,
186
              ADD user_viewemail tinyint(1) unsigned NOT NULL default '0',
187
              ADD actkey varchar(8) NOT NULL default '',
188
              ADD user_aim varchar(18) NOT NULL default '',
189
              ADD user_yim varchar(25) NOT NULL default '',
190
              ADD user_msnm varchar(100) NOT NULL default '',
191
              ADD posts mediumint(8) unsigned NOT NULL default '0',
192
              ADD attachsig tinyint(1) unsigned NOT NULL default '0',
193
              ADD theme varchar(100) NOT NULL default '',
194
              ADD timezone_offset float(3,1) NOT NULL default '0.0',
195
              ADD last_login int(10) unsigned NOT NULL default '0',
196
              ADD umode varchar(10) NOT NULL default '',
197
              ADD uorder tinyint(1) unsigned NOT NULL default '0',
198
              ADD notify_method tinyint(1) NOT NULL default '1',
199
              ADD notify_mode tinyint(1) NOT NULL default '0',
200
              ADD user_occ varchar(100) NOT NULL default '',
201
              ADD bio tinytext,
202
              ADD user_intrest varchar(150) NOT NULL default '',
203
              ADD user_mailok tinyint(1) unsigned NOT NULL default '1'
204
              ");
205
206
        // Copy data from profile table
207
        $profile_fields = array(
208
            'url',
209
            'user_regdate',
210
            'user_icq',
211
            'user_from',
212
            'user_sig',
213
            'user_viewemail',
214
            'actkey',
215
            'user_aim',
216
            'user_yim',
217
            'user_msnm',
218
            'posts',
219
            'attachsig',
220
            'theme',
221
            'timezone_offset',
222
            'last_login',
223
            'umode',
224
            'uorder',
225
            'notify_method',
226
            'notify_mode',
227
            'user_occ',
228
            'bio',
229
            'user_intrest',
230
            'user_mailok');
231
        foreach ($profile_fields as $field) {
232
            $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('users') . '` u, `' . $xoopsDB->prefix('user_profile') . "` p SET u.{$field} = p.{$field} WHERE u.uid=p.profileid");
233
        }
234
235
        //Set display name as real name
236
        $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('users') . "` SET name=uname WHERE name=''");
237
        //Set loginname as uname
238
        $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('users') . '` SET uname=loginname');
239
        //Drop loginname
240
        $xoopsDB->queryF('ALTER TABLE `' . $xoopsDB->prefix('users') . '` DROP loginname');
241
242
        return true;
243
    }
244
245
    /**
246
     * @param $block
247
     * @param $blocks
248
     *
249
     * @return int|null|string
250
     */
251
    public function _block_lookup($block, $blocks)
252
    {
253
        if ($block['show_func'] === 'b_system_custom_show') {
254
            return 0;
255
        }
256
257
        foreach ($blocks as $key => $bk) {
258
            if ($block['show_func'] == $bk['show_func'] && $block['edit_func'] == $bk['edit_func'] && $block['template'] == $bk['template']) {
259
                return $key;
260
            }
261
        }
262
263
        return null;
264
    }
265
266
    /**
267
     * @return bool
268
     */
269
    public function apply_block()
270
    {
271
        global $xoopsDB;
272
        $xoopsDB->queryF('UPDATE ' . $xoopsDB->prefix('block_module_link') . ' SET module_id = -1, pageid = 0 WHERE module_id < 2 AND pageid = 1');
273
274
        //Change block module link to remove pages
275
        //Remove page links for module subpages
276
        $xoopsDB->queryF('DELETE FROM ' . $xoopsDB->prefix('block_module_link') . ' WHERE pageid > 0');
277
278
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP PRIMARY KEY';
279
        $xoopsDB->queryF($sql);
280
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP pageid';
281
        $xoopsDB->queryF($sql);
282
        $sql = 'ALTER IGNORE TABLE `' . $xoopsDB->prefix('block_module_link') . '` ADD PRIMARY KEY (`block_id` , `module_id`)';
283
        $xoopsDB->queryF($sql);
284
285
        $xoopsDB->queryF('RENAME TABLE `' . $xoopsDB->prefix('newblocks') . '` TO `' . $xoopsDB->prefix('newblocks_bak') . '`');
286
287
        // Create new block table
288
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('newblocks') . " (
289
              bid mediumint(8) unsigned NOT NULL auto_increment,
290
              mid smallint(5) unsigned NOT NULL default '0',
291
              func_num tinyint(3) unsigned NOT NULL default '0',
292
              options varchar(255) NOT NULL default '',
293
              name varchar(150) NOT NULL default '',
294
              title varchar(255) NOT NULL default '',
295
              content text,
296
              side tinyint(1) unsigned NOT NULL default '0',
297
              weight smallint(5) unsigned NOT NULL default '0',
298
              visible tinyint(1) unsigned NOT NULL default '0',
299
              block_type char(1) NOT NULL default '',
300
              c_type char(1) NOT NULL default '',
301
              isactive tinyint(1) unsigned NOT NULL default '0',
302
              dirname varchar(50) NOT NULL default '',
303
              func_file varchar(50) NOT NULL default '',
304
              show_func varchar(50) NOT NULL default '',
305
              edit_func varchar(50) NOT NULL default '',
306
              template varchar(50) NOT NULL default '',
307
              bcachetime int(10) unsigned NOT NULL default '0',
308
              last_modified int(10) unsigned NOT NULL default '0',
309
              PRIMARY KEY  (bid),
310
              KEY `mid` (mid),
311
              KEY visible (visible),
312
              KEY isactive_visible_mid (isactive,visible,mid),
313
              KEY mid_funcnum (mid,func_num)
314
            ) TYPE=MyISAM;
315
            ";
316
        $xoopsDB->queryF($sql);
317
318
        $sql    = '   SELECT MAX(instanceid) FROM ' . $xoopsDB->prefix('block_instance');
319
        $result = $xoopsDB->query($sql);
320
        if (!$xoopsDB->isResultSet($result)) {
321
            throw new \RuntimeException(
322
                \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR
323
            );
324
        }
325
326
        list($MaxInstanceId) = $xoopsDB->fetchRow($result);
327
328
        // Change custom block mid from 1 to 0
329
        $sql    = 'UPDATE `' . $xoopsDB->prefix('newblocks_bak') . "` SET mid = 0 WHERE show_func = 'b_system_custom_show'";
330
        $result = $xoopsDB->queryF($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
331
332
        $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';
333
        $result = $xoopsDB->query($sql);
334
        if (!$xoopsDB->isResultSet($result)) {
335
            throw new \RuntimeException(
336
                \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR
337
            );
338
        }
339
        $dirname   = '';
340
        $bid       = 0;
341
        $block_key = null;
342
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
343
            if ($row['dirname'] != $dirname) {
344
                $dirname    = $row['dirname'];
345
                $modversion = array();
346
                if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') {
347
                    continue;
348
                }
349
            }
350
            if (empty($modversion['blocks']) && $dirname !== 'system') {
351
                continue;
352
            }
353
354
            $isClone = true;
355
            if ($row['bid'] != $bid) {
356
                $bid       = $row['bid'];
357
                $isClone   = false;
358
                $block_key = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $block_key is dead and can be removed.
Loading history...
359
                $block_key = @$this->_block_lookup($row, $modversion['blocks']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modversion does not seem to be defined for all execution paths leading up to this point.
Loading history...
360
            }
361
            if ($block_key === null) {
362
                continue;
363
            }
364
365
            // Copy data from block instance table and blocks table
366
            $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'];
367
            $xoopsDB->queryF($sql);
368
        }
369
370
        $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';
371
        '   GROUP BY b.dirname, b.bid';
372
        $result = $xoopsDB->query($sql);
373
        if (!$xoopsDB->isResultSet($result)) {
374
            throw new \RuntimeException(
375
                \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR
376
            );
377
        }
378
        $dirname   = '';
379
        $bid       = 0;
380
        $block_key = null;
381
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
382
            if ($row['dirname'] != $dirname) {
383
                $dirname    = $row['dirname'];
384
                $modversion = array();
385
                if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') {
386
                    continue;
387
                }
388
            }
389
            if (empty($modversion['blocks']) && $dirname !== 'system') {
390
                continue;
391
            }
392
393
            if ($row['bid'] != $bid) {
394
                $bid       = $row['bid'];
395
                $block_key = null;
396
                $block_key = @$this->_block_lookup($row, $modversion['blocks']);
397
            }
398
            if ($block_key === null) {
399
                continue;
400
            }
401
402
            // Copy data from blocks table
403
            $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'];
404
            $xoopsDB->queryF($sql);
405
406
            // Build block-module link
407
            $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'];
408
            $xoopsDB->queryF($sql);
409
        }
410
411
        // Dealing with tables
412
        $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('block_instance') . '`;');
413
        $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('newblocks_bak') . '`;');
414
415
        // Deal with custom blocks, convert options to type and content
416
        $sql    = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func='b_system_custom_show'";
417
        $result = $xoopsDB->query($sql);
418
        if (!$xoopsDB->isResultSet($result)) {
419
            throw new \RuntimeException(
420
                \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR
421
            );
422
        }
423
        while (false !== (list($bid, $options) = $xoopsDB->fetchRow($result))) {
424
            $_options = unserialize($options);
425
            $content  = $_options[0];
426
            $type     = $_options[1];
427
            $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('newblocks') . "` SET c_type = '{$type}', options = '', content = " . $xoopsDB->quote($content) . " WHERE bid = {$bid}");
428
        }
429
430
        // Deal with block options, convert array values to "," and "|" delimited
431
        $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:{}' )";
432
        $result = $xoopsDB->queryF($sql);
433
        $sql    = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func <> 'b_system_custom_show' AND options <> ''";
434
        $result = $xoopsDB->query($sql);
435
        if (!$xoopsDB->isResultSet($result)) {
436
            throw new \RuntimeException(
437
                \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR
438
            );
439
        }
440
        while (false !== (list($bid, $_options) = $xoopsDB->fetchRow($result))) {
441
            $options = unserialize($_options);
442
            if (empty($options) || !is_array($options)) {
443
                $options = array();
444
            }
445
            $count = count($options);
446
            //Convert array values to comma-separated
447
            for ($i = 0; $i < $count; ++$i) {
448
                if (is_array($options[$i])) {
449
                    $options[$i] = implode(',', $options[$i]);
450
                }
451
            }
452
            $options = implode('|', $options);
453
            $sql     = 'UPDATE `' . $xoopsDB->prefix('newblocks') . '` SET options = ' . $xoopsDB->quote($options) . " WHERE bid = {$bid}";
454
            $xoopsDB->queryF($sql);
455
        }
456
457
        return true;
458
    }
459
}
460
461
$upg = new Upgrade_220();
462
return $upg;
463