Added more Setup.php profiling
[mediawiki.git] / maintenance / sqlite.inc
blob08188cade4433a0698cc5122f1853e6e4fcb488e
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                 return extension_loaded( 'pdo_sqlite' );
37         }
39         /**
40          * Checks given files for correctness of SQL syntax. MySQL DDL will be converted to
41          * SQLite-compatible during processing.
42          * Will throw exceptions on SQL errors
43          * @param $files
44          * @throws MWException
45          * @return mixed true if no error or error string in case of errors
46          */
47         public static function checkSqlSyntax( $files ) {
48                 if ( !Sqlite::isPresent() ) {
49                         throw new MWException( "Can't check SQL syntax: SQLite not found" );
50                 }
51                 if ( !is_array( $files ) ) {
52                         $files = array( $files );
53                 }
55                 $allowedTypes = array_flip( array(
56                         'integer',
57                         'real',
58                         'text',
59                         'blob', // NULL type is omitted intentionally
60                 ) );
62                 $db = new DatabaseSqliteStandalone( ':memory:' );
63                 try {
64                         foreach ( $files as $file ) {
65                                 $err = $db->sourceFile( $file );
66                                 if ( $err != true ) {
67                                         return $err;
68                                 }
69                         }
71                         $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
72                         foreach ( $tables as $table ) {
73                                 if ( strpos( $table->name, 'sqlite_' ) === 0 ) {
74                                         continue;
75                                 }
77                                 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
78                                 foreach ( $columns as $col ) {
79                                         if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
80                                                 $db->close();
81                                                 return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
82                                         }
83                                 }
84                         }
85                 } catch ( DBError $e ) {
86                         return $e->getMessage();
87                 }
88                 $db->close();
89                 return true;
90         }