| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use Wikimedia\AtEase\AtEase; |
| 25 |
use Wikimedia\Rdbms\DBConnRef; |
| 26 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 27 |
|
| 28 |
|
| 29 |
require_once __DIR__ . '/Maintenance.php'; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class SqliteMaintenance extends Maintenance { |
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
$this->addDescription( 'Performs some operations specific to SQLite database backend' ); |
| 41 |
$this->addOption( |
| 42 |
'vacuum', |
| 43 |
'Clean up database by removing deleted pages. Decreases database file size' |
| 44 |
); |
| 45 |
$this->addOption( 'integrity', 'Check database for integrity' ); |
| 46 |
$this->addOption( 'backup-to', 'Backup database to the given file', false, true ); |
| 47 |
$this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true ); |
| 48 |
} |
| 49 |
|
| 50 |
public function execute() { |
| 51 |
|
| 52 |
if ( $this->hasOption( 'check-syntax' ) ) { |
| 53 |
$this->checkSyntax(); |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$lb = $this->getServiceContainer()->getDBLoadBalancer(); |
| 58 |
$dbw = $lb->getMaintenanceConnectionRef( DB_PRIMARY ); |
| 59 |
if ( $dbw->getType() !== 'sqlite' ) { |
| 60 |
$this->error( "This maintenance script requires a SQLite database.\n" ); |
| 61 |
|
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
if ( $this->hasOption( 'vacuum' ) ) { |
| 66 |
$this->vacuum( $dbw ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( $this->hasOption( 'integrity' ) ) { |
| 70 |
$this->integrityCheck( $dbw ); |
| 71 |
} |
| 72 |
|
| 73 |
if ( $this->hasOption( 'backup-to' ) ) { |
| 74 |
$this->backup( $dbw, $this->getOption( 'backup-to' ) ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
private function vacuum( DBConnRef $dbw ) { |
| 79 |
|
| 80 |
$prevSize = filesize( $dbw->__call( 'getDbFilePath', [] ) ); |
| 81 |
if ( $prevSize == 0 ) { |
| 82 |
$this->fatalError( "Can't vacuum an empty database.\n" ); |
| 83 |
} |
| 84 |
|
| 85 |
$this->output( 'VACUUM: ' ); |
| 86 |
if ( $dbw->query( 'VACUUM', __METHOD__ ) ) { |
| 87 |
clearstatcache(); |
| 88 |
$newSize = filesize( $dbw->getDbFilePath() ); |
| 89 |
$this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n", |
| 90 |
$prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) ); |
| 91 |
} else { |
| 92 |
$this->output( 'Error\n' ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
private function integrityCheck( IMaintainableDatabase $dbw ) { |
| 97 |
$this->output( "Performing database integrity checks:\n" ); |
| 98 |
$res = $dbw->query( 'PRAGMA integrity_check', __METHOD__ ); |
| 99 |
|
| 100 |
if ( !$res || $res->numRows() == 0 ) { |
| 101 |
$this->error( "Error: integrity check query returned nothing.\n" ); |
| 102 |
|
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
foreach ( $res as $row ) { |
| 107 |
$this->output( $row->integrity_check ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
private function backup( DBConnRef $dbw, $fileName ) { |
| 112 |
$this->output( "Backing up database:\n Locking..." ); |
| 113 |
$dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ ); |
| 114 |
$ourFile = $dbw->__call( 'getDbFilePath', [] ); |
| 115 |
$this->output( " Copying database file $ourFile to $fileName..." ); |
| 116 |
AtEase::suppressWarnings(); |
| 117 |
if ( !copy( $ourFile, $fileName ) ) { |
| 118 |
$err = error_get_last(); |
| 119 |
$this->error( " {$err['message']}" ); |
| 120 |
} |
| 121 |
AtEase::restoreWarnings(); |
| 122 |
$this->output( " Releasing lock...\n" ); |
| 123 |
$dbw->query( 'COMMIT TRANSACTION', __METHOD__ ); |
| 124 |
} |
| 125 |
|
| 126 |
private function checkSyntax() { |
| 127 |
if ( !Sqlite::isPresent() ) { |
| 128 |
$this->error( "Error: SQLite support not found\n" ); |
| 129 |
} |
| 130 |
$files = [ $this->getOption( 'check-syntax' ) ]; |
| 131 |
$files = array_merge( $files, $this->mArgs ); |
| 132 |
$result = Sqlite::checkSqlSyntax( $files ); |
| 133 |
if ( $result === true ) { |
| 134 |
$this->output( "SQL syntax check: no errors detected.\n" ); |
| 135 |
} else { |
| 136 |
$this->error( "Error: $result\n" ); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
$maintClass = SqliteMaintenance::class; |
| 143 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 144 |
|