MediaWiki master
deleteSelfExternals.php
Go to the documentation of this file.
1<?php
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Delete self-references to $wgServer from externallinks' );
42 $this->setBatchSize( 1000 );
43 }
44
45 public function execute() {
46 // Extract the host and scheme from $wgServer
47 $server = $this->getConfig()->get( MainConfigNames::Server );
48 $bits = $this->getServiceContainer()->getUrlUtils()->parse( $server );
49 if ( !$bits ) {
50 $this->fatalError( 'Could not parse $wgServer' );
51 }
52
53 $this->output( "Deleting self externals from $server\n" );
54 $db = $this->getPrimaryDB();
55
56 // If it's protocol-relative, we need to do both http and https.
57 // Otherwise, just do the specified scheme.
58 $host = $bits['host'];
59 if ( isset( $bits['port'] ) ) {
60 $host .= ':' . $bits['port'];
61 }
62 if ( $bits['scheme'] != '' ) {
63 $conds = [ LinkFilter::getQueryConditions( $host, [ 'protocol' => $bits['scheme'] . '://' ] ) ];
64 } else {
65 $conds = [
66 LinkFilter::getQueryConditions( $host, [ 'protocol' => 'http://' ] ),
67 LinkFilter::getQueryConditions( $host, [ 'protocol' => 'https://' ] ),
68 ];
69 }
70
71 // Convert the array of $conds into an IExpression object for use in the DELETE query
72 // The use of array_filter is just there for a sanity check, as LinkFilter::getQueryConditions
73 // only returns false if the host was invalid (we have already validated this above).
74 $conds = array_map( static function ( $cond ) use ( $db ) {
75 return $db->andExpr( $cond );
76 }, array_filter( $conds ) );
77 $domainExpr = $db->orExpr( $conds );
78
79 $totalRows = 0;
80 $batchStart = 0;
81 $batchEnd = $batchStart + $this->getBatchSize();
82 do {
83 $this->output( "Deleting self-externals with el_id $batchStart to $batchEnd\n" );
84
85 $db->newDeleteQueryBuilder()
86 ->deleteFrom( 'externallinks' )
87 ->where( $domainExpr )
88 ->andWhere( $db->expr( 'el_id', '>', $batchStart ) )
89 ->andWhere( $db->expr( 'el_id', '<=', $batchEnd ) )
90 ->caller( __METHOD__ )
91 ->execute();
92 $rowsDeletedInThisBatch = $db->affectedRows();
93 $totalRows += $rowsDeletedInThisBatch;
94
95 $batchStart += $this->getBatchSize();
96 $batchEnd += $this->getBatchSize();
97 $this->waitForReplication();
98 } while ( $rowsDeletedInThisBatch );
99
100 $this->output( "done; deleted $totalRows rows\n" );
101 }
102}
103
104// @codeCoverageIgnoreStart
105$maintClass = DeleteSelfExternals::class;
106require_once RUN_MAINTENANCE_IF_MAIN;
107// @codeCoverageIgnoreEnd
Maintenance script that deletes self-references to $wgServer from the externallinks table.
__construct()
Default constructor.
execute()
Do the actual work.
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
waitForReplication()
Wait for replica DB servers to catch up.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.