PluginProbe
SQLite Database Integration / 2.2.21
SQLite Database Integration v2.2.21
3.0.2 3.0.1 trunk 2.1.13 2.1.14 2.1.15 2.1.16 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.2.2 2.2.20 2.2.21 2.2.22 2.2.23 2.2.3 All 32 releases
sqlite-database-integration / wp-includes / database / sqlite / class-wp-sqlite-pdo-user-defined-functions.php

class-wp-sqlite-pdo-user-defined-functions.php in SQLite Database Integration 2.2.21, at wp-includes/database/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

900 lines 24.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Custom functions for the SQLite implementation.
4 *
5 * @package wp-sqlite-integration
6 * @since 1.0.0
7 */
8
9 /**
10 * This class defines user defined functions(UDFs) for PDO library.
11 *
12 * These functions replace those used in the SQL statement with the PHP functions.
13 *
14 * Usage:
15 *
16 * <code>
17 * new WP_SQLite_PDO_User_Defined_Functions(ref_to_pdo_obj);
18 * </code>
19 *
20 * This automatically enables ref_to_pdo_obj to replace the function in the SQL statement
21 * to the ones defined here.
22 */
23 class WP_SQLite_PDO_User_Defined_Functions {
24
25 /**
26 * Registers the user defined functions for SQLite to a PDO instance.
27 * The functions are registered using PDO::sqliteCreateFunction().
28 *
29 * @param PDO|PDO\SQLite $pdo The PDO object.
30 */
31 public static function register_for( $pdo ): self {
32 $instance = new self();
33 foreach ( $instance->functions as $f => $t ) {
34 if ( $pdo instanceof PDO\SQLite ) {
35 $pdo->createFunction( $f, array( $instance, $t ) );
36 } else {
37 $pdo->sqliteCreateFunction( $f, array( $instance, $t ) );
38 }
39 }
40 return $instance;
41 }
42
43 /**
44 * Array to define MySQL function => function defined with PHP.
45 *
46 * Replaced functions must be public.
47 *
48 * @var array
49 */
50 private $functions = array(
51 'throw' => 'throw',
52 'month' => 'month',
53 'monthnum' => 'month',
54 'year' => 'year',
55 'day' => 'day',
56 'hour' => 'hour',
57 'minute' => 'minute',
58 'second' => 'second',
59 'week' => 'week',
60 'weekday' => 'weekday',
61 'dayofweek' => 'dayofweek',
62 'dayofmonth' => 'dayofmonth',
63 'unix_timestamp' => 'unix_timestamp',
64 'now' => 'now',
65 'md5' => 'md5',
66 'curdate' => 'curdate',
67 'rand' => 'rand',
68 'from_unixtime' => 'from_unixtime',
69 'localtime' => 'now',
70 'localtimestamp' => 'now',
71 'isnull' => 'isnull',
72 'if' => '_if',
73 'regexp' => 'regexp',
74 'field' => 'field',
75 'log' => 'log',
76 'least' => 'least',
77 'greatest' => 'greatest',
78 'get_lock' => 'get_lock',
79 'release_lock' => 'release_lock',
80 'ucase' => 'ucase',
81 'lcase' => 'lcase',
82 'unhex' => 'unhex',
83 'from_base64' => 'from_base64',
84 'to_base64' => 'to_base64',
85 'inet_ntoa' => 'inet_ntoa',
86 'inet_aton' => 'inet_aton',
87 'datediff' => 'datediff',
88 'locate' => 'locate',
89 'utc_date' => 'utc_date',
90 'utc_time' => 'utc_time',
91 'utc_timestamp' => 'utc_timestamp',
92 'version' => 'version',
93
94 // Internal helper functions.
95 '_helper_like_to_glob_pattern' => '_helper_like_to_glob_pattern',
96 );
97
98 /**
99 * A helper function to throw an error from SQLite expressions.
100 *
101 * @param string $message The error message.
102 *
103 * @throws Exception The error message.
104 * @return void
105 */
106 public function throw( $message ): void {
107 throw new Exception( $message );
108 }
109
110 /**
111 * Method to return the unix timestamp.
112 *
113 * Used without an argument, it returns PHP time() function (total seconds passed
114 * from '1970-01-01 00:00:00' GMT). Used with the argument, it changes the value
115 * to the timestamp.
116 *
117 * @param string $field Representing the date formatted as '0000-00-00 00:00:00'.
118 *
119 * @return number of unsigned integer
120 */
121 public function unix_timestamp( $field = null ) {
122 return is_null( $field ) ? time() : strtotime( $field );
123 }
124
125 /**
126 * Method to emulate MySQL FROM_UNIXTIME() function.
127 *
128 * @param int $field The unix timestamp.
129 * @param string $format Indicate the way of formatting(optional).
130 *
131 * @return string
132 */
133 public function from_unixtime( $field, $format = null ) {
134 // Convert to ISO time.
135 $date = gmdate( 'Y-m-d H:i:s', $field );
136
137 return is_null( $format ) ? $date : $this->dateformat( $date, $format );
138 }
139
140 /**
141 * Method to emulate MySQL NOW() function.
142 *
143 * @return string representing current time formatted as '0000-00-00 00:00:00'.
144 */
145 public function now() {
146 return gmdate( 'Y-m-d H:i:s' );
147 }
148
149 /**
150 * Method to emulate MySQL CURDATE() function.
151 *
152 * @return string representing current time formatted as '0000-00-00'.
153 */
154 public function curdate() {
155 return gmdate( 'Y-m-d' );
156 }
157
158 /**
159 * Method to emulate MySQL MD5() function.
160 *
161 * @param string $field The string to be hashed.
162 *
163 * @return string of the md5 hash value of the argument.
164 */
165 public function md5( $field ) {
166 return md5( $field );
167 }
168
169 /**
170 * Method to emulate MySQL RAND() function.
171 *
172 * SQLite does have a random generator, but it is called RANDOM() and returns random
173 * number between -9223372036854775808 and +9223372036854775807. So we substitute it
174 * with PHP random generator.
175 *
176 * This function uses mt_rand() which is four times faster than rand() and returns
177 * the random number between 0 and 1.
178 *
179 * @return int
180 */
181 public function rand() {
182 return mt_rand( 0, 1 );
183 }
184
185 /**
186 * Method to emulate MySQL DATEFORMAT() function.
187 *
188 * @param string $date Formatted as '0000-00-00' or datetime as '0000-00-00 00:00:00'.
189 * @param string $format The string format.
190 *
191 * @return string formatted according to $format
192 */
193 public function dateformat( $date, $format ) {
194 $mysql_php_date_formats = array(
195 '%a' => 'D',
196 '%b' => 'M',
197 '%c' => 'n',
198 '%D' => 'jS',
199 '%d' => 'd',
200 '%e' => 'j',
201 '%H' => 'H',
202 '%h' => 'h',
203 '%I' => 'h',
204 '%i' => 'i',
205 '%j' => 'z',
206 '%k' => 'G',
207 '%l' => 'g',
208 '%M' => 'F',
209 '%m' => 'm',
210 '%p' => 'A',
211 '%r' => 'h:i:s A',
212 '%S' => 's',
213 '%s' => 's',
214 '%T' => 'H:i:s',
215 '%U' => 'W',
216 '%u' => 'W',
217 '%V' => 'W',
218 '%v' => 'W',
219 '%W' => 'l',
220 '%w' => 'w',
221 '%X' => 'Y',
222 '%x' => 'o',
223 '%Y' => 'Y',
224 '%y' => 'y',
225 );
226
227 $time = strtotime( $date );
228 $format = strtr( $format, $mysql_php_date_formats );
229
230 return gmdate( $format, $time );
231 }
232
233 /**
234 * Method to extract the month value from the date.
235 *
236 * @param string $field Representing the date formatted as 0000-00-00.
237 *
238 * @return string Representing the number of the month between 1 and 12.
239 */
240 public function month( $field ) {
241 /*
242 * MySQL returns 0 for MONTH('0000-00-00') and for dates with
243 * zero month parts like '2020-00-15'. PHP's strtotime() can't
244 * parse these, so we extract the month directly from the string.
245 */
246 if ( preg_match( '/^\d{4}-(\d{2})/', $field, $matches ) ) {
247 return intval( $matches[1] );
248 }
249 /*
250 * From https://www.php.net/manual/en/datetime.format.php:
251 *
252 * n - Numeric representation of a month, without leading zeros.
253 * 1 through 12
254 */
255 return intval( gmdate( 'n', strtotime( $field ) ) );
256 }
257
258 /**
259 * Method to extract the year value from the date.
260 *
261 * @param string $field Representing the date formatted as 0000-00-00.
262 *
263 * @return string Representing the number of the year.
264 */
265 public function year( $field ) {
266 /*
267 * MySQL returns 0 for YEAR('0000-00-00'). PHP's strtotime()
268 * can't parse zero dates, so we extract the year directly.
269 */
270 if ( preg_match( '/^(\d{4})-\d{2}/', $field, $matches ) ) {
271 return intval( $matches[1] );
272 }
273 /*
274 * From https://www.php.net/manual/en/datetime.format.php:
275 *
276 * Y - A full numeric representation of a year, 4 digits.
277 */
278 return intval( gmdate( 'Y', strtotime( $field ) ) );
279 }
280
281 /**
282 * Method to extract the day value from the date.
283 *
284 * @param string $field Representing the date formatted as 0000-00-00.
285 *
286 * @return string Representing the number of the day of the month from 1 and 31.
287 */
288 public function day( $field ) {
289 /*
290 * MySQL returns 0 for DAY('0000-00-00') and for dates with
291 * zero day parts like '2020-01-00'. PHP's strtotime() can't
292 * parse these, so we extract the day directly from the string.
293 */
294 if ( preg_match( '/^\d{4}-\d{2}-(\d{2})/', $field, $matches ) ) {
295 return intval( $matches[1] );
296 }
297 /*
298 * From https://www.php.net/manual/en/datetime.format.php:
299 *
300 * j - Day of the month without leading zeros.
301 * 1 to 31.
302 */
303 return intval( gmdate( 'j', strtotime( $field ) ) );
304 }
305
306 /**
307 * Method to emulate MySQL SECOND() function.
308 *
309 * @see https://www.php.net/manual/en/datetime.format.php
310 *
311 * @param string $field Representing the time formatted as '00:00:00'.
312 *
313 * @return number Unsigned integer
314 */
315 public function second( $field ) {
316 /*
317 * From https://www.php.net/manual/en/datetime.format.php:
318 *
319 * s - Seconds, with leading zeros (00 to 59)
320 */
321 return intval( gmdate( 's', strtotime( $field ) ) );
322 }
323
324 /**
325 * Method to emulate MySQL MINUTE() function.
326 *
327 * @param string $field Representing the time formatted as '00:00:00'.
328 *
329 * @return int
330 */
331 public function minute( $field ) {
332 /*
333 * From https://www.php.net/manual/en/datetime.format.php:
334 *
335 * i - Minutes with leading zeros.
336 * 00 to 59.
337 */
338 return intval( gmdate( 'i', strtotime( $field ) ) );
339 }
340
341 /**
342 * Method to emulate MySQL HOUR() function.
343 *
344 * Returns the hour for time, in 24-hour format, from 0 to 23.
345 * Importantly, midnight is 0, not 24.
346 *
347 * @param string $time Representing the time formatted, like '14:08:12'.
348 *
349 * @return int
350 */
351 public function hour( $time ) {
352 /*
353 * From https://www.php.net/manual/en/datetime.format.php:
354 *
355 * H 24-hour format of an hour with leading zeros.
356 * 00 through 23.
357 */
358 return intval( gmdate( 'H', strtotime( $time ) ) );
359 }
360
361 /**
362 * Covers MySQL WEEK() function.
363 *
364 * Always assumes $mode = 1.
365 *
366 * @TODO: Support other modes.
367 *
368 * From https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week:
369 *
370 * > Returns the week number for date. The two-argument form of WEEK()
371 * > enables you to specify whether the week starts on Sunday or Monday
372 * > and whether the return value should be in the range from 0 to 53
373 * > or from 1 to 53. If the mode argument is omitted, the value of the
374 * > default_week_format system variable is used.
375 * >
376 * > The following table describes how the mode argument works:
377 * >
378 * > Mode First day of week Range Week 1 is the first week …
379 * > 0 Sunday 0-53 with a Sunday in this year
380 * > 1 Monday 0-53 with 4 or more days this year
381 * > 2 Sunday 1-53 with a Sunday in this year
382 * > 3 Monday 1-53 with 4 or more days this year
383 * > 4 Sunday 0-53 with 4 or more days this year
384 * > 5 Monday 0-53 with a Monday in this year
385 * > 6 Sunday 1-53 with 4 or more days this year
386 * > 7 Monday 1-53 with a Monday in this year
387 *
388 * @param string $field Representing the date.
389 * @param int $mode The mode argument.
390 */
391 public function week( $field, $mode ) {
392 /*
393 * From https://www.php.net/manual/en/datetime.format.php:
394 *
395 * W - ISO-8601 week number of year, weeks starting on Monday.
396 * Example: 42 (the 42nd week in the year)
397 *
398 * Week 1 is the first week with a Thursday in it.
399 */
400 return intval( gmdate( 'W', strtotime( $field ) ) );
401 }
402
403 /**
404 * Simulates WEEKDAY() function in MySQL.
405 *
406 * Returns the day of the week as an integer.
407 * The days of the week are numbered 0 to 6:
408 * * 0 for Monday
409 * * 1 for Tuesday
410 * * 2 for Wednesday
411 * * 3 for Thursday
412 * * 4 for Friday
413 * * 5 for Saturday
414 * * 6 for Sunday
415 *
416 * @param string $field Representing the date.
417 *
418 * @return int
419 */
420 public function weekday( $field ) {
421 /*
422 * date('N') returns 1 (for Monday) through 7 (for Sunday)
423 * That's one more than MySQL.
424 * Let's subtract one to make it compatible.
425 */
426 return intval( gmdate( 'N', strtotime( $field ) ) ) - 1;
427 }
428
429 /**
430 * Method to emulate MySQL DAYOFMONTH() function.
431 *
432 * @see https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_dayofmonth
433 *
434 * @param string $field Representing the date.
435 *
436 * @return int Returns the day of the month for date as a number in the range 1 to 31.
437 */
438 public function dayofmonth( $field ) {
439 return intval( gmdate( 'j', strtotime( $field ) ) );
440 }
441
442 /**
443 * Method to emulate MySQL DAYOFWEEK() function.
444 *
445 * > Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday).
446 * > These index values correspond to the ODBC standard. Returns NULL if date is NULL.
447 *
448 * @param string $field Representing the date.
449 *
450 * @return int Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday).
451 */
452 public function dayofweek( $field ) {
453 /**
454 * From https://www.php.net/manual/en/datetime.format.php:
455 *
456 * `w` – Numeric representation of the day of the week
457 * 0 (for Sunday) through 6 (for Saturday)
458 */
459 return intval( gmdate( 'w', strtotime( $field ) ) ) + 1;
460 }
461
462 /**
463 * Method to emulate MySQL DATE() function.
464 *
465 * @see https://www.php.net/manual/en/datetime.format.php
466 *
467 * @param string $date formatted as unix time.
468 *
469 * @return string formatted as '0000-00-00'.
470 */
471 public function date( $date ) {
472 return gmdate( 'Y-m-d', strtotime( $date ) );
473 }
474
475 /**
476 * Method to emulate MySQL ISNULL() function.
477 *
478 * This function returns true if the argument is null, and true if not.
479 *
480 * @param mixed $field The field to be tested.
481 *
482 * @return boolean
483 */
484 public function isnull( $field ) {
485 return is_null( $field );
486 }
487
488 /**
489 * Method to emulate MySQL IF() function.
490 *
491 * As 'IF' is a reserved word for PHP, function name must be changed.
492 *
493 * @param mixed $expression The statement to be evaluated as true or false.
494 * @param mixed $truthy Statement or value returned if $expression is true.
495 * @param mixed $falsy Statement or value returned if $expression is false.
496 *
497 * @return mixed
498 */
499 public function _if( $expression, $truthy, $falsy ) {
500 return ( true === $expression ) ? $truthy : $falsy;
501 }
502
503 /**
504 * Method to emulate MySQL REGEXP() function.
505 *
506 * @param string $pattern Regular expression to match.
507 * @param string $field Haystack.
508 *
509 * @return integer 1 if matched, 0 if not matched.
510 */
511 public function regexp( $pattern, $field ) {
512 /*
513 * If the original query says REGEXP BINARY
514 * the comparison is byte-by-byte and letter casing now
515 * matters since lower- and upper-case letters have different
516 * byte codes.
517 *
518 * The REGEXP function can't be easily made to accept two
519 * parameters, so we'll have to use a hack to get around this.
520 *
521 * If the first character of the pattern is a null byte, we'll
522 * remove it and make the comparison case-sensitive. This should
523 * be reasonably safe since PHP does not allow null bytes in
524 * regular expressions anyway.
525 */
526 if ( "\x00" === $pattern[0] ) {
527 $pattern = substr( $pattern, 1 );
528 $flags = '';
529 } else {
530 // Otherwise, the search is case-insensitive.
531 $flags = 'i';
532 }
533 $pattern = str_replace( '/', '\/', $pattern );
534 $pattern = '/' . $pattern . '/' . $flags;
535
536 return preg_match( $pattern, $field );
537 }
538
539 /**
540 * Method to emulate MySQL FIELD() function.
541 *
542 * This function gets the list argument and compares the first item to all the others.
543 * If the same value is found, it returns the position of that value. If not, it
544 * returns 0.
545 *
546 * @return int
547 */
548 public function field() {
549 $num_args = func_num_args();
550 if ( $num_args < 2 || is_null( func_get_arg( 0 ) ) ) {
551 return 0;
552 }
553 $arg_list = func_get_args();
554 $search_string = strtolower( array_shift( $arg_list ) );
555
556 for ( $i = 0; $i < $num_args - 1; $i++ ) {
557 if ( strtolower( $arg_list[ $i ] ) === $search_string ) {
558 return $i + 1;
559 }
560 }
561
562 return 0;
563 }
564
565 /**
566 * Method to emulate MySQL LOG() function.
567 *
568 * Used with one argument, it returns the natural logarithm of X.
569 * <code>
570 * LOG(X)
571 * </code>
572 * Used with two arguments, it returns the natural logarithm of X base B.
573 * <code>
574 * LOG(B, X)
575 * </code>
576 * In this case, it returns the value of log(X) / log(B).
577 *
578 * Used without an argument, it returns false. This returned value will be
579 * rewritten to 0, because SQLite doesn't understand true/false value.
580 *
581 * @return double|null
582 */
583 public function log() {
584 $num_args = func_num_args();
585 if ( 1 === $num_args ) {
586 $arg1 = func_get_arg( 0 );
587
588 return log( $arg1 );
589 }
590 if ( 2 === $num_args ) {
591 $arg1 = func_get_arg( 0 );
592 $arg2 = func_get_arg( 1 );
593
594 return log( $arg1 ) / log( $arg2 );
595 }
596 return null;
597 }
598
599 /**
600 * Method to emulate MySQL LEAST() function.
601 *
602 * This function rewrites the function name to SQLite compatible function name.
603 *
604 * @return mixed
605 */
606 public function least() {
607 $arg_list = func_get_args();
608
609 return min( $arg_list );
610 }
611
612 /**
613 * Method to emulate MySQL GREATEST() function.
614 *
615 * This function rewrites the function name to SQLite compatible function name.
616 *
617 * @return mixed
618 */
619 public function greatest() {
620 $arg_list = func_get_args();
621
622 return max( $arg_list );
623 }
624
625 /**
626 * Method to dummy out MySQL GET_LOCK() function.
627 *
628 * This function is meaningless in SQLite, so we do nothing.
629 *
630 * @param string $name Not used.
631 * @param integer $timeout Not used.
632 *
633 * @return string
634 */
635 public function get_lock( $name, $timeout ) {
636 return '1=1';
637 }
638
639 /**
640 * Method to dummy out MySQL RELEASE_LOCK() function.
641 *
642 * This function is meaningless in SQLite, so we do nothing.
643 *
644 * @param string $name Not used.
645 *
646 * @return string
647 */
648 public function release_lock( $name ) {
649 return '1=1';
650 }
651
652 /**
653 * Method to emulate MySQL UCASE() function.
654 *
655 * This is MySQL alias for upper() function. This function rewrites it
656 * to SQLite compatible name upper().
657 *
658 * @param string $content String to be converted to uppercase.
659 *
660 * @return string SQLite compatible function name.
661 */
662 public function ucase( $content ) {
663 return "upper($content)";
664 }
665
666 /**
667 * Method to emulate MySQL LCASE() function.
668 *
669 * This is MySQL alias for lower() function. This function rewrites it
670 * to SQLite compatible name lower().
671 *
672 * @param string $content String to be converted to lowercase.
673 *
674 * @return string SQLite compatible function name.
675 */
676 public function lcase( $content ) {
677 return "lower($content)";
678 }
679
680 /**
681 * Method to emulate MySQL UNHEX() function.
682 *
683 * For a string argument str, UNHEX(str) interprets each pair of characters
684 * in the argument as a hexadecimal number and converts it to the byte represented
685 * by the number. The return value is a binary string.
686 *
687 * @param string $number Number to be unhexed.
688 *
689 * @return string Binary string
690 */
691 public function unhex( $number ) {
692 return pack( 'H*', $number );
693 }
694
695 /**
696 * Method to emulate MySQL FROM_BASE64() function.
697 *
698 * Takes a base64-encoded string and returns the decoded result as a binary
699 * string. Returns NULL if the argument is NULL or is not a valid base64 string.
700 *
701 * @param string|null $str The base64-encoded string.
702 *
703 * @return string|null Decoded binary string, or NULL.
704 */
705 public function from_base64( $str ) {
706 if ( null === $str ) {
707 return null;
708 }
709 $decoded = base64_decode( $str, true );
710 if ( false === $decoded ) {
711 return null;
712 }
713 return $decoded;
714 }
715
716 /**
717 * Method to emulate MySQL TO_BASE64() function.
718 *
719 * Takes a string and returns a base64-encoded result.
720 * Returns NULL if the argument is NULL.
721 *
722 * @param string|null $str The string to encode.
723 *
724 * @return string|null Base64-encoded string, or NULL.
725 */
726 public function to_base64( $str ) {
727 if ( null === $str ) {
728 return null;
729 }
730 return base64_encode( $str );
731 }
732
733 /**
734 * Method to emulate MySQL INET_NTOA() function.
735 *
736 * This function gets 4 or 8 bytes integer and turn it into the network address.
737 *
738 * @param integer $num Long integer.
739 *
740 * @return string
741 */
742 public function inet_ntoa( $num ) {
743 return long2ip( $num );
744 }
745
746 /**
747 * Method to emulate MySQL INET_ATON() function.
748 *
749 * This function gets the network address and turns it into integer.
750 *
751 * @param string $addr Network address.
752 *
753 * @return int long integer
754 */
755 public function inet_aton( $addr ) {
756 return absint( ip2long( $addr ) );
757 }
758
759 /**
760 * Method to emulate MySQL DATEDIFF() function.
761 *
762 * This function compares two dates value and returns the difference.
763 *
764 * @param string $start Start date.
765 * @param string $end End date.
766 *
767 * @return string
768 */
769 public function datediff( $start, $end ) {
770 $start_date = new DateTime( $start );
771 $end_date = new DateTime( $end );
772 $interval = $end_date->diff( $start_date, false );
773
774 return $interval->format( '%r%a' );
775 }
776
777 /**
778 * Method to emulate MySQL LOCATE() function.
779 *
780 * This function returns the position if $substr is found in $str. If not,
781 * it returns 0. If mbstring extension is loaded, mb_strpos() function is
782 * used.
783 *
784 * @param string $substr Needle.
785 * @param string $str Haystack.
786 * @param integer $pos Position.
787 *
788 * @return integer
789 */
790 public function locate( $substr, $str, $pos = 0 ) {
791 if ( ! extension_loaded( 'mbstring' ) ) {
792 $val = strpos( $str, $substr, $pos );
793 if ( false !== $val ) {
794 return $val + 1;
795 }
796 return 0;
797 }
798 $val = mb_strpos( $str, $substr, $pos );
799 if ( false !== $val ) {
800 return $val + 1;
801 }
802 return 0;
803 }
804
805 /**
806 * Method to return GMT date in the string format.
807 *
808 * @return string formatted GMT date 'dddd-mm-dd'
809 */
810 public function utc_date() {
811 return gmdate( 'Y-m-d', time() );
812 }
813
814 /**
815 * Method to return GMT time in the string format.
816 *
817 * @return string formatted GMT time '00:00:00'
818 */
819 public function utc_time() {
820 return gmdate( 'H:i:s', time() );
821 }
822
823 /**
824 * Method to return GMT time stamp in the string format.
825 *
826 * @return string formatted GMT timestamp 'yyyy-mm-dd 00:00:00'
827 */
828 public function utc_timestamp() {
829 return gmdate( 'Y-m-d H:i:s', time() );
830 }
831
832 /**
833 * Method to return MySQL version.
834 *
835 * This function only returns the current newest version number of MySQL,
836 * because it is meaningless for SQLite database.
837 *
838 * @return string representing the version number: major_version.minor_version
839 */
840 public function version() {
841 return '5.5';
842 }
843
844 /**
845 * A helper to covert LIKE pattern to a GLOB pattern for "LIKE BINARY" support.
846
847 * @TODO: Some of the MySQL string specifics described below are likely to
848 * affect also other patterns than just "LIKE BINARY". We should
849 * consider applying some of the conversions more broadly.
850 *
851 * @param string $pattern
852 * @return string
853 */
854 public function _helper_like_to_glob_pattern( $pattern ) {
855 if ( null === $pattern ) {
856 return null;
857 }
858
859 /*
860 * 1. Escape characters that have special meaning in GLOB patterns.
861 *
862 * We need to:
863 * 1. Escape "]" as "[]]" to avoid interpreting "[...]" as a character class.
864 * 2. Escape "*" as "[*]" (must be after 1 to avoid being escaped).
865 * 3. Escape "?" as "[?]" (must be after 1 to avoid being escaped).
866 */
867 $pattern = str_replace( ']', '[]]', $pattern );
868 $pattern = str_replace( '*', '[*]', $pattern );
869 $pattern = str_replace( '?', '[?]', $pattern );
870
871 /*
872 * 2. Convert LIKE wildcards to GLOB wildcards ("%" -> "*", "_" -> "?").
873 *
874 * We need to convert them only when they don't follow any backslashes,
875 * or when they follow an even number of backslashes (as "\\" is "\").
876 */
877 $pattern = preg_replace( '/(^|[^\\\\](?:\\\\{2})*)%/', '$1*', $pattern );
878 $pattern = preg_replace( '/(^|[^\\\\](?:\\\\{2})*)_/', '$1?', $pattern );
879
880 /*
881 * 3. Unescape LIKE escape sequences.
882 *
883 * While in MySQL LIKE patterns, a backslash is usually used to escape
884 * special characters ("%", "_", and "\"), it works with all characters.
885 *
886 * That is:
887 * SELECT '\\x' prints '\x', but LIKE '\\x' is equivalent to LIKE 'x'.
888 *
889 * This is true also for multi-byte characters:
890 * SELECT '\\©' prints '\©', but LIKE '\\©' is equivalent to LIKE '©'.
891 *
892 * However, the multi-byte behavior is likely to depend on the charset.
893 * For now, we'll assume UTF-8 and thus the "u" modifier for the regex.
894 */
895 $pattern = preg_replace( '/\\\\(.)/u', '$1', $pattern );
896
897 return $pattern;
898 }
899 }
900