From 222b01f5169f1c7e69762e0e8904c24f78f71882 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Wed, 28 Jul 2010 11:52:48 +0200 Subject: update to MediaWiki 1.16.0 --- maintenance/sqlite.php | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 maintenance/sqlite.php (limited to 'maintenance/sqlite.php') diff --git a/maintenance/sqlite.php b/maintenance/sqlite.php new file mode 100644 index 00000000..8886fe74 --- /dev/null +++ b/maintenance/sqlite.php @@ -0,0 +1,113 @@ +mDescription = "Performs some operations specific to SQLite database backend"; + $this->addOption( 'vacuum', 'Clean up database by removing deleted pages. Decreases database file size' ); + $this->addOption( 'integrity', 'Check database for integrity' ); + $this->addOption( 'backup-to', 'Backup database to the given file', false, true ); + } + + /** + * While we use database connection, this simple lie prevents useless --dbpass and + * --dbuser options from appearing in help message for this script. + */ + public function getDbType() { + return Maintenance::DB_NONE; + } + + public function execute() { + global $wgDBtype; + + if ( $wgDBtype != 'sqlite' ) { + $this->error( "This maintenance script requires a SQLite database.\n" ); + return; + } + + $this->db = wfGetDB( DB_MASTER ); + + if ( $this->hasOption( 'vacuum' ) ) { + $this->vacuum(); + } + + if ( $this->hasOption( 'integrity' ) ) { + $this->integrityCheck(); + } + + if ( $this->hasOption( 'backup-to' ) ) { + $this->backup( $this->getOption( 'backup-to' ) ); + } + } + + private function vacuum() { + $prevSize = filesize( $this->db->mDatabaseFile ); + if ( $prevSize == 0 ) { + $this->error( "Can't vacuum an empty database.\n", true ); + } + + $this->output( 'VACUUM: ' ); + if ( $this->db->query( 'VACUUM' ) ) { + clearstatcache(); + $newSize = filesize( $this->db->mDatabaseFile ); + $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n", + $prevSize, $newSize, ( $prevSize - $newSize) * 100.0 / $prevSize ) ); + } else { + $this->output( 'Error\n' ); + } + } + + private function integrityCheck() { + $this->output( "Performing database integrity checks:\n" ); + $res = $this->db->query( 'PRAGMA integrity_check' ); + + if ( !$res || $res->numRows() == 0 ) { + $this->error( "Error: integrity check query returned nothing.\n" ); + return; + } + + foreach ( $res as $row ) { + $this->output( $row->integrity_check ); + } + } + + private function backup( $fileName ) { + $this->output( "Backing up database:\n Locking..." ); + $this->db->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ ); + $ourFile = $this->db->mDatabaseFile; + $this->output( " Copying database file $ourFile to $fileName... " ); + wfSuppressWarnings( false ); + if ( !copy( $ourFile, $fileName ) ) { + $err = error_get_last(); + $this->error( " {$err['message']}" ); + } + wfSuppressWarnings( true ); + $this->output( " Releasing lock...\n" ); + $this->db->query( 'COMMIT TRANSACTION', __METHOD__ ); + } +} + +$maintClass = "SqliteMaintenance"; +require_once( DO_MAINTENANCE ); \ No newline at end of file -- cgit v1.2.2