MediaWiki  REL1_20
sqlite.php
Go to the documentation of this file.
00001 <?php
00024 require_once( __DIR__ . '/Maintenance.php' );
00025 
00031 class SqliteMaintenance extends Maintenance {
00032         public function __construct() {
00033                 parent::__construct();
00034                 $this->mDescription = "Performs some operations specific to SQLite database backend";
00035                 $this->addOption( 'vacuum', 'Clean up database by removing deleted pages. Decreases database file size' );
00036                 $this->addOption( 'integrity', 'Check database for integrity' );
00037                 $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
00038                 $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
00039         }
00040 
00047         public function getDbType() {
00048                 return Maintenance::DB_NONE;
00049         }
00050 
00051         public function execute() {
00052                 // Should work even if we use a non-SQLite database
00053                 if ( $this->hasOption( 'check-syntax' ) ) {
00054                         $this->checkSyntax();
00055                         return;
00056                 }
00057 
00058                 $this->db = wfGetDB( DB_MASTER );
00059 
00060                 if ( $this->db->getType() != 'sqlite' ) {
00061                         $this->error( "This maintenance script requires a SQLite database.\n" );
00062                         return;
00063                 }
00064 
00065                 if ( $this->hasOption( 'vacuum' ) ) {
00066                         $this->vacuum();
00067                 }
00068 
00069                 if ( $this->hasOption( 'integrity' ) ) {
00070                         $this->integrityCheck();
00071                 }
00072 
00073                 if ( $this->hasOption( 'backup-to' ) ) {
00074                         $this->backup( $this->getOption( 'backup-to' ) );
00075                 }
00076         }
00077 
00078         private function vacuum() {
00079                 $prevSize = filesize( $this->db->mDatabaseFile );
00080                 if ( $prevSize == 0 ) {
00081                         $this->error( "Can't vacuum an empty database.\n", true );
00082                 }
00083 
00084                 $this->output( 'VACUUM: ' );
00085                 if ( $this->db->query( 'VACUUM' ) ) {
00086                         clearstatcache();
00087                         $newSize = filesize( $this->db->mDatabaseFile );
00088                         $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
00089                                 $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
00090                 } else {
00091                         $this->output( 'Error\n' );
00092                 }
00093         }
00094 
00095         private function integrityCheck() {
00096                 $this->output( "Performing database integrity checks:\n" );
00097                 $res = $this->db->query( 'PRAGMA integrity_check' );
00098 
00099                 if ( !$res || $res->numRows() == 0 ) {
00100                         $this->error( "Error: integrity check query returned nothing.\n" );
00101                         return;
00102                 }
00103 
00104                 foreach ( $res as $row ) {
00105                         $this->output( $row->integrity_check );
00106                 }
00107         }
00108 
00109         private function backup( $fileName ) {
00110                 $this->output( "Backing up database:\n   Locking..." );
00111                 $this->db->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
00112                 $ourFile = $this->db->mDatabaseFile;
00113                 $this->output( "   Copying database file $ourFile to $fileName... " );
00114                 wfSuppressWarnings( false );
00115                 if ( !copy( $ourFile, $fileName ) ) {
00116                         $err = error_get_last();
00117                         $this->error( "      {$err['message']}" );
00118                 }
00119                 wfSuppressWarnings( true );
00120                 $this->output( "   Releasing lock...\n" );
00121                 $this->db->query( 'COMMIT TRANSACTION', __METHOD__ );
00122         }
00123 
00124         private function checkSyntax() {
00125                 if ( !Sqlite::IsPresent() ) {
00126                         $this->error( "Error: SQLite support not found\n" );
00127                 }
00128                 $files = array( $this->getOption( 'check-syntax' ) );
00129                 $files += $this->mArgs;
00130                 $result = Sqlite::checkSqlSyntax( $files );
00131                 if ( $result === true ) {
00132                         $this->output( "SQL syntax check: no errors detected.\n" );
00133                 } else {
00134                         $this->error( "Error: $result\n" );
00135                 }
00136         }
00137 }
00138 
00139 $maintClass = "SqliteMaintenance";
00140 require_once( RUN_MAINTENANCE_IF_MAIN );