xoops_module_update_newbb_v400()   F
last analyzed

Complexity

Conditions 18
Paths 7680

Size

Total Lines 139
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 74
nc 7680
nop 1
dl 0
loc 139
rs 0.7
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 declare(strict_types=1);
2
/**
3
 * NewBB 4.3x, the forum module for XOOPS project
4
 *
5
 * @copyright      XOOPS Project (https://xoops.org)
6
 * @license        GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
7
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
8
 * @since          4.00
9
 * @return bool
10
 */
11
12
use XoopsModules\Tag;
13
14
/**
15
 * @return true
16
 */
17
function xoops_module_update_newbb_v400(XoopsModule $module): bool
18
{
19
    $statsHandler = xoops_getModuleHandler('stats', 'newbb');
20
21
    $sql = 'SELECT `forum_id`, `forum_topics`, `forum_posts` FROM ' . $GLOBALS['xoopsDB']->prefix('bb_forums');
22
    $result = $GLOBALS['xoopsDB']->query($sql);
23
    if (!$GLOBALS['xoopsDB']->isResultSet($result)) {
24
        \trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), E_USER_ERROR);
25
    }
26
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
27
        $statsHandler->update($row['forum_id'], 'topic', $row['forum_topics']);
0 ignored issues
show
Bug introduced by
The method update() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

27
        $statsHandler->/** @scrutinizer ignore-call */ 
28
                       update($row['forum_id'], 'topic', $row['forum_topics']);
Loading history...
28
        $statsHandler->update($row['forum_id'], 'post', $row['forum_posts']);
29
    }
30
31
    $sql = 'SELECT `forum_id`, SUM(topic_views) AS views FROM ' . $GLOBALS['xoopsDB']->prefix('bb_topics') . ' GROUP BY `forum_id`';
32
    $result = $GLOBALS['xoopsDB']->query($sql);
33
    if (!$GLOBALS['xoopsDB']->isResultSet($result)) {
34
        \trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), E_USER_ERROR);
35
    }
36
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
37
        $statsHandler->update($row['forum_id'], 'view', $row['views']);
38
    }
39
40
    $sql = 'SELECT `forum_id`, COUNT(*) AS digests FROM ' . $GLOBALS['xoopsDB']->prefix('bb_topics') . ' WHERE topic_digest = 1 GROUP BY `forum_id`';
41
    $result = $GLOBALS['xoopsDB']->query($sql);
42
    if (!$GLOBALS['xoopsDB']->isResultSet($result)) {
43
        \trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), E_USER_ERROR);
44
    }
45
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
46
        $statsHandler->update($row['forum_id'], 'digest', $row['digests']);
47
    }
48
49
    $sql = 'SELECT SUM(forum_topics) AS topics, SUM(forum_posts) AS posts FROM ' . $GLOBALS['xoopsDB']->prefix('bb_forums');
50
    $result = $GLOBALS['xoopsDB']->query($sql);
51
    if (!$GLOBALS['xoopsDB']->isResultSet($result)) {
52
        \trigger_error("Query Failed! SQL: $sql- Error: " . $GLOBALS['xoopsDB']->error(), E_USER_ERROR);
53
    }
54
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
55
        $statsHandler->update(-1, 'topic', $row['topics']);
56
        $statsHandler->update(-1, 'post', $row['posts']);
57
    }
58
59
    /*
60
    $GLOBALS['xoopsDB']->queryF(
61
            "    INSERT INTO ".$GLOBALS['xoopsDB']->prefix("bb_stats").
62
            "        (`id`, `value`, `type`, `period`, `time_update`, `time_format`)".
63
            "    SELECT `forum_id`, `forum_topics`, '".NEWBB_STATS_TYPE_TOPIC."', '".NEWBB_STATS_PERIOD_TOTAL."', NOW() + 0, ''".
64
            "         FROM ".$GLOBALS['xoopsDB']->prefix("bb_forums")
65
            );
66
    $GLOBALS['xoopsDB']->queryF(
67
            "    INSERT INTO ".$GLOBALS['xoopsDB']->prefix("bb_stats").
68
            "        (`id`, `value`, `type`, `period`, `time_update`, `time_format`)".
69
            "    SELECT `forum_id`, `forum_posts`, '".NEWBB_STATS_TYPE_POST."', '".NEWBB_STATS_PERIOD_TOTAL."', NOW() + 0, ''".
70
            "         FROM ".$GLOBALS['xoopsDB']->prefix("bb_forums")
71
            );
72
    $GLOBALS['xoopsDB']->queryF(
73
            "    INSERT INTO ".$GLOBALS['xoopsDB']->prefix("bb_stats").
74
            "        (`id`, `value`, `type`, `period`, `time_update`, `time_format`)".
75
            "    SELECT `forum_id`, count(*), '".NEWBB_STATS_TYPE_DIGEST."', '".NEWBB_STATS_PERIOD_TOTAL."', NOW() + 0, ''".
76
            "         FROM ".$GLOBALS['xoopsDB']->prefix("bb_topics").
77
            "        WHERE topic_digest = 1".
78
            "        GROUP BY `forum_id`".
79
            );
80
    $GLOBALS['xoopsDB']->queryF(
81
            "    INSERT INTO ".$GLOBALS['xoopsDB']->prefix("bb_stats").
82
            "        (`id`, `value`, `type`, `period`, `time_update`, `time_format`)".
83
            "    SELECT `forum_id`, SUM(topic_views), '".NEWBB_STATS_TYPE_VIEW."', '".NEWBB_STATS_PERIOD_TOTAL."', NOW() + 0, ''".
84
            "         FROM ".$GLOBALS['xoopsDB']->prefix("bb_topics").
85
            "        WHERE topic_digest = 1".
86
            "        GROUP BY `forum_id`".
87
            );
88
    */
89
90
    $sql = '    UPDATE '
91
           . $GLOBALS['xoopsDB']->prefix('bb_posts_text')
92
           . ' AS t, '
93
           . $GLOBALS['xoopsDB']->prefix('bb_posts')
94
           . ' AS p'
95
           . '    SET t.dohtml = p.dohtml, '
96
           . '        t.dosmiley = p.dosmiley, '
97
           . '        t.doxcode = p.doxcode, '
98
           . '        t.doimage = p.doimage, '
99
           . '        t.dobr = p.dobr'
100
           . '    WHERE p.post_id =t.post_id ';
101
    if ($GLOBALS['xoopsDB']->queryF($sql)) {
102
        $sql = '    ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_posts') . '        DROP `dohtml`,' . '        DROP `dosmiley`,' . '        DROP `doxcode`,' . '        DROP `doimage`,' . '        DROP `dobr`';
103
        $GLOBALS['xoopsDB']->queryF($sql);
104
    } else {
105
        xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
106
    }
107
108
    if (\class_exists(\XoopsModules\Tag\TagHandler::class) && xoops_isActiveModule('tag')) {
109
        $tagHandler  = \XoopsModules\Tag\Helper::getInstance()
110
                                               ->getHandler('Tag');
111
        $table_topic = $GLOBALS['xoopsDB']->prefix('bb_topics');
112
        $sql    = '    SELECT topic_id, topic_tags' . "    FROM {$table_topic}";
113
        $result = $GLOBALS['xoopsDB']->query($sql);
114
        if ($GLOBALS['xoopsDB']->isResultSet($result)) {
115
            while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
116
                if (empty($myrow['topic_tags'])) {
117
                    continue;
118
                }
119
                $tagHandler->updateByItem($myrow['topic_tags'], $myrow['topic_id'], $module->getVar('mid'));
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('mid') can also be of type array and array; however, parameter $modid of XoopsModules\Tag\TagHandler::updateByItem() does only seem to accept integer|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

119
                $tagHandler->updateByItem($myrow['topic_tags'], $myrow['topic_id'], /** @scrutinizer ignore-type */ $module->getVar('mid'));
Loading history...
120
            }
121
        } else {
122
            xoops_error($GLOBALS['xoopsDB']->error());
123
            //            \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
124
125
        }
126
    }
127
128
    $sql = ' SELECT COUNT(*)
129
            FROM ' . $GLOBALS['xoopsDB']->prefix('bb_type_tmp') . ' AS a, ' . $GLOBALS['xoopsDB']->prefix('bb_type_forum_tmp') . ' AS b
130
            WHERE a.type_id = b.type_id AND a.type_id >0;';
131
    $result = $GLOBALS['xoopsDB']->query($sql);
132
    if (!$result) {
133
        //xoops_error($GLOBALS['xoopsDB']->error());
134
        $GLOBALS['xoopsDB']->queryF('DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_type_tmp'));
135
        $GLOBALS['xoopsDB']->queryF('DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_type_forum_tmp'));
136
137
        return true;
138
    }
139
140
    $sql = ' INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('bb_type') . '        (`type_id`, `type_name`, `type_color`)' . '    SELECT `type_id`, `type_name`, `type_color`' . '         FROM ' . $GLOBALS['xoopsDB']->prefix('bb_type_tmp');
141
    $result = $GLOBALS['xoopsDB']->queryF($sql);
142
    if (!$result){
143
    xoops_error($GLOBALS['xoopsDB']->error());
144
    }
145
146
    $sql = '    INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('bb_type_forum') . '        (`type_id`, `forum_id`, `type_order`)' . '    SELECT `type_id`, `forum_id`, `type_order`' . '         FROM ' . $GLOBALS['xoopsDB']->prefix('bb_type_forum_tmp');
147
    $result = $GLOBALS['xoopsDB']->queryF($sql);
148
    if (!$result){
149
        xoops_error($GLOBALS['xoopsDB']->error());
150
    }
151
152
    $GLOBALS['xoopsDB']->queryF('DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_type_tmp'));
153
    $GLOBALS['xoopsDB']->queryF('DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_type_forum_tmp'));
154
155
    return true;
156
}
157