MediaWiki  master
fixExtLinksProtocolRelative.php
Go to the documentation of this file.
1 <?php
26 require_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_MASTER );
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;
104 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:163
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
$res
$res
Definition: testCompression.php:57
FixExtLinksProtocolRelative\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: fixExtLinksProtocolRelative.php:43
FixExtLinksProtocolRelative
Maintenance script that fixes any entriy for protocol-relative URLs in the externallinks table.
Definition: fixExtLinksProtocolRelative.php:36
$maintClass
$maintClass
Definition: fixExtLinksProtocolRelative.php:103
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
DB_MASTER
const DB_MASTER
Definition: defines.php:26
FixExtLinksProtocolRelative\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: fixExtLinksProtocolRelative.php:51
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1370
FixExtLinksProtocolRelative\updateSkippedMessage
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
Definition: fixExtLinksProtocolRelative.php:47
FixExtLinksProtocolRelative\__construct
__construct()
Default constructor.
Definition: fixExtLinksProtocolRelative.php:37
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:463
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434