X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=maintenance%2Fsqlite.php;h=864d5ab640e09cd57fd3a611af7ca503c825d0aa;hb=d266f40a2f828810d0249de1fdd7107a026066d4;hp=46a038cecae2bad20b519b72b12b928baacbe3d9;hpb=1ddb7210be9e5910b5e0c2a87f5c300455d6b1ca;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/sqlite.php b/maintenance/sqlite.php index 46a038ceca..864d5ab640 100644 --- a/maintenance/sqlite.php +++ b/maintenance/sqlite.php @@ -20,7 +20,7 @@ * @ingroup Maintenance */ -require_once( dirname(__FILE__) . '/Maintenance.php' ); +require_once( dirname( __FILE__ ) . '/Maintenance.php' ); class SqliteMaintenance extends Maintenance { public function __construct() { @@ -28,37 +28,59 @@ class SqliteMaintenance extends Maintenance { $this->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 ); + $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true ); + } + + /** + * While we use database connection, this simple lie prevents useless --dbpass and + * --dbuser options from appearing in help message for this script. + * + * @return int DB constant + */ + 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" ); + // Should work even if we use a non-SQLite database + if ( $this->hasOption( 'check-syntax' ) ) { + $this->checkSyntax(); return; } $this->db = wfGetDB( DB_MASTER ); - if ( $this->hasOption( 'vacuum' ) ) + if ( $this->db->getType() != 'sqlite' ) { + $this->error( "This maintenance script requires a SQLite database.\n" ); + return; + } + + if ( $this->hasOption( 'vacuum' ) ) { $this->vacuum(); + } - if ( $this->hasOption( 'integrity' ) ) + 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 ) ); + $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) ); } else { $this->output( 'Error\n' ); } @@ -77,7 +99,36 @@ class SqliteMaintenance extends Maintenance { $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__ ); + } + + private function checkSyntax() { + if ( !Sqlite::IsPresent() ) { + $this->error( "Error: SQLite support not found\n" ); + } + $files = array( $this->getOption( 'check-syntax' ) ); + $files += $this->mArgs; + $result = Sqlite::checkSqlSyntax( $files ); + if ( $result === true ) { + $this->output( "SQL syntax check: no errors detected.\n" ); + } else { + $this->error( "Error: $result\n" ); + } + } } $maintClass = "SqliteMaintenance"; -require_once( DO_MAINTENANCE ); \ No newline at end of file +require_once( RUN_MAINTENANCE_IF_MAIN );