xoops_module_update_news()   F
last analyzed

Complexity

Conditions 36
Paths > 20000

Size

Total Lines 180
Code Lines 97

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 36
eloc 97
c 1
b 0
f 0
nc 99532800
nop 0
dl 0
loc 180
rs 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
/**
4
 * News functions
5
 *
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 *
13
 * @copyright   {@link https://xoops.org/ XOOPS Project}
14
 * @license     GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
15
 * @author      Voltan
16
 */
17
18
use XoopsModules\News;
19
20
/**
21
 * @return bool
22
 */
23
function xoops_module_update_news()
24
{
25
    global $xoopsDB;
26
    $errors = 0;
27
28
    //0) Rename all tables
29
30
    if (News\Utility::existTable($xoopsDB->prefix('stories_files'))) {
31
        $sql    = 'ALTER TABLE ' . $xoopsDB->prefix('stories_files') . ' RENAME ' . $xoopsDB->prefix('news_stories_files');
32
        $result = $xoopsDB->queryF($sql);
33
        if (!$result) {
34
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
35
            ++$errors;
36
        }
37
    } elseif (!News\Utility::existTable($xoopsDB->prefix('news_stories_files'))) {
38
        // 1) Create, if it does not exists, the stories_files table
39
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('news_stories_files') . " (
40
              fileid INT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
41
              filerealname VARCHAR(255) NOT NULL DEFAULT '',
42
              storyid INT(8) UNSIGNED NOT NULL DEFAULT '0',
43
              date INT(10) NOT NULL DEFAULT '0',
44
              mimetype VARCHAR(64) NOT NULL DEFAULT '',
45
              downloadname VARCHAR(255) NOT NULL DEFAULT '',
46
              counter INT(8) UNSIGNED NOT NULL DEFAULT '0',
47
              PRIMARY KEY  (fileid),
48
              KEY storyid (storyid)
49
            ) ENGINE=MyISAM;";
50
        if (!$xoopsDB->queryF($sql)) {
51
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED1;
52
            ++$errors;
53
        }
54
    }
55
56
    if (News\Utility::existTable($xoopsDB->prefix('stories'))) {
57
        $sql    = 'ALTER TABLE ' . $xoopsDB->prefix('stories') . ' RENAME ' . $xoopsDB->prefix('news_stories');
58
        $result = $xoopsDB->queryF($sql);
59
        if (!$result) {
60
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
61
            ++$errors;
62
        }
63
    }
64
65
    if (News\Utility::existTable($xoopsDB->prefix('topics'))) {
66
        $sql    = 'ALTER TABLE ' . $xoopsDB->prefix('topics') . ' RENAME ' . $xoopsDB->prefix('news_topics');
67
        $result = $xoopsDB->queryF($sql);
68
        if (!$result) {
69
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
70
            ++$errors;
71
        }
72
    }
73
74
    if (News\Utility::existTable($xoopsDB->prefix('stories_files'))) {
75
        $sql    = 'ALTER TABLE ' . $xoopsDB->prefix('stories_files') . ' RENAME ' . $xoopsDB->prefix('news_stories_files');
76
        $result = $xoopsDB->queryF($sql);
77
        if (!$result) {
78
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
79
            ++$errors;
80
        }
81
    }
82
83
    // 2) Change the topic title's length, in the topics table
84
    $sql    = 'ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' CHANGE topic_title topic_title VARCHAR( 255 ) NOT NULL;';
85
    $result = $xoopsDB->queryF($sql);
86
    if (!$result) {
87
        echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
88
        ++$errors;
89
    }
90
91
    // 2.0a) change column size for IP address from varchar(16) to varchar(45) for IPv6
92
    $sql    = 'ALTER TABLE ' . $xoopsDB->prefix('news_stories') . ' MODIFY hostname VARCHAR( 45 ) NOT NULL DEFAULT \'\';';
93
    $result = $xoopsDB->queryF($sql);
94
    if (!$result) {
95
        echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
96
        ++$errors;
97
    }
98
99
    // 2.0b)  change column size for Picture from varchar(50) to varchar(255) for SEO
100
    $sql    = 'ALTER TABLE ' . $xoopsDB->prefix('news_stories') . ' MODIFY picture VARCHAR( 255 ) NOT NULL DEFAULT \'\';';
101
    $result = $xoopsDB->queryF($sql);
102
    if (!$result) {
103
        echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
104
        ++$errors;
105
    }
106
107
108
    // 2.1) Add the new fields to the topic table
109
    if (!News\Utility::existField('menu', $xoopsDB->prefix('news_topics'))) {
110
        News\Utility::addField("menu TINYINT( 1 ) DEFAULT '0' NOT NULL", $xoopsDB->prefix('news_topics'));
111
    }
112
    if (!News\Utility::existField('topic_frontpage', $xoopsDB->prefix('news_topics'))) {
113
        News\Utility::addField("topic_frontpage TINYINT( 1 ) DEFAULT '1' NOT NULL", $xoopsDB->prefix('news_topics'));
114
    }
115
    if (!News\Utility::existField('topic_rssurl', $xoopsDB->prefix('news_topics'))) {
116
        News\Utility::addField('topic_rssurl VARCHAR( 255 ) NOT NULL', $xoopsDB->prefix('news_topics'));
117
    }
118
    if (!News\Utility::existField('topic_description', $xoopsDB->prefix('news_topics'))) {
119
        News\Utility::addField('topic_description TEXT NOT NULL', $xoopsDB->prefix('news_topics'));
120
    }
121
    if (!News\Utility::existField('topic_color', $xoopsDB->prefix('news_topics'))) {
122
        News\Utility::addField("topic_color varchar(6) NOT NULL default '000000'", $xoopsDB->prefix('news_topics'));
123
    }
124
125
    // 3) If it does not exists, create the table stories_votedata
126
    if (!News\Utility::existTable($xoopsDB->prefix('news_stories_votedata'))) {
127
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('news_stories_votedata') . " (
128
              ratingid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
129
              storyid INT(8) UNSIGNED NOT NULL DEFAULT '0',
130
              ratinguser INT(11) NOT NULL DEFAULT '0',
131
              rating TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
132
              ratinghostname VARCHAR(60) NOT NULL DEFAULT '',
133
              ratingtimestamp INT(10) NOT NULL DEFAULT '0',
134
              PRIMARY KEY  (ratingid),
135
              KEY ratinguser (ratinguser),
136
              KEY ratinghostname (ratinghostname),
137
              KEY storyid (storyid)
138
            ) ENGINE=MyISAM;";
139
        if (!$xoopsDB->queryF($sql)) {
140
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED3;
141
            ++$errors;
142
        }
143
    }
144
145
    // 4) Create the four new fields for the votes in the story table
146
    if (!News\Utility::existField('rating', $xoopsDB->prefix('news_stories'))) {
147
        News\Utility::addField("rating DOUBLE( 6, 4 ) DEFAULT '0.0000' NOT NULL", $xoopsDB->prefix('news_stories'));
148
    }
149
    if (!News\Utility::existField('votes', $xoopsDB->prefix('news_stories'))) {
150
        News\Utility::addField("votes INT( 11 ) UNSIGNED DEFAULT '0' NOT NULL", $xoopsDB->prefix('news_stories'));
151
    }
152
    if (!News\Utility::existField('keywords', $xoopsDB->prefix('news_stories'))) {
153
        News\Utility::addField('keywords VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
154
    }
155
    if (!News\Utility::existField('description', $xoopsDB->prefix('news_stories'))) {
156
        News\Utility::addField('description VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
157
    }
158
    if (!News\Utility::existField('pictureinfo', $xoopsDB->prefix('news_stories'))) {
159
        News\Utility::addField('pictureinfo VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
160
    }
161
    if (!News\Utility::existField('subtitle', $xoopsDB->prefix('news_stories'))) {
162
        News\Utility::addField('subtitle VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
163
    }
164
165
    // 5) Add some indexes to the topics table
166
    $sql    = 'ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' ADD INDEX ( `topic_title` );';
167
    $result = $xoopsDB->queryF($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
168
    $sql    = 'ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' ADD INDEX ( `menu` );';
169
    $result = $xoopsDB->queryF($sql);
170
171
    // 6) Make files and folders
172
    $dir = XOOPS_ROOT_PATH . '/uploads/news';
173
    if (!@mkdir($dir) && !is_dir($dir)) {
174
        throw new \RuntimeException('The directory ' . $dir . ' could not be created.');
175
    }
176
    if (!is_writable($dir)) {
177
        chmod($dir, 0777);
178
    }
179
180
    $dir = XOOPS_ROOT_PATH . '/uploads/news/file';
181
    if (!@mkdir($dir) && !is_dir($dir)) {
182
        throw new \RuntimeException('The directory ' . $dir . ' could not be created.');
183
    }
184
    if (!is_writable($dir)) {
185
        chmod($dir, 0777);
186
    }
187
188
    $dir = XOOPS_ROOT_PATH . '/uploads/news/image';
189
    if (!@mkdir($dir) && !is_dir($dir)) {
190
        throw new \RuntimeException('The directory ' . $dir . ' could not be created.');
191
    }
192
    if (!is_writable($dir)) {
193
        chmod($dir, 0777);
194
    }
195
196
    // Copy index.html files on uploads folders
197
    $indexFile = XOOPS_ROOT_PATH . '/modules/news/include/index.php';
198
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/index.php');
199
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/file/index.php');
200
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/image/index.php');
201
202
    return true;
203
}
204