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