]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/sqlite.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.git] / maintenance / sqlite.php
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  * @ingroup Maintenance
21  */
22
23 require_once( dirname(__FILE__) . '/Maintenance.php' );
24
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 );
32         }
33
34         /**
35          * While we use database connection, this simple lie prevents useless --dbpass and
36          * --dbuser options from appearing in help message for this script.
37          */
38         public function getDbType() {
39                 return Maintenance::DB_NONE;
40         }
41
42         public function execute() {
43                 global $wgDBtype;
44                 
45                 if ( $wgDBtype != 'sqlite' ) {
46                         $this->error( "This maintenance script requires a SQLite database.\n" );
47                         return;
48                 }
49
50                 $this->db = wfGetDB( DB_MASTER );
51
52                 if ( $this->hasOption( 'vacuum' ) ) {
53                         $this->vacuum();
54                 }
55
56                 if ( $this->hasOption( 'integrity' ) ) {
57                         $this->integrityCheck();
58                 }
59
60                 if ( $this->hasOption( 'backup-to' ) ) {
61                         $this->backup( $this->getOption( 'backup-to' ) );
62                 }
63         }
64
65         private function vacuum() {
66                 $prevSize = filesize( $this->db->mDatabaseFile );
67                 if ( $prevSize == 0 ) {
68                         $this->error( "Can't vacuum an empty database.\n", true );
69                 }                       
70
71                 $this->output( 'VACUUM: ' );
72                 if ( $this->db->query( 'VACUUM' ) ) {
73                         clearstatcache();
74                         $newSize = filesize( $this->db->mDatabaseFile );
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() {
83                 $this->output( "Performing database integrity checks:\n" );
84                 $res = $this->db->query( 'PRAGMA integrity_check' );
85
86                 if ( !$res || $res->numRows() == 0 ) {
87                         $this->error( "Error: integrity check query returned nothing.\n" );
88                         return;
89                 }
90
91                 foreach ( $res as $row ) {
92                         $this->output( $row->integrity_check );
93                 }
94         }
95
96         private function backup( $fileName ) {
97                 $this->output( "Backing up database:\n   Locking..." );
98                 $this->db->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
99                 $ourFile = $this->db->mDatabaseFile;
100                 $this->output( "   Copying database file $ourFile to $fileName... " );
101                 wfSuppressWarnings( false );
102                 if ( !copy( $ourFile, $fileName ) ) {
103                         $err = error_get_last();
104                         $this->error( "      {$err['message']}" );
105                 }
106                 wfSuppressWarnings( true );
107                 $this->output( "   Releasing lock...\n" );
108                 $this->db->query( 'COMMIT TRANSACTION', __METHOD__ );
109         }
110 }
111
112 $maintClass = "SqliteMaintenance";
113 require_once( DO_MAINTENANCE );