00001 <?php 00023 require_once( dirname(__FILE__) . '/Maintenance.php' ); 00024 00025 class SqliteMaintenance extends Maintenance { 00026 public function __construct() { 00027 parent::__construct(); 00028 $this->mDescription = "Performs some operations specific to SQLite database backend"; 00029 $this->addOption( 'vacuum', 'Clean up database by removing deleted pages. Decreases database file size' ); 00030 $this->addOption( 'integrity', 'Check database for integrity' ); 00031 $this->addOption( 'backup-to', 'Backup database to the given file', false, true ); 00032 } 00033 00038 public function getDbType() { 00039 return Maintenance::DB_NONE; 00040 } 00041 00042 public function execute() { 00043 global $wgDBtype; 00044 00045 if ( $wgDBtype != 'sqlite' ) { 00046 $this->error( "This maintenance script requires a SQLite database.\n" ); 00047 return; 00048 } 00049 00050 $this->db = wfGetDB( DB_MASTER ); 00051 00052 if ( $this->hasOption( 'vacuum' ) ) { 00053 $this->vacuum(); 00054 } 00055 00056 if ( $this->hasOption( 'integrity' ) ) { 00057 $this->integrityCheck(); 00058 } 00059 00060 if ( $this->hasOption( 'backup-to' ) ) { 00061 $this->backup( $this->getOption( 'backup-to' ) ); 00062 } 00063 } 00064 00065 private function vacuum() { 00066 $prevSize = filesize( $this->db->mDatabaseFile ); 00067 if ( $prevSize == 0 ) { 00068 $this->error( "Can't vacuum an empty database.\n", true ); 00069 } 00070 00071 $this->output( 'VACUUM: ' ); 00072 if ( $this->db->query( 'VACUUM' ) ) { 00073 clearstatcache(); 00074 $newSize = filesize( $this->db->mDatabaseFile ); 00075 $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n", 00076 $prevSize, $newSize, ( $prevSize - $newSize) * 100.0 / $prevSize ) ); 00077 } else { 00078 $this->output( 'Error\n' ); 00079 } 00080 } 00081 00082 private function integrityCheck() { 00083 $this->output( "Performing database integrity checks:\n" ); 00084 $res = $this->db->query( 'PRAGMA integrity_check' ); 00085 00086 if ( !$res || $res->numRows() == 0 ) { 00087 $this->error( "Error: integrity check query returned nothing.\n" ); 00088 return; 00089 } 00090 00091 foreach ( $res as $row ) { 00092 $this->output( $row->integrity_check ); 00093 } 00094 } 00095 00096 private function backup( $fileName ) { 00097 $this->output( "Backing up database:\n Locking..." ); 00098 $this->db->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ ); 00099 $ourFile = $this->db->mDatabaseFile; 00100 $this->output( " Copying database file $ourFile to $fileName... " ); 00101 wfSuppressWarnings( false ); 00102 if ( !copy( $ourFile, $fileName ) ) { 00103 $err = error_get_last(); 00104 $this->error( " {$err['message']}" ); 00105 } 00106 wfSuppressWarnings( true ); 00107 $this->output( " Releasing lock...\n" ); 00108 $this->db->query( 'COMMIT TRANSACTION', __METHOD__ ); 00109 } 00110 } 00111 00112 $maintClass = "SqliteMaintenance"; 00113 require_once( DO_MAINTENANCE );