MediaWiki master
migrateExternallinks.php
Go to the documentation of this file.
1<?php
2
6
7// @codeCoverageIgnoreStart
8require_once __DIR__ . '/Maintenance.php';
9// @codeCoverageIgnoreEnd
10
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
33 protected function getUpdateKey() {
34 return __CLASS__;
35 }
36
38 protected function doDBUpdates() {
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;
132require_once RUN_MAINTENANCE_IF_MAIN;
133// @codeCoverageIgnoreEnd
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.