cl_sortkey should be 230 bytes long, not 255
[lhc/web/wiklou.git] / maintenance / sqlite.inc
1 <?php
2
3 /**
4 * This class contains code common to different SQLite-related maintenance scripts
5 */
6 class Sqlite {
7
8 /**
9 * Checks whether PHP has SQLite support
10 * @return bool
11 */
12 public static function isPresent() {
13 wfSuppressWarnings();
14 $compiled = wfDl( 'pdo_sqlite' );
15 wfRestoreWarnings();
16 return $compiled;
17 }
18
19 /**
20 * Checks given files for correctness of SQL syntax. MySQL DDL will be converted to
21 * SQLite-compatible during processing.
22 * Will throw exceptions on SQL errors
23 * @return mixed true if no error or error string in case of errors
24 */
25 public static function checkSqlSyntax( $files ) {
26 if ( !Sqlite::isPresent() ) {
27 throw new MWException( "Can't check SQL syntax: SQLite not found" );
28 }
29 if ( !is_array( $files ) ) {
30 $files = array( $files );
31 }
32
33 $allowedTypes = array_flip( array(
34 'integer',
35 'real',
36 'text',
37 'blob', // NULL type is omitted intentionally
38 ) );
39
40 $db = new DatabaseSqliteStandalone( ':memory:' );
41 try {
42 foreach ( $files as $file ) {
43 $err = $db->sourceFile( $file );
44 if ( $err != true ) {
45 return $err;
46 }
47 }
48
49 $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
50 foreach ( $tables as $table ) {
51 if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue;
52
53 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
54 foreach ( $columns as $col ) {
55 if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
56 $db->close();
57 return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
58 }
59 }
60 }
61 } catch ( DBError $e ) {
62 return $e->getMessage();
63 }
64 $db->close();
65 return true;
66 }
67 };