Completed
Push — master ( 8becb2...e269ef )
by Lars
03:04
created

Helper::get_mysql_client_version()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace voku\db;
4
5
/**
6
 * Helper: this handles extra functions that use the "DB"-Class
7
 *
8
 * @package   voku\db
9
 */
10
class Helper
11
{
12
  /**
13
   * Check if "mysqlnd"-driver is used.
14
   *
15
   * @return bool
16
   */
17 34
  public static function isMysqlndIsUsed()
18
  {
19 34
    static $_mysqlnd_is_used = null;
20
21 34
    if ($_mysqlnd_is_used === null) {
22 1
      $_mysqlnd_is_used = (extension_loaded('mysqlnd') && function_exists('mysqli_fetch_all'));
23 1
    }
24
25 34
    return $_mysqlnd_is_used;
26
  }
27
28
  /**
29
   * Check if the current environment supports "utf8mb4".
30
   *
31
   * @param DB $db
32
   *
33
   * @return bool
34
   */
35 5
  public static function isUtf8mb4Supported(DB $db)
36
  {
37
    /**
38
     *  https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/
39
     *
40
     * - You’re currently using the utf8 character set.
41
     * - Your MySQL server is version 5.5.3 or higher (including all 10.x versions of MariaDB).
42
     * - Your MySQL client libraries are version 5.5.3 or higher. If you’re using mysqlnd, 5.0.9 or higher.
43
     *
44
     * INFO: utf8mb4 is 100% backwards compatible with utf8.
45
     */
46
47 5
    $server_version = self::get_mysql_server_version($db);
48 5
    $client_version = self::get_mysql_client_version($db);
49
50
    if (
51
        $server_version >= 50503
52 5
        &&
53
        (
54
            (
55 5
                self::isMysqlndIsUsed() === true
56 5
                &&
57
                $client_version >= 50009
58 5
            )
59
            ||
60
            (
61
                self::isMysqlndIsUsed() === false
62
                &&
63
                $client_version >= 50503
64
            )
65
        )
66
67 5
    ) {
68 5
      return true;
69
    } else {
70
      return false;
71
    }
72
  }
73
74
  /**
75
   * A string that represents the MySQL client library version.
76
   *
77
   * @param DB $db
78
   *
79
   * @return string
80
   */
81 5
  public static function get_mysql_client_version(DB $db)
82
  {
83 5
    static $_mysqli_client_version = null;
84
85 5
    if ($_mysqli_client_version === null) {
86 1
      $_mysqli_client_version = \mysqli_get_client_version($db->getLink());
87 1
    }
88
89 5
    return $_mysqli_client_version;
90
  }
91
92
93
  /**
94
   * Returns a string representing the version of the MySQL server that the MySQLi extension is connected to.
95
   *
96
   * @param DB $db
97
   *
98
   * @return string
99
   */
100 5
  public static function get_mysql_server_version(DB $db)
101
  {
102 5
    static $_mysqli_server_version = null;
103
104 5
    if ($_mysqli_server_version === null) {
105 1
      $_mysqli_server_version = \mysqli_get_server_version($db->getLink());
106 1
    }
107
108 5
    return $_mysqli_server_version;
109
  }
110
111
  /**
112
   * Return all db-fields from a table.
113
   *
114
   * @param string  $table
115
   * @param bool    $useStaticCache
116
   * @param DB|null $db
117
   *
118
   * @return array
119
   */
120 1
  public static function getDbFields($table, $useStaticCache = true, DB $db = null)
121
  {
122 1
    static $dbFieldsCache = array();
123
124
    // use the static cache
125
    if (
126
        $useStaticCache === true
127 1
        &&
128 1
        isset($dbFieldsCache[$table])
129 1
    ) {
130 1
      return $dbFieldsCache[$table];
131
    }
132
133
    // init
134 1
    $dbFields = array();
135
136 1
    if ($db === null) {
137 1
      $db = DB::getInstance();
138 1
    }
139
140 1
    $debug = new Debug($db);
141 1
    if ($table === '') {
142
      $debug->displayError('invalid table name');
143
144
      return array();
145
    }
146
147 1
    $sql = 'SHOW COLUMNS FROM ' . $db->quote_string($table);
148 1
    $result = $db->query($sql);
149
150 1
    if ($result && $result->num_rows > 0) {
151 1
      foreach ($result->fetchAllArray() as $tmpResult) {
152 1
        $dbFields[] = $tmpResult['Field'];
153 1
      }
154 1
    }
155
156
    // add to static cache
157 1
    $dbFieldsCache[$table] = $dbFields;
158
159 1
    return $dbFields;
160
  }
161
162
  /**
163
   * Copy row within a DB table and making updates to the columns.
164
   *
165
   * @param string  $table
166
   * @param array   $whereArray
167
   * @param array   $updateArray
168
   * @param array   $ignoreArray
169
   * @param DB|null $db           <p>Use <strong>null</strong>
170
   *
171
   * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br />
172
   *                   "false" on error
173
   */
174 1
  public static function copyTableRow($table, array $whereArray, array $updateArray = array(), array $ignoreArray = array(), DB $db = null)
175
  {
176
    // init
177 1
    $table = trim($table);
178
179 1
    if ($db === null) {
180 1
      $db = DB::getInstance();
181 1
    }
182
183 1
    $debug = new Debug($db);
184 1
    if ($table === '') {
185
      $debug->displayError('invalid table name');
186
187
      return false;
188
    }
189
190 1
    $whereSQL = '';
191 1
    foreach ($whereArray as $key => $value) {
192 1
      $whereSQL .= ' AND ' . $db->escape($key) . ' = ' . $db->escape($value);
193 1
    }
194
195
    // get the row
196 1
    $query = 'SELECT * FROM ' . $db->quote_string($table) . '
197
      WHERE 1 = 1
198 1
      ' . $whereSQL . '
199 1
    ';
200 1
    $result = $db->query($query);
201
202
    // make sure the row exists
203 1
    if ($result->num_rows > 0) {
204
205
      /** @noinspection LoopWhichDoesNotLoopInspection */
206 1
      while ($tmpArray = $result->fetchArray()) {
207
208
        // re-build a new DB query and ignore some field-names
209 1
        $bindings = array();
210 1
        $insert_keys = '';
211 1
        $insert_values = '';
212
213 1
        foreach ($tmpArray as $fieldName => $value) {
214
215 1
          if (!in_array($fieldName, $ignoreArray, true)) {
216 1
            if (array_key_exists($fieldName, $updateArray)) {
217 1
              $insert_keys .= ',' . $fieldName;
218 1
              $insert_values .= ',?';
219 1
              $bindings[] = $updateArray[$fieldName]; // INFO: do not escape non selected data
220 1
            } else {
221 1
              $insert_keys .= ',' . $fieldName;
222 1
              $insert_values .= ',?';
223 1
              $bindings[] = $value; // INFO: do not escape non selected data
224
            }
225 1
          }
226 1
        }
227
228 1
        $insert_keys = ltrim($insert_keys, ',');
229 1
        $insert_values = ltrim($insert_values, ',');
230
231
        // insert the "copied" row
232 1
        $new_query = 'INSERT INTO ' . $db->quote_string($table) . ' (' . $insert_keys . ')
233 1
          VALUES (' . $insert_values . ')
234 1
        ';
235 1
        return $db->query($new_query, $bindings);
236
      }
237
    }
238
239
    return false;
240
  }
241
}
242