Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SqliteMaintenance
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 6
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
 vacuum
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 integrityCheck
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 backup
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 checkSyntax
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Performs some operations specific to SQLite database backend.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\Maintenance\Maintenance;
25use Wikimedia\AtEase\AtEase;
26use Wikimedia\Rdbms\DBConnRef;
27use Wikimedia\Rdbms\IMaintainableDatabase;
28
29// @codeCoverageIgnoreStart
30require_once __DIR__ . '/Maintenance.php';
31// @codeCoverageIgnoreEnd
32
33/**
34 * Maintenance script that performs some operations specific to SQLite database backend.
35 *
36 * @ingroup Maintenance
37 */
38class SqliteMaintenance extends Maintenance {
39    public function __construct() {
40        parent::__construct();
41        $this->addDescription( 'Performs some operations specific to SQLite database backend' );
42        $this->addOption(
43            'vacuum',
44            'Clean up database by removing deleted pages. Decreases database file size'
45        );
46        $this->addOption( 'integrity', 'Check database for integrity' );
47        $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
48        $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
49    }
50
51    public function execute() {
52        // Should work even if we use a non-SQLite database
53        if ( $this->hasOption( 'check-syntax' ) ) {
54            $this->checkSyntax();
55            return;
56        }
57
58        $lb = $this->getServiceContainer()->getDBLoadBalancer();
59        $dbw = $lb->getMaintenanceConnectionRef( DB_PRIMARY );
60        if ( $dbw->getType() !== 'sqlite' ) {
61            $this->error( "This maintenance script requires a SQLite database.\n" );
62
63            return;
64        }
65
66        if ( $this->hasOption( 'vacuum' ) ) {
67            $this->vacuum( $dbw );
68        }
69
70        if ( $this->hasOption( 'integrity' ) ) {
71            $this->integrityCheck( $dbw );
72        }
73
74        if ( $this->hasOption( 'backup-to' ) ) {
75            $this->backup( $dbw, $this->getOption( 'backup-to' ) );
76        }
77    }
78
79    private function vacuum( DBConnRef $dbw ) {
80        // Call non-standard DatabaseSqlite::getDbFilePath method
81        $prevSize = filesize( $dbw->__call( '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( DBConnRef $dbw, string $fileName ) {
113        $this->output( "Backing up database:\n   Locking..." );
114        $dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
115        $ourFile = $dbw->__call( '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