Passed
Push — master ( 193549...c42bd6 )
by Michael
15:55 queued 12s
created

xoops_module_update_newbb_v400()   D

Complexity

Conditions 13
Paths 320

Size

Total Lines 115
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 56
nc 320
nop 1
dl 0
loc 115
rs 4.2833
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        https://www.fsf.org/copyleft/gpl.html GNU public license
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
function xoops_module_update_newbb_v400(XoopsModule $module)
15
{
16
    $statsHandler = xoops_getModuleHandler('stats', 'newbb');
17
18
    $result = $GLOBALS['xoopsDB']->query('SELECT `forum_id`, `forum_topics`, `forum_posts` FROM ' . $GLOBALS['xoopsDB']->prefix('bb_forums'));
19
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
20
        $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

20
        $statsHandler->/** @scrutinizer ignore-call */ 
21
                       update($row['forum_id'], 'topic', $row['forum_topics']);
Loading history...
21
        $statsHandler->update($row['forum_id'], 'post', $row['forum_posts']);
22
    }
23
    $result = $GLOBALS['xoopsDB']->query('SELECT `forum_id`, SUM(topic_views) AS views FROM ' . $GLOBALS['xoopsDB']->prefix('bb_topics') . ' GROUP BY `forum_id`');
24
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
25
        $statsHandler->update($row['forum_id'], 'view', $row['views']);
26
    }
27
    $result = $GLOBALS['xoopsDB']->query('SELECT `forum_id`, COUNT(*) AS digests FROM ' . $GLOBALS['xoopsDB']->prefix('bb_topics') . ' WHERE topic_digest = 1 GROUP BY `forum_id`');
28
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
29
        $statsHandler->update($row['forum_id'], 'digest', $row['digests']);
30
    }
31
    $result = $GLOBALS['xoopsDB']->query('SELECT SUM(forum_topics) AS topics, SUM(forum_posts) AS posts FROM ' . $GLOBALS['xoopsDB']->prefix('bb_forums'));
32
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
33
        $statsHandler->update(-1, 'topic', $row['topics']);
34
        $statsHandler->update(-1, 'post', $row['posts']);
35
    }
36
37
    /*
38
    $GLOBALS['xoopsDB']->queryF(
39
            "    INSERT INTO ".$GLOBALS['xoopsDB']->prefix("bb_stats").
40
            "        (`id`, `value`, `type`, `period`, `time_update`, `time_format`)".
41
            "    SELECT `forum_id`, `forum_topics`, '".NEWBB_STATS_TYPE_TOPIC."', '".NEWBB_STATS_PERIOD_TOTAL."', NOW() + 0, ''".
42
            "         FROM ".$GLOBALS['xoopsDB']->prefix("bb_forums")
43
            );
44
    $GLOBALS['xoopsDB']->queryF(
45
            "    INSERT INTO ".$GLOBALS['xoopsDB']->prefix("bb_stats").
46
            "        (`id`, `value`, `type`, `period`, `time_update`, `time_format`)".
47
            "    SELECT `forum_id`, `forum_posts`, '".NEWBB_STATS_TYPE_POST."', '".NEWBB_STATS_PERIOD_TOTAL."', NOW() + 0, ''".
48
            "         FROM ".$GLOBALS['xoopsDB']->prefix("bb_forums")
49
            );
50
    $GLOBALS['xoopsDB']->queryF(
51
            "    INSERT INTO ".$GLOBALS['xoopsDB']->prefix("bb_stats").
52
            "        (`id`, `value`, `type`, `period`, `time_update`, `time_format`)".
53
            "    SELECT `forum_id`, count(*), '".NEWBB_STATS_TYPE_DIGEST."', '".NEWBB_STATS_PERIOD_TOTAL."', NOW() + 0, ''".
54
            "         FROM ".$GLOBALS['xoopsDB']->prefix("bb_topics").
55
            "        WHERE topic_digest = 1".
56
            "        GROUP BY `forum_id`".
57
            );
58
    $GLOBALS['xoopsDB']->queryF(
59
            "    INSERT INTO ".$GLOBALS['xoopsDB']->prefix("bb_stats").
60
            "        (`id`, `value`, `type`, `period`, `time_update`, `time_format`)".
61
            "    SELECT `forum_id`, SUM(topic_views), '".NEWBB_STATS_TYPE_VIEW."', '".NEWBB_STATS_PERIOD_TOTAL."', NOW() + 0, ''".
62
            "         FROM ".$GLOBALS['xoopsDB']->prefix("bb_topics").
63
            "        WHERE topic_digest = 1".
64
            "        GROUP BY `forum_id`".
65
            );
66
    */
67
68
    $sql = '    UPDATE '
69
           . $GLOBALS['xoopsDB']->prefix('bb_posts_text')
70
           . ' AS t, '
71
           . $GLOBALS['xoopsDB']->prefix('bb_posts')
72
           . ' AS p'
73
           . '    SET t.dohtml = p.dohtml, '
74
           . '        t.dosmiley = p.dosmiley, '
75
           . '        t.doxcode = p.doxcode, '
76
           . '        t.doimage = p.doimage, '
77
           . '        t.dobr = p.dobr'
78
           . '    WHERE p.post_id =t.post_id ';
79
    if ($GLOBALS['xoopsDB']->queryF($sql)) {
80
        $sql = '    ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_posts') . '        DROP `dohtml`,' . '        DROP `dosmiley`,' . '        DROP `doxcode`,' . '        DROP `doimage`,' . '        DROP `dobr`';
81
        $GLOBALS['xoopsDB']->queryF($sql);
82
    } else {
83
        xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
84
    }
85
86
    if (\class_exists(\XoopsModules\Tag\TagHandler::class) && xoops_isActiveModule('tag')) {
87
        $tagHandler  = \XoopsModules\Tag\Helper::getInstance()->getHandler('Tag');
88
        $table_topic = $GLOBALS['xoopsDB']->prefix('bb_topics');
89
90
        $sql = '    SELECT topic_id, topic_tags' . "    FROM {$table_topic}";
91
        if (false === ($result = $GLOBALS['xoopsDB']->query($sql))) {
92
            xoops_error($GLOBALS['xoopsDB']->error());
93
        }
94
        if ($result instanceof \mysqli_result) {
95
            while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
96
                if (empty($myrow['topic_tags'])) {
97
                    continue;
98
                }
99
                $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

99
                $tagHandler->updateByItem($myrow['topic_tags'], $myrow['topic_id'], /** @scrutinizer ignore-type */ $module->getVar('mid'));
Loading history...
100
            }
101
        }
102
    }
103
104
    if (!$GLOBALS['xoopsDB']->query(
105
        '
106
            SELECT COUNT(*)
107
            FROM ' . $GLOBALS['xoopsDB']->prefix('bb_type_tmp') . ' AS a, ' . $GLOBALS['xoopsDB']->prefix('bb_type_forum_tmp') . ' AS b
108
            WHERE a.type_id = b.type_id AND a.type_id >0;
109
        '
110
    )) {
111
        //xoops_error($GLOBALS['xoopsDB']->error());
112
        $GLOBALS['xoopsDB']->queryF('DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_type_tmp'));
113
        $GLOBALS['xoopsDB']->queryF('DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_type_forum_tmp'));
114
115
        return true;
116
    }
117
118
    $GLOBALS['xoopsDB']->queryF(
119
        '    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')
120
    );
121
    $GLOBALS['xoopsDB']->queryF(
122
        '    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')
123
    );
124
125
    $GLOBALS['xoopsDB']->queryF('DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_type_tmp'));
126
    $GLOBALS['xoopsDB']->queryF('DROP TABLE ' . $GLOBALS['xoopsDB']->prefix('bb_type_forum_tmp'));
127
128
    return true;
129
}
130