Followup to r59869, add to MySQL section, and copy patch to SQLite directory
[lhc/web/wiklou.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 }
32
33 public function execute() {
34 global $wgDBtype;
35
36 if ( $wgDBtype != 'sqlite' ) {
37 $this->error( "This maintenance script requires a SQLite database.\n" );
38 return;
39 }
40
41 $this->db = wfGetDB( DB_MASTER );
42
43 if ( $this->hasOption( 'vacuum' ) )
44 $this->vacuum();
45
46 if ( $this->hasOption( 'integrity' ) )
47 $this->integrityCheck();
48 }
49
50 private function vacuum() {
51 $prevSize = filesize( $this->db->mDatabaseFile );
52
53 $this->output( 'VACUUM: ' );
54 if ( $this->db->query( 'VACUUM' ) ) {
55 clearstatcache();
56 $newSize = filesize( $this->db->mDatabaseFile );
57 $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
58 $prevSize, $newSize, ( $prevSize - $newSize) * 100.0 / $prevSize ) );
59 } else {
60 $this->output( 'Error\n' );
61 }
62 }
63
64 private function integrityCheck() {
65 $this->output( "Performing database integrity checks:\n" );
66 $res = $this->db->query( 'PRAGMA integrity_check' );
67
68 if ( !$res || $res->numRows() == 0 ) {
69 $this->error( "Error: integrity check query returned nothing.\n" );
70 return;
71 }
72
73 foreach ( $res as $row ) {
74 $this->output( $row->integrity_check );
75 }
76 }
77 }
78
79 $maintClass = "SqliteMaintenance";
80 require_once( DO_MAINTENANCE );