Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 83
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReviewAllPages
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 autoReviewCurrent
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2/**
3 * @ingroup Maintenance
4 */
5
6use MediaWiki\Maintenance\Maintenance;
7use MediaWiki\Title\Title;
8use MediaWiki\User\User;
9use Wikimedia\Rdbms\IDBAccessObject;
10
11if ( getenv( 'MW_INSTALL_PATH' ) ) {
12    $IP = getenv( 'MW_INSTALL_PATH' );
13} else {
14    $IP = __DIR__ . '/../../..';
15}
16
17require_once "$IP/maintenance/Maintenance.php";
18
19class ReviewAllPages extends Maintenance {
20
21    public function __construct() {
22        parent::__construct();
23        $this->addDescription( "Review all pages in reviewable namespaces. " .
24            "A user ID must be given to specifiy the \"reviewer\" who accepted the pages." );
25        $this->addOption( 'username',
26            'The user name of the existing user to use as the "reviewer"', true, true );
27        $this->setBatchSize( 100 );
28        $this->requireExtension( 'FlaggedRevs' );
29    }
30
31    /**
32     * @inheritDoc
33     */
34    public function execute() {
35        $user = User::newFromName( $this->getOption( 'username' ) );
36        $this->autoReviewCurrent( $user );
37    }
38
39    /**
40     * @param User $user
41     */
42    private function autoReviewCurrent( User $user ) {
43        $services = $this->getServiceContainer();
44        $this->output( "Auto-reviewing all current page versions...\n" );
45        if ( !$user->isRegistered() ) {
46            $this->output( "Invalid user specified.\n" );
47            return;
48        } elseif ( !$services->getPermissionManager()->userHasRight( $user, 'review' ) ) {
49            $this->output( "User specified (id: {$user->getId()}) does not have \"review\" rights.\n" );
50            return;
51        }
52
53        $db = $this->getPrimaryDB();
54
55        $this->output( "Reviewer username: " . $user->getName() . "\n" );
56
57        $start = $db->newSelectQueryBuilder()
58            ->select( 'MIN(page_id)' )
59            ->from( 'page' )
60            ->caller( __METHOD__ )
61            ->fetchField();
62        $end = $db->newSelectQueryBuilder()
63            ->select( 'MAX(page_id)' )
64            ->from( 'page' )
65            ->caller( __METHOD__ )
66            ->fetchField();
67        if ( $start === null || $end === null ) {
68            $this->output( "...page table seems to be empty.\n" );
69            return;
70        }
71        # Do remaining chunk
72        $end += $this->mBatchSize - 1;
73        $blockStart = (int)$start;
74        $blockEnd = (int)( $start + $this->mBatchSize - 1 );
75        $count = 0;
76        $changed = 0;
77        $flags = FlaggedRevs::quickTags(); // Assume basic level
78
79        $wikiPageFactory = $services->getWikiPageFactory();
80        $revisionStore = $services->getRevisionStore();
81
82        while ( $blockEnd <= $end ) {
83            $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
84            $res = $db->newSelectQueryBuilder()
85                ->select( '*' )
86                ->from( 'page' )
87                ->join( 'revision', null, 'rev_id = page_latest' )
88                ->where( [
89                    $db->expr( 'page_id', '>=', $blockStart ),
90                    $db->expr( 'page_id', '<=', $blockEnd ),
91                    'page_namespace' => FlaggedRevs::getReviewNamespaces(),
92                ] )
93                ->caller( __METHOD__ )
94                ->fetchResultSet();
95            # Go through and autoreview the current version of every page...
96            foreach ( $res as $row ) {
97                $title = Title::newFromRow( $row );
98                $rev = $revisionStore->newRevisionFromRow( $row, IDBAccessObject::READ_LATEST );
99                # Is it already reviewed?
100                $frev = FlaggedRevision::newFromTitle( $title, $row->page_latest, IDBAccessObject::READ_LATEST );
101                # Rev should exist, but to be safe...
102                if ( !$frev && $rev ) {
103                    $wikiPage = $wikiPageFactory->newFromTitle( $title );
104                    $db->startAtomic( __METHOD__ );
105                    FlaggedRevs::autoReviewEdit(
106                        $wikiPage,
107                        $user,
108                        $rev,
109                        $flags,
110                        true,
111                        true // approve the reverted tag update
112                    );
113                    FlaggedRevs::updateHtmlCaches( $wikiPage->getTitle() );
114                    $db->endAtomic( __METHOD__ );
115                    $changed++;
116                }
117                $count++;
118            }
119            $blockStart += $this->mBatchSize - 1;
120            $blockEnd += $this->mBatchSize - 1;
121
122            $this->waitForReplication();
123        }
124
125        $this->output( "Auto-reviewing of all pages complete ..." .
126            "{$count} rows [{$changed} changed]\n" );
127    }
128}
129
130$maintClass = ReviewAllPages::class;
131require_once RUN_MAINTENANCE_IF_MAIN;