(bug 25095) Special:Categories doesn't show first relevant item when "from" is filled
[mediawiki.git] / maintenance / sqlite.inc
blob1f821917c1b63770853184aaec1a3ae4060e956d
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 class Sqlite {
29         /**
30          * Checks whether PHP has SQLite support
31          * @return bool
32          */
33         public static function isPresent() {
34                 wfSuppressWarnings();
35                 $compiled = wfDl( 'pdo_sqlite' );
36                 wfRestoreWarnings();
37                 return $compiled;
38         }
40         /**
41          * Checks given files for correctness of SQL syntax. MySQL DDL will be converted to
42          * SQLite-compatible during processing.
43          * Will throw exceptions on SQL errors
44          * @return mixed true if no error or error string in case of errors
45          */
46         public static function checkSqlSyntax( $files ) {
47                 if ( !Sqlite::isPresent() ) {
48                         throw new MWException( "Can't check SQL syntax: SQLite not found" );
49                 }
50                 if ( !is_array( $files ) ) {
51                         $files = array( $files );
52                 }
54                 $allowedTypes = array_flip( array(
55                         'integer',
56                         'real',
57                         'text',
58                         'blob', // NULL type is omitted intentionally
59                 ) );
61                 $db = new DatabaseSqliteStandalone( ':memory:' );
62                 try {
63                         foreach ( $files as $file ) {
64                                 $err = $db->sourceFile( $file );
65                                 if ( $err != true ) {
66                                         return $err;
67                                 }
68                         }
70                         $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
71                         foreach ( $tables as $table ) {
72                                 if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue;
74                                 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
75                                 foreach ( $columns as $col ) {
76                                         if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
77                                                 $db->close();
78                                                 return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
79                                         }
80                                 }
81                         }
82                 } catch ( DBError $e ) {
83                         return $e->getMessage();
84                 }
85                 $db->close();
86                 return true;
87         }
88  };