Merge "Use non-deprecated login in ApiLoginTest"
[lhc/web/wiklou.git] / includes / api / ApiQueryContributors.php
1 <?php
2 /**
3 * Query the list of contributors to a page
4 *
5 * Copyright © 2013 Wikimedia Foundation and contributors
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @since 1.23
24 */
25
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\Revision\RevisionRecord;
28
29 /**
30 * A query module to show contributors to a page
31 *
32 * @ingroup API
33 * @since 1.23
34 */
35 class ApiQueryContributors extends ApiQueryBase {
36 /** We don't want to process too many pages at once (it hits cold
37 * database pages too heavily), so only do the first MAX_PAGES input pages
38 * in each API call (leaving the rest for continuation).
39 */
40 const MAX_PAGES = 100;
41
42 public function __construct( ApiQuery $query, $moduleName ) {
43 // "pc" is short for "page contributors", "co" was already taken by the
44 // GeoData extension's prop=coordinates.
45 parent::__construct( $query, $moduleName, 'pc' );
46 }
47
48 public function execute() {
49 global $wgActorTableSchemaMigrationStage;
50
51 $db = $this->getDB();
52 $params = $this->extractRequestParams();
53 $this->requireMaxOneParameter( $params, 'group', 'excludegroup', 'rights', 'excluderights' );
54
55 // Only operate on existing pages
56 $pages = array_keys( $this->getPageSet()->getGoodTitles() );
57
58 // Filter out already-processed pages
59 if ( $params['continue'] !== null ) {
60 $cont = explode( '|', $params['continue'] );
61 $this->dieContinueUsageIf( count( $cont ) != 2 );
62 $cont_page = (int)$cont[0];
63 $pages = array_filter( $pages, function ( $v ) use ( $cont_page ) {
64 return $v >= $cont_page;
65 } );
66 }
67 if ( !count( $pages ) ) {
68 // Nothing to do
69 return;
70 }
71
72 // Apply MAX_PAGES, leaving any over the limit for a continue.
73 sort( $pages );
74 $continuePages = null;
75 if ( count( $pages ) > self::MAX_PAGES ) {
76 $continuePages = $pages[self::MAX_PAGES] . '|0';
77 $pages = array_slice( $pages, 0, self::MAX_PAGES );
78 }
79
80 $result = $this->getResult();
81 $revQuery = MediaWikiServices::getInstance()->getRevisionStore()->getQueryInfo();
82
83 // For MIGRATION_NEW, target indexes on the revision_actor_temp table.
84 // Otherwise, revision is fine because it'll have to check all revision rows anyway.
85 $pageField = $wgActorTableSchemaMigrationStage === MIGRATION_NEW ? 'revactor_page' : 'rev_page';
86 $idField = $wgActorTableSchemaMigrationStage === MIGRATION_NEW
87 ? 'revactor_actor' : $revQuery['fields']['rev_user'];
88 $countField = $wgActorTableSchemaMigrationStage === MIGRATION_NEW
89 ? 'revactor_actor' : $revQuery['fields']['rev_user_text'];
90
91 // First, count anons
92 $this->addTables( $revQuery['tables'] );
93 $this->addJoinConds( $revQuery['joins'] );
94 $this->addFields( [
95 'page' => $pageField,
96 'anons' => "COUNT(DISTINCT $countField)",
97 ] );
98 $this->addWhereFld( $pageField, $pages );
99 $this->addWhere( ActorMigration::newMigration()->isAnon( $revQuery['fields']['rev_user'] ) );
100 $this->addWhere( $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' );
101 $this->addOption( 'GROUP BY', $pageField );
102 $res = $this->select( __METHOD__ );
103 foreach ( $res as $row ) {
104 $fit = $result->addValue( [ 'query', 'pages', $row->page ],
105 'anoncontributors', (int)$row->anons
106 );
107 if ( !$fit ) {
108 // This not fitting isn't reasonable, so it probably means that
109 // some other module used up all the space. Just set a dummy
110 // continue and hope it works next time.
111 $this->setContinueEnumParameter( 'continue',
112 $params['continue'] ?? '0|0'
113 );
114
115 return;
116 }
117 }
118
119 // Next, add logged-in users
120 $this->resetQueryParams();
121 $this->addTables( $revQuery['tables'] );
122 $this->addJoinConds( $revQuery['joins'] );
123 $this->addFields( [
124 'page' => $pageField,
125 'id' => $idField,
126 // Non-MySQL databases don't like partial group-by
127 'userid' => 'MAX(' . $revQuery['fields']['rev_user'] . ')',
128 'username' => 'MAX(' . $revQuery['fields']['rev_user_text'] . ')',
129 ] );
130 $this->addWhereFld( $pageField, $pages );
131 $this->addWhere( ActorMigration::newMigration()->isNotAnon( $revQuery['fields']['rev_user'] ) );
132 $this->addWhere( $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' );
133 $this->addOption( 'GROUP BY', [ $pageField, $idField ] );
134 $this->addOption( 'LIMIT', $params['limit'] + 1 );
135
136 // Force a sort order to ensure that properties are grouped by page
137 // But only if rev_page is not constant in the WHERE clause.
138 if ( count( $pages ) > 1 ) {
139 $this->addOption( 'ORDER BY', [ 'page', 'id' ] );
140 } else {
141 $this->addOption( 'ORDER BY', 'id' );
142 }
143
144 $limitGroups = [];
145 if ( $params['group'] ) {
146 $excludeGroups = false;
147 $limitGroups = $params['group'];
148 } elseif ( $params['excludegroup'] ) {
149 $excludeGroups = true;
150 $limitGroups = $params['excludegroup'];
151 } elseif ( $params['rights'] ) {
152 $excludeGroups = false;
153 foreach ( $params['rights'] as $r ) {
154 $limitGroups = array_merge( $limitGroups, User::getGroupsWithPermission( $r ) );
155 }
156
157 // If no group has the rights requested, no need to query
158 if ( !$limitGroups ) {
159 if ( $continuePages !== null ) {
160 // But we still need to continue for the next page's worth
161 // of anoncontributors
162 $this->setContinueEnumParameter( 'continue', $continuePages );
163 }
164
165 return;
166 }
167 } elseif ( $params['excluderights'] ) {
168 $excludeGroups = true;
169 foreach ( $params['excluderights'] as $r ) {
170 $limitGroups = array_merge( $limitGroups, User::getGroupsWithPermission( $r ) );
171 }
172 }
173
174 if ( $limitGroups ) {
175 $limitGroups = array_unique( $limitGroups );
176 $this->addTables( 'user_groups' );
177 $this->addJoinConds( [ 'user_groups' => [
178 $excludeGroups ? 'LEFT OUTER JOIN' : 'INNER JOIN',
179 [
180 'ug_user=' . $revQuery['fields']['rev_user'],
181 'ug_group' => $limitGroups,
182 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
183 ]
184 ] ] );
185 $this->addWhereIf( 'ug_user IS NULL', $excludeGroups );
186 }
187
188 if ( $params['continue'] !== null ) {
189 $cont = explode( '|', $params['continue'] );
190 $this->dieContinueUsageIf( count( $cont ) != 2 );
191 $cont_page = (int)$cont[0];
192 $cont_id = (int)$cont[1];
193 $this->addWhere(
194 "$pageField > $cont_page OR " .
195 "($pageField = $cont_page AND " .
196 "$idField >= $cont_id)"
197 );
198 }
199
200 $res = $this->select( __METHOD__ );
201 $count = 0;
202 foreach ( $res as $row ) {
203 if ( ++$count > $params['limit'] ) {
204 // We've reached the one extra which shows that
205 // there are additional pages to be had. Stop here...
206 $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
207 return;
208 }
209
210 $fit = $this->addPageSubItem( $row->page,
211 [ 'userid' => (int)$row->userid, 'name' => $row->username ],
212 'user'
213 );
214 if ( !$fit ) {
215 $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
216 return;
217 }
218 }
219
220 if ( $continuePages !== null ) {
221 $this->setContinueEnumParameter( 'continue', $continuePages );
222 }
223 }
224
225 public function getCacheMode( $params ) {
226 return 'public';
227 }
228
229 public function getAllowedParams() {
230 $userGroups = User::getAllGroups();
231 $userRights = User::getAllRights();
232
233 return [
234 'group' => [
235 ApiBase::PARAM_TYPE => $userGroups,
236 ApiBase::PARAM_ISMULTI => true,
237 ],
238 'excludegroup' => [
239 ApiBase::PARAM_TYPE => $userGroups,
240 ApiBase::PARAM_ISMULTI => true,
241 ],
242 'rights' => [
243 ApiBase::PARAM_TYPE => $userRights,
244 ApiBase::PARAM_ISMULTI => true,
245 ],
246 'excluderights' => [
247 ApiBase::PARAM_TYPE => $userRights,
248 ApiBase::PARAM_ISMULTI => true,
249 ],
250 'limit' => [
251 ApiBase::PARAM_DFLT => 10,
252 ApiBase::PARAM_TYPE => 'limit',
253 ApiBase::PARAM_MIN => 1,
254 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
255 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
256 ],
257 'continue' => [
258 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
259 ],
260 ];
261 }
262
263 protected function getExamplesMessages() {
264 return [
265 'action=query&prop=contributors&titles=Main_Page'
266 => 'apihelp-query+contributors-example-simple',
267 ];
268 }
269
270 public function getHelpUrls() {
271 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Contributors';
272 }
273 }