MediaWiki master
SqliteMaintenance.php
Go to the documentation of this file.
1<?php
24use Wikimedia\AtEase\AtEase;
27
28require_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
48 public function execute() {
49 // Should work even if we use a non-SQLite database
50 if ( $this->hasOption( 'check-syntax' ) ) {
51 $this->checkSyntax();
52 return;
53 }
54
55 $lb = $this->getServiceContainer()->getDBLoadBalancer();
56 $dbw = $lb->getMaintenanceConnectionRef( DB_PRIMARY );
57 if ( $dbw->getType() !== 'sqlite' ) {
58 $this->error( "This maintenance script requires a SQLite database.\n" );
59
60 return;
61 }
62
63 if ( $this->hasOption( 'vacuum' ) ) {
64 $this->vacuum( $dbw );
65 }
66
67 if ( $this->hasOption( 'integrity' ) ) {
68 $this->integrityCheck( $dbw );
69 }
70
71 if ( $this->hasOption( 'backup-to' ) ) {
72 $this->backup( $dbw, $this->getOption( 'backup-to' ) );
73 }
74 }
75
76 private function vacuum( DBConnRef $dbw ) {
77 // Call non-standard DatabaseSqlite::getDbFilePath method
78 $prevSize = filesize( $dbw->__call( 'getDbFilePath', [] ) );
79 if ( $prevSize == 0 ) {
80 $this->fatalError( "Can't vacuum an empty database.\n" );
81 }
82
83 $this->output( 'VACUUM: ' );
84 if ( $dbw->query( 'VACUUM', __METHOD__ ) ) {
85 clearstatcache();
86 $newSize = filesize( $dbw->getDbFilePath() );
87 $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
88 $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
89 } else {
90 $this->output( 'Error\n' );
91 }
92 }
93
94 private function integrityCheck( IMaintainableDatabase $dbw ) {
95 $this->output( "Performing database integrity checks:\n" );
96 $res = $dbw->query( 'PRAGMA integrity_check', __METHOD__ );
97
98 if ( !$res || $res->numRows() == 0 ) {
99 $this->error( "Error: integrity check query returned nothing.\n" );
100
101 return;
102 }
103
104 foreach ( $res as $row ) {
105 $this->output( $row->integrity_check );
106 }
107 }
108
109 private function backup( DBConnRef $dbw, $fileName ) {
110 $this->output( "Backing up database:\n Locking..." );
111 $dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
112 $ourFile = $dbw->__call( 'getDbFilePath', [] );
113 $this->output( " Copying database file $ourFile to $fileName..." );
114 AtEase::suppressWarnings();
115 if ( !copy( $ourFile, $fileName ) ) {
116 $err = error_get_last();
117 $this->error( " {$err['message']}" );
118 }
119 AtEase::restoreWarnings();
120 $this->output( " Releasing lock...\n" );
121 $dbw->query( 'COMMIT TRANSACTION', __METHOD__ );
122 }
123
124 private function checkSyntax() {
125 if ( !Sqlite::isPresent() ) {
126 $this->error( "Error: SQLite support not found\n" );
127 }
128 $files = [ $this->getOption( 'check-syntax' ) ];
129 $files = array_merge( $files, $this->mArgs );
130 $result = Sqlite::checkSqlSyntax( $files );
131 if ( $result === true ) {
132 $this->output( "SQL syntax check: no errors detected.\n" );
133 } else {
134 $this->error( "Error: $result\n" );
135 }
136 }
137}
138
139$maintClass = SqliteMaintenance::class;
140require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that performs some operations specific to SQLite database backend.
execute()
Do the actual work.
__construct()
Default constructor.
static isPresent()
Checks whether PHP has SQLite support.
Definition Sqlite.php:38
static checkSqlSyntax( $files)
Checks given files for correctness of SQL syntax.
Definition Sqlite.php:49
Helper class used for automatically re-using IDatabase connections and lazily establishing the actual...
Definition DBConnRef.php:36
__call( $name, array $arguments)
query( $sql, $fname=__METHOD__, $flags=0)
Run an SQL query statement and return the result.
getType()
Get the RDBMS type of the server (e.g.
query( $sql, $fname=__METHOD__, $flags=0)
Run an SQL query statement and return the result.
Advanced database interface for IDatabase handles that include maintenance methods.
const DB_PRIMARY
Definition defines.php:28