Revision: Remove some unnecessary temporary variables for returns
[mediawiki.git] / maintenance / refreshExternallinksIndex.php
blobcbde356be463596b4e47ef4d4e25c723a066eb1c
1 <?php
2 /**
3 * Refresh the externallinks table el_index and el_index_60 from el_to
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 use MediaWiki\MediaWikiServices;
28 /**
29 * Maintenance script that refreshes the externallinks table el_index and
30 * el_index_60 from el_to
32 * @ingroup Maintenance
33 * @since 1.33
35 class RefreshExternallinksIndex extends LoggedUpdateMaintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription(
39 'Refresh the externallinks table el_index and el_index_60 from el_to' );
40 $this->setBatchSize( 10000 );
43 protected function getUpdateKey() {
44 return static::class
45 . ' v' . LinkFilter::VERSION
46 . ( LinkFilter::supportsIDN() ? '+' : '-' ) . 'IDN';
49 protected function updateSkippedMessage() {
50 return 'externallinks table indexes up to date';
53 protected function doDBUpdates() {
54 $dbw = $this->getDB( DB_MASTER );
55 if ( !$dbw->tableExists( 'externallinks', __METHOD__ ) ) {
56 $this->error( "externallinks table does not exist" );
57 return false;
59 $this->output( "Updating externallinks table index fields\n" );
61 $minmax = $dbw->selectRow(
62 'externallinks',
63 [ 'min' => 'MIN(el_id)', 'max' => 'MAX(el_id)' ],
64 '',
65 __METHOD__
68 $updated = 0;
69 $deleted = 0;
70 $start = $minmax->min - 1;
71 $last = $minmax->max;
72 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
73 while ( $start < $last ) {
74 $end = min( $start + $this->mBatchSize, $last );
75 $this->output( "el_id $start - $end of $last\n" );
76 $res = $dbw->select( 'externallinks', [ 'el_id', 'el_to', 'el_index' ],
78 "el_id > $start",
79 "el_id <= $end",
81 __METHOD__,
82 [ 'ORDER BY' => 'el_id' ]
84 foreach ( $res as $row ) {
85 $newIndexes = LinkFilter::makeIndexes( $row->el_to );
86 if ( !$newIndexes ) {
87 $dbw->delete( 'externallinks', [ 'el_id' => $row->el_id ], __METHOD__ );
88 $deleted++;
89 continue;
91 if ( in_array( $row->el_index, $newIndexes, true ) ) {
92 continue;
95 if ( count( $newIndexes ) === 1 ) {
96 $newIndex = $newIndexes[0];
97 } else {
98 // Assume the scheme is the only difference between the different $newIndexes.
99 // Keep this row's scheme, assuming there's another row with the other scheme.
100 $newIndex = substr( $row->el_index, 0, strpos( $row->el_index, ':' ) ) .
101 substr( $newIndexes[0], strpos( $newIndexes[0], ':' ) );
103 $dbw->update( 'externallinks',
105 'el_index' => $newIndex,
106 'el_index_60' => substr( $newIndex, 0, 60 ),
108 [ 'el_id' => $row->el_id ],
109 __METHOD__
111 $updated++;
113 $lbFactory->waitForReplication();
114 $start = $end;
116 $this->output( "Done, $updated rows updated, $deleted deleted.\n" );
118 return true;
122 $maintClass = RefreshExternallinksIndex::class;
123 require_once RUN_MAINTENANCE_IF_MAIN;