MediaWiki 1.40.4
SqliteMaintenance.php
Go to the documentation of this file.
1<?php
25use Wikimedia\AtEase\AtEase;
28
29require_once __DIR__ . '/Maintenance.php';
30
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Performs some operations specific to SQLite database backend' );
40 $this->addOption(
41 'vacuum',
42 'Clean up database by removing deleted pages. Decreases database file size'
43 );
44 $this->addOption( 'integrity', 'Check database for integrity' );
45 $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
46 $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
47 }
48
49 public function execute() {
50 // Should work even if we use a non-SQLite database
51 if ( $this->hasOption( 'check-syntax' ) ) {
52 $this->checkSyntax();
53 return;
54 }
55
56 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
57 $dbw = $lb->getMaintenanceConnectionRef( DB_PRIMARY );
58 if ( $dbw->getType() !== 'sqlite' ) {
59 $this->error( "This maintenance script requires a SQLite database.\n" );
60
61 return;
62 }
63
64 if ( $this->hasOption( 'vacuum' ) ) {
65 $this->vacuum( $dbw );
66 }
67
68 if ( $this->hasOption( 'integrity' ) ) {
69 $this->integrityCheck( $dbw );
70 }
71
72 if ( $this->hasOption( 'backup-to' ) ) {
73 $this->backup( $dbw, $this->getOption( 'backup-to' ) );
74 }
75 }
76
77 private function vacuum( DBConnRef $dbw ) {
78 // Call non-standard DatabaseSqlite::getDbFilePath method
79 $prevSize = filesize( $dbw->__call( 'getDbFilePath', [] ) );
80 if ( $prevSize == 0 ) {
81 $this->fatalError( "Can't vacuum an empty database.\n" );
82 }
83
84 $this->output( 'VACUUM: ' );
85 if ( $dbw->query( 'VACUUM', __METHOD__ ) ) {
86 clearstatcache();
87 $newSize = filesize( $dbw->getDbFilePath() );
88 $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
89 $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
90 } else {
91 $this->output( 'Error\n' );
92 }
93 }
94
95 private function integrityCheck( IMaintainableDatabase $dbw ) {
96 $this->output( "Performing database integrity checks:\n" );
97 $res = $dbw->query( 'PRAGMA integrity_check', __METHOD__ );
98
99 if ( !$res || $res->numRows() == 0 ) {
100 $this->error( "Error: integrity check query returned nothing.\n" );
101
102 return;
103 }
104
105 foreach ( $res as $row ) {
106 $this->output( $row->integrity_check );
107 }
108 }
109
110 private function backup( DBConnRef $dbw, $fileName ) {
111 $this->output( "Backing up database:\n Locking..." );
112 $dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
113 $ourFile = $dbw->__call( 'getDbFilePath', [] );
114 $this->output( " Copying database file $ourFile to $fileName..." );
115 AtEase::suppressWarnings();
116 if ( !copy( $ourFile, $fileName ) ) {
117 $err = error_get_last();
118 $this->error( " {$err['message']}" );
119 }
120 AtEase::restoreWarnings();
121 $this->output( " Releasing lock...\n" );
122 $dbw->query( 'COMMIT TRANSACTION', __METHOD__ );
123 }
124
125 private function checkSyntax() {
126 if ( !Sqlite::isPresent() ) {
127 $this->error( "Error: SQLite support not found\n" );
128 }
129 $files = [ $this->getOption( 'check-syntax' ) ];
130 $files = array_merge( $files, $this->mArgs );
131 $result = Sqlite::checkSqlSyntax( $files );
132 if ( $result === true ) {
133 $this->output( "SQL syntax check: no errors detected.\n" );
134 } else {
135 $this->error( "Error: $result\n" );
136 }
137 }
138}
139
140$maintClass = SqliteMaintenance::class;
141require_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.
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.
Service locator for MediaWiki core services.
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:50
Helper class used for automatically marking an IDatabase connection as reusable (once it no longer ma...
Definition DBConnRef.php:29
__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