Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 79 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| MigrateExternallinks | |
0.00% |
0 / 79 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| getUpdateKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doDBUpdates | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
30 | |||
| handleBatch | |
0.00% |
0 / 40 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | use MediaWiki\Deferred\LinksUpdate\ExternalLinksTable; |
| 4 | use MediaWiki\ExternalLinks\LinkFilter; |
| 5 | use MediaWiki\Maintenance\LoggedUpdateMaintenance; |
| 6 | |
| 7 | // @codeCoverageIgnoreStart |
| 8 | require_once __DIR__ . '/Maintenance.php'; |
| 9 | // @codeCoverageIgnoreEnd |
| 10 | |
| 11 | /** |
| 12 | * Maintenance script that migrates externallinks data |
| 13 | * |
| 14 | * @ingroup Maintenance |
| 15 | * @since 1.40 |
| 16 | */ |
| 17 | class MigrateExternallinks extends LoggedUpdateMaintenance { |
| 18 | public function __construct() { |
| 19 | parent::__construct(); |
| 20 | $this->addDescription( |
| 21 | 'Migrate externallinks data' |
| 22 | ); |
| 23 | $this->addOption( |
| 24 | 'sleep', |
| 25 | 'Sleep time (in seconds) between every batch. Default: 0', |
| 26 | false, |
| 27 | true |
| 28 | ); |
| 29 | $this->setBatchSize( 1000 ); |
| 30 | } |
| 31 | |
| 32 | /** @inheritDoc */ |
| 33 | protected function getUpdateKey() { |
| 34 | return __CLASS__; |
| 35 | } |
| 36 | |
| 37 | /** @inheritDoc */ |
| 38 | protected function doDBUpdates() { |
| 39 | /** @var \Wikimedia\Rdbms\Database $dbw */ |
| 40 | $dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( |
| 41 | ExternalLinksTable::VIRTUAL_DOMAIN |
| 42 | ); |
| 43 | '@phan-var \Wikimedia\Rdbms\Database $dbw'; |
| 44 | $table = 'externallinks'; |
| 45 | if ( !$dbw->fieldExists( $table, 'el_to', __METHOD__ ) ) { |
| 46 | $this->output( "Old fields don't exist. There is no need to run this script\n" ); |
| 47 | return true; |
| 48 | } |
| 49 | if ( !$dbw->fieldExists( $table, 'el_to_path', __METHOD__ ) ) { |
| 50 | $this->output( "Run update.php to create the el_to_path column.\n" ); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | $this->output( "Populating el_to_domain_index and el_to_path columns\n" ); |
| 55 | $updated = 0; |
| 56 | |
| 57 | $highestId = $dbw->newSelectQueryBuilder() |
| 58 | ->select( 'el_id' ) |
| 59 | ->from( $table ) |
| 60 | ->caller( __METHOD__ ) |
| 61 | ->orderBy( 'el_id', 'DESC' ) |
| 62 | ->fetchField(); |
| 63 | if ( !$highestId ) { |
| 64 | $this->output( "Page table is empty.\n" ); |
| 65 | return true; |
| 66 | } |
| 67 | $id = 0; |
| 68 | while ( $id <= $highestId ) { |
| 69 | $updated += $this->handleBatch( $id ); |
| 70 | $id += $this->getBatchSize(); |
| 71 | } |
| 72 | |
| 73 | $this->output( "Completed normalization of $table, $updated rows updated.\n" ); |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | private function handleBatch( int $lowId ): int { |
| 79 | $batchSize = $this->getBatchSize(); |
| 80 | // range is inclusive, let's subtract one. |
| 81 | $highId = $lowId + $batchSize - 1; |
| 82 | $dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( |
| 83 | ExternalLinksTable::VIRTUAL_DOMAIN |
| 84 | ); |
| 85 | $updated = 0; |
| 86 | $res = $dbw->newSelectQueryBuilder() |
| 87 | ->select( [ 'el_id', 'el_to' ] ) |
| 88 | ->from( 'externallinks' ) |
| 89 | ->where( [ |
| 90 | 'el_to_domain_index' => '', |
| 91 | $dbw->expr( 'el_id', '>=', $lowId ), |
| 92 | $dbw->expr( 'el_id', '<=', $highId ), |
| 93 | ] ) |
| 94 | ->limit( $batchSize ) |
| 95 | ->caller( __METHOD__ ) |
| 96 | ->fetchResultSet(); |
| 97 | if ( !$res->numRows() ) { |
| 98 | return $updated; |
| 99 | } |
| 100 | foreach ( $res as $row ) { |
| 101 | $url = $row->el_to; |
| 102 | $paths = LinkFilter::makeIndexes( $url ); |
| 103 | if ( !$paths ) { |
| 104 | continue; |
| 105 | } |
| 106 | $dbw->newUpdateQueryBuilder() |
| 107 | ->update( 'externallinks' ) |
| 108 | // just take the first one, we are not sending proto-relative to LinkFilter |
| 109 | ->set( [ |
| 110 | 'el_to_domain_index' => substr( $paths[0][0], 0, 255 ), |
| 111 | 'el_to_path' => $paths[0][1] |
| 112 | ] ) |
| 113 | ->where( [ 'el_id' => $row->el_id ] ) |
| 114 | ->caller( __METHOD__ )->execute(); |
| 115 | |
| 116 | $updated += $dbw->affectedRows(); |
| 117 | } |
| 118 | $this->output( "Updated $updated rows\n" ); |
| 119 | // Sleep between batches for replication to catch up |
| 120 | $this->waitForReplication(); |
| 121 | $sleep = (int)$this->getOption( 'sleep', 0 ); |
| 122 | if ( $sleep > 0 ) { |
| 123 | sleep( $sleep ); |
| 124 | } |
| 125 | return $updated; |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | // @codeCoverageIgnoreStart |
| 131 | $maintClass = MigrateExternallinks::class; |
| 132 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 133 | // @codeCoverageIgnoreEnd |