MediaWiki  master
deleteSelfExternals.php
Go to the documentation of this file.
1 <?php
26 
27 require_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 = wfParseUrl( $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->getDB( DB_PRIMARY );
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;
85 require_once RUN_MAINTENANCE_IF_MAIN;
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.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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.
A class containing constants representing the names of configuration variables.
const DB_PRIMARY
Definition: defines.php:28