Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteSelfExternals
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Delete self-references to $wgServer from the externallinks table.
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
24use MediaWiki\ExternalLinks\LinkFilter;
25use MediaWiki\MainConfigNames;
26
27require_once __DIR__ . '/Maintenance.php';
28
29/**
30 * Maintenance script that deletes self-references to $wgServer
31 * from the externallinks table.
32 *
33 * @ingroup Maintenance
34 */
35class DeleteSelfExternals extends Maintenance {
36    public function __construct() {
37        parent::__construct();
38        $this->addDescription( 'Delete self-references to $wgServer from externallinks' );
39        $this->setBatchSize( 1000 );
40    }
41
42    public function execute() {
43        // Extract the host and scheme from $wgServer
44        $server = $this->getConfig()->get( MainConfigNames::Server );
45        $bits = $this->getServiceContainer()->getUrlUtils()->parse( $server );
46        if ( !$bits ) {
47            $this->fatalError( 'Could not parse $wgServer' );
48        }
49
50        $this->output( "Deleting self externals from $server\n" );
51        $db = $this->getPrimaryDB();
52
53        // If it's protocol-relative, we need to do both http and https.
54        // Otherwise, just do the specified scheme.
55        $host = $bits['host'];
56        if ( isset( $bits['port'] ) ) {
57            $host .= ':' . $bits['port'];
58        }
59        if ( $bits['scheme'] != '' ) {
60            $conds = [ LinkFilter::getQueryConditions( $host, [ 'protocol' => $bits['scheme'] . '://' ] ) ];
61        } else {
62            $conds = [
63                LinkFilter::getQueryConditions( $host, [ 'protocol' => 'http://' ] ),
64                LinkFilter::getQueryConditions( $host, [ 'protocol' => 'https://' ] ),
65            ];
66        }
67
68        foreach ( $conds as $cond ) {
69            if ( !$cond ) {
70                continue;
71            }
72            $cond = $db->makeList( $cond, LIST_AND );
73            do {
74                $this->commitTransaction( $db, __METHOD__ );
75                $q = $db->limitResult( "DELETE /* deleteSelfExternals */ FROM externallinks WHERE $cond",
76                    $this->mBatchSize );
77                $this->output( "Deleting a batch\n" );
78                $db->query( $q, __METHOD__ );
79            } while ( $db->affectedRows() );
80        }
81    }
82}
83
84$maintClass = DeleteSelfExternals::class;
85require_once RUN_MAINTENANCE_IF_MAIN;