Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CheckBadRedirects
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * Check that pages marked as being redirects really are.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Maintenance
8 */
9
10use MediaWiki\Maintenance\Maintenance;
11use MediaWiki\Revision\SlotRecord;
12use MediaWiki\Title\Title;
13
14// @codeCoverageIgnoreStart
15require_once __DIR__ . '/Maintenance.php';
16// @codeCoverageIgnoreEnd
17
18/**
19 * Maintenance script to check that pages marked as being redirects really are.
20 *
21 * @ingroup Maintenance
22 */
23class CheckBadRedirects extends Maintenance {
24    public function __construct() {
25        parent::__construct();
26        $this->addDescription( 'Check for bad redirects' );
27    }
28
29    public function execute() {
30        $this->output( "Fetching redirects...\n" );
31        $dbr = $this->getReplicaDB();
32        $result = $dbr->newSelectQueryBuilder()
33            ->select( [ 'page_namespace', 'page_title', 'page_latest' ] )
34            ->from( 'page' )
35            ->where( [ 'page_is_redirect' => 1 ] )
36            ->caller( __METHOD__ )
37            ->fetchResultSet();
38
39        $count = $result->numRows();
40        $this->output( "Found $count redirects.\n" .
41            "Checking for bad redirects:\n\n" );
42
43        $revLookup = $this->getServiceContainer()->getRevisionLookup();
44        foreach ( $result as $row ) {
45            $title = Title::makeTitle( $row->page_namespace, $row->page_title );
46            $revRecord = $revLookup->getRevisionById( $row->page_latest );
47            if ( $revRecord ) {
48                $target = $revRecord->getContent( SlotRecord::MAIN )->getRedirectTarget();
49                if ( !$target ) {
50                    $this->output( $title->getPrefixedText() . "\n" );
51                }
52            }
53        }
54        $this->output( "\nDone.\n" );
55    }
56}
57
58// @codeCoverageIgnoreStart
59$maintClass = CheckBadRedirects::class;
60require_once RUN_MAINTENANCE_IF_MAIN;
61// @codeCoverageIgnoreEnd