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