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 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryUnreviewedpages
0.00% covered (danger)
0.00%
0 / 102
0.00% covered (danger)
0.00%
0 / 7
240
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 / 54
0.00% covered (danger)
0.00%
0 / 1
90
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 38
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 June 29, 2009
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 Wikimedia\ParamValidator\ParamValidator;
30use Wikimedia\ParamValidator\TypeDef\IntegerDef;
31
32/**
33 * Query module to list pages unreviewed pages
34 *
35 * @ingroup FlaggedRevs
36 */
37class ApiQueryUnreviewedpages extends ApiQueryGeneratorBase {
38
39    public function __construct( ApiQuery $query, string $moduleName ) {
40        parent::__construct( $query, $moduleName, 'ur' );
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' ] );
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
73        $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
74        $this->addWhereRange(
75            'page_title',
76            $dir,
77            $params['start'],
78            $params['end']
79        );
80        $this->addJoinConds(
81            [ 'flaggedpages' => [ 'LEFT JOIN', 'fp_page_id=page_id' ] ]
82        );
83        $this->addWhere( $this->getDB()->expr( 'fp_page_id', '=', null )
84            ->or( 'fp_quality', '<', intval( $params['filterlevel'] ) ) );
85
86        $this->addOption(
87            'USE INDEX',
88            [ 'page' => 'page_name_title', 'flaggedpages' => 'PRIMARY' ]
89        );
90
91        if ( $resultPageSet === null ) {
92            $this->addFields( [
93                'page_id',
94                'page_namespace',
95                'page_title',
96                'page_len',
97                'page_latest',
98            ] );
99        } else {
100            $this->addFields( $resultPageSet->getPageTableFields() );
101        }
102
103        $limit = $params['limit'];
104        $this->addOption( 'LIMIT', $limit + 1 );
105        $res = $this->select( __METHOD__ );
106
107        $data = [];
108        $count = 0;
109        foreach ( $res as $row ) {
110            if ( ++$count > $limit ) {
111                // We've reached the one extra which shows that there are
112                // additional pages to be had. Stop here...
113                $this->setContinueEnumParameter( 'start', $row->page_title );
114                break;
115            }
116
117            if ( $resultPageSet === null ) {
118                $title = Title::newFromRow( $row );
119                $data[] = [
120                    'pageid'        => intval( $row->page_id ),
121                    'ns'            => $title->getNamespace(),
122                    'title'         => $title->getPrefixedText(),
123                    'revid'         => intval( $row->page_latest ),
124                ];
125            } else {
126                $resultPageSet->processDbRow( $row );
127            }
128        }
129
130        if ( $resultPageSet === null ) {
131            $result = $this->getResult();
132            $result->setIndexedTagName( $data, 'p' );
133            $result->addValue( 'query', $this->getModuleName(), $data );
134        }
135    }
136
137    /**
138     * @inheritDoc
139     */
140    public function getCacheMode( $params ) {
141        return 'public';
142    }
143
144    /**
145     * @inheritDoc
146     */
147    protected function getAllowedParams() {
148        return [
149            'start' => [
150                ParamValidator::PARAM_TYPE => 'string'
151            ],
152            'end' => [
153                ParamValidator::PARAM_TYPE => 'string'
154            ],
155            'dir' => [
156                ParamValidator::PARAM_DEFAULT => 'ascending',
157                ParamValidator::PARAM_TYPE => [ 'ascending', 'descending' ],
158            ],
159            'namespace' => [
160                ParamValidator::PARAM_DEFAULT => FlaggedRevs::getFirstReviewNamespace(),
161                ParamValidator::PARAM_TYPE => 'namespace',
162                ParamValidator::PARAM_ISMULTI => true,
163            ],
164            'filterredir' => [
165                ParamValidator::PARAM_DEFAULT => 'all',
166                ParamValidator::PARAM_TYPE => [
167                    'redirects',
168                    'nonredirects',
169                    'all'
170                ]
171            ],
172            'filterlevel' => [
173                ParamValidator::PARAM_DEFAULT => 0,
174                ParamValidator::PARAM_TYPE => 'integer',
175                IntegerDef::PARAM_MIN  => 0,
176                IntegerDef::PARAM_MAX  => 2,
177            ],
178            'limit' => [
179                ParamValidator::PARAM_DEFAULT => 10,
180                ParamValidator::PARAM_TYPE => 'limit',
181                IntegerDef::PARAM_MIN => 1,
182                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
183                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
184            ]
185        ];
186    }
187
188    /**
189     * @inheritDoc
190     */
191    protected function getExamplesMessages() {
192        return [
193            'action=query&list=unreviewedpages&urnamespace=0&urfilterlevel=0'
194                => 'apihelp-query+unreviewedpages-example-1',
195            'action=query&generator=unreviewedpages&urnamespace=0&gurlimit=4&prop=info'
196                => 'apihelp-query+unreviewedpages-example-2',
197        ];
198    }
199}