HTMLForm: throw a warning when no callback is set
[mediawiki.git] / maintenance / sqlite.php
blob864d5ab640e09cd57fd3a611af7ca503c825d0aa
1 <?php
2 /**
3 * Performs some operations specific to SQLite database backend
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.
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.
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
20 * @ingroup Maintenance
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
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 );
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.
39 * @return int DB constant
41 public function getDbType() {
42 return Maintenance::DB_NONE;
45 public function execute() {
46 // Should work even if we use a non-SQLite database
47 if ( $this->hasOption( 'check-syntax' ) ) {
48 $this->checkSyntax();
49 return;
52 $this->db = wfGetDB( DB_MASTER );
54 if ( $this->db->getType() != 'sqlite' ) {
55 $this->error( "This maintenance script requires a SQLite database.\n" );
56 return;
59 if ( $this->hasOption( 'vacuum' ) ) {
60 $this->vacuum();
63 if ( $this->hasOption( 'integrity' ) ) {
64 $this->integrityCheck();
67 if ( $this->hasOption( 'backup-to' ) ) {
68 $this->backup( $this->getOption( 'backup-to' ) );
72 private function vacuum() {
73 $prevSize = filesize( $this->db->mDatabaseFile );
74 if ( $prevSize == 0 ) {
75 $this->error( "Can't vacuum an empty database.\n", true );
78 $this->output( 'VACUUM: ' );
79 if ( $this->db->query( 'VACUUM' ) ) {
80 clearstatcache();
81 $newSize = filesize( $this->db->mDatabaseFile );
82 $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
83 $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
84 } else {
85 $this->output( 'Error\n' );
89 private function integrityCheck() {
90 $this->output( "Performing database integrity checks:\n" );
91 $res = $this->db->query( 'PRAGMA integrity_check' );
93 if ( !$res || $res->numRows() == 0 ) {
94 $this->error( "Error: integrity check query returned nothing.\n" );
95 return;
98 foreach ( $res as $row ) {
99 $this->output( $row->integrity_check );
103 private function backup( $fileName ) {
104 $this->output( "Backing up database:\n Locking..." );
105 $this->db->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
106 $ourFile = $this->db->mDatabaseFile;
107 $this->output( " Copying database file $ourFile to $fileName... " );
108 wfSuppressWarnings( false );
109 if ( !copy( $ourFile, $fileName ) ) {
110 $err = error_get_last();
111 $this->error( " {$err['message']}" );
113 wfSuppressWarnings( true );
114 $this->output( " Releasing lock...\n" );
115 $this->db->query( 'COMMIT TRANSACTION', __METHOD__ );
118 private function checkSyntax() {
119 if ( !Sqlite::IsPresent() ) {
120 $this->error( "Error: SQLite support not found\n" );
122 $files = array( $this->getOption( 'check-syntax' ) );
123 $files += $this->mArgs;
124 $result = Sqlite::checkSqlSyntax( $files );
125 if ( $result === true ) {
126 $this->output( "SQL syntax check: no errors detected.\n" );
127 } else {
128 $this->error( "Error: $result\n" );
133 $maintClass = "SqliteMaintenance";
134 require_once( RUN_MAINTENANCE_IF_MAIN );