41 private const REPORTING_INTERVAL = 100;
44 parent::__construct();
46 $this->
addOption(
'verbose',
'Output information about link refresh progress',
false,
false,
'v' );
47 $this->
addOption(
'dfn-only',
'Delete links from nonexistent articles only' );
48 $this->
addOption(
'new-only',
'Only affect articles with just a single edit' );
49 $this->
addOption(
'redirects-only',
'Only fix redirects, not all links' );
50 $this->
addOption(
'touched-only',
'Only fix pages that have been touched after last update' );
51 $this->
addOption(
'e',
'Last page id to refresh',
false,
true );
52 $this->
addOption(
'dfn-chunk-size',
'Maximum number of existent IDs to check per ' .
53 'query, default 100,000',
false,
true );
54 $this->
addOption(
'namespace',
'Only fix pages in this namespace',
false,
true );
55 $this->
addOption(
'category',
'Only fix pages in this category',
false,
true );
56 $this->
addOption(
'tracking-category',
'Only fix pages in this tracking category',
false,
true );
57 $this->
addOption(
'before-timestamp',
'Only fix pages that were last updated before this timestamp',
59 $this->
addArg(
'start',
'Page_id to start from, default 1',
false );
68 $start = (int)$this->
getArg( 0 ) ?:
null;
69 $end = (int)$this->
getOption(
'e' ) ?:
null;
70 $dfnChunkSize = (int)$this->
getOption(
'dfn-chunk-size', 100_000 );
73 $this->deleteLinksFromNonexistent( $start, $end, $this->
getBatchSize(), $dfnChunkSize );
78 $builder = $dbr->newSelectQueryBuilder()
80 ->where( self::intervalCond( $dbr,
'page_id', $start, $end ) )
84 $builder->
andWhere( [
'page_namespace' => (
int)$this->
getOption(
'namespace' ) ] );
87 if ( $this->
hasOption(
'before-timestamp' ) ) {
89 $dbr->expr(
'page_links_updated',
'<', $this->getOption(
'before-timestamp' ) )
90 ->or(
'page_links_updated',
'=',
null )
95 $category = $this->
getOption(
'category' );
96 $title = Title::makeTitleSafe(
NS_CATEGORY, $category );
98 $this->
fatalError(
"'$category' is an invalid category name!\n" );
100 $this->refreshCategory( $builder, $title );
101 } elseif ( $this->
hasOption(
'tracking-category' ) ) {
103 $this->refreshTrackingCategory( $builder, $this->
getOption(
'tracking-category' ) );
106 $redir = $this->
hasOption(
'redirects-only' );
107 $touched = $this->
hasOption(
'touched-only' );
108 $what = $redir ?
'redirects' :
'links';
110 $builder->
andWhere( [
'page_is_new' => 1 ] );
111 $this->
output(
"Refreshing $what from new pages...\n" );
115 $dbr->expr(
'page_touched',
'>',
'page_links_updated' )
116 ->or(
'page_links_updated',
'=',
null ),
119 $this->
output(
"Refreshing $what from pages...\n" );
121 $this->doRefreshLinks( $builder, $redir );
122 if ( !$this->
hasOption(
'namespace' ) ) {
123 $this->deleteLinksFromNonexistent( $start, $end, $this->
getBatchSize(), $dfnChunkSize );
134 private function doRefreshLinks(
136 bool $redirectsOnly =
false,
137 array $indexFields = [
'page_id' ]
140 $this->getHookRunner()->onMaintenanceRefreshLinksInit( $this );
142 $estimateCount = $builder->
caller( __METHOD__ )->estimateRowCount();
143 $this->output(
"Estimated page count: $estimateCount\n" );
146 $lastIndexes = array_fill_keys( $indexFields, 0 );
147 $selectFields = in_array(
'page_id', $indexFields )
148 ? $indexFields : [
'page_id', ...$indexFields ];
149 $verbose = $this->hasOption(
'verbose' );
150 $dbr = $this->getDB(
DB_REPLICA, [
'vslow' ] );
152 $batchCond = $dbr->buildComparison(
'>', $lastIndexes );
153 $res = ( clone $builder )->select( $selectFields )
154 ->andWhere( [ $batchCond ] )
155 ->orderBy( $indexFields )
156 ->caller( __METHOD__ )->fetchResultSet();
159 $this->output(
"Refreshing links for {$res->numRows()} pages\n" );
162 foreach ( $res as $row ) {
163 if ( !( ++$i % self::REPORTING_INTERVAL ) ) {
168 $this->
output(
"Refreshing links for page ID {$row->page_id}\n" );
170 self::fixRedirect( $this, $row->page_id );
171 if ( !$redirectsOnly ) {
172 self::fixLinksFromArticle( $row->page_id );
175 if ( $res->numRows() ) {
176 $res->seek( $res->numRows() - 1 );
177 foreach ( $indexFields as $field ) {
178 $lastIndexes[$field] = $res->current()->$field;
182 }
while ( $res->numRows() == $this->getBatchSize() );
203 if ( $page ===
null ) {
208 $content = $page->getContent( RevisionRecord::RAW );
209 if ( $content !==
null ) {
210 $rt = $content->getRedirectTarget();
214 if ( $rt ===
null ) {
217 $dbw->newDeleteQueryBuilder()
218 ->deleteFrom(
'redirect' )
219 ->where( [
'rd_from' => $id ] )
220 ->caller( __METHOD__ )->execute();
223 $page->insertRedirectEntry( $rt );
228 $dbw->newUpdateQueryBuilder()
230 ->set( [
'page_is_redirect' => $fieldValue ] )
231 ->where( [
'page_id' => $id ] )
232 ->caller( __METHOD__ )
241 $services = MediaWikiServices::getInstance();
242 $page = $services->getWikiPageFactory()->newFromID( $id );
245 if ( $page ===
null ) {
252 $page->doSecondaryDataUpdates( [
253 'defer' => DeferredUpdates::POSTSEND,
254 'causeAction' =>
'refresh-links-maintenance',
255 'recursive' =>
false,
257 DeferredUpdates::doUpdates();
271 private function deleteLinksFromNonexistent( $start =
null, $end =
null, $batchSize = 100,
274 $this->waitForReplication();
275 $this->output(
"Deleting illegal entries from the links tables...\n" );
276 $dbr = $this->getDB(
DB_REPLICA, [
'vslow' ] );
280 $nextStart = $dbr->newSelectQueryBuilder()
281 ->select(
'page_id' )
283 ->where( [ self::intervalCond( $dbr,
'page_id', $start, $end ) ] )
284 ->orderBy(
'page_id' )
285 ->offset( $chunkSize )
286 ->caller( __METHOD__ )->fetchField();
288 if ( $nextStart !==
false ) {
293 $chunkEnd = $nextStart - 1;
299 $fmtStart = $start !==
null ?
"[$start" :
'(-INF';
300 $fmtChunkEnd = $chunkEnd !==
null ?
"$chunkEnd]" :
'INF)';
301 $this->
output(
" Checking interval $fmtStart, $fmtChunkEnd\n" );
302 $this->dfnCheckInterval( $start, $chunkEnd, $batchSize );
306 }
while ( $nextStart !==
false );
315 private function dfnCheckInterval( $start =
null, $end =
null, $batchSize = 100 ) {
321 'pagelinks' =>
'pl_from',
322 'imagelinks' =>
'il_from',
323 'categorylinks' =>
'cl_from',
324 'templatelinks' =>
'tl_from',
325 'externallinks' =>
'el_from',
326 'iwlinks' =>
'iwl_from',
327 'langlinks' =>
'll_from',
328 'redirect' =>
'rd_from',
329 'page_props' =>
'pp_page',
332 foreach ( $linksTables as $table => $field ) {
333 $this->
output(
" $table: 0" );
334 $tableStart = $start;
337 $ids = $dbr->newSelectQueryBuilder()
341 ->leftJoin(
'page',
null,
"$field = page_id" )
342 ->where( self::intervalCond( $dbr, $field, $tableStart, $end ) )
343 ->andWhere( [
'page_id' =>
null ] )
345 ->limit( $batchSize )
346 ->caller( __METHOD__ )->fetchFieldValues();
348 $numIds = count( $ids );
351 $dbw->newDeleteQueryBuilder()
352 ->deleteFrom( $table )
353 ->where( [ $field => $ids ] )
354 ->caller( __METHOD__ )->execute();
355 $this->
output(
", $counter" );
356 $tableStart = $ids[$numIds - 1] + 1;
360 }
while ( $numIds >= $batchSize && ( $end ===
null || $tableStart <= $end ) );
362 $this->
output(
" deleted.\n" );
378 private static function intervalCond(
IReadableDatabase $db, $var, $start, $end ) {
379 if ( $start ===
null && $end ===
null ) {
380 return $db->
expr( $var,
'!=',
null );
381 } elseif ( $end ===
null ) {
382 return $db->
expr( $var,
'>=', $start );
383 } elseif ( $start ===
null ) {
384 return $db->
expr( $var,
'<=', $end );
386 return $db->
expr( $var,
'>=', $start )->and( $var,
'<=', $end );
397 $cats = $this->getPossibleCategories( $category );
400 $this->
error(
"Tracking category '$category' is disabled\n" );
404 foreach ( $cats as $cat ) {
405 $this->refreshCategory( clone $builder, $cat );
416 $this->
output(
"Refreshing pages in category '{$category->getText()}'...\n" );
418 $builder->
join(
'categorylinks',
null,
'page_id=cl_from' )
419 ->andWhere( [
'cl_to' => $category->
getDBkey() ] );
420 $this->doRefreshLinks( $builder,
false, [
'cl_timestamp',
'cl_from' ] );
429 private function getPossibleCategories( $categoryKey ) {
431 if ( isset( $cats[$categoryKey] ) ) {
432 return $cats[$categoryKey][
'cats'];
434 $this->
fatalError(
"Unknown tracking category {$categoryKey}\n" );