Completed
Pull Request — master (#568)
by Michael
06:11
created

upgrade_240::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * Upgrader from 2.3.3 to 2.4.0
14
 * See the enclosed file license.txt for licensing information.
15
 * If you did not receive this file, get it at http://www.fsf.org/copyleft/gpl.html
16
 *
17
 * @copyright   The XOOPS project http://www.xoops.org/
18
 * @license     http://www.fsf.org/copyleft/gpl.html GNU General Public License (GPL)
19
 * @package     upgrader
20
 * @since       2.4.0
21
 * @author      Taiwen Jiang <[email protected]>
22
 * @author      trabis <[email protected]>
23
 * @version     $Id$
24
 */
25
26
class upgrade_240 extends xoopsUpgrade
27
{
28
    public $tasks = array('keys');
29
30
    /**
31
     * Check if keys already exist
32
33
     */
34
    public function check_keys()
35
    {
36
        $xoops = Xoops::getInstance();
37
        $db = $xoops->db();
0 ignored issues
show
Unused Code introduced by
The assignment to $db is dead and can be removed.
Loading history...
38
        $tables['modules'] = array('isactive', 'weight', 'hascomments');
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...
39
        $tables['users'] = array('level');
40
        $tables['online'] = array('online_updated', 'online_uid');
41
        $tables['config'] = array('conf_order');
42
        $tables['xoopscomments'] = array('com_status');
43
44
        foreach ($tables as $table => $keys) {
45
            $sql = "SHOW KEYS FROM `" . $xoops->db()->prefix($table) . "`";
46
            if (!$result = $xoops->db()->queryF($sql)) {
0 ignored issues
show
Bug introduced by
The method queryF() does not exist on Xoops\Core\Database\Connection. Did you maybe mean queryFromFile()? ( Ignorable by Annotation )

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

46
            if (!$result = $xoops->db()->/** @scrutinizer ignore-call */ queryF($sql)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
                continue;
48
            }
49
            $existing_keys = array();
50
            while ($row = $xoops->db()->fetchArray($result)) {
51
                $existing_keys[] = $row['Key_name'];
52
            }
53
            foreach ($keys as $key) {
54
                if (!in_array($key, $existing_keys)) {
55
                    return false;
56
                }
57
            }
58
        }
59
        return true;
60
    }
61
62
    /**
63
     * Apply keys that are missing
64
65
     */
66
    public function apply_keys()
67
    {
68
        $xoops = Xoops::getInstance();
69
        $db = $xoops->db();
0 ignored issues
show
Unused Code introduced by
The assignment to $db is dead and can be removed.
Loading history...
70
        $tables['modules'] = array('isactive', 'weight', 'hascomments');
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...
71
        $tables['users'] = array('level');
72
        $tables['online'] = array('online_updated', 'online_uid');
73
        $tables['config'] = array('conf_order');
74
        $tables['xoopscomments'] = array('com_status');
75
76
        foreach ($tables as $table => $keys) {
77
            $sql = "SHOW KEYS FROM `" . $xoops->db()->prefix($table) . "`";
78
            if (!$result = $xoops->db()->queryF($sql)) {
79
                continue;
80
            }
81
            $existing_keys = array();
82
            while ($row = $xoops->db()->fetchArray($result)) {
83
                $existing_keys[] = $row['Key_name'];
84
            }
85
            foreach ($keys as $key) {
86
                if (!in_array($key, $existing_keys)) {
87
                    $sql = "ALTER TABLE `" . $xoops->db()->prefix($table) . "` ADD INDEX `{$key}` (`{$key}`)";
88
                    if (!$result = $xoops->db()->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
89
                        return false;
90
                    }
91
                }
92
            }
93
        }
94
        return true;
95
    }
96
97
    public function __construct()
98
    {
99
        xoopsUpgrade::__construct(basename(__DIR__));
0 ignored issues
show
Bug Best Practice introduced by
The method xoopsUpgrade::__construct() is not static, but was called statically. ( Ignorable by Annotation )

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

99
        xoopsUpgrade::/** @scrutinizer ignore-call */ 
100
                      __construct(basename(__DIR__));
Loading history...
100
    }
101
}
102
103
$upg = new upgrade_240();
104
return $upg;
105