3 use MediaWiki\ExternalLinks\LinkFilter
;
4 use MediaWiki\Maintenance\LoggedUpdateMaintenance
;
6 // @codeCoverageIgnoreStart
7 require_once __DIR__
. '/Maintenance.php';
8 // @codeCoverageIgnoreEnd
11 * Maintenance script that migrates externallinks data
13 * @ingroup Maintenance
16 class MigrateExternallinks
extends LoggedUpdateMaintenance
{
17 public function __construct() {
18 parent
::__construct();
19 $this->addDescription(
20 'Migrate externallinks data'
24 'Sleep time (in seconds) between every batch. Default: 0',
28 $this->setBatchSize( 1000 );
31 protected function getUpdateKey() {
35 protected function doDBUpdates() {
36 $dbw = $this->getDB( DB_PRIMARY
);
37 $table = 'externallinks';
38 if ( !$dbw->fieldExists( $table, 'el_to', __METHOD__
) ) {
39 $this->output( "Old fields don't exist. There is no need to run this script\n" );
42 if ( !$dbw->fieldExists( $table, 'el_to_path', __METHOD__
) ) {
43 $this->output( "Run update.php to create the el_to_path column.\n" );
47 $this->output( "Populating el_to_domain_index and el_to_path columns\n" );
50 $highestId = $dbw->newSelectQueryBuilder()
54 ->caller( __METHOD__
)
55 ->orderBy( 'el_id', 'DESC' )
56 ->fetchResultSet()->fetchRow();
58 $this->output( "Page table is empty.\n" );
61 $highestId = $highestId[0];
63 while ( $id <= $highestId ) {
64 $updated +
= $this->handleBatch( $id );
65 $id +
= $this->getBatchSize();
68 $this->output( "Completed normalization of $table, $updated rows updated.\n" );
73 private function handleBatch( $lowId ) {
74 $batchSize = $this->getBatchSize();
75 // range is inclusive, let's subtract one.
76 $highId = $lowId +
$batchSize - 1;
77 $dbw = $this->getPrimaryDB();
79 $res = $dbw->newSelectQueryBuilder()
80 ->select( [ 'el_id', 'el_to' ] )
81 ->from( 'externallinks' )
83 'el_to_domain_index' => '',
84 $dbw->expr( 'el_id', '>=', $lowId ),
85 $dbw->expr( 'el_id', '<=', $highId ),
88 ->caller( __METHOD__
)
90 if ( !$res->numRows() ) {
93 foreach ( $res as $row ) {
95 $paths = LinkFilter
::makeIndexes( $url );
99 $dbw->newUpdateQueryBuilder()
100 ->update( 'externallinks' )
101 // just take the first one, we are not sending proto-relative to LinkFilter
103 'el_to_domain_index' => substr( $paths[0][0], 0, 255 ),
104 'el_to_path' => $paths[0][1]
106 ->where( [ 'el_id' => $row->el_id
] )
107 ->caller( __METHOD__
)->execute();
109 $updated +
= $dbw->affectedRows();
111 $this->output( "Updated $updated rows\n" );
112 // Sleep between batches for replication to catch up
113 $this->waitForReplication();
114 $sleep = (int)$this->getOption( 'sleep', 0 );
123 // @codeCoverageIgnoreStart
124 $maintClass = MigrateExternallinks
::class;
125 require_once RUN_MAINTENANCE_IF_MAIN
;
126 // @codeCoverageIgnoreEnd