Update git submodules
[mediawiki.git] / maintenance / SqliteMaintenance.php
blobbd5228d5e6e2db67cd8064892af1aa6f02ec7d98
1 <?php
2 /**
3 * Performs some operations specific to SQLite database backend.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 use Wikimedia\AtEase\AtEase;
25 use Wikimedia\Rdbms\DBConnRef;
26 use Wikimedia\Rdbms\IMaintainableDatabase;
28 require_once __DIR__ . '/Maintenance.php';
30 /**
31 * Maintenance script that performs some operations specific to SQLite database backend.
33 * @ingroup Maintenance
35 class SqliteMaintenance extends Maintenance {
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'
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 );
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;
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" );
60 return;
63 if ( $this->hasOption( 'vacuum' ) ) {
64 $this->vacuum( $dbw );
67 if ( $this->hasOption( 'integrity' ) ) {
68 $this->integrityCheck( $dbw );
71 if ( $this->hasOption( 'backup-to' ) ) {
72 $this->backup( $dbw, $this->getOption( 'backup-to' ) );
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" );
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' );
94 private function integrityCheck( IMaintainableDatabase $dbw ) {
95 $this->output( "Performing database integrity checks:\n" );
96 $res = $dbw->query( 'PRAGMA integrity_check', __METHOD__ );
98 if ( !$res || $res->numRows() == 0 ) {
99 $this->error( "Error: integrity check query returned nothing.\n" );
101 return;
104 foreach ( $res as $row ) {
105 $this->output( $row->integrity_check );
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']}" );
119 AtEase::restoreWarnings();
120 $this->output( " Releasing lock...\n" );
121 $dbw->query( 'COMMIT TRANSACTION', __METHOD__ );
124 private function checkSyntax() {
125 if ( !Sqlite::isPresent() ) {
126 $this->error( "Error: SQLite support not found\n" );
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" );
139 $maintClass = SqliteMaintenance::class;
140 require_once RUN_MAINTENANCE_IF_MAIN;