15require_once __DIR__ .
'/Maintenance.php';
25 parent::__construct();
26 $this->
addDescription(
'Performs some operations specific to SQLite database backend' );
29 'Clean up database by removing deleted pages. Decreases database file size'
31 $this->
addOption(
'integrity',
'Check database for integrity' );
32 $this->
addOption(
'backup-to',
'Backup database to the given file',
false,
true );
33 $this->
addOption(
'check-syntax',
'Check SQL file(s) for syntax errors',
false,
true );
38 if ( $this->
hasOption(
'check-syntax' ) ) {
44 $dbw = $lb->getMaintenanceConnectionRef(
DB_PRIMARY );
45 if ( $dbw->
getType() !==
'sqlite' ) {
46 $this->
error(
"This maintenance script requires a SQLite database.\n" );
52 $this->vacuum( $dbw );
56 $this->integrityCheck( $dbw );
60 $this->backup( $dbw, $this->
getOption(
'backup-to' ) );
64 private function vacuum(
DBConnRef $dbw ) {
66 $prevSize = filesize( $dbw->
__call(
'getDbFilePath', [] ) );
67 if ( $prevSize == 0 ) {
68 $this->
fatalError(
"Can't vacuum an empty database.\n" );
71 $this->
output(
'VACUUM: ' );
72 if ( $dbw->
query(
'VACUUM', __METHOD__ ) ) {
74 $newSize = filesize( $dbw->getDbFilePath() );
75 $this->
output( sprintf(
"Database size was %d, now %d (%.1f%% reduction).\n",
76 $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
78 $this->
output(
'Error\n' );
83 $this->
output(
"Performing database integrity checks:\n" );
84 $res = $dbw->
query(
'PRAGMA integrity_check', __METHOD__ );
86 if ( !$res || $res->numRows() == 0 ) {
87 $this->
error(
"Error: integrity check query returned nothing.\n" );
92 foreach ( $res as $row ) {
93 $this->
output( $row->integrity_check );
97 private function backup(
DBConnRef $dbw,
string $fileName ) {
98 $this->
output(
"Backing up database:\n Locking..." );
99 $dbw->
query(
'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
100 $ourFile = $dbw->
__call(
'getDbFilePath', [] );
101 $this->
output(
" Copying database file $ourFile to $fileName..." );
103 if ( !@copy( $ourFile, $fileName ) ) {
104 $err = error_get_last();
105 $this->
error(
" {$err['message']}" );
107 $this->
output(
" Releasing lock...\n" );
108 $dbw->
query(
'COMMIT TRANSACTION', __METHOD__ );
111 private function checkSyntax() {
113 $this->
error(
"Error: SQLite support not found\n" );
115 $files = [ $this->
getOption(
'check-syntax' ) ];
116 $files = array_merge( $files, $this->mArgs );
118 if ( $result ===
true ) {
119 $this->
output(
"SQL syntax check: no errors detected.\n" );
121 $this->
error(
"Error: $result\n" );
128require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Maintenance script that performs some operations specific to SQLite database backend.
execute()
Do the actual work.
__construct()
Default constructor.
static isPresent()
Checks whether PHP has SQLite support.
static checkSqlSyntax( $files)
Checks given files for correctness of SQL syntax.
Advanced database interface for IDatabase handles that include maintenance methods.