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 * @ingroup Maintenance
23 require_once( dirname(__FILE__
) . '/Maintenance.php' );
25 class SqliteMaintenance
extends Maintenance
{
26 public function __construct() {
27 parent
::__construct();
28 $this->mDescription
= "Performs some operations specific to SQLite database backend";
29 $this->addOption( 'vacuum', 'Clean up database by removing deleted pages. Decreases database file size' );
30 $this->addOption( 'integrity', 'Check database for integrity' );
31 $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
34 public function execute() {
37 if ( $wgDBtype != 'sqlite' ) {
38 $this->error( "This maintenance script requires a SQLite database.\n" );
42 $this->db
= wfGetDB( DB_MASTER
);
44 if ( $this->hasOption( 'vacuum' ) ) {
48 if ( $this->hasOption( 'integrity' ) ) {
49 $this->integrityCheck();
52 if ( $this->hasOption( 'backup-to' ) ) {
53 $this->backup( $this->getOption( 'backup-to' ) );
57 private function vacuum() {
58 $prevSize = filesize( $this->db
->mDatabaseFile
);
59 if ( $prevSize == 0 ) {
60 $this->error( "Can't vacuum an empty database.\n", true );
63 $this->output( 'VACUUM: ' );
64 if ( $this->db
->query( 'VACUUM' ) ) {
66 $newSize = filesize( $this->db
->mDatabaseFile
);
67 $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
68 $prevSize, $newSize, ( $prevSize - $newSize) * 100.0 / $prevSize ) );
70 $this->output( 'Error\n' );
74 private function integrityCheck() {
75 $this->output( "Performing database integrity checks:\n" );
76 $res = $this->db
->query( 'PRAGMA integrity_check' );
78 if ( !$res ||
$res->numRows() == 0 ) {
79 $this->error( "Error: integrity check query returned nothing.\n" );
83 foreach ( $res as $row ) {
84 $this->output( $row->integrity_check
);
88 private function backup( $fileName ) {
89 $this->output( "Backing up database:\n Locking..." );
90 $this->db
->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__
);
91 $ourFile = $this->db
->mDatabaseFile
;
92 $this->output( " Copying database file $ourFile to $fileName... " );
93 wfSuppressWarnings( false );
94 if ( !copy( $ourFile, $fileName ) ) {
95 $err = error_get_last();
96 $this->error( " {$err['message']}" );
98 wfSuppressWarnings( true );
99 $this->output( " Releasing lock...\n" );
100 $this->db
->query( 'COMMIT TRANSACTION', __METHOD__
);
104 $maintClass = "SqliteMaintenance";
105 require_once( DO_MAINTENANCE
);