MediaWiki  master
SqliteMaintenance.php
Go to the documentation of this file.
1 <?php
25 
27 
28 require_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;
140 require_once RUN_MAINTENANCE_IF_MAIN;
SqliteMaintenance\execute
execute()
Do the actual work.
Definition: SqliteMaintenance.php:48
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:36
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
$maintClass
$maintClass
Definition: SqliteMaintenance.php:139
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$res
$res
Definition: testCompression.php:57
SqliteMaintenance\__construct
__construct()
Default constructor.
Definition: SqliteMaintenance.php:36
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
SqliteMaintenance\integrityCheck
integrityCheck(DatabaseSqlite $dbw)
Definition: SqliteMaintenance.php:94
Wikimedia\Rdbms\DatabaseSqlite\getDbFilePath
getDbFilePath()
Definition: DatabaseSqlite.php:219
Sqlite\isPresent
static isPresent()
Checks whether PHP has SQLite support.
Definition: Sqlite.php:38
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Wikimedia\Rdbms\Database\query
query( $sql, $fname=__METHOD__, $flags=self::QUERY_NORMAL)
Run an SQL query and return the result.
Definition: Database.php:1316
SqliteMaintenance
Maintenance script that performs some operations specific to SQLite database backend.
Definition: SqliteMaintenance.php:35
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Sqlite\checkSqlSyntax
static checkSqlSyntax( $files)
Checks given files for correctness of SQL syntax.
Definition: Sqlite.php:50
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:464
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
SqliteMaintenance\backup
backup(DatabaseSqlite $dbw, $fileName)
Definition: SqliteMaintenance.php:109
SqliteMaintenance\vacuum
vacuum(DatabaseSqlite $dbw)
Definition: SqliteMaintenance.php:77
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
SqliteMaintenance\checkSyntax
checkSyntax()
Definition: SqliteMaintenance.php:124