Revert "Show a "(blocked)" hint on Special:ListUsers/ActiveUsers"
[mediawiki.git] / maintenance / sqlite.inc
bloba8a1fce680c9b1e0cdd0c8311e7a90bd2bf3815f
1 <?php
2 /**
3  * Helper class for sqlite-specific scripts
4  *
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.
9  *
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.
14  *
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
19  *
20  * @file
21  * @ingroup Maintenance
22  */
24 /**
25  * This class contains code common to different SQLite-related maintenance scripts
26  *
27  * @ingroup Maintenance
28  */
29 class Sqlite {
31         /**
32          * Checks whether PHP has SQLite support
33          * @return bool
34          */
35         public static function isPresent() {
36                 wfSuppressWarnings();
37                 $compiled = wfDl( 'pdo_sqlite' );
38                 wfRestoreWarnings();
39                 return $compiled;
40         }
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          * @return mixed true if no error or error string in case of errors
47          */
48         public static function checkSqlSyntax( $files ) {
49                 if ( !Sqlite::isPresent() ) {
50                         throw new MWException( "Can't check SQL syntax: SQLite not found" );
51                 }
52                 if ( !is_array( $files ) ) {
53                         $files = array( $files );
54                 }
56                 $allowedTypes = array_flip( array(
57                         'integer',
58                         'real',
59                         'text',
60                         'blob', // NULL type is omitted intentionally
61                 ) );
63                 $db = new DatabaseSqliteStandalone( ':memory:' );
64                 try {
65                         foreach ( $files as $file ) {
66                                 $err = $db->sourceFile( $file );
67                                 if ( $err != true ) {
68                                         return $err;
69                                 }
70                         }
72                         $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
73                         foreach ( $tables as $table ) {
74                                 if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue;
76                                 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
77                                 foreach ( $columns as $col ) {
78                                         if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
79                                                 $db->close();
80                                                 return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
81                                         }
82                                 }
83                         }
84                 } catch ( DBError $e ) {
85                         return $e->getMessage();
86                 }
87                 $db->close();
88                 return true;
89         }
90  };