Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 127
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryOldreviewedpages
0.00% covered (danger)
0.00%
0 / 127
0.00% covered (danger)
0.00%
0 / 7
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 1
210
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
2
 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\Title\Title;
25use Wikimedia\ParamValidator\ParamValidator;
26use Wikimedia\ParamValidator\TypeDef\IntegerDef;
27
28/**
29 * Query module to list pages with outdated review flag.
30 *
31 * @ingroup FlaggedRevs
32 */
33class ApiQueryOldreviewedpages extends ApiQueryGeneratorBase {
34
35    /**
36     * @param ApiQuery $query
37     * @param string $moduleName
38     */
39    public function __construct( ApiQuery $query, $moduleName ) {
40        parent::__construct( $query, $moduleName, 'or' );
41    }
42
43    /**
44     * @inheritDoc
45     */
46    public function execute() {
47        $this->run();
48    }
49
50    /**
51     * @param ApiPageSet $resultPageSet
52     */
53    public function executeGenerator( $resultPageSet ) {
54        $this->run( $resultPageSet );
55    }
56
57    /**
58     * @param ApiPageSet|null $resultPageSet
59     */
60    private function run( $resultPageSet = null ) {
61        $params = $this->extractRequestParams();
62
63        // Construct SQL Query
64        $this->addTables( [ 'page', 'flaggedpages', 'revision' ] );
65        $this->addWhereFld( 'page_namespace', $params['namespace'] );
66        if ( $params['filterredir'] == 'redirects' ) {
67            $this->addWhereFld( 'page_is_redirect', 1 );
68        }
69        if ( $params['filterredir'] == 'nonredirects' ) {
70            $this->addWhereFld( 'page_is_redirect', 0 );
71        }
72        if ( $params['maxsize'] !== null ) {
73            # Get absolute difference for comparison. ABS(x-y)
74            # is broken due to mysql unsigned int design.
75            $this->addWhere( 'GREATEST(page_len,rev_len)-LEAST(page_len,rev_len) <= ' .
76                intval( $params['maxsize'] ) );
77        }
78        if ( $params['filterwatched'] == 'watched' ) {
79            $uid = $this->getUser()->getId();
80            if ( !$uid ) {
81                $this->dieWithError( 'watchlistanontext', 'notloggedin' );
82            }
83            $this->addTables( 'watchlist' );
84            $this->addWhereFld( 'wl_user', $uid );
85            $this->addWhere( 'page_namespace = wl_namespace' );
86            $this->addWhere( 'page_title = wl_title' );
87        }
88        if ( $params['category'] != '' ) {
89            $this->addTables( 'categorylinks' );
90            $this->addWhere( 'cl_from = fp_page_id' );
91            $this->addWhereFld( 'cl_to', $params['category'] );
92        }
93
94        $this->addWhereRange(
95            'fp_pending_since',
96            $params['dir'],
97            $params['start'],
98            $params['end']
99        );
100        $this->addWhere( 'page_id=fp_page_id' );
101        $this->addWhere( 'rev_id=fp_stable' );
102        if ( !isset( $params['start'] ) && !isset( $params['end'] ) ) {
103            $this->addWhere( 'fp_pending_since IS NOT NULL' );
104        }
105
106        if ( $resultPageSet === null ) {
107            $this->addFields( [
108                'page_id',
109                'page_namespace',
110                'page_title',
111                'page_latest',
112                'page_len',
113                'rev_len',
114                'fp_stable',
115                'fp_pending_since',
116                'fp_quality'
117            ] );
118        } else {
119            $this->addFields( $resultPageSet->getPageTableFields() );
120            $this->addFields( 'fp_pending_since' );
121        }
122
123        $limit = $params['limit'];
124        $this->addOption( 'LIMIT', $limit + 1 );
125        $res = $this->select( __METHOD__ );
126
127        $data = [];
128        $count = 0;
129        foreach ( $res as $row ) {
130            if ( ++$count > $limit ) {
131                // We've reached the one extra which shows that there are
132                // additional pages to be had. Stop here...
133                $this->setContinueEnumParameter(
134                    'start',
135                    wfTimestamp( TS_ISO_8601, $row->fp_pending_since )
136                );
137                break;
138            }
139
140            if ( $resultPageSet === null ) {
141                $title = Title::newFromRow( $row );
142                $data[] = [
143                    'pageid'             => intval( $row->page_id ),
144                    'ns'                 => intval( $row->page_namespace ),
145                    'title'             => $title->getPrefixedText(),
146                    'revid'             => intval( $row->page_latest ),
147                    'stable_revid'         => intval( $row->fp_stable ),
148                    'pending_since'     => wfTimestamp( TS_ISO_8601, $row->fp_pending_since ),
149                    'flagged_level'     => intval( $row->fp_quality ),
150                    'flagged_level_text' => 'stable',
151                    'diff_size'         => (int)$row->page_len - (int)$row->rev_len,
152                ];
153            } else {
154                $resultPageSet->processDbRow( $row );
155            }
156        }
157
158        if ( $resultPageSet === null ) {
159            $result = $this->getResult();
160            $result->setIndexedTagName( $data, 'p' );
161            $result->addValue( 'query', $this->getModuleName(), $data );
162        }
163    }
164
165    /**
166     * @inheritDoc
167     */
168    public function getCacheMode( $params ) {
169        return $params['filterwatched'] === 'watched' ? 'private' : 'public';
170    }
171
172    /**
173     * @inheritDoc
174     */
175    protected function getAllowedParams() {
176        return [
177            'start' => [
178                ParamValidator::PARAM_TYPE => 'timestamp'
179            ],
180            'end' => [
181                ParamValidator::PARAM_TYPE => 'timestamp'
182            ],
183            'dir' => [
184                ParamValidator::PARAM_DEFAULT => 'newer',
185                ParamValidator::PARAM_TYPE => [ 'newer', 'older' ],
186                ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
187            ],
188            'maxsize' => [
189                ParamValidator::PARAM_TYPE => 'integer',
190                ParamValidator::PARAM_DEFAULT => null,
191                IntegerDef::PARAM_MIN     => 0
192            ],
193            'filterwatched' => [
194                ParamValidator::PARAM_DEFAULT => 'all',
195                ParamValidator::PARAM_TYPE => [ 'watched', 'all' ]
196            ],
197            'namespace' => [
198                ParamValidator::PARAM_DEFAULT => FlaggedRevs::getFirstReviewNamespace(),
199                ParamValidator::PARAM_TYPE => 'namespace',
200                ParamValidator::PARAM_ISMULTI => true,
201            ],
202            'category' => [
203                ParamValidator::PARAM_TYPE => 'string'
204            ],
205            'filterredir' => [
206                ParamValidator::PARAM_DEFAULT => 'all',
207                ParamValidator::PARAM_TYPE => [ 'redirects', 'nonredirects', 'all' ]
208            ],
209            'limit' => [
210                ParamValidator::PARAM_DEFAULT => 10,
211                ParamValidator::PARAM_TYPE => 'limit',
212                IntegerDef::PARAM_MIN     => 1,
213                IntegerDef::PARAM_MAX     => ApiBase::LIMIT_BIG1,
214                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
215            ]
216        ];
217    }
218
219    /**
220     * @inheritDoc
221     */
222    protected function getExamplesMessages() {
223        return [
224            'action=query&list=oldreviewedpages&ornamespace=0'
225                => 'apihelp-query+oldreviewedpages-example-1',
226            'action=query&generator=oldreviewedpages&gorlimit=4&prop=info'
227                => 'apihelp-query+oldreviewedpages-example-2',
228        ];
229    }
230}