MediaWiki  master
sqlite.php
Go to the documentation of this file.
1 <?php
25 
27 
28 require_once __DIR__ . '/Maintenance.php';
29 
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription( 'Performs some operations specific to SQLite database backend' );
39  $this->addOption(
40  'vacuum',
41  'Clean up database by removing deleted pages. Decreases database file size'
42  );
43  $this->addOption( 'integrity', 'Check database for integrity' );
44  $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
45  $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
46  }
47 
54  public function getDbType() {
55  return Maintenance::DB_NONE;
56  }
57 
58  public function execute() {
59  // Should work even if we use a non-SQLite database
60  if ( $this->hasOption( 'check-syntax' ) ) {
61  $this->checkSyntax();
62 
63  return;
64  }
65 
66  $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
67  $dbw = $lb->getConnection( DB_MASTER );
68  if ( !( $dbw instanceof DatabaseSqlite ) ) {
69  $this->error( "This maintenance script requires a SQLite database.\n" );
70 
71  return;
72  }
73 
74  if ( $this->hasOption( 'vacuum' ) ) {
75  $this->vacuum( $dbw );
76  }
77 
78  if ( $this->hasOption( 'integrity' ) ) {
79  $this->integrityCheck( $dbw );
80  }
81 
82  if ( $this->hasOption( 'backup-to' ) ) {
83  $this->backup( $dbw, $this->getOption( 'backup-to' ) );
84  }
85  }
86 
87  private function vacuum( DatabaseSqlite $dbw ) {
88  $prevSize = filesize( $dbw->getDbFilePath() );
89  if ( $prevSize == 0 ) {
90  $this->fatalError( "Can't vacuum an empty database.\n" );
91  }
92 
93  $this->output( 'VACUUM: ' );
94  if ( $dbw->query( 'VACUUM' ) ) {
95  clearstatcache();
96  $newSize = filesize( $dbw->getDbFilePath() );
97  $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
98  $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
99  } else {
100  $this->output( 'Error\n' );
101  }
102  }
103 
104  private function integrityCheck( DatabaseSqlite $dbw ) {
105  $this->output( "Performing database integrity checks:\n" );
106  $res = $dbw->query( 'PRAGMA integrity_check' );
107 
108  if ( !$res || $res->numRows() == 0 ) {
109  $this->error( "Error: integrity check query returned nothing.\n" );
110 
111  return;
112  }
113 
114  foreach ( $res as $row ) {
115  $this->output( $row->integrity_check );
116  }
117  }
118 
119  private function backup( DatabaseSqlite $dbw, $fileName ) {
120  $this->output( "Backing up database:\n Locking..." );
121  $dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
122  $ourFile = $dbw->getDbFilePath();
123  $this->output( " Copying database file $ourFile to $fileName... " );
124  Wikimedia\suppressWarnings();
125  if ( !copy( $ourFile, $fileName ) ) {
126  $err = error_get_last();
127  $this->error( " {$err['message']}" );
128  }
129  Wikimedia\restoreWarnings();
130  $this->output( " Releasing lock...\n" );
131  $dbw->query( 'COMMIT TRANSACTION', __METHOD__ );
132  }
133 
134  private function checkSyntax() {
135  if ( !Sqlite::isPresent() ) {
136  $this->error( "Error: SQLite support not found\n" );
137  }
138  $files = [ $this->getOption( 'check-syntax' ) ];
139  $files = array_merge( $files, $this->mArgs );
140  $result = Sqlite::checkSqlSyntax( $files );
141  if ( $result === true ) {
142  $this->output( "SQL syntax check: no errors detected.\n" );
143  } else {
144  $this->error( "Error: $result\n" );
145  }
146  }
147 }
148 
149 $maintClass = SqliteMaintenance::class;
150 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script that performs some operations specific to SQLite database backend.
Definition: sqlite.php:35
backup(DatabaseSqlite $dbw, $fileName)
Definition: sqlite.php:119
const DB_NONE
Constants for DB access type.
Definition: Maintenance.php:91
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
error( $err, $die=0)
Throw an error to the user.
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
getDbType()
While we use database connection, this simple lie prevents useless –dbpass and –dbuser options from...
Definition: sqlite.php:54
vacuum(DatabaseSqlite $dbw)
Definition: sqlite.php:87
hasOption( $name)
Checks to see if a particular option exists.
query( $sql, $fname=__METHOD__, $flags=0)
Run an SQL query and return the result.
Definition: Database.php:1155
const DB_MASTER
Definition: defines.php:26
integrityCheck(DatabaseSqlite $dbw)
Definition: sqlite.php:104
$maintClass
Definition: sqlite.php:149
addDescription( $text)
Set the description text.
output( $out, $channel=null)
Throw some output to the user.
static isPresent()
Checks whether PHP has SQLite support.
Definition: sqlite.inc:38
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
static checkSqlSyntax( $files)
Checks given files for correctness of SQL syntax.
Definition: sqlite.inc:50