Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryContributors.php
blob3d55f83058758c3aec272b3f3110c67ada5f2082
1 <?php
2 /**
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
22 * @file
23 * @since 1.23
26 namespace MediaWiki\Api;
28 use MediaWiki\Permissions\GroupPermissionsLookup;
29 use MediaWiki\Revision\RevisionRecord;
30 use MediaWiki\Revision\RevisionStore;
31 use MediaWiki\Title\Title;
32 use MediaWiki\User\ActorMigration;
33 use MediaWiki\User\TempUser\TempUserConfig;
34 use MediaWiki\User\UserGroupManager;
35 use Wikimedia\ParamValidator\ParamValidator;
36 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
38 /**
39 * A query module to show contributors to a page
41 * @ingroup API
42 * @since 1.23
44 class ApiQueryContributors extends ApiQueryBase {
45 /** We don't want to process too many pages at once (it hits cold
46 * database pages too heavily), so only do the first MAX_PAGES input pages
47 * in each API call (leaving the rest for continuation).
49 private const MAX_PAGES = 100;
51 private RevisionStore $revisionStore;
52 private ActorMigration $actorMigration;
53 private UserGroupManager $userGroupManager;
54 private GroupPermissionsLookup $groupPermissionsLookup;
55 private TempUserConfig $tempUserConfig;
57 public function __construct(
58 ApiQuery $query,
59 string $moduleName,
60 RevisionStore $revisionStore,
61 ActorMigration $actorMigration,
62 UserGroupManager $userGroupManager,
63 GroupPermissionsLookup $groupPermissionsLookup,
64 TempUserConfig $tempUserConfig
65 ) {
66 // "pc" is short for "page contributors", "co" was already taken by the
67 // GeoData extension's prop=coordinates.
68 parent::__construct( $query, $moduleName, 'pc' );
69 $this->revisionStore = $revisionStore;
70 $this->actorMigration = $actorMigration;
71 $this->userGroupManager = $userGroupManager;
72 $this->groupPermissionsLookup = $groupPermissionsLookup;
73 $this->tempUserConfig = $tempUserConfig;
76 public function execute() {
77 $db = $this->getDB();
78 $params = $this->extractRequestParams();
79 $this->requireMaxOneParameter( $params, 'group', 'excludegroup', 'rights', 'excluderights' );
81 // Only operate on existing pages
82 $pages = array_keys( $this->getPageSet()->getGoodPages() );
84 // Filter out already-processed pages
85 if ( $params['continue'] !== null ) {
86 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'int' ] );
87 $cont_page = (int)$cont[0];
88 $pages = array_filter( $pages, static function ( $v ) use ( $cont_page ) {
89 return $v >= $cont_page;
90 } );
92 if ( $pages === [] ) {
93 // Nothing to do
94 return;
97 // Apply MAX_PAGES, leaving any over the limit for a continue.
98 sort( $pages );
99 $continuePages = null;
100 if ( count( $pages ) > self::MAX_PAGES ) {
101 $continuePages = $pages[self::MAX_PAGES] . '|0';
102 $pages = array_slice( $pages, 0, self::MAX_PAGES );
105 $result = $this->getResult();
106 $revQuery = $this->revisionStore->getQueryInfo();
107 $pageField = 'rev_page';
108 $idField = 'rev_actor';
109 $countField = 'rev_actor';
111 // First, count anons
112 $this->addTables( $revQuery['tables'] );
113 $this->addJoinConds( $revQuery['joins'] );
114 $this->addFields( [
115 'page' => $pageField,
116 'anons' => "COUNT(DISTINCT $countField)",
117 ] );
118 $this->addWhereFld( $pageField, $pages );
119 $this->addWhere( $this->actorMigration->isAnon( $revQuery['fields']['rev_user'] ) );
120 $this->addWhere( [ $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) => 0 ] );
121 $this->addOption( 'GROUP BY', $pageField );
122 $res = $this->select( __METHOD__ );
123 foreach ( $res as $row ) {
124 $fit = $result->addValue( [ 'query', 'pages', $row->page ],
125 'anoncontributors', (int)$row->anons
127 if ( !$fit ) {
128 // This not fitting isn't reasonable, so it probably means that
129 // some other module used up all the space. Just set a dummy
130 // continue and hope it works next time.
131 $this->setContinueEnumParameter( 'continue',
132 $params['continue'] ?? '0|0'
135 return;
139 // Next, add logged-in users
140 $this->resetQueryParams();
141 $this->addTables( $revQuery['tables'] );
142 $this->addJoinConds( $revQuery['joins'] );
143 $this->addFields( [
144 'page' => $pageField,
145 'id' => $idField,
146 // Non-MySQL databases don't like partial group-by
147 'userid' => 'MAX(' . $revQuery['fields']['rev_user'] . ')',
148 'username' => 'MAX(' . $revQuery['fields']['rev_user_text'] . ')',
149 ] );
150 $this->addWhereFld( $pageField, $pages );
151 $this->addWhere( $this->actorMigration->isNotAnon( $revQuery['fields']['rev_user'] ) );
152 $this->addWhere( [ $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) => 0 ] );
153 $this->addOption( 'GROUP BY', [ $pageField, $idField ] );
154 $this->addOption( 'LIMIT', $params['limit'] + 1 );
156 // Force a sort order to ensure that properties are grouped by page
157 // But only if rev_page is not constant in the WHERE clause.
158 if ( count( $pages ) > 1 ) {
159 $this->addOption( 'ORDER BY', [ 'page', 'id' ] );
160 } else {
161 $this->addOption( 'ORDER BY', 'id' );
164 $limitGroups = [];
165 if ( $params['group'] ) {
166 $excludeGroups = false;
167 $limitGroups = $params['group'];
168 } elseif ( $params['excludegroup'] ) {
169 $excludeGroups = true;
170 $limitGroups = $params['excludegroup'];
171 } elseif ( $params['rights'] ) {
172 $excludeGroups = false;
173 foreach ( $params['rights'] as $r ) {
174 $limitGroups = array_merge( $limitGroups,
175 $this->groupPermissionsLookup->getGroupsWithPermission( $r ) );
178 // If no group has the rights requested, no need to query
179 if ( !$limitGroups ) {
180 if ( $continuePages !== null ) {
181 // But we still need to continue for the next page's worth
182 // of anoncontributors
183 $this->setContinueEnumParameter( 'continue', $continuePages );
186 return;
188 } elseif ( $params['excluderights'] ) {
189 $excludeGroups = true;
190 foreach ( $params['excluderights'] as $r ) {
191 $limitGroups = array_merge( $limitGroups,
192 $this->groupPermissionsLookup->getGroupsWithPermission( $r ) );
196 if ( $limitGroups ) {
197 $limitGroups = array_unique( $limitGroups );
198 $this->addTables( 'user_groups' );
199 $this->addJoinConds( [ 'user_groups' => [
200 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable excludeGroups declared when limitGroups set
201 $excludeGroups ? 'LEFT JOIN' : 'JOIN',
203 'ug_user=' . $revQuery['fields']['rev_user'],
204 'ug_group' => $limitGroups,
205 $db->expr( 'ug_expiry', '=', null )->or( 'ug_expiry', '>=', $db->timestamp() )
207 ] ] );
208 // @phan-suppress-next-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable
209 // excludeGroups declared when limitGroups set
210 $this->addWhereIf( [ 'ug_user' => null ], $excludeGroups );
213 if ( $params['continue'] !== null ) {
214 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'int' ] );
215 $this->addWhere( $db->buildComparison( '>=', [
216 $pageField => $cont[0],
217 $idField => $cont[1],
218 ] ) );
221 $res = $this->select( __METHOD__ );
222 $count = 0;
223 foreach ( $res as $row ) {
224 if ( ++$count > $params['limit'] ) {
225 // We've reached the one extra which shows that
226 // there are additional pages to be had. Stop here...
227 $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
228 return;
231 $fit = $this->addPageSubItem( $row->page,
232 [ 'userid' => (int)$row->userid, 'name' => $row->username ],
233 'user'
235 if ( !$fit ) {
236 $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
237 return;
241 if ( $continuePages !== null ) {
242 $this->setContinueEnumParameter( 'continue', $continuePages );
246 public function getCacheMode( $params ) {
247 return 'public';
250 public function getAllowedParams( $flags = 0 ) {
251 $userGroups = $this->userGroupManager->listAllGroups();
252 $userRights = $this->getPermissionManager()->getAllPermissions();
254 if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
255 sort( $userGroups );
258 return [
259 'group' => [
260 ParamValidator::PARAM_TYPE => $userGroups,
261 ParamValidator::PARAM_ISMULTI => true,
263 'excludegroup' => [
264 ParamValidator::PARAM_TYPE => $userGroups,
265 ParamValidator::PARAM_ISMULTI => true,
267 'rights' => [
268 ParamValidator::PARAM_TYPE => $userRights,
269 ParamValidator::PARAM_ISMULTI => true,
271 'excluderights' => [
272 ParamValidator::PARAM_TYPE => $userRights,
273 ParamValidator::PARAM_ISMULTI => true,
275 'limit' => [
276 ParamValidator::PARAM_DEFAULT => 10,
277 ParamValidator::PARAM_TYPE => 'limit',
278 IntegerDef::PARAM_MIN => 1,
279 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
280 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
282 'continue' => [
283 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
288 protected function getExamplesMessages() {
289 $title = Title::newMainPage()->getPrefixedText();
290 $mp = rawurlencode( $title );
292 return [
293 "action=query&prop=contributors&titles={$mp}"
294 => 'apihelp-query+contributors-example-simple',
298 public function getHelpUrls() {
299 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Contributors';
302 protected function getSummaryMessage() {
303 if ( $this->tempUserConfig->isKnown() ) {
304 return 'apihelp-query+contributors-summary-tempusers-enabled';
306 return parent::getSummaryMessage();
310 /** @deprecated class alias since 1.43 */
311 class_alias( ApiQueryContributors::class, 'ApiQueryContributors' );