Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.45% covered (success)
95.45%
21 / 22
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PatchSql
95.45% covered (success)
95.45%
21 / 22
66.67% covered (warning)
66.67%
2 / 3
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getDbType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
4.00
1<?php
2/**
3 * Manually run an SQL patch outside of the general updaters.
4 * This ensures that the DB options (charset, prefix, engine) are correctly set.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25use MediaWiki\Installer\DatabaseUpdater;
26use MediaWiki\Maintenance\Maintenance;
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
32/**
33 * Maintenance script that manually runs an SQL patch outside of the general updaters.
34 *
35 * @ingroup Maintenance
36 */
37class PatchSql extends Maintenance {
38    public function __construct() {
39        parent::__construct();
40        $this->addDescription( 'Run an SQL file into the DB, replacing prefix and charset vars' );
41        $this->addArg(
42            'patch-name',
43            'Name of the patch file, either full path or in sql/$dbtype/'
44        );
45    }
46
47    public function getDbType() {
48        return Maintenance::DB_ADMIN;
49    }
50
51    public function execute() {
52        $dbw = $this->getDB( DB_PRIMARY );
53        $updater = DatabaseUpdater::newForDB( $dbw, true, $this );
54
55        foreach ( $this->getArgs() as $name ) {
56            $files = [
57                $name,
58                $updater->patchPath( $dbw, $name ),
59                $updater->patchPath( $dbw, "patch-$name.sql" ),
60            ];
61            foreach ( $files as $file ) {
62                if ( file_exists( $file ) ) {
63                    $this->output( "$file ...\n" );
64                    $dbw->sourceFile( $file );
65                    continue 2;
66                }
67            }
68            $this->error( "Could not find $name\n" );
69        }
70        $this->output( "done.\n" );
71    }
72}
73
74// @codeCoverageIgnoreStart
75$maintClass = PatchSql::class;
76require_once RUN_MAINTENANCE_IF_MAIN;
77// @codeCoverageIgnoreEnd