Passed
Push — master ( 0deb8f...f54565 )
by Goffy
04:13
created

wggithub_check_db()   C

Complexity

Conditions 11
Paths 243

Size

Total Lines 83
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 51
nc 243
nop 1
dl 0
loc 83
rs 5.8257
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
 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
 * Wggithub module for xoops
14
 *
15
 * @param mixed      $module
16
 * @param null|mixed $prev_version
17
 * @package        Wggithub
18
 * @since          1.0
19
 * @min_xoops      2.5.9
20
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
21
 * @version        $Id: 1.0 update.php 1 Mon 2018-03-19 10:04:53Z XOOPS Project (www.xoops.org) $
22
 * @copyright      module for xoops
23
 * @license        GPL 2.0 or later
24
 */
25
26
/**
27
 * @param      $module
28
 * @param null $prev_version
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $prev_version is correct as it would always require null to be passed?
Loading history...
29
 *
30
 * @return bool|null
31
 */
32
function xoops_module_update_wggithub($module, $prev_version = null)
33
{
34
    $ret = null;
35
    if ($prev_version < 10) {
36
        $ret = update_wggithub_v10($module);
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
37
    }
38
39
    $ret = wggithub_check_db($module);
40
41
    //check upload directory
42
	include_once __DIR__ . '/install.php';
43
    $ret = xoops_module_install_wggithub($module);
44
45
    $errors = $module->getErrors();
46
    if (!empty($errors)) {
47
        print_r($errors);
48
    }
49
50
    return $ret;
51
52
}
53
54
// irmtfan bug fix: solve templates duplicate issue
55
/**
56
 * @param $module
57
 *
58
 * @return bool
59
 */
60
function update_wggithub_v10($module)
61
{
62
    global $xoopsDB;
63
    $result = $xoopsDB->query(
64
        'SELECT t1.tpl_id FROM ' . $xoopsDB->prefix('tplfile') . ' t1, ' . $xoopsDB->prefix('tplfile') . ' t2 WHERE t1.tpl_refid = t2.tpl_refid AND t1.tpl_module = t2.tpl_module AND t1.tpl_tplset=t2.tpl_tplset AND t1.tpl_file = t2.tpl_file AND t1.tpl_type = t2.tpl_type AND t1.tpl_id > t2.tpl_id'
65
    );
66
    $tplids = [];
67
    while (false !== (list($tplid) = $xoopsDB->fetchRow($result))) {
68
        $tplids[] = $tplid;
69
    }
70
    if (\count($tplids) > 0) {
71
        $tplfileHandler  = \xoops_getHandler('tplfile');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

71
        $tplfileHandler  = /** @scrutinizer ignore-call */ \xoops_getHandler('tplfile');
Loading history...
72
        $duplicate_files = $tplfileHandler->getObjects(new \Criteria('tpl_id', '(' . \implode(',', $tplids) . ')', 'IN'));
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
74
        if (\count($duplicate_files) > 0) {
75
            foreach (\array_keys($duplicate_files) as $i) {
76
                $tplfileHandler->delete($duplicate_files[$i]);
77
            }
78
        }
79
    }
80
    $sql = 'SHOW INDEX FROM ' . $xoopsDB->prefix('tplfile') . " WHERE KEY_NAME = 'tpl_refid_module_set_file_type'";
81
    if (!$result = $xoopsDB->queryF($sql)) {
82
        xoops_error($xoopsDB->error() . '<br>' . $sql);
0 ignored issues
show
Bug introduced by
The function xoops_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

82
        /** @scrutinizer ignore-call */ 
83
        xoops_error($xoopsDB->error() . '<br>' . $sql);
Loading history...
83
84
        return false;
85
    }
86
    $ret = [];
87
    while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
88
        $ret[] = $myrow;
89
    }
90
    if (!empty($ret)) {
91
        $module->setErrors("'tpl_refid_module_set_file_type' unique index is exist. Note: check 'tplfile' table to be sure this index is UNIQUE because XOOPS CORE need it.");
92
93
        return true;
94
    }
95
    $sql = 'ALTER TABLE ' . $xoopsDB->prefix('tplfile') . ' ADD UNIQUE tpl_refid_module_set_file_type ( tpl_refid, tpl_module, tpl_tplset, tpl_file, tpl_type )';
96
    if (!$result = $xoopsDB->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
97
        xoops_error($xoopsDB->error() . '<br>' . $sql);
98
        $module->setErrors("'tpl_refid_module_set_file_type' unique index is not added to 'tplfile' table. Warning: do not use XOOPS until you add this unique index.");
99
100
        return false;
101
    }
102
103
    return true;
104
}
105
106
// irmtfan bug fix: solve templates duplicate issue
107
108
/**
109
 * @param $module
110
 *
111
 * @return bool
112
 */
113
function wggithub_check_db($module)
114
{
115
    $ret = true;
116
	//insert here code for database check
117
118
    // Example: update table (add new field)
119
    $table   = $GLOBALS['xoopsDB']->prefix('wggithub_repositories');
120
    $field   = 'repo_release';
121
    $check   = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'");
122
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
123
    if (!$numRows) {
124
        $sql = "ALTER TABLE `$table` ADD `$field` INT(1) NOT NULL DEFAULT '0' AFTER `repo_htmlurl`;";
125
        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...
126
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
0 ignored issues
show
Bug introduced by
The function xoops_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

126
            /** @scrutinizer ignore-call */ 
127
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
Loading history...
127
            $module->setErrors("Error when adding '$field' to table '$table'.");
128
            $ret = false;
129
        }
130
    }
131
132
    // Example: update table (add new field)
133
    $table   = $GLOBALS['xoopsDB']->prefix('wggithub_repositories');
134
    $field   = 'repo_prerelease';
135
    $check   = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'");
136
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
137
    if (!$numRows) {
138
        $sql = "ALTER TABLE `$table` ADD `$field` INT(1) NOT NULL DEFAULT '0' AFTER `repo_htmlurl`;";
139
        if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
140
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
141
            $module->setErrors("Error when adding '$field' to table '$table'.");
142
            $ret = false;
143
        }
144
    }
145
    // Example: update table (add new field)
146
    $table   = $GLOBALS['xoopsDB']->prefix('wggithub_repositories');
147
    $field   = 'repo_readme';
148
    $check   = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'");
149
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
150
    if (!$numRows) {
151
        $sql = "ALTER TABLE `$table` ADD `$field` INT(1) NOT NULL DEFAULT '0' AFTER `repo_htmlurl`;";
152
        if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
153
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
154
            $module->setErrors("Error when adding '$field' to table '$table'.");
155
            $ret = false;
156
        }
157
    }
158
159
// Example: update table (add new field)
160
    $table   = $GLOBALS['xoopsDB']->prefix('wggithub_directories');
161
    $field   = 'dir_filterrelease';
162
    $check   = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'");
163
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
164
    if (!$numRows) {
165
        $sql = "ALTER TABLE `$table` ADD `$field` INT(1) NOT NULL DEFAULT '0' AFTER `dir_online`;";
166
        if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
167
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
168
            $module->setErrors("Error when adding '$field' to table '$table'.");
169
            $ret = false;
170
        }
171
    }
172
173
    // Example: create new table
174
    $table   = $GLOBALS['xoopsDB']->prefix('wggithub_logs');
175
    $check   = $GLOBALS['xoopsDB']->queryF("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='$table'");
176
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
177
    if (!$numRows) {
178
        // create new table
179
        $sql = "CREATE TABLE `$table` (
180
              `log_id` INT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
181
              `log_type` INT(1) NOT NULL DEFAULT '0',
182
              `log_details` VARCHAR(255) NOT NULL DEFAULT '',
183
              `log_result` TEXT NOT NULL ,
184
              `log_datecreated` INT(11) NOT NULL DEFAULT '0',
185
              `log_submitter` INT(10) NOT NULL DEFAULT '0',
186
              PRIMARY KEY (`log_id`)
187
                ) ENGINE=InnoDB;";
188
        if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
189
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
190
            $module->setErrors("Error when creating table '$table'.");
191
            $ret = false;
192
        }
193
    }
194
195
    return $ret;
196
}
197