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->select(
44  [ 'page' ],
45  [ 'page_namespace', 'page_title', 'page_latest' ],
46  [ 'page_is_redirect' => 1 ],
47  __METHOD__
48  );
49 
50  $count = $result->numRows();
51  $this->output( "Found $count redirects.\n" .
52  "Checking for bad redirects:\n\n" );
53 
54  $revLookup = MediaWikiServices::getInstance()->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;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$dbr
$dbr
Definition: testCompression.php:54
$maintClass
$maintClass
Definition: checkBadRedirects.php:69
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
CheckBadRedirects\execute
execute()
Do the actual work.
Definition: checkBadRedirects.php:40
CheckBadRedirects
Maintenance script to check that pages marked as being redirects really are.
Definition: checkBadRedirects.php:34
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
CheckBadRedirects\__construct
__construct()
Default constructor.
Definition: checkBadRedirects.php:35
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39