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