Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 60 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
SqliteMaintenance | |
0.00% |
0 / 60 |
|
0.00% |
0 / 6 |
380 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
vacuum | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
integrityCheck | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
backup | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
checkSyntax | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * Performs some operations specific to SQLite database backend. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Maintenance |
22 | */ |
23 | |
24 | use MediaWiki\Maintenance\Maintenance; |
25 | use Wikimedia\AtEase\AtEase; |
26 | use Wikimedia\Rdbms\DatabaseSqlite; |
27 | use Wikimedia\Rdbms\IMaintainableDatabase; |
28 | |
29 | // @codeCoverageIgnoreStart |
30 | require_once __DIR__ . '/Maintenance.php'; |
31 | // @codeCoverageIgnoreEnd |
32 | |
33 | /** |
34 | * Maintenance script that performs some operations specific to SQLite database backend. |
35 | * |
36 | * @ingroup Maintenance |
37 | */ |
38 | class SqliteMaintenance extends Maintenance { |
39 | public function __construct() { |
40 | parent::__construct(); |
41 | $this->addDescription( 'Performs some operations specific to SQLite database backend' ); |
42 | $this->addOption( |
43 | 'vacuum', |
44 | 'Clean up database by removing deleted pages. Decreases database file size' |
45 | ); |
46 | $this->addOption( 'integrity', 'Check database for integrity' ); |
47 | $this->addOption( 'backup-to', 'Backup database to the given file', false, true ); |
48 | $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true ); |
49 | } |
50 | |
51 | public function execute() { |
52 | // Should work even if we use a non-SQLite database |
53 | if ( $this->hasOption( 'check-syntax' ) ) { |
54 | $this->checkSyntax(); |
55 | return; |
56 | } |
57 | |
58 | $lb = $this->getServiceContainer()->getDBLoadBalancer(); |
59 | $dbw = $lb->getMaintenanceConnectionRef( DB_PRIMARY ); |
60 | if ( $dbw->getType() !== 'sqlite' ) { |
61 | $this->error( "This maintenance script requires a SQLite database.\n" ); |
62 | |
63 | return; |
64 | } |
65 | /** @var DatabaseSqlite $dbw */ |
66 | '@phan-var DatabaseSqlite $dbw'; |
67 | |
68 | if ( $this->hasOption( 'vacuum' ) ) { |
69 | $this->vacuum( $dbw ); |
70 | } |
71 | |
72 | if ( $this->hasOption( 'integrity' ) ) { |
73 | $this->integrityCheck( $dbw ); |
74 | } |
75 | |
76 | if ( $this->hasOption( 'backup-to' ) ) { |
77 | $this->backup( $dbw, $this->getOption( 'backup-to' ) ); |
78 | } |
79 | } |
80 | |
81 | private function vacuum( DatabaseSqlite $dbw ) { |
82 | $prevSize = filesize( $dbw->getDbFilePath() ); |
83 | if ( $prevSize == 0 ) { |
84 | $this->fatalError( "Can't vacuum an empty database.\n" ); |
85 | } |
86 | |
87 | $this->output( 'VACUUM: ' ); |
88 | if ( $dbw->query( 'VACUUM', __METHOD__ ) ) { |
89 | clearstatcache(); |
90 | $newSize = filesize( $dbw->getDbFilePath() ); |
91 | $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n", |
92 | $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) ); |
93 | } else { |
94 | $this->output( 'Error\n' ); |
95 | } |
96 | } |
97 | |
98 | private function integrityCheck( IMaintainableDatabase $dbw ) { |
99 | $this->output( "Performing database integrity checks:\n" ); |
100 | $res = $dbw->query( 'PRAGMA integrity_check', __METHOD__ ); |
101 | |
102 | if ( !$res || $res->numRows() == 0 ) { |
103 | $this->error( "Error: integrity check query returned nothing.\n" ); |
104 | |
105 | return; |
106 | } |
107 | |
108 | foreach ( $res as $row ) { |
109 | $this->output( $row->integrity_check ); |
110 | } |
111 | } |
112 | |
113 | private function backup( DatabaseSqlite $dbw, $fileName ) { |
114 | $this->output( "Backing up database:\n Locking..." ); |
115 | $dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ ); |
116 | $ourFile = $dbw->getDbFilePath(); |
117 | $this->output( " Copying database file $ourFile to $fileName..." ); |
118 | AtEase::suppressWarnings(); |
119 | if ( !copy( $ourFile, $fileName ) ) { |
120 | $err = error_get_last(); |
121 | $this->error( " {$err['message']}" ); |
122 | } |
123 | AtEase::restoreWarnings(); |
124 | $this->output( " Releasing lock...\n" ); |
125 | $dbw->query( 'COMMIT TRANSACTION', __METHOD__ ); |
126 | } |
127 | |
128 | private function checkSyntax() { |
129 | if ( !Sqlite::isPresent() ) { |
130 | $this->error( "Error: SQLite support not found\n" ); |
131 | } |
132 | $files = [ $this->getOption( 'check-syntax' ) ]; |
133 | $files = array_merge( $files, $this->mArgs ); |
134 | $result = Sqlite::checkSqlSyntax( $files ); |
135 | if ( $result === true ) { |
136 | $this->output( "SQL syntax check: no errors detected.\n" ); |
137 | } else { |
138 | $this->error( "Error: $result\n" ); |
139 | } |
140 | } |
141 | } |
142 | |
143 | // @codeCoverageIgnoreStart |
144 | $maintClass = SqliteMaintenance::class; |
145 | require_once RUN_MAINTENANCE_IF_MAIN; |
146 | // @codeCoverageIgnoreEnd |