MediaWiki  master
checkBadRedirects.php
Go to the documentation of this file.
1 <?php
26 
27 require_once __DIR__ . '/Maintenance.php';
28 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Check for bad redirects' );
38  }
39 
40  public function execute() {
41  $this->output( "Fetching redirects...\n" );
42  $dbr = $this->getDB( DB_REPLICA );
43  $result = $dbr->newSelectQueryBuilder()
44  ->select( [ 'page_namespace', 'page_title', 'page_latest' ] )
45  ->from( 'page' )
46  ->where( [ 'page_is_redirect' => 1 ] )
47  ->caller( __METHOD__ )
48  ->fetchResultSet();
49 
50  $count = $result->numRows();
51  $this->output( "Found $count redirects.\n" .
52  "Checking for bad redirects:\n\n" );
53 
54  $revLookup = $this->getServiceContainer()->getRevisionLookup();
55  foreach ( $result as $row ) {
56  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
57  $revRecord = $revLookup->getRevisionById( $row->page_latest );
58  if ( $revRecord ) {
59  $target = $revRecord->getContent( SlotRecord::MAIN )->getRedirectTarget();
60  if ( !$target ) {
61  $this->output( $title->getPrefixedText() . "\n" );
62  }
63  }
64  }
65  $this->output( "\nDone.\n" );
66  }
67 }
68 
69 $maintClass = CheckBadRedirects::class;
70 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script to check that pages marked as being redirects really are.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Represents a title within MediaWiki.
Definition: Title.php:76
const DB_REPLICA
Definition: defines.php:26