MediaWiki fundraising/REL1_35
sqlite.inc
Go to the documentation of this file.
1<?php
26
32class Sqlite {
33
38 public static function isPresent() {
39 return extension_loaded( 'pdo_sqlite' );
40 }
41
50 public static function checkSqlSyntax( $files ) {
51 if ( !self::isPresent() ) {
52 throw new MWException( "Can't check SQL syntax: SQLite not found" );
53 }
54 if ( !is_array( $files ) ) {
55 $files = [ $files ];
56 }
57
58 $allowedTypes = array_flip( [
59 'integer',
60 'real',
61 'text',
62 'blob',
63 // NULL type is omitted intentionally
64 ] );
65
66 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
67 try {
68 foreach ( $files as $file ) {
69 $err = $db->sourceFile( $file );
70 if ( $err != true ) {
71 return $err;
72 }
73 }
74
75 $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
76 foreach ( $tables as $table ) {
77 if ( strpos( $table->name, 'sqlite_' ) === 0 ) {
78 continue;
79 }
80
81 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
82 foreach ( $columns as $col ) {
83 if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
84 $db->close( __METHOD__ );
85
86 return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
87 }
88 }
89 }
90 } catch ( DBError $e ) {
91 return $e->getMessage();
92 }
93 $db->close( __METHOD__ );
94
95 return true;
96 }
97}
MediaWiki exception.
This class contains code common to different SQLite-related maintenance scripts.
Definition sqlite.inc:32
static isPresent()
Checks whether PHP has SQLite support.
Definition sqlite.inc:38
static checkSqlSyntax( $files)
Checks given files for correctness of SQL syntax.
Definition sqlite.inc:50
Database error base class @newable Stable to extend.
Definition DBError.php:32
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42