Completed
Push — master ( ad6325...4bb5f3 )
by Michael
02:50
created

update.php ➔ xoops_module_update_gwiki()   D

Complexity

Conditions 14
Paths 54

Size

Total Lines 169
Code Lines 121

Duplication

Lines 17
Ratio 10.06 %

Importance

Changes 11
Bugs 1 Features 3
Metric Value
cc 14
eloc 121
c 11
b 1
f 3
nc 54
nop 2
dl 17
loc 169
rs 4.9516

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
 * update.php - initializations on module update
4
 *
5
 * This file is part of gwiki - geekwright wiki
6
 *
7
 * @copyright  Copyright © 2013 geekwright, LLC. All rights reserved.
8
 * @license    gwiki/docs/license.txt  GNU General Public License (GPL)
9
 * @since      1.0
10
 * @author     Richard Griffith <[email protected]>
11
 * @package    gwiki
12
 */
13
14
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
15
16
/**
17
 * @param XoopsModule $module
18
 * @param             $old_version
19
 *
20
 * @return bool
21
 */
22
function xoops_module_update_gwiki(XoopsModule $module, $old_version)
23
{
24
    global $xoopsDB;
25
26
    $error = false;
27
28
    // recompile namespace templates
29
    $tplfileHandler = xoops_getHandler('tplfile');
30
31
    $dir = basename(dirname(__DIR__));
32
    $mid = $module->getVar('mid');
33
34
    $sql = 'SELECT * FROM ' . $xoopsDB->prefix('gwiki_prefix') . ', ' . $xoopsDB->prefix('gwiki_template');
35
    $sql .= ' WHERE prefix_template_id = template_id ';
36
37
    $result = $xoopsDB->query($sql);
38
39
    //$rows=$xoopsDB->getRowsNum($result);
40
    while ($template = $xoopsDB->fetchArray($result)) {
41
        $pid  = $template['prefix_id'];
42
        $file = $dir . '_prefix_' . $pid . '.tpl';
43
44
        $tplfiles = $tplfileHandler->find('default', 'module', $mid, $dir, $file, false);
45 View Code Duplication
        if (count($tplfiles)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
            $tplfile = $tplfiles[0];
47
            $isnew   = false;
48
        } else {
49
            $tplfile = $tplfileHandler->create();
50
            $isnew   = true;
51
        }
52
53
        $tplfile->setVar('tpl_source', $template['template_body'], true);
54
        $tplfile->setVar('tpl_refid', $mid);
55
        $tplfile->setVar('tpl_tplset', 'default');
56
        $tplfile->setVar('tpl_file', $file);
57
        $tplfile->setVar('tpl_desc', $template['template'], true);
58
        $tplfile->setVar('tpl_module', $dir);
59
        $tplfile->setVar('tpl_lastmodified', time());
60
        $tplfile->setVar('tpl_lastimported', 0);
61
        $tplfile->setVar('tpl_type', 'module');
62
63
        if ($isnew) {
64 View Code Duplication
            if (!$tplfileHandler->insert($tplfile)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
                $module->setErrors('ERROR: Could not insert template ' . htmlspecialchars($file) . ' to the database.');
66
                $error = true;
67
            }
68 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            if (!$tplfileHandler->forceUpdate($tplfile)) {
70
                $module->setErrors('ERROR: Could not update template ' . htmlspecialchars($file) . ' in the database.');
71
                $error = true;
72
            }
73
        }
74
    }
75
76
    // table alterations - these will quietly fail if already done
77
    // these are all to bring development versions to current
78
    if ($old_version < 100) {
79
        //  trigger_error($old_version);
80
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . ' ADD COLUMN toc_cache TEXT NOT NULL AFTER search_body';
81
        $xoopsDB->queryF($sql);
82
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . ' ADD COLUMN show_in_index TINYINT NOT NULL DEFAULT 1 AFTER toc_cache';
83
        $xoopsDB->queryF($sql);
84
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . ' DROP PRIMARY KEY, ADD PRIMARY KEY(gwiki_id, active)';
85
        $xoopsDB->queryF($sql);
86
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_prefix') . ' ADD COLUMN prefix_auto_name TINYINT NOT NULL DEFAULT 0 AFTER prefix_home';
87
        $xoopsDB->queryF($sql);
88
89
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pageids') . " CHANGE keyword  keyword VARCHAR(128) NOT NULL DEFAULT ''";
90
        $xoopsDB->queryF($sql);
91
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . " CHANGE keyword  keyword VARCHAR(128) NOT NULL DEFAULT ''";
92
        $xoopsDB->queryF($sql);
93
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_prefix') . " CHANGE prefix  prefix VARCHAR(128) NOT NULL DEFAULT '', " . " CHANGE prefix_home  prefix_home VARCHAR(128) NOT NULL DEFAULT ''";
94
        $xoopsDB->queryF($sql);
95
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_template') . " CHANGE template  template VARCHAR(128) NOT NULL DEFAULT ''";
96
        $xoopsDB->queryF($sql);
97
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_page_images') . " CHANGE keyword  keyword VARCHAR(128) NOT NULL DEFAULT '', "
98
               . " CHANGE image_name  image_name VARCHAR(128) NOT NULL DEFAULT ''";
99
        $xoopsDB->queryF($sql);
100
101
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('gwiki_page_files') . " (file_id int(10) NOT NULL AUTO_INCREMENT, keyword varchar(128) NOT NULL DEFAULT '',"
102
               . " file_name varchar(128) NOT NULL DEFAULT '', file_path varchar(255) NOT NULL DEFAULT '', "
103
               . " file_type varchar(128) NOT NULL DEFAULT '', file_icon varchar(64) NOT NULL DEFAULT '', " . " file_size int(10) NOT NULL DEFAULT '0', file_upload_date int(10) NOT NULL DEFAULT '0',"
104
               . " file_description text, file_uid int(10) NOT NULL DEFAULT '0', " . ' PRIMARY KEY (file_id), UNIQUE KEY (keyword, file_name) ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;';
105
        $xoopsDB->queryF($sql);
106
107
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_page_files') . " ADD COLUMN file_type varchar(128) NOT NULL DEFAULT ''" . ", ADD COLUMN file_icon varchar(64) NOT NULL DEFAULT '', "
108
               . " ADD COLUMN file_size int(10) NOT NULL DEFAULT '0'" . ", ADD COLUMN file_upload_date int(10) NOT NULL DEFAULT '0', ADD COLUMN file_description text"
109
               . ", ADD COLUMN file_uid int(10) NOT NULL DEFAULT '0' ";
110
        $xoopsDB->queryF($sql);
111
112
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_page_files') . " ADD COLUMN file_uid int(10) NOT NULL DEFAULT '0' ";
113
        $xoopsDB->queryF($sql);
114
115
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pageids') . " ADD COLUMN hit_count int(10) NOT NULL DEFAULT '0' ";
116
        $xoopsDB->queryF($sql);
117
118
        // shift all tables to MyISAM
119
        $tabs = array(
120
            'gwiki_pages',
121
            'gwiki_pageids',
122
            'gwiki_group_prefix',
123
            'gwiki_prefix',
124
            'gwiki_template',
125
            'gwiki_page_images',
126
            'gwiki_page_files'
127
        );
128
        foreach ($tabs as $v) {
129
            $sql = 'ALTER TABLE ' . $xoopsDB->prefix($v) . ' ENGINE = MyISAM';
130
            $xoopsDB->queryF($sql);
131
        }
132
133
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pageids') . ' ENGINE = MyISAM';
134
        $xoopsDB->queryF($sql);
135
136
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . " CHANGE parent_page  parent_page VARCHAR(128) NOT NULL DEFAULT ''";
137
        $xoopsDB->queryF($sql);
138
139
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . " CHANGE page_set_home  page_set_home VARCHAR(128) NOT NULL DEFAULT ''";
140
        $xoopsDB->queryF($sql);
141
142
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . " CHANGE active active tinyint NOT NULL DEFAULT '0'";
143
        $xoopsDB->queryF($sql);
144
145
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . " CHANGE admin_lock admin_lock tinyint NOT NULL DEFAULT '0'";
146
        $xoopsDB->queryF($sql);
147
148
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . " CHANGE display_keyword  display_keyword VARCHAR(128) NOT NULL DEFAULT ''";
149
        $xoopsDB->queryF($sql);
150
151
        // drop all indexes except PRIMARY
152
        $tabs   = array();
153
        $sql    = 'SHOW INDEX FROM ' . $xoopsDB->prefix('gwiki_pages');
154
        $result = $xoopsDB->queryF($sql);
155
        while ($row = $xoopsDB->fetchArray($result)) {
156
            if ($row['Key_name'] !== 'PRIMARY') {
157
                $tabs[$row['Key_name']] = $row['Non_unique'];
158
            }
159
        }
160
        $xoopsDB->freeRecordSet($result);
161
        if (!empty($tabs)) {
162
            $sql = '';
163
            foreach ($tabs as $i => $v) {
164
                if (empty($sql)) {
165
                    $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages') . ' DROP KEY ' . $i;
166
                } else {
167
                    $sql .= ' , DROP KEY ' . $i;
168
                }
169
            }
170
            $xoopsDB->queryF($sql);
171
        }
172
173
        $sql = 'ALTER TABLE ' . $xoopsDB->prefix('gwiki_pages');
174
        $sql .= ' ADD KEY activekey (active,keyword), ADD KEY keyword (keyword), ' . ' ADD KEY parent (active,parent_page), ADD KEY pageset (active,page_set_home), '
175
                . ' ADD KEY lastmod (active,lastmodified), ADD KEY pageindex (active,show_in_index,display_keyword) ';
176
        $xoopsDB->queryF($sql);
177
    }
178
    if ($old_version < 101) {
179
        $sql = 'CREATE TABLE IF NOT EXISTS ' . $xoopsDB->prefix('gwiki_pagelinks') . ' (';
180
        $sql .= ' from_keyword varchar(128) NOT NULL DEFAULT \'\',';
181
        $sql .= ' to_keyword varchar(128) NOT NULL DEFAULT \'\',';
182
        $sql .= ' PRIMARY KEY (from_keyword, to_keyword),';
183
        $sql .= ' KEY (to_keyword),';
184
        $sql .= ' KEY (from_keyword)';
185
        $sql .= ') ENGINE=MyISAM  DEFAULT CHARSET=utf8;';
186
        $xoopsDB->queryF($sql);
187
    }
188
189
    return !$error;
190
}
191