Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.00% covered (success)
92.00%
46 / 50
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateDraftQueue
92.00% covered (success)
92.00%
46 / 50
50.00% covered (danger)
50.00%
1 / 2
7.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
91.30% covered (success)
91.30%
42 / 46
0.00% covered (danger)
0.00%
0 / 1
6.02
1<?php
2
3namespace MediaWiki\Extension\PageTriage\Maintenance;
4
5use MediaWiki\Extension\PageTriage\ArticleCompile\ArticleCompileProcessor;
6use MediaWiki\Extension\PageTriage\ArticleMetadata;
7use MediaWiki\Extension\PageTriage\PageTriage;
8use MediaWiki\Maintenance\Maintenance;
9
10/**
11 * A maintenance script for populating the Draft namespace queue.
12 */
13class PopulateDraftQueue extends Maintenance {
14
15    public function __construct() {
16        parent::__construct();
17        $this->addDescription( "Add missing Draft namespace pages to the AfC triage queue." );
18        $this->requireExtension( 'PageTriage' );
19        $this->setBatchSize( 100 );
20    }
21
22    /**
23     * @return bool|null True for success, false for failure.
24     */
25    public function execute() {
26        // Find the Draft namespace ID.
27        $pageTriageDraftNamespaceId = $this->getConfig()->get( 'PageTriageDraftNamespaceId' );
28        if ( !$pageTriageDraftNamespaceId ) {
29            $this->output(
30                'Unable to determine Draft namespace. Please set $wgPageTriageDraftNamespaceId'
31            );
32            return false;
33        }
34
35        $afcStateTagId = ArticleMetadata::getValidTags()['afc_state'];
36
37        // Set up.
38        $this->output( "Processing drafts in NS $pageTriageDraftNamespaceId...\n" );
39        $db = $this->getDB( DB_PRIMARY );
40
41        // Loop through all batches.
42        $batchNum = 1;
43        $totalProcessed = 0;
44        $lastBatch = false;
45        while ( !$lastBatch ) {
46            // Progress indicator.
47            $this->output( "- batch $batchNum\n" );
48            $batchNum++;
49
50            // Find all Draft NS pages that don't have records in the PageTriage table.
51            $drafts = $db->newSelectQueryBuilder()
52                ->select( 'page_id' )
53                ->from( 'page' )
54                ->leftJoin( 'pagetriage_page', 'pagetriage_page', 'page_id = ptrp_page_id' )
55                ->leftJoin( 'pagetriage_page_tags', 'pagetriage_page_tags',
56                    [
57                        'page_id = ptrpt_page_id',
58                        'ptrpt_tag_id' => $afcStateTagId,
59                    ]
60                )
61                ->where( [
62                    $db->expr( 'ptrp_page_id', '=', null )->or( 'ptrpt_page_id', '=', null ),
63                    'page_namespace' => $pageTriageDraftNamespaceId,
64                    'page_is_redirect' => '0',
65                ] )
66                ->limit( $this->getBatchSize() )
67                ->caller( __METHOD__ )
68                ->fetchResultSet();
69
70            // The loop will exit if this is the last batch.
71            if ( $drafts->numRows() < $this->getBatchSize() ) {
72                $lastBatch = true;
73            }
74
75            // Go through this batch, and add each page to the PageTriage queue.
76            foreach ( $drafts as $draft ) {
77                $this->beginTransaction( $db, __METHOD__ );
78                $pageTriage = new PageTriage( $draft->page_id );
79                $pageTriage->addToPageTriageQueue();
80                $acp = ArticleCompileProcessor::newFromPageId( [ $draft->page_id ] );
81                if ( $acp ) {
82                    $acp->compileMetadata();
83                }
84                $this->commitTransaction( $db, __METHOD__ );
85                $totalProcessed++;
86            }
87        }
88
89        // Finish.
90        $this->output( "Complete; $totalProcessed drafts processed.\n" );
91        return true;
92    }
93
94}