Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.21% covered (warning)
83.21%
109 / 131
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryOldreviewedpages
83.21% covered (warning)
83.21%
109 / 131
57.14% covered (warning)
57.14%
4 / 7
25.51
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
81.01% covered (warning)
81.01%
64 / 79
0.00% covered (danger)
0.00%
0 / 1
17.75
 getCacheMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getAllowedParams
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Created on Sep 17, 2008
5 *
6 * API module for MediaWiki's FlaggedRevs extension
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 */
23
24use MediaWiki\Api\ApiBase;
25use MediaWiki\Api\ApiPageSet;
26use MediaWiki\Api\ApiQuery;
27use MediaWiki\Api\ApiQueryGeneratorBase;
28use MediaWiki\Title\Title;
29use MediaWiki\User\UserIdentityUtils;
30use Wikimedia\ParamValidator\ParamValidator;
31use Wikimedia\ParamValidator\TypeDef\IntegerDef;
32
33/**
34 * Query module to list pages with outdated review flag.
35 *
36 * @ingroup FlaggedRevs
37 */
38class ApiQueryOldreviewedpages extends ApiQueryGeneratorBase {
39    private UserIdentityUtils $userIdentityUtils;
40
41    public function __construct(
42        ApiQuery $query,
43        string $moduleName,
44        UserIdentityUtils $userIdentityUtils
45    ) {
46        parent::__construct( $query, $moduleName, 'or' );
47        $this->userIdentityUtils = $userIdentityUtils;
48    }
49
50    /**
51     * @inheritDoc
52     */
53    public function execute() {
54        $this->run();
55    }
56
57    /**
58     * @param ApiPageSet $resultPageSet
59     */
60    public function executeGenerator( $resultPageSet ) {
61        $this->run( $resultPageSet );
62    }
63
64    /**
65     * @param ApiPageSet|null $resultPageSet
66     */
67    private function run( $resultPageSet = null ) {
68        $params = $this->extractRequestParams();
69
70        // Construct SQL Query
71        $this->addTables( [ 'page', 'flaggedpages', 'revision' ] );
72        $this->addWhereFld( 'page_namespace', $params['namespace'] );
73        if ( $params['filterredir'] == 'redirects' ) {
74            $this->addWhereFld( 'page_is_redirect', 1 );
75        }
76        if ( $params['filterredir'] == 'nonredirects' ) {
77            $this->addWhereFld( 'page_is_redirect', 0 );
78        }
79        if ( $params['maxsize'] !== null ) {
80            # Get absolute difference for comparison. ABS(x-y)
81            # is broken due to mysql unsigned int design.
82            $this->addWhere( 'GREATEST(page_len,rev_len)-LEAST(page_len,rev_len) <= ' .
83                intval( $params['maxsize'] ) );
84        }
85        if ( $params['filterwatched'] == 'watched' ) {
86            $performer = $this->getAuthority();
87            if (
88                !$performer->isRegistered() ||
89                (
90                    $this->userIdentityUtils->isTemp( $performer->getUser() ) &&
91                    !$performer->isAllowed( 'viewmywatchlist' )
92                )
93            ) {
94                $this->dieWithError( 'watchlistanontext', 'notloggedin' );
95            }
96
97            $this->checkUserRightsAny( 'viewmywatchlist' );
98
99            $this->addTables( 'watchlist' );
100            $this->addWhereFld( 'wl_user', $performer->getUser()->getId() );
101            $this->addWhere( 'page_namespace = wl_namespace' );
102            $this->addWhere( 'page_title = wl_title' );
103        }
104        if ( $params['category'] != '' ) {
105            $this->addTables( 'categorylinks' );
106            $this->addWhere( 'cl_from = fp_page_id' );
107            $this->addWhereFld( 'cl_to', $params['category'] );
108        }
109
110        $this->addWhereRange(
111            'fp_pending_since',
112            $params['dir'],
113            $params['start'],
114            $params['end']
115        );
116        $this->addWhere( 'page_id=fp_page_id' );
117        $this->addWhere( 'rev_id=fp_stable' );
118        if ( !isset( $params['start'] ) && !isset( $params['end'] ) ) {
119            $this->addWhere( $this->getDB()->expr( 'fp_pending_since', '!=', null ) );
120        }
121
122        if ( $resultPageSet === null ) {
123            $this->addFields( [
124                'page_id',
125                'page_namespace',
126                'page_title',
127                'page_latest',
128                'page_len',
129                'rev_len',
130                'fp_stable',
131                'fp_pending_since',
132                'fp_quality'
133            ] );
134        } else {
135            $this->addFields( $resultPageSet->getPageTableFields() );
136            $this->addFields( 'fp_pending_since' );
137        }
138
139        $limit = $params['limit'];
140        $this->addOption( 'LIMIT', $limit + 1 );
141        $res = $this->select( __METHOD__ );
142
143        $data = [];
144        $count = 0;
145        foreach ( $res as $row ) {
146            if ( ++$count > $limit ) {
147                // We've reached the one extra which shows that there are
148                // additional pages to be had. Stop here...
149                $this->setContinueEnumParameter(
150                    'start',
151                    wfTimestamp( TS_ISO_8601, $row->fp_pending_since )
152                );
153                break;
154            }
155
156            if ( $resultPageSet === null ) {
157                $title = Title::newFromRow( $row );
158                $data[] = [
159                    'pageid'             => intval( $row->page_id ),
160                    'ns'                 => intval( $row->page_namespace ),
161                    'title'             => $title->getPrefixedText(),
162                    'revid'             => intval( $row->page_latest ),
163                    'stable_revid'         => intval( $row->fp_stable ),
164                    'pending_since'     => wfTimestamp( TS_ISO_8601, $row->fp_pending_since ),
165                    'flagged_level'     => intval( $row->fp_quality ),
166                    'flagged_level_text' => 'stable',
167                    'diff_size'         => (int)$row->page_len - (int)$row->rev_len,
168                ];
169            } else {
170                $resultPageSet->processDbRow( $row );
171            }
172        }
173
174        if ( $resultPageSet === null ) {
175            $result = $this->getResult();
176            $result->setIndexedTagName( $data, 'p' );
177            $result->addValue( 'query', $this->getModuleName(), $data );
178        }
179    }
180
181    /**
182     * @inheritDoc
183     */
184    public function getCacheMode( $params ) {
185        return $params['filterwatched'] === 'watched' ? 'private' : 'public';
186    }
187
188    /**
189     * @inheritDoc
190     */
191    protected function getAllowedParams() {
192        return [
193            'start' => [
194                ParamValidator::PARAM_TYPE => 'timestamp'
195            ],
196            'end' => [
197                ParamValidator::PARAM_TYPE => 'timestamp'
198            ],
199            'dir' => [
200                ParamValidator::PARAM_DEFAULT => 'newer',
201                ParamValidator::PARAM_TYPE => [ 'newer', 'older' ],
202                ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
203            ],
204            'maxsize' => [
205                ParamValidator::PARAM_TYPE => 'integer',
206                ParamValidator::PARAM_DEFAULT => null,
207                IntegerDef::PARAM_MIN     => 0
208            ],
209            'filterwatched' => [
210                ParamValidator::PARAM_DEFAULT => 'all',
211                ParamValidator::PARAM_TYPE => [ 'watched', 'all' ]
212            ],
213            'namespace' => [
214                ParamValidator::PARAM_DEFAULT => FlaggedRevs::getFirstReviewNamespace(),
215                ParamValidator::PARAM_TYPE => 'namespace',
216                ParamValidator::PARAM_ISMULTI => true,
217            ],
218            'category' => [
219                ParamValidator::PARAM_TYPE => 'string'
220            ],
221            'filterredir' => [
222                ParamValidator::PARAM_DEFAULT => 'all',
223                ParamValidator::PARAM_TYPE => [ 'redirects', 'nonredirects', 'all' ]
224            ],
225            'limit' => [
226                ParamValidator::PARAM_DEFAULT => 10,
227                ParamValidator::PARAM_TYPE => 'limit',
228                IntegerDef::PARAM_MIN     => 1,
229                IntegerDef::PARAM_MAX     => ApiBase::LIMIT_BIG1,
230                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
231            ]
232        ];
233    }
234
235    /**
236     * @inheritDoc
237     */
238    protected function getExamplesMessages() {
239        return [
240            'action=query&list=oldreviewedpages&ornamespace=0'
241                => 'apihelp-query+oldreviewedpages-example-1',
242            'action=query&generator=oldreviewedpages&gorlimit=4&prop=info'
243                => 'apihelp-query+oldreviewedpages-example-2',
244        ];
245    }
246}