34 private const REPORTING_INTERVAL = 100;
37 parent::__construct();
39 $this->
addOption(
'verbose',
'Output information about link refresh progress',
false,
false,
'v' );
40 $this->
addOption(
'dfn-only',
'Delete links from nonexistent articles only' );
41 $this->
addOption(
'new-only',
'Only affect articles with just a single edit' );
42 $this->
addOption(
'redirects-only',
'Only fix redirects, not all links' );
43 $this->
addOption(
'touched-only',
'Only fix pages that have been touched after last update' );
44 $this->
addOption(
'e',
'Last page id to refresh',
false,
true );
45 $this->
addOption(
'dfn-chunk-size',
'Maximum number of existent IDs to check per ' .
46 'query, default 100,000',
false,
true );
47 $this->
addOption(
'namespace',
'Only fix pages in this namespace',
false,
true );
48 $this->
addOption(
'category',
'Only fix pages in this category',
false,
true );
49 $this->
addOption(
'tracking-category',
'Only fix pages in this tracking category',
false,
true );
50 $this->
addOption(
'before-timestamp',
'Only fix pages that were last updated before this timestamp',
52 $this->
addArg(
'start',
'Page_id to start from, default 1',
false );
61 $start = (int)$this->
getArg( 0 ) ?:
null;
62 $end = (int)$this->
getOption(
'e' ) ?:
null;
63 $dfnChunkSize = (int)$this->
getOption(
'dfn-chunk-size', 100_000 );
66 $this->deleteLinksFromNonexistent( $start, $end, $this->
getBatchSize(), $dfnChunkSize );
71 $builder = $dbr->newSelectQueryBuilder()
73 ->where( self::intervalCond( $dbr,
'page_id', $start, $end ) )
77 $builder->
andWhere( [
'page_namespace' => (
int)$this->
getOption(
'namespace' ) ] );
80 if ( $this->
hasOption(
'before-timestamp' ) ) {
82 $dbr->expr(
'page_links_updated',
'<', $this->getOption(
'before-timestamp' ) )
83 ->or(
'page_links_updated',
'=',
null )
88 $category = $this->
getOption(
'category' );
89 $title = Title::makeTitleSafe(
NS_CATEGORY, $category );
91 $this->
fatalError(
"'$category' is an invalid category name!\n" );
93 $this->refreshCategory( $builder, $title );
94 } elseif ( $this->
hasOption(
'tracking-category' ) ) {
96 $this->refreshTrackingCategory( $builder, $this->
getOption(
'tracking-category' ) );
99 $redir = $this->
hasOption(
'redirects-only' );
100 $touched = $this->
hasOption(
'touched-only' );
101 $what = $redir ?
'redirects' :
'links';
103 $builder->
andWhere( [
'page_is_new' => 1 ] );
104 $this->
output(
"Refreshing $what from new pages...\n" );
108 $dbr->expr(
'page_links_updated',
'=',
null )
112 $this->
output(
"Refreshing $what from pages...\n" );
114 $this->doRefreshLinks( $builder, $redir );
115 if ( !$this->
hasOption(
'namespace' ) ) {
116 $this->deleteLinksFromNonexistent( $start, $end, $this->
getBatchSize(), $dfnChunkSize );
127 private function doRefreshLinks(
129 bool $redirectsOnly =
false,
130 array $indexFields = [
'page_id' ]
133 $this->getHookRunner()->onMaintenanceRefreshLinksInit( $this );
135 $estimateCount = $builder->
caller( __METHOD__ )->estimateRowCount();
136 $this->output(
"Estimated page count: $estimateCount\n" );
139 $lastIndexes = array_fill_keys( $indexFields, 0 );
140 $selectFields = in_array(
'page_id', $indexFields )
141 ? $indexFields : [
'page_id', ...$indexFields ];
142 $verbose = $this->hasOption(
'verbose' );
143 $dbr = $this->getDB(
DB_REPLICA, [
'vslow' ] );
145 $batchCond = $dbr->buildComparison(
'>', $lastIndexes );
146 $res = ( clone $builder )->select( $selectFields )
147 ->andWhere( [ $batchCond ] )
148 ->orderBy( $indexFields )
149 ->caller( __METHOD__ )->fetchResultSet();
152 $this->output(
"Refreshing links for {$res->numRows()} pages\n" );
155 foreach ( $res as $row ) {
156 if ( !( ++$i % self::REPORTING_INTERVAL ) ) {
161 $this->
output(
"Refreshing links for page ID {$row->page_id}\n" );
163 self::fixRedirect( $this, $row->page_id );
164 if ( !$redirectsOnly ) {
165 self::fixLinksFromArticle( $row->page_id );
168 if ( $res->numRows() ) {
169 $res->seek( $res->numRows() - 1 );
170 foreach ( $indexFields as $field ) {
171 $lastIndexes[$field] = $res->current()->$field;
175 }
while ( $res->numRows() == $this->getBatchSize() );
196 if ( $page ===
null ) {
201 $content = $page->getContent( RevisionRecord::RAW );
202 if ( $content !==
null ) {
203 $rt = $content->getRedirectTarget();
207 if ( $rt ===
null ) {
210 $dbw->newDeleteQueryBuilder()
211 ->deleteFrom(
'redirect' )
212 ->where( [
'rd_from' => $id ] )
213 ->caller( __METHOD__ )->execute();
216 $page->insertRedirectEntry( $rt );
221 $dbw->newUpdateQueryBuilder()
223 ->set( [
'page_is_redirect' => $fieldValue ] )
224 ->where( [
'page_id' => $id ] )
225 ->caller( __METHOD__ )
234 $services = MediaWikiServices::getInstance();
235 $page = $services->getWikiPageFactory()->newFromID( $id );
238 if ( $page ===
null ) {
245 $page->doSecondaryDataUpdates( [
246 'defer' => DeferredUpdates::POSTSEND,
247 'causeAction' =>
'refresh-links-maintenance',
248 'recursive' =>
false,
250 DeferredUpdates::doUpdates();
264 private function deleteLinksFromNonexistent( $start =
null, $end =
null, $batchSize = 100,
267 $this->waitForReplication();
268 $this->output(
"Deleting illegal entries from the links tables...\n" );
269 $dbr = $this->getDB(
DB_REPLICA, [
'vslow' ] );
273 $nextStart = $dbr->newSelectQueryBuilder()
274 ->select(
'page_id' )
276 ->where( [ self::intervalCond( $dbr,
'page_id', $start, $end ) ] )
277 ->orderBy(
'page_id' )
278 ->offset( $chunkSize )
279 ->caller( __METHOD__ )->fetchField();
281 if ( $nextStart !==
false ) {
286 $chunkEnd = $nextStart - 1;
292 $fmtStart = $start !==
null ?
"[$start" :
'(-INF';
293 $fmtChunkEnd = $chunkEnd !==
null ?
"$chunkEnd]" :
'INF)';
294 $this->
output(
" Checking interval $fmtStart, $fmtChunkEnd\n" );
295 $this->dfnCheckInterval( $start, $chunkEnd, $batchSize );
299 }
while ( $nextStart !==
false );
308 private function dfnCheckInterval( $start =
null, $end =
null, $batchSize = 100 ) {
311 'pagelinks' =>
'pl_from',
312 'imagelinks' =>
'il_from',
313 'categorylinks' =>
'cl_from',
314 'templatelinks' =>
'tl_from',
315 'externallinks' =>
'el_from',
316 'iwlinks' =>
'iwl_from',
317 'langlinks' =>
'll_from',
318 'redirect' =>
'rd_from',
319 'page_props' =>
'pp_page',
323 'categorylinks' => CategoryLinksTable::VIRTUAL_DOMAIN,
324 'externallinks' => ExternalLinksTable::VIRTUAL_DOMAIN,
325 'imagelinks' => ImageLinksTable::VIRTUAL_DOMAIN,
326 'iwlinks' => InterwikiLinksTable::VIRTUAL_DOMAIN,
327 'pagelinks' => PageLinksTable::VIRTUAL_DOMAIN,
328 'templatelinks' => TemplateLinksTable::VIRTUAL_DOMAIN,
331 foreach ( $linksTables as $table => $field ) {
332 $domain = $domains[$table] ??
false;
333 $dbw = $this->
getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $domain );
334 $dbr = $this->
getServiceContainer()->getConnectionProvider()->getReplicaDatabase( $domain,
'vslow' );
336 $this->
output(
" $table: 0" );
337 $tableStart = $start;
340 $ids = $dbr->newSelectQueryBuilder()
344 ->leftJoin(
'page',
null,
"$field = page_id" )
345 ->where( self::intervalCond( $dbr, $field, $tableStart, $end ) )
346 ->andWhere( [
'page_id' =>
null ] )
348 ->limit( $batchSize )
349 ->caller( __METHOD__ )->fetchFieldValues();
351 $numIds = count( $ids );
354 $dbw->newDeleteQueryBuilder()
355 ->deleteFrom( $table )
356 ->where( [ $field => $ids ] )
357 ->caller( __METHOD__ )->execute();
358 $this->
output(
", $counter" );
359 $tableStart = $ids[$numIds - 1] + 1;
363 }
while ( $numIds >= $batchSize && ( $end ===
null || $tableStart <= $end ) );
365 $this->
output(
" deleted.\n" );
381 private static function intervalCond(
IReadableDatabase $db, $var, $start, $end ) {
382 if ( $start ===
null && $end ===
null ) {
383 return $db->
expr( $var,
'!=',
null );
384 } elseif ( $end ===
null ) {
385 return $db->
expr( $var,
'>=', $start );
386 } elseif ( $start ===
null ) {
387 return $db->
expr( $var,
'<=', $end );
389 return $db->
expr( $var,
'>=', $start )->and( $var,
'<=', $end );
400 $cats = $this->getPossibleCategories( $category );
403 $this->
error(
"Tracking category '$category' is disabled\n" );
407 foreach ( $cats as $cat ) {
408 $this->refreshCategory( clone $builder, $cat );
419 $this->
output(
"Refreshing pages in category '{$category->getText()}'...\n" );
421 $builder->
join(
'categorylinks',
null,
'page_id=cl_from' )
422 ->join(
'linktarget',
null,
'lt_id=cl_target_id' )
424 $this->doRefreshLinks( $builder,
false, [
'cl_timestamp',
'cl_from' ] );
433 private function getPossibleCategories( $categoryKey ) {
435 if ( isset( $cats[$categoryKey] ) ) {
436 return $cats[$categoryKey][
'cats'];
438 $this->
fatalError(
"Unknown tracking category {$categoryKey}\n" );