Merge "MultiUsernameFilter: Don't try to split ids if they're not a string"
[mediawiki.git] / maintenance / migrateExternallinks.php
blob21f8cf5f6468a3411eb6d0298c4f0a679ae9c51a
1 <?php
3 use MediaWiki\ExternalLinks\LinkFilter;
4 use MediaWiki\Maintenance\LoggedUpdateMaintenance;
6 // @codeCoverageIgnoreStart
7 require_once __DIR__ . '/Maintenance.php';
8 // @codeCoverageIgnoreEnd
10 /**
11 * Maintenance script that migrates externallinks data
13 * @ingroup Maintenance
14 * @since 1.40
16 class MigrateExternallinks extends LoggedUpdateMaintenance {
17 public function __construct() {
18 parent::__construct();
19 $this->addDescription(
20 'Migrate externallinks data'
22 $this->addOption(
23 'sleep',
24 'Sleep time (in seconds) between every batch. Default: 0',
25 false,
26 true
28 $this->setBatchSize( 1000 );
31 protected function getUpdateKey() {
32 return __CLASS__;
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;
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;
47 $this->output( "Populating el_to_domain_index and el_to_path columns\n" );
48 $updated = 0;
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;
61 $highestId = $highestId[0];
62 $id = 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" );
70 return true;
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;
93 foreach ( $res as $row ) {
94 $url = $row->el_to;
95 $paths = LinkFilter::makeIndexes( $url );
96 if ( !$paths ) {
97 continue;
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]
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 );
115 if ( $sleep > 0 ) {
116 sleep( $sleep );
118 return $updated;
123 // @codeCoverageIgnoreStart
124 $maintClass = MigrateExternallinks::class;
125 require_once RUN_MAINTENANCE_IF_MAIN;
126 // @codeCoverageIgnoreEnd