maintenance/sqlite.php: added an option for safe backup
[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 $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
32 }
33
34 public function execute() {
35 global $wgDBtype;
36
37 if ( $wgDBtype != 'sqlite' ) {
38 $this->error( "This maintenance script requires a SQLite database.\n" );
39 return;
40 }
41
42 $this->db = wfGetDB( DB_MASTER );
43
44 if ( $this->hasOption( 'vacuum' ) ) {
45 $this->vacuum();
46 }
47
48 if ( $this->hasOption( 'integrity' ) ) {
49 $this->integrityCheck();
50 }
51
52 if ( $this->hasOption( 'backup-to' ) ) {
53 $this->backup( $this->getOption( 'backup-to' ) );
54 }
55 }
56
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 );
61 }
62
63 $this->output( 'VACUUM: ' );
64 if ( $this->db->query( 'VACUUM' ) ) {
65 clearstatcache();
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 ) );
69 } else {
70 $this->output( 'Error\n' );
71 }
72 }
73
74 private function integrityCheck() {
75 $this->output( "Performing database integrity checks:\n" );
76 $res = $this->db->query( 'PRAGMA integrity_check' );
77
78 if ( !$res || $res->numRows() == 0 ) {
79 $this->error( "Error: integrity check query returned nothing.\n" );
80 return;
81 }
82
83 foreach ( $res as $row ) {
84 $this->output( $row->integrity_check );
85 }
86 }
87
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']}" );
97 }
98 wfSuppressWarnings( true );
99 $this->output( " Releasing lock...\n" );
100 $this->db->query( 'COMMIT TRANSACTION', __METHOD__ );
101 }
102 }
103
104 $maintClass = "SqliteMaintenance";
105 require_once( DO_MAINTENANCE );