xoops_module_update_xsitemap()   C
last analyzed

Complexity

Conditions 17
Paths 14

Size

Total Lines 103
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 17
eloc 50
c 4
b 0
f 0
nc 14
nop 2
dl 0
loc 103
rs 5.2166

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
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project (https://xoops.org)
14
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author      XOOPS Development Team
16
 */
17
18
use XoopsModules\Xsitemap\{
19
    Common\Configurator,
20
    Helper,
21
    Utility
22
};
23
/** @var Utility $utility */
24
/** @var Helper $helper */
25
/** @var Configurator $configurator */
26
27
// require_once  \dirname(__DIR__) . '/class/Utility.php';
28
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
29
    || !$GLOBALS['xoopsUser']->isAdmin()) {
30
    exit('Restricted access' . PHP_EOL);
31
}
32
/**
33
 * Prepares system prior to attempting to update module
34
 *
35
 * @param \XoopsModule $module
36
 *
37
 * @return bool true if successfully ready to update module, false if not
38
 */
39
function xoops_module_pre_update_xsitemap(\XoopsModule $module)
40
{
41
    $moduleDirName = \basename(\dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
42
    $utility       = new Utility();
43
    $xoopsSuccess  = $utility::checkVerXoops($module);
44
    $phpSuccess    = $utility::checkVerPhp($module);
45
    return $xoopsSuccess && $phpSuccess;
46
}
47
48
/**
49
 * Functions to upgrade from previous version of the module
50
 *
51
 * @param \XoopsModule $module
52
 * @param int|null     $previousVersion
53
 * @return bool true if successfully updated module, false if not
54
 * @internal param int $curr_version version number of module currently installed
55
 */
56
function xoops_module_update_xsitemap(\XoopsModule $module, $previousVersion = null)
57
{
58
    /*======================================================================
59
        //----------------------------------------------------------------
60
        // Remove xSitemap uploads folder (and all subfolders) if they exist
61
        //----------------------------------------------------------------*
62
        $utility = new Utility();
63
        if (!class_exists($utility)) {
64
            xoops_load('utility', $moduleDirName);
65
        }
66
67
        // Recursively delete directories
68
        $xsUploadDir = realpath(XOOPS_UPLOAD_PATH . "/" . $module->dirname());
69
        $success = $utility::rrmdir($xsUploadDir);
70
        if (true !== $success) {
71
            \Xmf\Language::load('admin', $module->dirname());
72
            $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $xsUploadDir));
73
        }
74
        return $success;
75
    ======================================================================*/
76
    $moduleDirName      = \basename(\dirname(__DIR__));
77
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
78
    $helper       = Helper::getInstance();
79
    $utility      = new Utility();
80
    //-----------------------------------------------------------------------
81
    // Upgrade for Xsitemap < 1.54
82
    //-----------------------------------------------------------------------
83
    $success = true;
84
    $helper->loadLanguage('modinfo');
85
    $helper->loadLanguage('admin');
86
    if ($previousVersion < 154) {
87
        //----------------------------------------------------------------
88
        // Remove previous css & images directories since they've been relocated to ./assets
89
        // Also remove uploads directories since they're no longer used
90
        //----------------------------------------------------------------
91
        $old_directories = [
92
            $helper->path('css/'),
93
            $helper->path('js/'),
94
            $helper->path('images/'),
95
            XOOPS_UPLOAD_PATH . '/' . $module->dirname(),
96
        ];
97
        foreach ($old_directories as $old_dir) {
98
            $dirInfo = new \SplFileInfo($old_dir);
99
            if ($dirInfo->isDir()) {
100
                // The directory exists so delete it
101
                if (false === $utility::rrmdir($old_dir)) {
102
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $old_dir));
103
                    return false;
104
                }
105
            }
106
            unset($dirInfo);
107
        }
108
        //-----------------------------------------------------------------------
109
        // Remove ./template/*.html (except index.html) files since they've
110
        // been replaced by *.tpl files
111
        // Note: this will also remove /template/xsitemap_style.html since it's no longer used
112
        //-----------------------------------------------------------------------
113
        $path       = $helper->path('templates/');
114
        $unfiltered = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
115
        $iterator   = new RegexIterator($unfiltered, '/.*\.html/');
116
        foreach ($iterator as $name => $fObj) {
117
            if ($fObj->isFile() && ('index.html' !== $fObj->getFilename())) {
118
                if (false === ($success = unlink($fObj->getPathname()))) {
119
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $fObj->getPathname()));
120
                    return false;
121
                }
122
            }
123
        }
124
        //-----------------------------------------------------------------------
125
        // Now remove a some misc files that were renamed or deprecated
126
        //-----------------------------------------------------------------------
127
        $oldFiles = [
128
            $helper->path('include/install.php'),
129
            $helper->path('class/module.php'),
130
            $helper->path('class/menu.php'),
131
        ];
132
        foreach ($oldFiles as $file) {
133
            if (is_file($file)) {
134
                if (false === ($delOk = unlink($file))) {
135
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $file));
136
                }
137
                $success = $success && $delOk;
138
            }
139
        }
140
    }
141
    if ($previousVersion < 156) {
142
        // update table (add new field)
143
        $table = $GLOBALS['xoopsDB']->prefix('xsitemap_plugin');
144
        $field = 'plugin_where';
145
        $check = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'");
146
        $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
147
        if (!$numRows) {
148
            $sql = "ALTER TABLE `$table` ADD `$field` VARCHAR(255) NOT NULL AFTER `plugin_weight`;";
149
            if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
150
                xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
151
                $module->setErrors("Error when adding '$field' to table '$table'.");
152
                $ret = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
153
            }
154
            $success = $success && $result;
155
        }
156
    }
157
158
    return $success;
159
}
160