MediaWiki master
migrateExternallinks.php
Go to the documentation of this file.
1<?php
2
5
6// @codeCoverageIgnoreStart
7require_once __DIR__ . '/Maintenance.php';
8// @codeCoverageIgnoreEnd
9
17 public function __construct() {
18 parent::__construct();
19 $this->addDescription(
20 'Migrate externallinks data'
21 );
22 $this->addOption(
23 'sleep',
24 'Sleep time (in seconds) between every batch. Default: 0',
25 false,
26 true
27 );
28 $this->setBatchSize( 1000 );
29 }
30
31 protected function getUpdateKey() {
32 return __CLASS__;
33 }
34
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" );
40 return true;
41 }
42 if ( !$dbw->fieldExists( $table, 'el_to_path', __METHOD__ ) ) {
43 $this->output( "Run update.php to create the el_to_path column.\n" );
44 return false;
45 }
46
47 $this->output( "Populating el_to_domain_index and el_to_path columns\n" );
48 $updated = 0;
49
50 $highestId = $dbw->newSelectQueryBuilder()
51 ->select( 'el_id' )
52 ->from( $table )
53 ->limit( 1 )
54 ->caller( __METHOD__ )
55 ->orderBy( 'el_id', 'DESC' )
56 ->fetchResultSet()->fetchRow();
57 if ( !$highestId ) {
58 $this->output( "Page table is empty.\n" );
59 return true;
60 }
61 $highestId = $highestId[0];
62 $id = 0;
63 while ( $id <= $highestId ) {
64 $updated += $this->handleBatch( $id );
65 $id += $this->getBatchSize();
66 }
67
68 $this->output( "Completed normalization of $table, $updated rows updated.\n" );
69
70 return true;
71 }
72
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();
78 $updated = 0;
79 $res = $dbw->newSelectQueryBuilder()
80 ->select( [ 'el_id', 'el_to' ] )
81 ->from( 'externallinks' )
82 ->where( [
83 'el_to_domain_index' => '',
84 $dbw->expr( 'el_id', '>=', $lowId ),
85 $dbw->expr( 'el_id', '<=', $highId ),
86 ] )
87 ->limit( $batchSize )
88 ->caller( __METHOD__ )
89 ->fetchResultSet();
90 if ( !$res->numRows() ) {
91 return $updated;
92 }
93 foreach ( $res as $row ) {
94 $url = $row->el_to;
95 $paths = LinkFilter::makeIndexes( $url );
96 if ( !$paths ) {
97 continue;
98 }
99 $dbw->newUpdateQueryBuilder()
100 ->update( 'externallinks' )
101 // just take the first one, we are not sending proto-relative to LinkFilter
102 ->set( [
103 'el_to_domain_index' => substr( $paths[0][0], 0, 255 ),
104 'el_to_path' => $paths[0][1]
105 ] )
106 ->where( [ 'el_id' => $row->el_id ] )
107 ->caller( __METHOD__ )->execute();
108
109 $updated += $dbw->affectedRows();
110 }
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 );
115 if ( $sleep > 0 ) {
116 sleep( $sleep );
117 }
118 return $updated;
119 }
120
121}
122
123// @codeCoverageIgnoreStart
124$maintClass = MigrateExternallinks::class;
125require_once RUN_MAINTENANCE_IF_MAIN;
126// @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.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
waitForReplication()
Wait for replica DB servers to catch up.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
const DB_PRIMARY
Definition defines.php:28