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