| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\ExternalLinks\LinkFilter; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
|
| 27 |
|
| 28 |
require_once __DIR__ . '/Maintenance.php'; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class DeleteSelfExternals extends Maintenance { |
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
$this->addDescription( 'Delete self-references to $wgServer from externallinks' ); |
| 41 |
$this->setBatchSize( 1000 ); |
| 42 |
} |
| 43 |
|
| 44 |
public function execute() { |
| 45 |
|
| 46 |
$server = $this->getConfig()->get( MainConfigNames::Server ); |
| 47 |
$bits = $this->getServiceContainer()->getUrlUtils()->parse( $server ); |
| 48 |
if ( !$bits ) { |
| 49 |
$this->fatalError( 'Could not parse $wgServer' ); |
| 50 |
} |
| 51 |
|
| 52 |
$this->output( "Deleting self externals from $server\n" ); |
| 53 |
$db = $this->getPrimaryDB(); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
$host = $bits['host']; |
| 58 |
if ( isset( $bits['port'] ) ) { |
| 59 |
$host .= ':' . $bits['port']; |
| 60 |
} |
| 61 |
if ( $bits['scheme'] != '' ) { |
| 62 |
$conds = [ LinkFilter::getQueryConditions( $host, [ 'protocol' => $bits['scheme'] . '://' ] ) ]; |
| 63 |
} else { |
| 64 |
$conds = [ |
| 65 |
LinkFilter::getQueryConditions( $host, [ 'protocol' => 'http://' ] ), |
| 66 |
LinkFilter::getQueryConditions( $host, [ 'protocol' => 'https://' ] ), |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
$conds = array_map( static function ( $cond ) use ( $db ) { |
| 74 |
return $db->andExpr( $cond ); |
| 75 |
}, array_filter( $conds ) ); |
| 76 |
$domainExpr = $db->orExpr( $conds ); |
| 77 |
|
| 78 |
$totalRows = 0; |
| 79 |
$batchStart = 0; |
| 80 |
$batchEnd = $batchStart + $this->getBatchSize(); |
| 81 |
do { |
| 82 |
$this->output( "Deleting self-externals with el_id $batchStart to $batchEnd\n" ); |
| 83 |
|
| 84 |
$db->newDeleteQueryBuilder() |
| 85 |
->deleteFrom( 'externallinks' ) |
| 86 |
->where( $domainExpr ) |
| 87 |
->andWhere( $db->expr( 'el_id', '>', $batchStart ) ) |
| 88 |
->andWhere( $db->expr( 'el_id', '<=', $batchEnd ) ) |
| 89 |
->caller( __METHOD__ ) |
| 90 |
->execute(); |
| 91 |
$rowsDeletedInThisBatch = $db->affectedRows(); |
| 92 |
$totalRows += $rowsDeletedInThisBatch; |
| 93 |
|
| 94 |
$batchStart += $this->getBatchSize(); |
| 95 |
$batchEnd += $this->getBatchSize(); |
| 96 |
$this->waitForReplication(); |
| 97 |
} while ( $rowsDeletedInThisBatch ); |
| 98 |
|
| 99 |
$this->output( "done; deleted $totalRows rows\n" ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
$maintClass = DeleteSelfExternals::class; |
| 105 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 106 |
|