3 * Query the list of contributors to a page
5 * Copyright © 2013 Wikimedia Foundation and contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
27 * A query module to show contributors to a page
32 class ApiQueryContributors
extends ApiQueryBase
{
33 /** We don't want to process too many pages at once (it hits cold
34 * database pages too heavily), so only do the first MAX_PAGES input pages
35 * in each API call (leaving the rest for continuation).
37 const MAX_PAGES
= 100;
39 public function __construct( ApiQuery
$query, $moduleName ) {
40 // "pc" is short for "page contributors", "co" was already taken by the
41 // GeoData extension's prop=coordinates.
42 parent
::__construct( $query, $moduleName, 'pc' );
45 public function execute() {
47 $params = $this->extractRequestParams();
48 $this->requireMaxOneParameter( $params, 'group', 'excludegroup', 'rights', 'excluderights' );
50 // Only operate on existing pages
51 $pages = array_keys( $this->getPageSet()->getGoodTitles() );
53 // Filter out already-processed pages
54 if ( $params['continue'] !== null ) {
55 $cont = explode( '|', $params['continue'] );
56 $this->dieContinueUsageIf( count( $cont ) != 2 );
57 $cont_page = (int)$cont[0];
58 $pages = array_filter( $pages, function ( $v ) use ( $cont_page ) {
59 return $v >= $cont_page;
62 if ( !count( $pages ) ) {
67 // Apply MAX_PAGES, leaving any over the limit for a continue.
69 $continuePages = null;
70 if ( count( $pages ) > self
::MAX_PAGES
) {
71 $continuePages = $pages[self
::MAX_PAGES
] . '|0';
72 $pages = array_slice( $pages, 0, self
::MAX_PAGES
);
75 $result = $this->getResult();
78 $this->addTables( 'revision' );
81 'anons' => 'COUNT(DISTINCT rev_user_text)',
83 $this->addWhereFld( 'rev_page', $pages );
84 $this->addWhere( 'rev_user = 0' );
85 $this->addWhere( $db->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' );
86 $this->addOption( 'GROUP BY', 'rev_page' );
87 $res = $this->select( __METHOD__
);
88 foreach ( $res as $row ) {
89 $fit = $result->addValue( [ 'query', 'pages', $row->page
],
90 'anoncontributors', (int)$row->anons
93 // This not fitting isn't reasonable, so it probably means that
94 // some other module used up all the space. Just set a dummy
95 // continue and hope it works next time.
96 $this->setContinueEnumParameter( 'continue',
97 $params['continue'] !== null ?
$params['continue'] : '0|0'
104 // Next, add logged-in users
105 $this->resetQueryParams();
106 $this->addTables( 'revision' );
108 'page' => 'rev_page',
109 'user' => 'rev_user',
110 'username' => 'MAX(rev_user_text)', // Non-MySQL databases don't like partial group-by
112 $this->addWhereFld( 'rev_page', $pages );
113 $this->addWhere( 'rev_user != 0' );
114 $this->addWhere( $db->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' );
115 $this->addOption( 'GROUP BY', 'rev_page, rev_user' );
116 $this->addOption( 'LIMIT', $params['limit'] +
1 );
118 // Force a sort order to ensure that properties are grouped by page
119 // But only if pp_page is not constant in the WHERE clause.
120 if ( count( $pages ) > 1 ) {
121 $this->addOption( 'ORDER BY', 'rev_page, rev_user' );
123 $this->addOption( 'ORDER BY', 'rev_user' );
127 if ( $params['group'] ) {
128 $excludeGroups = false;
129 $limitGroups = $params['group'];
130 } elseif ( $params['excludegroup'] ) {
131 $excludeGroups = true;
132 $limitGroups = $params['excludegroup'];
133 } elseif ( $params['rights'] ) {
134 $excludeGroups = false;
135 foreach ( $params['rights'] as $r ) {
136 $limitGroups = array_merge( $limitGroups, User
::getGroupsWithPermission( $r ) );
139 // If no group has the rights requested, no need to query
140 if ( !$limitGroups ) {
141 if ( $continuePages !== null ) {
142 // But we still need to continue for the next page's worth
143 // of anoncontributors
144 $this->setContinueEnumParameter( 'continue', $continuePages );
149 } elseif ( $params['excluderights'] ) {
150 $excludeGroups = true;
151 foreach ( $params['excluderights'] as $r ) {
152 $limitGroups = array_merge( $limitGroups, User
::getGroupsWithPermission( $r ) );
156 if ( $limitGroups ) {
157 $limitGroups = array_unique( $limitGroups );
158 $this->addTables( 'user_groups' );
159 $this->addJoinConds( [ 'user_groups' => [
160 $excludeGroups ?
'LEFT OUTER JOIN' : 'INNER JOIN',
163 'ug_group' => $limitGroups,
164 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
167 $this->addWhereIf( 'ug_user IS NULL', $excludeGroups );
170 if ( $params['continue'] !== null ) {
171 $cont = explode( '|', $params['continue'] );
172 $this->dieContinueUsageIf( count( $cont ) != 2 );
173 $cont_page = (int)$cont[0];
174 $cont_user = (int)$cont[1];
176 "rev_page > $cont_page OR " .
177 "(rev_page = $cont_page AND " .
178 "rev_user >= $cont_user)"
182 $res = $this->select( __METHOD__
);
184 foreach ( $res as $row ) {
185 if ( ++
$count > $params['limit'] ) {
186 // We've reached the one extra which shows that
187 // there are additional pages to be had. Stop here...
188 $this->setContinueEnumParameter( 'continue', $row->page
. '|' . $row->user
);
193 $fit = $this->addPageSubItem( $row->page
,
194 [ 'userid' => (int)$row->user
, 'name' => $row->username
],
198 $this->setContinueEnumParameter( 'continue', $row->page
. '|' . $row->user
);
204 if ( $continuePages !== null ) {
205 $this->setContinueEnumParameter( 'continue', $continuePages );
209 public function getCacheMode( $params ) {
213 public function getAllowedParams() {
214 $userGroups = User
::getAllGroups();
215 $userRights = User
::getAllRights();
219 ApiBase
::PARAM_TYPE
=> $userGroups,
220 ApiBase
::PARAM_ISMULTI
=> true,
223 ApiBase
::PARAM_TYPE
=> $userGroups,
224 ApiBase
::PARAM_ISMULTI
=> true,
227 ApiBase
::PARAM_TYPE
=> $userRights,
228 ApiBase
::PARAM_ISMULTI
=> true,
231 ApiBase
::PARAM_TYPE
=> $userRights,
232 ApiBase
::PARAM_ISMULTI
=> true,
235 ApiBase
::PARAM_DFLT
=> 10,
236 ApiBase
::PARAM_TYPE
=> 'limit',
237 ApiBase
::PARAM_MIN
=> 1,
238 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
239 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
242 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
247 protected function getExamplesMessages() {
249 'action=query&prop=contributors&titles=Main_Page'
250 => 'apihelp-query+contributors-example-simple',
254 public function getHelpUrls() {
255 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Contributors';