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