Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 102
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateFRAutoPromote
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 2
72
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 / 92
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 * @ingroup Maintenance
4 */
5
6use MediaWiki\Maintenance\Maintenance;
7use MediaWiki\User\ActorMigration;
8use MediaWiki\User\User;
9use Wikimedia\Rdbms\IDBAccessObject;
10use Wikimedia\Rdbms\IExpression;
11use Wikimedia\Rdbms\LikeValue;
12
13if ( getenv( 'MW_INSTALL_PATH' ) ) {
14    $IP = getenv( 'MW_INSTALL_PATH' );
15} else {
16    $IP = __DIR__ . '/../../..';
17}
18
19require_once "$IP/maintenance/Maintenance.php";
20
21class UpdateFRAutoPromote extends Maintenance {
22
23    public function __construct() {
24        parent::__construct();
25        $this->addDescription( "Update autopromote table" );
26        $this->setBatchSize( 50 );
27        $this->requireExtension( 'FlaggedRevs' );
28    }
29
30    /**
31     * @inheritDoc
32     */
33    public function execute() {
34        $this->output( "Populating and updating flaggedrevs_promote table\n" );
35
36        $services = $this->getServiceContainer();
37        $commentQuery = $services->getCommentStore()->getJoin( 'rev_comment' );
38        $revisionStore = $services->getRevisionStore();
39        $revQuery = $revisionStore->getQueryInfo();
40        $revPageQuery = $revisionStore->getQueryInfo( [ 'page' ] );
41        $dbr = $this->getReplicaDB();
42        $dbw = $this->getPrimaryDB();
43        $start = $dbr->newSelectQueryBuilder()
44            ->select( 'MIN(user_id)' )
45            ->from( 'user' )
46            ->caller( __METHOD__ )
47            ->fetchField();
48        $end = $dbr->newSelectQueryBuilder()
49            ->select( 'MAX(user_id)' )
50            ->from( 'user' )
51            ->caller( __METHOD__ )
52            ->fetchField();
53        if ( $start === null || $end === null ) {
54            $this->output( "...user table seems to be empty.\n" );
55            return;
56        }
57        $count = 0;
58        $changed = 0;
59
60        $contentNamespaces = $services->getNamespaceInfo()->getContentNamespaces();
61        $autopromote = $this->getConfig()->get( 'FlaggedRevsAutopromote' );
62
63        for ( $blockStart = (int)$start; $blockStart <= $end; $blockStart += (int)$this->mBatchSize ) {
64            $blockEnd = (int)min( $end, $blockStart + $this->mBatchSize - 1 );
65            $this->output( "...doing user_id from $blockStart to $blockEnd\n" );
66            $res = $dbr->newSelectQueryBuilder()
67                ->select( '*' )
68                ->from( 'user' )
69                ->where( $dbr->expr( 'user_id', '>=', $blockStart )->and( 'user_id', '<=', $blockEnd ) )
70                ->caller( __METHOD__ )
71                ->fetchResultSet();
72            # Go through and clean up missing items
73            foreach ( $res as $row ) {
74                $this->beginTransaction( $dbw, __METHOD__ );
75                $user = User::newFromRow( $row );
76                $p = FRUserCounters::getUserParams( $user->getId(), IDBAccessObject::READ_EXCLUSIVE );
77                $oldp = $p;
78                # Get edit comments used
79                $revWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user );
80                $sres = $dbr->newSelectQueryBuilder()
81                    ->select( '1' )
82                    ->tables( $revQuery['tables'] )
83                    ->tables( $commentQuery['tables'] )
84                    ->where( [
85                        $revWhere['conds'],
86                        // @todo Should there be a "rev_comment != ''" here too?
87                        $dbw->expr(
88                            $commentQuery['fields']['rev_comment_text'],
89                            IExpression::NOT_LIKE,
90                            new LikeValue( '/*', $dbw->anyString(), '*/' ) // manual comments only
91                        ),
92                    ] )
93                    ->limit( max( $autopromote['editComments'], 500 ) )
94                    ->joinConds( $commentQuery['joins'] )
95                    ->caller( __METHOD__ )
96                    ->fetchResultSet();
97                $p['editComments'] = $sres->numRows();
98                # Get content page edits
99                $sres = $dbr->newSelectQueryBuilder()
100                    ->select( '1' )
101                    ->tables( $revPageQuery['tables'] )
102                    ->where( [
103                        $revWhere['conds'],
104                        'page_namespace' => $contentNamespaces,
105                    ] )
106                    ->limit( max( $autopromote['totalContentEdits'], 500 ) )
107                    ->joinConds( $revPageQuery['joins'] )
108                    ->caller( __METHOD__ )
109                    ->fetchResultSet();
110                $p['totalContentEdits'] = $sres->numRows();
111                # Get unique content pages edited
112                $sres = $dbr->newSelectQueryBuilder()
113                    ->select( 'rev_page' )
114                    ->distinct()
115                    ->tables( $revPageQuery['tables'] )
116                    ->where( [
117                        $revWhere['conds'],
118                        'page_namespace' => $contentNamespaces,
119                    ] )
120                    ->limit( max( $autopromote['uniqueContentPages'], 50 ) )
121                    ->joinConds( $revPageQuery['joins'] )
122                    ->caller( __METHOD__ )
123                    ->fetchResultSet();
124                $p['uniqueContentPages'] = [];
125                foreach ( $sres as $innerRow ) {
126                    $p['uniqueContentPages'][] = (int)$innerRow->rev_page;
127                }
128                # Save the new params...
129                if ( $oldp != $p ) {
130                    FRUserCounters::saveUserParams( $user->getId(), $p );
131                    $changed++;
132                }
133
134                $count++;
135                $this->commitTransaction( $dbw, __METHOD__ );
136            }
137            $this->waitForReplication();
138        }
139        $this->output( "flaggedrevs_promote table update complete ..." .
140            " {$count} rows [{$changed} changed or added]\n" );
141    }
142}
143
144$maintClass = UpdateFRAutoPromote::class;
145require_once RUN_MAINTENANCE_IF_MAIN;