MediaWiki master
deleteSelfExternals.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
37class 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;
105require_once RUN_MAINTENANCE_IF_MAIN;
106// @codeCoverageIgnoreEnd
Maintenance script that deletes self-references to $wgServer from the externallinks table.
A class containing constants representing the names of configuration variables.