Debian Codegraph @

maintenance/deleteSelfExternals.php

raw symbols SCIP
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
24 use MediaWiki\ExternalLinks\LinkFilter;
25 use MediaWiki\MainConfigNames;
26
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
30
31 /**
32 * Maintenance script that deletes self-references to $wgServer
33 * from the externallinks table.
34 *
35 * @ingroup Maintenance
36 */
37 class DeleteSelfExternals extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Delete self-references to $wgServer from externallinks' );
41 $this->setBatchSize( 1000 );
42 }
43
44 public function execute() {
45 // Extract the host and scheme from $wgServer
46 $server = $this->getConfig()->get( MainConfigNames::Server );
47 $bits = $this->getServiceContainer()->getUrlUtils()->parse( $server );
48 if ( !$bits ) {
49 $this->fatalError( 'Could not parse $wgServer' );
50 }
51
52 $this->output( "Deleting self externals from $server\n" );
53 $db = $this->getPrimaryDB();
54
55 // If it's protocol-relative, we need to do both http and https.
56 // Otherwise, just do the specified scheme.
57 $host = $bits['host'];
58 if ( isset( $bits['port'] ) ) {
59 $host .= ':' . $bits['port'];
60 }
61 if ( $bits['scheme'] != '' ) {
62 $conds = [ LinkFilter::getQueryConditions( $host, [ 'protocol' => $bits['scheme'] . '://' ] ) ];
63 } else {
64 $conds = [
65 LinkFilter::getQueryConditions( $host, [ 'protocol' => 'http://' ] ),
66 LinkFilter::getQueryConditions( $host, [ 'protocol' => 'https://' ] ),
67 ];
68 }
69
70 // Convert the array of $conds into an IExpression object for use in the DELETE query
71 // The use of array_filter is just there for a sanity check, as LinkFilter::getQueryConditions
72 // only returns false if the host was invalid (we have already validated this above).
73 $conds = array_map( static function ( $cond ) use ( $db ) {
74 return $db->andExpr( $cond );
75 }, array_filter( $conds ) );
76 $domainExpr = $db->orExpr( $conds );
77
78 $totalRows = 0;
79 $batchStart = 0;
80 $batchEnd = $batchStart + $this->getBatchSize();
81 do {
82 $this->output( "Deleting self-externals with el_id $batchStart to $batchEnd\n" );
83
84 $db->newDeleteQueryBuilder()
85 ->deleteFrom( 'externallinks' )
86 ->where( $domainExpr )
87 ->andWhere( $db->expr( 'el_id', '>', $batchStart ) )
88 ->andWhere( $db->expr( 'el_id', '<=', $batchEnd ) )
89 ->caller( __METHOD__ )
90 ->execute();
91 $rowsDeletedInThisBatch = $db->affectedRows();
92 $totalRows += $rowsDeletedInThisBatch;
93
94 $batchStart += $this->getBatchSize();
95 $batchEnd += $this->getBatchSize();
96 $this->waitForReplication();
97 } while ( $rowsDeletedInThisBatch );
98
99 $this->output( "done; deleted $totalRows rows\n" );
100 }
101 }
102
103 // @codeCoverageIgnoreStart
104 $maintClass = DeleteSelfExternals::class;
105 require_once RUN_MAINTENANCE_IF_MAIN;
106 // @codeCoverageIgnoreEnd