MediaWiki  master
checkBadRedirects.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Check for bad redirects' );
35  }
36 
37  public function execute() {
38  $this->output( "Fetching redirects...\n" );
39  $dbr = $this->getDB( DB_REPLICA );
40  $result = $dbr->select(
41  [ 'page' ],
42  [ 'page_namespace', 'page_title', 'page_latest' ],
43  [ 'page_is_redirect' => 1 ] );
44 
45  $count = $result->numRows();
46  $this->output( "Found $count redirects.\n" .
47  "Checking for bad redirects:\n\n" );
48 
49  foreach ( $result as $row ) {
50  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
51  $rev = Revision::newFromId( $row->page_latest );
52  if ( $rev ) {
53  $target = $rev->getContent()->getRedirectTarget();
54  if ( !$target ) {
55  $this->output( $title->getPrefixedText() . "\n" );
56  }
57  }
58  }
59  $this->output( "\nDone.\n" );
60  }
61 }
62 
63 $maintClass = CheckBadRedirects::class;
64 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script to check that pages marked as being redirects really are.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
addDescription( $text)
Set the description text.
output( $out, $channel=null)
Throw some output to the user.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
const DB_REPLICA
Definition: defines.php:25
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:119
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.