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