diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2011-06-22 11:28:20 +0200 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2011-06-22 11:28:20 +0200 |
commit | 9db190c7e736ec8d063187d4241b59feaf7dc2d1 (patch) | |
tree | 46d1a0dee7febef5c2d57a9f7b972be16a163b3d /maintenance/sqlite.inc | |
parent | 78677c7bbdcc9739f6c10c75935898a20e1acd9e (diff) |
update to MediaWiki 1.17.0
Diffstat (limited to 'maintenance/sqlite.inc')
-rw-r--r-- | maintenance/sqlite.inc | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/maintenance/sqlite.inc b/maintenance/sqlite.inc new file mode 100644 index 00000000..238fe82b --- /dev/null +++ b/maintenance/sqlite.inc @@ -0,0 +1,67 @@ +<?php + +/** + * This class contains code common to different SQLite-related maintenance scripts + */ +class Sqlite { + + /** + * Checks whether PHP has SQLite support + * @return bool + */ + public static function isPresent() { + wfSuppressWarnings(); + $compiled = wfDl( 'pdo_sqlite' ); + wfRestoreWarnings(); + return $compiled; + } + + /** + * Checks given files for correctness of SQL syntax. MySQL DDL will be converted to + * SQLite-compatible during processing. + * Will throw exceptions on SQL errors + * @return mixed true if no error or error string in case of errors + */ + public static function checkSqlSyntax( $files ) { + if ( !Sqlite::isPresent() ) { + throw new MWException( "Can't check SQL syntax: SQLite not found" ); + } + if ( !is_array( $files ) ) { + $files = array( $files ); + } + + $allowedTypes = array_flip( array( + 'integer', + 'real', + 'text', + 'blob', // NULL type is omitted intentionally + ) ); + + $db = new DatabaseSqliteStandalone( ':memory:' ); + try { + foreach ( $files as $file ) { + $err = $db->sourceFile( $file ); + if ( $err != true ) { + return $err; + } + } + + $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ ); + foreach ( $tables as $table ) { + if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue; + + $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ ); + foreach ( $columns as $col ) { + if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) { + $db->close(); + return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'"; + } + } + } + } catch ( DBError $e ) { + return $e->getMessage(); + } + $db->close(); + return true; + } + };
\ No newline at end of file |