Upgrade_255   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 41
dl 0
loc 99
rs 10
c 3
b 1
f 0
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
B apply_keys() 0 25 7
A check_imptotal() 0 10 2
A apply_imptotal() 0 8 2
A __construct() 0 4 1
A check_keys() 0 22 6
1
<?php
2
3
/**
4
 * Upgrader from 2.5.4 to 2.5.5
5
 *
6
 * See the enclosed file license.txt for licensing information.
7
 * If you did not receive this file, get it at https://www.gnu.org/licenses/gpl-2.0.html
8
 *
9
 * @copyright    (c) 2000-2016 XOOPS Project (www.xoops.org)
10
 * @license          GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
11
 * @package          upgrader
12
 * @since            2.5.5
13
 * @author           Taiwen Jiang <[email protected]>
14
 * @author           trabis <[email protected]>
15
 */
16
class Upgrade_255 extends XoopsUpgrade
17
{
18
    /**
19
     * Check if keys already exist
20
     *
21
     * @return bool
22
     */
23
    public function check_keys()
24
    {
25
        $tables['groups_users_link'] = array('uid');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$tables was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tables = array(); before regardless.
Loading history...
26
27
        foreach ($tables as $table => $keys) {
28
            $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix($table) . '`';
29
            $result = $GLOBALS['xoopsDB']->queryF($sql);
30
            if (!$GLOBALS['xoopsDB']->isResultSet($result)) {
31
                continue;
32
            }
33
            $existing_keys = array();
34
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
35
                $existing_keys[] = $row['Key_name'];
36
            }
37
            foreach ($keys as $key) {
38
                if (!in_array($key, $existing_keys)) {
39
                    return false;
40
                }
41
            }
42
        }
43
44
        return true;
45
    }
46
47
    /**
48
     * Apply keys that are missing
49
     *
50
     * @return bool
51
     */
52
    public function apply_keys()
53
    {
54
        $tables['groups_users_link'] = array('uid');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$tables was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tables = array(); before regardless.
Loading history...
55
56
        foreach ($tables as $table => $keys) {
57
            $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix($table) . '`';
58
            $result = $GLOBALS['xoopsDB']->queryF($sql);
59
            if (!$GLOBALS['xoopsDB']->isResultSet($result)) {
60
                continue;
61
            }
62
            $existing_keys = array();
63
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
64
                $existing_keys[] = $row['Key_name'];
65
            }
66
            foreach ($keys as $key) {
67
                if (!in_array($key, $existing_keys)) {
68
                    $sql = 'ALTER TABLE `' . $GLOBALS['xoopsDB']->prefix($table) . "` ADD INDEX `{$key}` (`{$key}`)";
69
                    if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
70
                        return false;
71
                    }
72
                }
73
            }
74
        }
75
76
        return true;
77
    }
78
79
    /**
80
     * Check imptotal
81
     *
82
     * @return bool
83
     */
84
    public function check_imptotal()
85
    {
86
        $sql = 'SELECT `imptotal` FROM `' . $GLOBALS['xoopsDB']->prefix('banner') . '` WHERE `bid` = 1';
87
        if ($result = $GLOBALS['xoopsDB']->queryF($sql)) {
88
            $fieldInfo = mysqli_fetch_field_direct($result, 0);
89
            $length = $fieldInfo->length;
90
91
            return ($length != 8);
92
        }
93
        return null;
94
    }
95
96
    /**
97
     * Apply imptotal
98
     *
99
     * @return bool
100
     */
101
    public function apply_imptotal()
102
    {
103
        $sql = 'ALTER TABLE `' . $GLOBALS['xoopsDB']->prefix('banner') . "` CHANGE `imptotal` `imptotal` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0'";
104
        if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
105
            return false;
106
        }
107
108
        return true;
109
    }
110
111
    public function __construct()
112
    {
113
        parent::__construct(basename(__DIR__));
114
        $this->tasks = array('keys', 'imptotal');
115
    }
116
}
117
118
$upg = new Upgrade_255();
119
return $upg;
120