Completed
Push — master ( 2653be...f5b70a )
by Michael
26s queued 22s
created

Upgrade_2511::check_bannerintsize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
use Xmf\Database\Tables;
4
5
/**
6
 * Upgrade from 2.5.10 to 2.5.11
7
 *
8
 * @copyright    (c) 2000-2021 XOOPS Project (https://xoops.org)
9
 * @license          GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
10
 * @package          Upgrade
11
 * @since            2.5.11
12
 * @author           XOOPS Team
13
 */
14
class Upgrade_2511 extends XoopsUpgrade
15
{
16
    /**
17
     * __construct
18
     */
19
    public function __construct()
20
    {
21
        parent::__construct(basename(__DIR__));
22
        $this->tasks = array(
23
            'bannerintsize',
24
            'qmail',
25
        );
26
        $this->usedFiles = array();
27
    }
28
29
30
    /**
31
     * Determine if columns are declared mediumint, and if
32
     * so, queue ddl to alter to int.
33
     *
34
     * @param $migrate           \Xmf\Database\Tables
35
     * @param $bannerTableName   string
36
     * @param $bannerColumnNames string[] array of columns to check
37
     *
38
     * @return integer count of queue items added
39
     */
40
    protected function fromMediumToInt(Tables $migrate, $bannerTableName, $bannerColumnNames)
41
    {
42
        $migrate->useTable($bannerTableName);
43
        $count = 0;
44
        foreach ($bannerColumnNames as $column) {
45
            $attributes = $migrate->getColumnAttributes($bannerTableName, $column);
46
            if (0 === strpos(trim($attributes), 'mediumint')) {
0 ignored issues
show
Bug introduced by
It seems like $attributes can also be of type boolean; however, parameter $string of trim() does only seem to accept 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

46
            if (0 === strpos(trim(/** @scrutinizer ignore-type */ $attributes), 'mediumint')) {
Loading history...
47
                $count++;
48
                $migrate->alterColumn($bannerTableName, $column, 'int(10) UNSIGNED NOT NULL DEFAULT \'0\'');
49
            }
50
        }
51
        return $count;
52
    }
53
54
    private $bannerTableName = 'banner';
55
    private $bannerColumnNames = array('impmade', 'clicks');
56
57
    /**
58
     * Increase count columns from mediumint to int
59
     *
60
     * @return bool true if patch IS applied, false if NOT applied
61
     */
62
    public function check_bannerintsize()
63
    {
64
        $migrate = new Tables();
65
        $count = $this->fromMediumToInt($migrate, $this->bannerTableName, $this->bannerColumnNames);
66
67
        return $count==0;
68
    }
69
70
    /**
71
     * Increase count columns from mediumint to int (Think BIG!)
72
     *
73
     * @return bool true if applied, false if failed
74
     */
75
    public function apply_bannerintsize()
76
    {
77
        $migrate = new \Xmf\Database\Tables();
78
79
        $count = $this->fromMediumToInt($migrate, $this->bannerTableName, $this->bannerColumnNames);
80
81
        $result = $migrate->executeQueue(true);
82
        if (false === $result) {
83
            $this->logs[] = sprintf('Migration of %s table failed. Error: %s - %s' .
84
                $this->bannerTableName,
85
                $migrate->getLastErrNo(),
86
                $migrate->getLastError()
87
            );
88
            return false;
89
        }
90
91
        return $count!==0;
92
    }
93
94
    /**
95
     * Add qmail as valid mailmethod
96
     *
97
     * @return bool
98
     */
99
    public function check_qmail()
100
    {
101
        /* @var XoopsMySQLDatabase $db */
102
        $db = XoopsDatabaseFactory::getDatabaseConnection();
103
104
        $table = $db->prefix('configoption');
105
106
        $sql = sprintf(
107
            'SELECT count(*) FROM `%s` '
108
            . "WHERE `conf_id` = 64 AND `confop_name` = 'qmail'",
109
            $db->escape($table)
110
        );
111
112
        /** @var mysqli_result $result */
113
        $result = $db->query($sql);
114
        if ($result) {
0 ignored issues
show
introduced by
$result is of type mysqli_result, thus it always evaluated to true.
Loading history...
115
            $row = $db->fetchRow($result);
116
            if ($row) {
117
                $count = $row[0];
118
                return (0 === (int) $count) ? false : true;
119
            }
120
        }
121
        return false;
122
    }
123
124
    /**
125
     * Add qmail as valid mailmethod
126
     *
127
     * phpMailer has qmail support, similar to but slightly different than sendmail
128
     * This will allow webmasters to utilize qmail if it is provisioned on server.
129
     *
130
     * @return bool
131
     */
132
    public function apply_qmail()
133
    {
134
        $migrate = new Tables();
135
        $migrate->useTable('configoption');
136
        $migrate->insert(
137
            'configoption',
138
            array('confop_name' => 'qmail', 'confop_value' => 'qmail', 'conf_id' => 64)
139
        );
140
        return $migrate->executeQueue(true);
141
    }
142
}
143
144
return new Upgrade_2511();
145