xoops_module_update_newbb_v400()   C
last analyzed

Complexity

Conditions 12
Paths 192

Size

Total Lines 113
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 55
nc 192
nop 1
dl 0
loc 113
rs 6.2
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
 * NewBB 4.3x, the forum module for XOOPS project
4
 *
5
 * @copyright      XOOPS Project (https://xoops.org)
6
 * @license        http://www.fsf.org/copyleft/gpl.html GNU public license
7
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
8
 * @since          4.00
9
 * @package        module::newbb
10
 * @param XoopsModule $module
11
 * @return bool
12
 */
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
    @require_once $GLOBALS['xoops']->path('modules/tag/include/functions.php');
87
    if (function_exists('tag_getTagHandler') && $tag_handler = tag_getTagHandler()) {
0 ignored issues
show
Deprecated Code introduced by
The function tag_getTagHandler() has been deprecated: - use Tag\Helper::getInstance()->getHandler('Tag') instead ( Ignorable by Annotation )

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

87
    if (function_exists('tag_getTagHandler') && $tag_handler = /** @scrutinizer ignore-deprecated */ tag_getTagHandler()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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
        while (false !== ($myrow = $GLOBALS['xoopsDB']->fetchArray($result))) {
95
            if (empty($myrow['topic_tags'])) {
96
                continue;
97
            }
98
            $tag_handler->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

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