MediaWiki  master
deleteSelfExternals.php
Go to the documentation of this file.
1 <?php
24 require_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;
83 require_once RUN_MAINTENANCE_IF_MAIN;
LinkFilter\getQueryConditions
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
Definition: LinkFilter.php:256
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
LIST_AND
const LIST_AND
Definition: Defines.php:43
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:776
DeleteSelfExternals\__construct
__construct()
Default constructor.
Definition: deleteSelfExternals.php:33
DeleteSelfExternals\execute
execute()
Do the actual work.
Definition: deleteSelfExternals.php:39
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
$wgServer
$wgServer
URL of the server.
Definition: DefaultSettings.php:108
DeleteSelfExternals
Maintenance script that deletes self-references to $wgServer from the externallinks table.
Definition: deleteSelfExternals.php:32
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1421
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1375
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
$maintClass
$maintClass
Definition: deleteSelfExternals.php:82
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition: Maintenance.php:375