MediaWiki  1.34.0
sqlite.inc
Go to the documentation of this file.
1 <?php
26 
32 class 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', // NULL type is omitted intentionally
63  ] );
64 
65  $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
66  try {
67  foreach ( $files as $file ) {
68  $err = $db->sourceFile( $file );
69  if ( $err != true ) {
70  return $err;
71  }
72  }
73 
74  $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
75  foreach ( $tables as $table ) {
76  if ( strpos( $table->name, 'sqlite_' ) === 0 ) {
77  continue;
78  }
79 
80  $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
81  foreach ( $columns as $col ) {
82  if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
83  $db->close();
84 
85  return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
86  }
87  }
88  }
89  } catch ( DBError $e ) {
90  return $e->getMessage();
91  }
92  $db->close();
93 
94  return true;
95  }
96 }
Sqlite
This class contains code common to different SQLite-related maintenance scripts.
Definition: sqlite.inc:32
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:38
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Wikimedia\Rdbms\DBError
Database error base class.
Definition: DBError.php:30
MWException
MediaWiki exception.
Definition: MWException.php:26
Sqlite\isPresent
static isPresent()
Checks whether PHP has SQLite support.
Definition: sqlite.inc:38
Sqlite\checkSqlSyntax
static checkSqlSyntax( $files)
Checks given files for correctness of SQL syntax.
Definition: sqlite.inc:50