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
PurgeMapPages
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2$IP = getenv( 'MW_INSTALL_PATH' );
3if ( $IP === false ) {
4    $IP = __DIR__ . '/../../..';
5}
6require_once "$IP/maintenance/Maintenance.php";
7
8use MediaWiki\Maintenance\Maintenance;
9use MediaWiki\Title\Title;
10
11/**
12 * Purges all pages that use <maplink> or <mapframe>, using the tracking category.
13 *
14 * @license MIT
15 */
16class PurgeMapPages extends Maintenance {
17
18    public function __construct() {
19        parent::__construct();
20        $this->addDescription( 'Purge all pages that use <maplink> or <mapframe>.' );
21        $this->addOption( 'dry-run', 'Only print page names, do not purge them' );
22        $this->setBatchSize( 100 );
23        $this->requireExtension( 'Kartographer' );
24    }
25
26    /** @inheritDoc */
27    public function execute() {
28        $categoryMessage = wfMessage( 'kartographer-tracking-category' );
29        if ( $categoryMessage->isDisabled() ) {
30            $this->error( "Tracking category for maps pages is disabled\n" );
31            return;
32        }
33        $categoryTitle = Title::makeTitle( NS_CATEGORY, $categoryMessage->inContentLanguage()->text() );
34        $dryRun = $this->hasOption( 'dry-run' );
35        $iterator = new BatchRowIterator(
36            $this->getReplicaDB(),
37            [ 'categorylinks', 'page' ],
38            [ 'cl_type', 'cl_sortkey', 'cl_from' ],
39            $this->getBatchSize()
40        );
41        $iterator->addConditions( [ 'cl_to' => $categoryTitle->getDBkey() ] );
42        $iterator->addJoinConditions( [ 'page' => [ 'INNER JOIN', [ 'page_id=cl_from' ] ] ] );
43        $iterator->setFetchColumns( [ 'page_id', 'page_namespace', 'page_title' ] );
44        $iterator->setCaller( __METHOD__ );
45
46        $pages = 0;
47        $failures = 0;
48        $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
49        foreach ( $iterator as $batch ) {
50            foreach ( $batch as $row ) {
51                $title = Title::newFromRow( $row );
52                if ( $dryRun ) {
53                    $this->output( $title->getPrefixedText() . "\n" );
54                } else {
55                    $page = $wikiPageFactory->newFromTitle( $title );
56                    if ( $page->doPurge() ) {
57                        $this->output( "Purged {$title->getPrefixedText()}\n" );
58                    } else {
59                        $this->error( "FAILED TO PURGE {$title->getPrefixedText()}\n" );
60                        $failures++;
61                    }
62                }
63            }
64            $pages += count( $batch );
65        }
66        $this->output( "\nFinished. Total: $pages pages. Failed: $failures.\n" );
67    }
68}
69
70$maintClass = PurgeMapPages::class;
71require_once RUN_MAINTENANCE_IF_MAIN;