Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ValidateTemplateData
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3$IP = getenv( 'MW_INSTALL_PATH' );
4if ( $IP === false ) {
5    $IP = __DIR__ . '/../../..';
6}
7require_once "$IP/maintenance/Maintenance.php";
8
9use MediaWiki\Extension\TemplateData\TemplateDataBlob;
10use MediaWiki\Title\Title;
11
12/**
13 * @license GPL-2.0-or-later
14 */
15class ValidateTemplateData extends Maintenance {
16
17    public function __construct() {
18        parent::__construct();
19        $this->addDescription( 'Checks all TemplateData JSON pages are valid and outputs the name of invalid rows' );
20
21        $this->setBatchSize( 500 );
22        $this->requireExtension( 'TemplateData' );
23    }
24
25    public function execute() {
26        $db = $this->getDB( DB_REPLICA );
27
28        $lastId = 0;
29        $rowsChecked = 0;
30        $badRows = 0;
31        $this->output( "Pages with invalid Template Data:\n" );
32        do {
33            $res = $db->newSelectQueryBuilder()
34                ->from( 'page_props' )
35                ->join( 'page', null, 'pp_page=page_id' )
36                ->fields( [ 'pp_page', 'pp_value', 'page_namespace', 'page_title' ] )
37                ->where( [
38                    $db->expr( 'pp_page', '>', $lastId ),
39                    'pp_propname' => 'templatedata'
40                ] )
41                ->orderBy( 'pp_page' )
42                ->limit( $this->getBatchSize() )
43                ->caller( __METHOD__ )
44                ->fetchResultSet();
45
46            $count = $res->numRows();
47
48            foreach ( $res as $row ) {
49                $tdb = TemplateDataBlob::newFromDatabase( $db, $row->pp_value );
50                $status = $tdb->getStatus();
51                if ( !$status->isOK() ) {
52                    $this->output(
53                        Title::newFromRow( $row )->getPrefixedText() . "\n"
54                    );
55                    $badRows++;
56                }
57                $lastId = $row->pp_page;
58                $rowsChecked++;
59            }
60        } while ( $count !== 0 );
61
62        $this->output( "\nDone!\n" );
63        $this->output( "Rows checked: {$rowsChecked}\n" );
64        $this->output( "Bad rows: {$badRows}\n" );
65    }
66
67}
68
69$maintClass = ValidateTemplateData::class;
70require_once RUN_MAINTENANCE_IF_MAIN;