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