Use TOCData methods to process new headings
[mediawiki.git] / maintenance / Sqlite.php
blob06eb5484993391888d34e4845cfb297b46cbc76c
1 <?php
2 /**
3 * Helper class for sqlite-specific scripts
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 use Wikimedia\Rdbms\DatabaseSqlite;
25 use Wikimedia\Rdbms\DBError;
27 /**
28 * This class contains code common to different SQLite-related maintenance scripts
30 * @ingroup Maintenance
32 class Sqlite {
34 /**
35 * Checks whether PHP has SQLite support
36 * @return bool
38 public static function isPresent() {
39 return extension_loaded( 'pdo_sqlite' );
42 /**
43 * Checks given files for correctness of SQL syntax. MySQL DDL will be converted to
44 * SQLite-compatible during processing.
45 * Will throw exceptions on SQL errors
46 * @param array|string $files
47 * @throws MWException
48 * @return true|string True if no error or error string in case of errors
50 public static function checkSqlSyntax( $files ) {
51 if ( !self::isPresent() ) {
52 throw new MWException( "Can't check SQL syntax: SQLite not found" );
54 if ( !is_array( $files ) ) {
55 $files = [ $files ];
58 $allowedTypes = array_fill_keys( [
59 'integer',
60 'real',
61 'text',
62 'blob',
63 // NULL type is omitted intentionally
64 ], true );
66 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
67 try {
68 foreach ( $files as $file ) {
69 $err = $db->sourceFile( $file );
70 if ( $err ) {
71 return $err;
75 $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
76 foreach ( $tables as $table ) {
77 if ( strpos( $table->name, 'sqlite_' ) === 0 ) {
78 continue;
81 $columns = $db->query(
82 'PRAGMA table_info(' . $db->addIdentifierQuotes( $table->name ) . ')',
83 __METHOD__
85 foreach ( $columns as $col ) {
86 if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
87 $db->close( __METHOD__ );
89 return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
93 } catch ( DBError $e ) {
94 return $e->getMessage();
96 $db->close( __METHOD__ );
98 return true;