1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Fixes any entries for protocol-relative URLs in the externallinks table, |
4
|
|
|
* replacing each protocol-relative entry with two entries, one for http |
5
|
|
|
* and one for https. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
21
|
|
|
* |
22
|
|
|
* @file |
23
|
|
|
* @ingroup Maintenance |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
require_once __DIR__ . '/Maintenance.php'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Maintenance script that fixes any entriy for protocol-relative URLs |
30
|
|
|
* in the externallinks table. |
31
|
|
|
* |
32
|
|
|
* @ingroup Maintenance |
33
|
|
|
*/ |
34
|
|
|
class FixExtLinksProtocolRelative extends LoggedUpdateMaintenance { |
35
|
|
|
public function __construct() { |
36
|
|
|
parent::__construct(); |
37
|
|
|
$this->addDescription( |
38
|
|
|
'Fixes any entries in the externallinks table containing protocol-relative URLs' ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function getUpdateKey() { |
42
|
|
|
return 'fix protocol-relative URLs in externallinks'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function updateSkippedMessage() { |
46
|
|
|
return 'protocol-relative URLs in externallinks table already fixed.'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function doDBUpdates() { |
50
|
|
|
$db = $this->getDB( DB_MASTER ); |
51
|
|
|
if ( !$db->tableExists( 'externallinks' ) ) { |
52
|
|
|
$this->error( "externallinks table does not exist" ); |
53
|
|
|
|
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
$this->output( "Fixing protocol-relative entries in the externallinks table...\n" ); |
57
|
|
|
$res = $db->select( 'externallinks', [ 'el_from', 'el_to', 'el_index' ], |
58
|
|
|
[ 'el_index' . $db->buildLike( '//', $db->anyString() ) ], |
59
|
|
|
__METHOD__ |
60
|
|
|
); |
61
|
|
|
$count = 0; |
62
|
|
|
foreach ( $res as $row ) { |
63
|
|
|
$count++; |
64
|
|
|
if ( $count % 100 == 0 ) { |
65
|
|
|
$this->output( $count . "\n" ); |
66
|
|
|
wfWaitForSlaves(); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
$db->insert( 'externallinks', |
69
|
|
|
[ |
70
|
|
|
[ |
71
|
|
|
'el_id' => $db->nextSequenceValue( 'externallinks_el_id_seq' ), |
72
|
|
|
'el_from' => $row->el_from, |
73
|
|
|
'el_to' => $row->el_to, |
74
|
|
|
'el_index' => "http:{$row->el_index}", |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
'el_id' => $db->nextSequenceValue( 'externallinks_el_id_seq' ), |
78
|
|
|
'el_from' => $row->el_from, |
79
|
|
|
'el_to' => $row->el_to, |
80
|
|
|
'el_index' => "https:{$row->el_index}", |
81
|
|
|
] |
82
|
|
|
], __METHOD__, [ 'IGNORE' ] |
83
|
|
|
); |
84
|
|
|
$db->delete( |
85
|
|
|
'externallinks', |
86
|
|
|
[ |
87
|
|
|
'el_index' => $row->el_index, |
88
|
|
|
'el_from' => $row->el_from, |
89
|
|
|
'el_to' => $row->el_to |
90
|
|
|
], |
91
|
|
|
__METHOD__ |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
$this->output( "Done, $count rows updated.\n" ); |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$maintClass = "FixExtLinksProtocolRelative"; |
101
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN; |
102
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.