sqlite.php: don't fail after --check-syntax on a non-SQLite DB
[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 $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
33 }
34
35 /**
36 * While we use database connection, this simple lie prevents useless --dbpass and
37 * --dbuser options from appearing in help message for this script.
38 */
39 public function getDbType() {
40 return Maintenance::DB_NONE;
41 }
42
43 public function execute() {
44 // Should work even if we use a non-SQLite database
45 if ( $this->hasOption( 'check-syntax' ) ) {
46 $this->checkSyntax();
47 return;
48 }
49
50 $this->db = wfGetDB( DB_MASTER );
51
52 if ( $this->db->getType() != 'sqlite' ) {
53 $this->error( "This maintenance script requires a SQLite database.\n" );
54 return;
55 }
56
57 if ( $this->hasOption( 'vacuum' ) ) {
58 $this->vacuum();
59 }
60
61 if ( $this->hasOption( 'integrity' ) ) {
62 $this->integrityCheck();
63 }
64
65 if ( $this->hasOption( 'backup-to' ) ) {
66 $this->backup( $this->getOption( 'backup-to' ) );
67 }
68 }
69
70 private function vacuum() {
71 $prevSize = filesize( $this->db->mDatabaseFile );
72 if ( $prevSize == 0 ) {
73 $this->error( "Can't vacuum an empty database.\n", true );
74 }
75
76 $this->output( 'VACUUM: ' );
77 if ( $this->db->query( 'VACUUM' ) ) {
78 clearstatcache();
79 $newSize = filesize( $this->db->mDatabaseFile );
80 $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
81 $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
82 } else {
83 $this->output( 'Error\n' );
84 }
85 }
86
87 private function integrityCheck() {
88 $this->output( "Performing database integrity checks:\n" );
89 $res = $this->db->query( 'PRAGMA integrity_check' );
90
91 if ( !$res || $res->numRows() == 0 ) {
92 $this->error( "Error: integrity check query returned nothing.\n" );
93 return;
94 }
95
96 foreach ( $res as $row ) {
97 $this->output( $row->integrity_check );
98 }
99 }
100
101 private function backup( $fileName ) {
102 $this->output( "Backing up database:\n Locking..." );
103 $this->db->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
104 $ourFile = $this->db->mDatabaseFile;
105 $this->output( " Copying database file $ourFile to $fileName... " );
106 wfSuppressWarnings( false );
107 if ( !copy( $ourFile, $fileName ) ) {
108 $err = error_get_last();
109 $this->error( " {$err['message']}" );
110 }
111 wfSuppressWarnings( true );
112 $this->output( " Releasing lock...\n" );
113 $this->db->query( 'COMMIT TRANSACTION', __METHOD__ );
114 }
115
116 private function checkSyntax() {
117 if ( !Sqlite::IsPresent() ) {
118 $this->error( "Error: SQLite support not found\n" );
119 }
120 $files = array( $this->getOption( 'check-syntax' ) );
121 $files += $this->mArgs;
122 $result = Sqlite::checkSqlSyntax( $files );
123 if ( $result === true ) {
124 $this->output( "SQL syntax check: no errors detected.\n" );
125 } else {
126 $this->error( "Error: $result\n" );
127 }
128 }
129 }
130
131 $maintClass = "SqliteMaintenance";
132 require_once( RUN_MAINTENANCE_IF_MAIN );