MediaWiki REL1_37
SqliteMaintenance.php
Go to the documentation of this file.
1<?php
25
27
28require_once __DIR__ . '/Maintenance.php';
29
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Performs some operations specific to SQLite database backend' );
39 $this->addOption(
40 'vacuum',
41 'Clean up database by removing deleted pages. Decreases database file size'
42 );
43 $this->addOption( 'integrity', 'Check database for integrity' );
44 $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
45 $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
46 }
47
48 public function execute() {
49 // Should work even if we use a non-SQLite database
50 if ( $this->hasOption( 'check-syntax' ) ) {
51 $this->checkSyntax();
52
53 return;
54 }
55
56 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
57 $dbw = $lb->getConnection( DB_PRIMARY );
58 if ( !( $dbw instanceof DatabaseSqlite ) ) {
59 $this->error( "This maintenance script requires a SQLite database.\n" );
60
61 return;
62 }
63
64 if ( $this->hasOption( 'vacuum' ) ) {
65 $this->vacuum( $dbw );
66 }
67
68 if ( $this->hasOption( 'integrity' ) ) {
69 $this->integrityCheck( $dbw );
70 }
71
72 if ( $this->hasOption( 'backup-to' ) ) {
73 $this->backup( $dbw, $this->getOption( 'backup-to' ) );
74 }
75 }
76
77 private function vacuum( DatabaseSqlite $dbw ) {
78 $prevSize = filesize( $dbw->getDbFilePath() );
79 if ( $prevSize == 0 ) {
80 $this->fatalError( "Can't vacuum an empty database.\n" );
81 }
82
83 $this->output( 'VACUUM: ' );
84 if ( $dbw->query( 'VACUUM', __METHOD__ ) ) {
85 clearstatcache();
86 $newSize = filesize( $dbw->getDbFilePath() );
87 $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
88 $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
89 } else {
90 $this->output( 'Error\n' );
91 }
92 }
93
94 private function integrityCheck( DatabaseSqlite $dbw ) {
95 $this->output( "Performing database integrity checks:\n" );
96 $res = $dbw->query( 'PRAGMA integrity_check', __METHOD__ );
97
98 if ( !$res || $res->numRows() == 0 ) {
99 $this->error( "Error: integrity check query returned nothing.\n" );
100
101 return;
102 }
103
104 foreach ( $res as $row ) {
105 $this->output( $row->integrity_check );
106 }
107 }
108
109 private function backup( DatabaseSqlite $dbw, $fileName ) {
110 $this->output( "Backing up database:\n Locking..." );
111 $dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
112 $ourFile = $dbw->getDbFilePath();
113 $this->output( " Copying database file $ourFile to $fileName..." );
114 Wikimedia\suppressWarnings();
115 if ( !copy( $ourFile, $fileName ) ) {
116 $err = error_get_last();
117 $this->error( " {$err['message']}" );
118 }
119 Wikimedia\restoreWarnings();
120 $this->output( " Releasing lock...\n" );
121 $dbw->query( 'COMMIT TRANSACTION', __METHOD__ );
122 }
123
124 private function checkSyntax() {
125 if ( !Sqlite::isPresent() ) {
126 $this->error( "Error: SQLite support not found\n" );
127 }
128 $files = [ $this->getOption( 'check-syntax' ) ];
129 $files = array_merge( $files, $this->mArgs );
130 $result = Sqlite::checkSqlSyntax( $files );
131 if ( $result === true ) {
132 $this->output( "SQL syntax check: no errors detected.\n" );
133 } else {
134 $this->error( "Error: $result\n" );
135 }
136 }
137}
138
139$maintClass = SqliteMaintenance::class;
140require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Maintenance script that performs some operations specific to SQLite database backend.
backup(DatabaseSqlite $dbw, $fileName)
execute()
Do the actual work.
__construct()
Default constructor.
vacuum(DatabaseSqlite $dbw)
integrityCheck(DatabaseSqlite $dbw)
static isPresent()
Checks whether PHP has SQLite support.
Definition Sqlite.php:38
static checkSqlSyntax( $files)
Checks given files for correctness of SQL syntax.
Definition Sqlite.php:50
query( $sql, $fname=__METHOD__, $flags=self::QUERY_NORMAL)
Run an SQL query and return the result.
const DB_PRIMARY
Definition defines.php:27