MediaWiki REL1_37
fixExtLinksProtocolRelative.php
Go to the documentation of this file.
1<?php
26require_once __DIR__ . '/Maintenance.php';
27
29
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription(
40 'Fixes any entries in the externallinks table containing protocol-relative URLs' );
41 }
42
43 protected function getUpdateKey() {
44 return 'fix protocol-relative URLs in externallinks';
45 }
46
47 protected function updateSkippedMessage() {
48 return 'protocol-relative URLs in externallinks table already fixed.';
49 }
50
51 protected function doDBUpdates() {
52 $db = $this->getDB( DB_PRIMARY );
53 if ( !$db->tableExists( 'externallinks', __METHOD__ ) ) {
54 $this->error( "externallinks table does not exist" );
55
56 return false;
57 }
58 $this->output( "Fixing protocol-relative entries in the externallinks table...\n" );
59 $res = $db->select( 'externallinks', [ 'el_from', 'el_to', 'el_index' ],
60 [ 'el_index' . $db->buildLike( '//', $db->anyString() ) ],
61 __METHOD__
62 );
63 $count = 0;
64 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
65 foreach ( $res as $row ) {
66 $count++;
67 if ( $count % 100 == 0 ) {
68 $this->output( $count . "\n" );
69 $lbFactory->waitForReplication();
70 }
71 $db->insert( 'externallinks',
72 [
73 [
74 'el_from' => $row->el_from,
75 'el_to' => $row->el_to,
76 'el_index' => "http:{$row->el_index}",
77 'el_index_60' => substr( "http:{$row->el_index}", 0, 60 ),
78 ],
79 [
80 'el_from' => $row->el_from,
81 'el_to' => $row->el_to,
82 'el_index' => "https:{$row->el_index}",
83 'el_index_60' => substr( "https:{$row->el_index}", 0, 60 ),
84 ]
85 ], __METHOD__, [ 'IGNORE' ]
86 );
87 $db->delete(
88 'externallinks',
89 [
90 'el_index' => $row->el_index,
91 'el_from' => $row->el_from,
92 'el_to' => $row->el_to
93 ],
94 __METHOD__
95 );
96 }
97 $this->output( "Done, $count rows updated.\n" );
98
99 return true;
100 }
101}
102
103$maintClass = FixExtLinksProtocolRelative::class;
104require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Maintenance script that fixes any entriy for protocol-relative URLs in the externallinks table.
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
getUpdateKey()
Get the update key name to go in the update log table.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
addDescription( $text)
Set the description text.
MediaWikiServices is the service locator for the application scope of MediaWiki.
const DB_PRIMARY
Definition defines.php:27