Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 80 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MigrateExternallinks | |
0.00% |
0 / 77 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
getUpdateKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doDBUpdates | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
30 | |||
handleBatch | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | use MediaWiki\ExternalLinks\LinkFilter; |
4 | |
5 | require_once __DIR__ . '/Maintenance.php'; |
6 | |
7 | /** |
8 | * Maintenance script that migrates externallinks data |
9 | * |
10 | * @ingroup Maintenance |
11 | * @since 1.40 |
12 | */ |
13 | class MigrateExternallinks extends LoggedUpdateMaintenance { |
14 | public function __construct() { |
15 | parent::__construct(); |
16 | $this->addDescription( |
17 | 'Migrate externallinks data' |
18 | ); |
19 | $this->addOption( |
20 | 'sleep', |
21 | 'Sleep time (in seconds) between every batch. Default: 0', |
22 | false, |
23 | true |
24 | ); |
25 | $this->setBatchSize( 1000 ); |
26 | } |
27 | |
28 | protected function getUpdateKey() { |
29 | return __CLASS__; |
30 | } |
31 | |
32 | protected function doDBUpdates() { |
33 | $dbw = $this->getDB( DB_PRIMARY ); |
34 | $table = 'externallinks'; |
35 | if ( !$dbw->fieldExists( $table, 'el_to', __METHOD__ ) ) { |
36 | $this->output( "Old fields don't exist. There is no need to run this script\n" ); |
37 | return true; |
38 | } |
39 | if ( !$dbw->fieldExists( $table, 'el_to_path', __METHOD__ ) ) { |
40 | $this->output( "Run update.php to create the el_to_path column.\n" ); |
41 | return false; |
42 | } |
43 | |
44 | $this->output( "Populating el_to_domain_index and el_to_path columns\n" ); |
45 | $updated = 0; |
46 | |
47 | $highestId = $dbw->newSelectQueryBuilder() |
48 | ->select( 'el_id' ) |
49 | ->from( $table ) |
50 | ->limit( 1 ) |
51 | ->caller( __METHOD__ ) |
52 | ->orderBy( 'el_id', 'DESC' ) |
53 | ->fetchResultSet()->fetchRow(); |
54 | if ( !$highestId ) { |
55 | $this->output( "Page table is empty.\n" ); |
56 | return true; |
57 | } |
58 | $highestId = $highestId[0]; |
59 | $id = 0; |
60 | while ( $id <= $highestId ) { |
61 | $updated += $this->handleBatch( $id ); |
62 | $id += $this->getBatchSize(); |
63 | } |
64 | |
65 | $this->output( "Completed normalization of $table, $updated rows updated.\n" ); |
66 | |
67 | return true; |
68 | } |
69 | |
70 | private function handleBatch( $lowId ) { |
71 | $batchSize = $this->getBatchSize(); |
72 | // range is inclusive, let's subtract one. |
73 | $highId = $lowId + $batchSize - 1; |
74 | $dbw = $this->getPrimaryDB(); |
75 | $updated = 0; |
76 | $res = $dbw->newSelectQueryBuilder() |
77 | ->select( [ 'el_id', 'el_to' ] ) |
78 | ->from( 'externallinks' ) |
79 | ->where( [ |
80 | 'el_to_domain_index' => '', |
81 | $dbw->expr( 'el_id', '>=', $lowId ), |
82 | $dbw->expr( 'el_id', '<=', $highId ), |
83 | ] ) |
84 | ->limit( $batchSize ) |
85 | ->caller( __METHOD__ ) |
86 | ->fetchResultSet(); |
87 | if ( !$res->numRows() ) { |
88 | return $updated; |
89 | } |
90 | foreach ( $res as $row ) { |
91 | $url = $row->el_to; |
92 | $paths = LinkFilter::makeIndexes( $url ); |
93 | if ( !$paths ) { |
94 | continue; |
95 | } |
96 | $dbw->newUpdateQueryBuilder() |
97 | ->update( 'externallinks' ) |
98 | // just take the first one, we are not sending proto-relative to LinkFilter |
99 | ->set( [ |
100 | 'el_to_domain_index' => substr( $paths[0][0], 0, 255 ), |
101 | 'el_to_path' => $paths[0][1] |
102 | ] ) |
103 | ->where( [ 'el_id' => $row->el_id ] ) |
104 | ->caller( __METHOD__ )->execute(); |
105 | |
106 | $updated += $dbw->affectedRows(); |
107 | } |
108 | $this->output( "Updated $updated rows\n" ); |
109 | // Sleep between batches for replication to catch up |
110 | $this->waitForReplication(); |
111 | $sleep = (int)$this->getOption( 'sleep', 0 ); |
112 | if ( $sleep > 0 ) { |
113 | sleep( $sleep ); |
114 | } |
115 | return $updated; |
116 | } |
117 | |
118 | } |
119 | |
120 | $maintClass = MigrateExternallinks::class; |
121 | require_once RUN_MAINTENANCE_IF_MAIN; |