MediaWiki master
deleteSelfExternals.php
Go to the documentation of this file.
1<?php
26
27require_once __DIR__ . '/Maintenance.php';
28
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;
const LIST_AND
Definition Defines.php:43
Maintenance script that deletes self-references to $wgServer from the externallinks table.
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.