MediaWiki REL1_37
deleteSelfExternals.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription( 'Delete self-references to $wgServer from externallinks' );
36 $this->setBatchSize( 1000 );
37 }
38
39 public function execute() {
40 global $wgServer;
41
42 // Extract the host and scheme from $wgServer
43 $bits = wfParseUrl( $wgServer );
44 if ( !$bits ) {
45 $this->fatalError( 'Could not parse $wgServer' );
46 }
47
48 $this->output( "Deleting self externals from $wgServer\n" );
49 $db = $this->getDB( DB_PRIMARY );
50
51 // If it's protocol-relative, we need to do both http and https.
52 // Otherwise, just do the specified scheme.
53 $host = $bits['host'];
54 if ( isset( $bits['port'] ) ) {
55 $host .= ':' . $bits['port'];
56 }
57 if ( $bits['scheme'] != '' ) {
58 $conds = [ LinkFilter::getQueryConditions( $host, [ 'protocol' => $bits['scheme'] . '://' ] ) ];
59 } else {
60 $conds = [
61 LinkFilter::getQueryConditions( $host, [ 'protocol' => 'http://' ] ),
62 LinkFilter::getQueryConditions( $host, [ 'protocol' => 'https://' ] ),
63 ];
64 }
65
66 foreach ( $conds as $cond ) {
67 if ( !$cond ) {
68 continue;
69 }
70 $cond = $db->makeList( $cond, LIST_AND );
71 do {
72 $this->commitTransaction( $db, __METHOD__ );
73 $q = $db->limitResult( "DELETE /* deleteSelfExternals */ FROM externallinks WHERE $cond",
74 $this->mBatchSize );
75 $this->output( "Deleting a batch\n" );
76 $db->query( $q, __METHOD__ );
77 } while ( $db->affectedRows() );
78 }
79 }
80}
81
82$maintClass = DeleteSelfExternals::class;
83require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
$wgServer
URL of the server.
const LIST_AND
Definition Defines.php:43
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Maintenance script that deletes self-references to $wgServer from the externallinks table.
__construct()
Default constructor.
execute()
Do the actual work.
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
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.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
const DB_PRIMARY
Definition defines.php:27