Split out ConvertableTimestamp class
[lhc/web/wiklou.git] / includes / db / DatabaseSqlite.php
1 <?php
2 /**
3 * This is the SQLite database abstraction layer.
4 * See maintenance/sqlite/README for development notes and other specific information
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Database
23 */
24
25 /**
26 * @ingroup Database
27 */
28 class DatabaseSqlite extends Database {
29 /** @var bool Whether full text is enabled */
30 private static $fulltextEnabled = null;
31
32 /** @var string Directory */
33 protected $dbDir;
34
35 /** @var string File name for SQLite database file */
36 protected $dbPath;
37
38 /** @var string Transaction mode */
39 protected $trxMode;
40
41 /** @var int The number of rows affected as an integer */
42 protected $mAffectedRows;
43
44 /** @var resource */
45 protected $mLastResult;
46
47 /** @var PDO */
48 protected $mConn;
49
50 /** @var FSLockManager (hopefully on the same server as the DB) */
51 protected $lockMgr;
52
53 /**
54 * Additional params include:
55 * - dbDirectory : directory containing the DB and the lock file directory
56 * [defaults to $wgSQLiteDataDir]
57 * - dbFilePath : use this to force the path of the DB file
58 * - trxMode : one of (deferred, immediate, exclusive)
59 * @param array $p
60 */
61 function __construct( array $p ) {
62 global $wgSQLiteDataDir;
63
64 $this->dbDir = isset( $p['dbDirectory'] ) ? $p['dbDirectory'] : $wgSQLiteDataDir;
65
66 if ( isset( $p['dbFilePath'] ) ) {
67 parent::__construct( $p );
68 // Standalone .sqlite file mode.
69 // Super doesn't open when $user is false, but we can work with $dbName,
70 // which is derived from the file path in this case.
71 $this->openFile( $p['dbFilePath'] );
72 } else {
73 $this->mDBname = $p['dbname'];
74 // Stock wiki mode using standard file names per DB.
75 parent::__construct( $p );
76 // Super doesn't open when $user is false, but we can work with $dbName
77 if ( $p['dbname'] && !$this->isOpen() ) {
78 if ( $this->open( $p['host'], $p['user'], $p['password'], $p['dbname'] ) ) {
79 $done = [];
80 foreach ( $this->tableAliases as $params ) {
81 if ( isset( $done[$params['dbname']] ) ) {
82 continue;
83 }
84 $this->attachDatabase( $params['dbname'] );
85 $done[$params['dbname']] = 1;
86 }
87 }
88 }
89 }
90
91 $this->trxMode = isset( $p['trxMode'] ) ? strtoupper( $p['trxMode'] ) : null;
92 if ( $this->trxMode &&
93 !in_array( $this->trxMode, [ 'DEFERRED', 'IMMEDIATE', 'EXCLUSIVE' ] )
94 ) {
95 $this->trxMode = null;
96 wfWarn( "Invalid SQLite transaction mode provided." );
97 }
98
99 $this->lockMgr = new FSLockManager( [ 'lockDirectory' => "{$this->dbDir}/locks" ] );
100 }
101
102 /**
103 * @param string $filename
104 * @param array $p Options map; supports:
105 * - flags : (same as __construct counterpart)
106 * - trxMode : (same as __construct counterpart)
107 * - dbDirectory : (same as __construct counterpart)
108 * @return DatabaseSqlite
109 * @since 1.25
110 */
111 public static function newStandaloneInstance( $filename, array $p = [] ) {
112 $p['dbFilePath'] = $filename;
113 $p['schema'] = false;
114 $p['tablePrefix'] = '';
115
116 return DatabaseBase::factory( 'sqlite', $p );
117 }
118
119 /**
120 * @return string
121 */
122 function getType() {
123 return 'sqlite';
124 }
125
126 /**
127 * @todo Check if it should be true like parent class
128 *
129 * @return bool
130 */
131 function implicitGroupby() {
132 return false;
133 }
134
135 /** Open an SQLite database and return a resource handle to it
136 * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases
137 *
138 * @param string $server
139 * @param string $user
140 * @param string $pass
141 * @param string $dbName
142 *
143 * @throws DBConnectionError
144 * @return PDO
145 */
146 function open( $server, $user, $pass, $dbName ) {
147 $this->close();
148 $fileName = self::generateFileName( $this->dbDir, $dbName );
149 if ( !is_readable( $fileName ) ) {
150 $this->mConn = false;
151 throw new DBConnectionError( $this, "SQLite database not accessible" );
152 }
153 $this->openFile( $fileName );
154
155 return $this->mConn;
156 }
157
158 /**
159 * Opens a database file
160 *
161 * @param string $fileName
162 * @throws DBConnectionError
163 * @return PDO|bool SQL connection or false if failed
164 */
165 protected function openFile( $fileName ) {
166 $err = false;
167
168 $this->dbPath = $fileName;
169 try {
170 if ( $this->mFlags & DBO_PERSISTENT ) {
171 $this->mConn = new PDO( "sqlite:$fileName", '', '',
172 [ PDO::ATTR_PERSISTENT => true ] );
173 } else {
174 $this->mConn = new PDO( "sqlite:$fileName", '', '' );
175 }
176 } catch ( PDOException $e ) {
177 $err = $e->getMessage();
178 }
179
180 if ( !$this->mConn ) {
181 wfDebug( "DB connection error: $err\n" );
182 throw new DBConnectionError( $this, $err );
183 }
184
185 $this->mOpened = !!$this->mConn;
186 if ( $this->mOpened ) {
187 # Set error codes only, don't raise exceptions
188 $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
189 # Enforce LIKE to be case sensitive, just like MySQL
190 $this->query( 'PRAGMA case_sensitive_like = 1' );
191
192 return $this->mConn;
193 }
194
195 return false;
196 }
197
198 /**
199 * @return string SQLite DB file path
200 * @since 1.25
201 */
202 public function getDbFilePath() {
203 return $this->dbPath;
204 }
205
206 /**
207 * Does not actually close the connection, just destroys the reference for GC to do its work
208 * @return bool
209 */
210 protected function closeConnection() {
211 $this->mConn = null;
212
213 return true;
214 }
215
216 /**
217 * Generates a database file name. Explicitly public for installer.
218 * @param string $dir Directory where database resides
219 * @param string $dbName Database name
220 * @return string
221 */
222 public static function generateFileName( $dir, $dbName ) {
223 return "$dir/$dbName.sqlite";
224 }
225
226 /**
227 * Check if the searchindext table is FTS enabled.
228 * @return bool False if not enabled.
229 */
230 function checkForEnabledSearch() {
231 if ( self::$fulltextEnabled === null ) {
232 self::$fulltextEnabled = false;
233 $table = $this->tableName( 'searchindex' );
234 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name = '$table'", __METHOD__ );
235 if ( $res ) {
236 $row = $res->fetchRow();
237 self::$fulltextEnabled = stristr( $row['sql'], 'fts' ) !== false;
238 }
239 }
240
241 return self::$fulltextEnabled;
242 }
243
244 /**
245 * Returns version of currently supported SQLite fulltext search module or false if none present.
246 * @return string
247 */
248 static function getFulltextSearchModule() {
249 static $cachedResult = null;
250 if ( $cachedResult !== null ) {
251 return $cachedResult;
252 }
253 $cachedResult = false;
254 $table = 'dummy_search_test';
255
256 $db = self::newStandaloneInstance( ':memory:' );
257 if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__, true ) ) {
258 $cachedResult = 'FTS3';
259 }
260 $db->close();
261
262 return $cachedResult;
263 }
264
265 /**
266 * Attaches external database to our connection, see http://sqlite.org/lang_attach.html
267 * for details.
268 *
269 * @param string $name Database name to be used in queries like
270 * SELECT foo FROM dbname.table
271 * @param bool|string $file Database file name. If omitted, will be generated
272 * using $name and configured data directory
273 * @param string $fname Calling function name
274 * @return ResultWrapper
275 */
276 function attachDatabase( $name, $file = false, $fname = __METHOD__ ) {
277 if ( !$file ) {
278 $file = self::generateFileName( $this->dbDir, $name );
279 }
280 $file = $this->addQuotes( $file );
281
282 return $this->query( "ATTACH DATABASE $file AS $name", $fname );
283 }
284
285 function isWriteQuery( $sql ) {
286 return parent::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql );
287 }
288
289 /**
290 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
291 *
292 * @param string $sql
293 * @return bool|ResultWrapper
294 */
295 protected function doQuery( $sql ) {
296 $res = $this->mConn->query( $sql );
297 if ( $res === false ) {
298 return false;
299 } else {
300 $r = $res instanceof ResultWrapper ? $res->result : $res;
301 $this->mAffectedRows = $r->rowCount();
302 $res = new ResultWrapper( $this, $r->fetchAll() );
303 }
304
305 return $res;
306 }
307
308 /**
309 * @param ResultWrapper|mixed $res
310 */
311 function freeResult( $res ) {
312 if ( $res instanceof ResultWrapper ) {
313 $res->result = null;
314 } else {
315 $res = null;
316 }
317 }
318
319 /**
320 * @param ResultWrapper|array $res
321 * @return stdClass|bool
322 */
323 function fetchObject( $res ) {
324 if ( $res instanceof ResultWrapper ) {
325 $r =& $res->result;
326 } else {
327 $r =& $res;
328 }
329
330 $cur = current( $r );
331 if ( is_array( $cur ) ) {
332 next( $r );
333 $obj = new stdClass;
334 foreach ( $cur as $k => $v ) {
335 if ( !is_numeric( $k ) ) {
336 $obj->$k = $v;
337 }
338 }
339
340 return $obj;
341 }
342
343 return false;
344 }
345
346 /**
347 * @param ResultWrapper|mixed $res
348 * @return array|bool
349 */
350 function fetchRow( $res ) {
351 if ( $res instanceof ResultWrapper ) {
352 $r =& $res->result;
353 } else {
354 $r =& $res;
355 }
356 $cur = current( $r );
357 if ( is_array( $cur ) ) {
358 next( $r );
359
360 return $cur;
361 }
362
363 return false;
364 }
365
366 /**
367 * The PDO::Statement class implements the array interface so count() will work
368 *
369 * @param ResultWrapper|array $res
370 * @return int
371 */
372 function numRows( $res ) {
373 $r = $res instanceof ResultWrapper ? $res->result : $res;
374
375 return count( $r );
376 }
377
378 /**
379 * @param ResultWrapper $res
380 * @return int
381 */
382 function numFields( $res ) {
383 $r = $res instanceof ResultWrapper ? $res->result : $res;
384 if ( is_array( $r ) && count( $r ) > 0 ) {
385 // The size of the result array is twice the number of fields. (Bug: 65578)
386 return count( $r[0] ) / 2;
387 } else {
388 // If the result is empty return 0
389 return 0;
390 }
391 }
392
393 /**
394 * @param ResultWrapper $res
395 * @param int $n
396 * @return bool
397 */
398 function fieldName( $res, $n ) {
399 $r = $res instanceof ResultWrapper ? $res->result : $res;
400 if ( is_array( $r ) ) {
401 $keys = array_keys( $r[0] );
402
403 return $keys[$n];
404 }
405
406 return false;
407 }
408
409 /**
410 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
411 *
412 * @param string $name
413 * @param string $format
414 * @return string
415 */
416 function tableName( $name, $format = 'quoted' ) {
417 // table names starting with sqlite_ are reserved
418 if ( strpos( $name, 'sqlite_' ) === 0 ) {
419 return $name;
420 }
421
422 return str_replace( '"', '', parent::tableName( $name, $format ) );
423 }
424
425 /**
426 * Index names have DB scope
427 *
428 * @param string $index
429 * @return string
430 */
431 protected function indexName( $index ) {
432 return $index;
433 }
434
435 /**
436 * This must be called after nextSequenceVal
437 *
438 * @return int
439 */
440 function insertId() {
441 // PDO::lastInsertId yields a string :(
442 return intval( $this->mConn->lastInsertId() );
443 }
444
445 /**
446 * @param ResultWrapper|array $res
447 * @param int $row
448 */
449 function dataSeek( $res, $row ) {
450 if ( $res instanceof ResultWrapper ) {
451 $r =& $res->result;
452 } else {
453 $r =& $res;
454 }
455 reset( $r );
456 if ( $row > 0 ) {
457 for ( $i = 0; $i < $row; $i++ ) {
458 next( $r );
459 }
460 }
461 }
462
463 /**
464 * @return string
465 */
466 function lastError() {
467 if ( !is_object( $this->mConn ) ) {
468 return "Cannot return last error, no db connection";
469 }
470 $e = $this->mConn->errorInfo();
471
472 return isset( $e[2] ) ? $e[2] : '';
473 }
474
475 /**
476 * @return string
477 */
478 function lastErrno() {
479 if ( !is_object( $this->mConn ) ) {
480 return "Cannot return last error, no db connection";
481 } else {
482 $info = $this->mConn->errorInfo();
483
484 return $info[1];
485 }
486 }
487
488 /**
489 * @return int
490 */
491 function affectedRows() {
492 return $this->mAffectedRows;
493 }
494
495 /**
496 * Returns information about an index
497 * Returns false if the index does not exist
498 * - if errors are explicitly ignored, returns NULL on failure
499 *
500 * @param string $table
501 * @param string $index
502 * @param string $fname
503 * @return array
504 */
505 function indexInfo( $table, $index, $fname = __METHOD__ ) {
506 $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
507 $res = $this->query( $sql, $fname );
508 if ( !$res ) {
509 return null;
510 }
511 if ( $res->numRows() == 0 ) {
512 return false;
513 }
514 $info = [];
515 foreach ( $res as $row ) {
516 $info[] = $row->name;
517 }
518
519 return $info;
520 }
521
522 /**
523 * @param string $table
524 * @param string $index
525 * @param string $fname
526 * @return bool|null
527 */
528 function indexUnique( $table, $index, $fname = __METHOD__ ) {
529 $row = $this->selectRow( 'sqlite_master', '*',
530 [
531 'type' => 'index',
532 'name' => $this->indexName( $index ),
533 ], $fname );
534 if ( !$row || !isset( $row->sql ) ) {
535 return null;
536 }
537
538 // $row->sql will be of the form CREATE [UNIQUE] INDEX ...
539 $indexPos = strpos( $row->sql, 'INDEX' );
540 if ( $indexPos === false ) {
541 return null;
542 }
543 $firstPart = substr( $row->sql, 0, $indexPos );
544 $options = explode( ' ', $firstPart );
545
546 return in_array( 'UNIQUE', $options );
547 }
548
549 /**
550 * Filter the options used in SELECT statements
551 *
552 * @param array $options
553 * @return array
554 */
555 function makeSelectOptions( $options ) {
556 foreach ( $options as $k => $v ) {
557 if ( is_numeric( $k ) && ( $v == 'FOR UPDATE' || $v == 'LOCK IN SHARE MODE' ) ) {
558 $options[$k] = '';
559 }
560 }
561
562 return parent::makeSelectOptions( $options );
563 }
564
565 /**
566 * @param array $options
567 * @return string
568 */
569 protected function makeUpdateOptionsArray( $options ) {
570 $options = parent::makeUpdateOptionsArray( $options );
571 $options = self::fixIgnore( $options );
572
573 return $options;
574 }
575
576 /**
577 * @param array $options
578 * @return array
579 */
580 static function fixIgnore( $options ) {
581 # SQLite uses OR IGNORE not just IGNORE
582 foreach ( $options as $k => $v ) {
583 if ( $v == 'IGNORE' ) {
584 $options[$k] = 'OR IGNORE';
585 }
586 }
587
588 return $options;
589 }
590
591 /**
592 * @param array $options
593 * @return string
594 */
595 function makeInsertOptions( $options ) {
596 $options = self::fixIgnore( $options );
597
598 return parent::makeInsertOptions( $options );
599 }
600
601 /**
602 * Based on generic method (parent) with some prior SQLite-sepcific adjustments
603 * @param string $table
604 * @param array $a
605 * @param string $fname
606 * @param array $options
607 * @return bool
608 */
609 function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
610 if ( !count( $a ) ) {
611 return true;
612 }
613
614 # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
615 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
616 $ret = true;
617 foreach ( $a as $v ) {
618 if ( !parent::insert( $table, $v, "$fname/multi-row", $options ) ) {
619 $ret = false;
620 }
621 }
622 } else {
623 $ret = parent::insert( $table, $a, "$fname/single-row", $options );
624 }
625
626 return $ret;
627 }
628
629 /**
630 * @param string $table
631 * @param array $uniqueIndexes Unused
632 * @param string|array $rows
633 * @param string $fname
634 * @return bool|ResultWrapper
635 */
636 function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
637 if ( !count( $rows ) ) {
638 return true;
639 }
640
641 # SQLite can't handle multi-row replaces, so divide up into multiple single-row queries
642 if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
643 $ret = true;
644 foreach ( $rows as $v ) {
645 if ( !$this->nativeReplace( $table, $v, "$fname/multi-row" ) ) {
646 $ret = false;
647 }
648 }
649 } else {
650 $ret = $this->nativeReplace( $table, $rows, "$fname/single-row" );
651 }
652
653 return $ret;
654 }
655
656 /**
657 * Returns the size of a text field, or -1 for "unlimited"
658 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
659 *
660 * @param string $table
661 * @param string $field
662 * @return int
663 */
664 function textFieldSize( $table, $field ) {
665 return -1;
666 }
667
668 /**
669 * @return bool
670 */
671 function unionSupportsOrderAndLimit() {
672 return false;
673 }
674
675 /**
676 * @param string $sqls
677 * @param bool $all Whether to "UNION ALL" or not
678 * @return string
679 */
680 function unionQueries( $sqls, $all ) {
681 $glue = $all ? ' UNION ALL ' : ' UNION ';
682
683 return implode( $glue, $sqls );
684 }
685
686 /**
687 * @return bool
688 */
689 function wasDeadlock() {
690 return $this->lastErrno() == 5; // SQLITE_BUSY
691 }
692
693 /**
694 * @return bool
695 */
696 function wasErrorReissuable() {
697 return $this->lastErrno() == 17; // SQLITE_SCHEMA;
698 }
699
700 /**
701 * @return bool
702 */
703 function wasReadOnlyError() {
704 return $this->lastErrno() == 8; // SQLITE_READONLY;
705 }
706
707 /**
708 * @return string Wikitext of a link to the server software's web site
709 */
710 public function getSoftwareLink() {
711 return "[{{int:version-db-sqlite-url}} SQLite]";
712 }
713
714 /**
715 * @return string Version information from the database
716 */
717 function getServerVersion() {
718 $ver = $this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION );
719
720 return $ver;
721 }
722
723 /**
724 * @return string User-friendly database information
725 */
726 public function getServerInfo() {
727 return wfMessage( self::getFulltextSearchModule()
728 ? 'sqlite-has-fts'
729 : 'sqlite-no-fts', $this->getServerVersion() )->text();
730 }
731
732 /**
733 * Get information about a given field
734 * Returns false if the field does not exist.
735 *
736 * @param string $table
737 * @param string $field
738 * @return SQLiteField|bool False on failure
739 */
740 function fieldInfo( $table, $field ) {
741 $tableName = $this->tableName( $table );
742 $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
743 $res = $this->query( $sql, __METHOD__ );
744 foreach ( $res as $row ) {
745 if ( $row->name == $field ) {
746 return new SQLiteField( $row, $tableName );
747 }
748 }
749
750 return false;
751 }
752
753 protected function doBegin( $fname = '' ) {
754 if ( $this->trxMode ) {
755 $this->query( "BEGIN {$this->trxMode}", $fname );
756 } else {
757 $this->query( 'BEGIN', $fname );
758 }
759 $this->mTrxLevel = 1;
760 }
761
762 /**
763 * @param string $s
764 * @return string
765 */
766 function strencode( $s ) {
767 return substr( $this->addQuotes( $s ), 1, -1 );
768 }
769
770 /**
771 * @param string $b
772 * @return Blob
773 */
774 function encodeBlob( $b ) {
775 return new Blob( $b );
776 }
777
778 /**
779 * @param Blob|string $b
780 * @return string
781 */
782 function decodeBlob( $b ) {
783 if ( $b instanceof Blob ) {
784 $b = $b->fetch();
785 }
786
787 return $b;
788 }
789
790 /**
791 * @param Blob|string $s
792 * @return string
793 */
794 function addQuotes( $s ) {
795 if ( $s instanceof Blob ) {
796 return "x'" . bin2hex( $s->fetch() ) . "'";
797 } elseif ( is_bool( $s ) ) {
798 return (int)$s;
799 } elseif ( strpos( $s, "\0" ) !== false ) {
800 // SQLite doesn't support \0 in strings, so use the hex representation as a workaround.
801 // This is a known limitation of SQLite's mprintf function which PDO
802 // should work around, but doesn't. I have reported this to php.net as bug #63419:
803 // https://bugs.php.net/bug.php?id=63419
804 // There was already a similar report for SQLite3::escapeString, bug #62361:
805 // https://bugs.php.net/bug.php?id=62361
806 // There is an additional bug regarding sorting this data after insert
807 // on older versions of sqlite shipped with ubuntu 12.04
808 // https://phabricator.wikimedia.org/T74367
809 wfDebugLog(
810 __CLASS__,
811 __FUNCTION__ .
812 ': Quoting value containing null byte. ' .
813 'For consistency all binary data should have been ' .
814 'first processed with self::encodeBlob()'
815 );
816 return "x'" . bin2hex( $s ) . "'";
817 } else {
818 return $this->mConn->quote( $s );
819 }
820 }
821
822 /**
823 * @return string
824 */
825 function buildLike() {
826 $params = func_get_args();
827 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
828 $params = $params[0];
829 }
830
831 return parent::buildLike( $params ) . "ESCAPE '\' ";
832 }
833
834 /**
835 * @param string $field Field or column to cast
836 * @return string
837 * @since 1.28
838 */
839 public function buildStringCast( $field ) {
840 return 'CAST ( ' . $field . ' AS TEXT )';
841 }
842
843 /**
844 * @return string
845 */
846 public function getSearchEngine() {
847 return "SearchSqlite";
848 }
849
850 /**
851 * No-op version of deadlockLoop
852 *
853 * @return mixed
854 */
855 public function deadlockLoop( /*...*/ ) {
856 $args = func_get_args();
857 $function = array_shift( $args );
858
859 return call_user_func_array( $function, $args );
860 }
861
862 /**
863 * @param string $s
864 * @return string
865 */
866 protected function replaceVars( $s ) {
867 $s = parent::replaceVars( $s );
868 if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
869 // CREATE TABLE hacks to allow schema file sharing with MySQL
870
871 // binary/varbinary column type -> blob
872 $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
873 // no such thing as unsigned
874 $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
875 // INT -> INTEGER
876 $s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
877 // floating point types -> REAL
878 $s = preg_replace(
879 '/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i',
880 'REAL',
881 $s
882 );
883 // varchar -> TEXT
884 $s = preg_replace( '/\b(var)?char\s*\(.*?\)/i', 'TEXT', $s );
885 // TEXT normalization
886 $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
887 // BLOB normalization
888 $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
889 // BOOL -> INTEGER
890 $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
891 // DATETIME -> TEXT
892 $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
893 // No ENUM type
894 $s = preg_replace( '/\benum\s*\([^)]*\)/i', 'TEXT', $s );
895 // binary collation type -> nothing
896 $s = preg_replace( '/\bbinary\b/i', '', $s );
897 // auto_increment -> autoincrement
898 $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
899 // No explicit options
900 $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
901 // AUTOINCREMENT should immedidately follow PRIMARY KEY
902 $s = preg_replace( '/primary key (.*?) autoincrement/i', 'PRIMARY KEY AUTOINCREMENT $1', $s );
903 } elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
904 // No truncated indexes
905 $s = preg_replace( '/\(\d+\)/', '', $s );
906 // No FULLTEXT
907 $s = preg_replace( '/\bfulltext\b/i', '', $s );
908 } elseif ( preg_match( '/^\s*DROP INDEX/i', $s ) ) {
909 // DROP INDEX is database-wide, not table-specific, so no ON <table> clause.
910 $s = preg_replace( '/\sON\s+[^\s]*/i', '', $s );
911 } elseif ( preg_match( '/^\s*INSERT IGNORE\b/i', $s ) ) {
912 // INSERT IGNORE --> INSERT OR IGNORE
913 $s = preg_replace( '/^\s*INSERT IGNORE\b/i', 'INSERT OR IGNORE', $s );
914 }
915
916 return $s;
917 }
918
919 public function lock( $lockName, $method, $timeout = 5 ) {
920 if ( !is_dir( "{$this->dbDir}/locks" ) ) { // create dir as needed
921 if ( !is_writable( $this->dbDir ) || !mkdir( "{$this->dbDir}/locks" ) ) {
922 throw new DBError( $this, "Cannot create directory \"{$this->dbDir}/locks\"." );
923 }
924 }
925
926 return $this->lockMgr->lock( [ $lockName ], LockManager::LOCK_EX, $timeout )->isOK();
927 }
928
929 public function unlock( $lockName, $method ) {
930 return $this->lockMgr->unlock( [ $lockName ], LockManager::LOCK_EX )->isOK();
931 }
932
933 /**
934 * Build a concatenation list to feed into a SQL query
935 *
936 * @param string[] $stringList
937 * @return string
938 */
939 function buildConcat( $stringList ) {
940 return '(' . implode( ') || (', $stringList ) . ')';
941 }
942
943 public function buildGroupConcatField(
944 $delim, $table, $field, $conds = '', $join_conds = []
945 ) {
946 $fld = "group_concat($field," . $this->addQuotes( $delim ) . ')';
947
948 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
949 }
950
951 /**
952 * @param string $oldName
953 * @param string $newName
954 * @param bool $temporary
955 * @param string $fname
956 * @return bool|ResultWrapper
957 * @throws RuntimeException
958 */
959 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) {
960 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" .
961 $this->addQuotes( $oldName ) . " AND type='table'", $fname );
962 $obj = $this->fetchObject( $res );
963 if ( !$obj ) {
964 throw new RuntimeException( "Couldn't retrieve structure for table $oldName" );
965 }
966 $sql = $obj->sql;
967 $sql = preg_replace(
968 '/(?<=\W)"?' . preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ) ) . '"?(?=\W)/',
969 $this->addIdentifierQuotes( $newName ),
970 $sql,
971 1
972 );
973 if ( $temporary ) {
974 if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) {
975 wfDebug( "Table $oldName is virtual, can't create a temporary duplicate.\n" );
976 } else {
977 $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
978 }
979 }
980
981 $res = $this->query( $sql, $fname );
982
983 // Take over indexes
984 $indexList = $this->query( 'PRAGMA INDEX_LIST(' . $this->addQuotes( $oldName ) . ')' );
985 foreach ( $indexList as $index ) {
986 if ( strpos( $index->name, 'sqlite_autoindex' ) === 0 ) {
987 continue;
988 }
989
990 if ( $index->unique ) {
991 $sql = 'CREATE UNIQUE INDEX';
992 } else {
993 $sql = 'CREATE INDEX';
994 }
995 // Try to come up with a new index name, given indexes have database scope in SQLite
996 $indexName = $newName . '_' . $index->name;
997 $sql .= ' ' . $indexName . ' ON ' . $newName;
998
999 $indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name ) . ')' );
1000 $fields = [];
1001 foreach ( $indexInfo as $indexInfoRow ) {
1002 $fields[$indexInfoRow->seqno] = $indexInfoRow->name;
1003 }
1004
1005 $sql .= '(' . implode( ',', $fields ) . ')';
1006
1007 $this->query( $sql );
1008 }
1009
1010 return $res;
1011 }
1012
1013 /**
1014 * List all tables on the database
1015 *
1016 * @param string $prefix Only show tables with this prefix, e.g. mw_
1017 * @param string $fname Calling function name
1018 *
1019 * @return array
1020 */
1021 function listTables( $prefix = null, $fname = __METHOD__ ) {
1022 $result = $this->select(
1023 'sqlite_master',
1024 'name',
1025 "type='table'"
1026 );
1027
1028 $endArray = [];
1029
1030 foreach ( $result as $table ) {
1031 $vars = get_object_vars( $table );
1032 $table = array_pop( $vars );
1033
1034 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
1035 if ( strpos( $table, 'sqlite_' ) !== 0 ) {
1036 $endArray[] = $table;
1037 }
1038 }
1039 }
1040
1041 return $endArray;
1042 }
1043
1044 /**
1045 * @return string
1046 */
1047 public function __toString() {
1048 return 'SQLite ' . (string)$this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION );
1049 }
1050
1051 } // end DatabaseSqlite class