Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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 ( $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 SCHEMA_COMPAT_READ_NEW, target indexes on the
84 // revision_actor_temp table, otherwise on the revision table.
85 $pageField = ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW )
86 ? 'revactor_page' : 'rev_page';
87 $idField = ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW )
88 ? 'revactor_actor' : $revQuery['fields']['rev_user'];
89 $countField = ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW )
90 ? 'revactor_actor' : $revQuery['fields']['rev_user_text'];
91
92 // First, count anons
93 $this->addTables( $revQuery['tables'] );
94 $this->addJoinConds( $revQuery['joins'] );
95 $this->addFields( [
96 'page' => $pageField,
97 'anons' => "COUNT(DISTINCT $countField)",
98 ] );
99 $this->addWhereFld( $pageField, $pages );
100 $this->addWhere( ActorMigration::newMigration()->isAnon( $revQuery['fields']['rev_user'] ) );
101 $this->addWhere( $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' );
102 $this->addOption( 'GROUP BY', $pageField );
103 $res = $this->select( __METHOD__ );
104 foreach ( $res as $row ) {
105 $fit = $result->addValue( [ 'query', 'pages', $row->page ],
106 'anoncontributors', (int)$row->anons
107 );
108 if ( !$fit ) {
109 // This not fitting isn't reasonable, so it probably means that
110 // some other module used up all the space. Just set a dummy
111 // continue and hope it works next time.
112 $this->setContinueEnumParameter( 'continue',
113 $params['continue'] ?? '0|0'
114 );
115
116 return;
117 }
118 }
119
120 // Next, add logged-in users
121 $this->resetQueryParams();
122 $this->addTables( $revQuery['tables'] );
123 $this->addJoinConds( $revQuery['joins'] );
124 $this->addFields( [
125 'page' => $pageField,
126 'id' => $idField,
127 // Non-MySQL databases don't like partial group-by
128 'userid' => 'MAX(' . $revQuery['fields']['rev_user'] . ')',
129 'username' => 'MAX(' . $revQuery['fields']['rev_user_text'] . ')',
130 ] );
131 $this->addWhereFld( $pageField, $pages );
132 $this->addWhere( ActorMigration::newMigration()->isNotAnon( $revQuery['fields']['rev_user'] ) );
133 $this->addWhere( $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' );
134 $this->addOption( 'GROUP BY', [ $pageField, $idField ] );
135 $this->addOption( 'LIMIT', $params['limit'] + 1 );
136
137 // Force a sort order to ensure that properties are grouped by page
138 // But only if rev_page is not constant in the WHERE clause.
139 if ( count( $pages ) > 1 ) {
140 $this->addOption( 'ORDER BY', [ 'page', 'id' ] );
141 } else {
142 $this->addOption( 'ORDER BY', 'id' );
143 }
144
145 $limitGroups = [];
146 if ( $params['group'] ) {
147 $excludeGroups = false;
148 $limitGroups = $params['group'];
149 } elseif ( $params['excludegroup'] ) {
150 $excludeGroups = true;
151 $limitGroups = $params['excludegroup'];
152 } elseif ( $params['rights'] ) {
153 $excludeGroups = false;
154 foreach ( $params['rights'] as $r ) {
155 $limitGroups = array_merge( $limitGroups, $this->getPermissionManager()
156 ->getGroupsWithPermission( $r ) );
157 }
158
159 // If no group has the rights requested, no need to query
160 if ( !$limitGroups ) {
161 if ( $continuePages !== null ) {
162 // But we still need to continue for the next page's worth
163 // of anoncontributors
164 $this->setContinueEnumParameter( 'continue', $continuePages );
165 }
166
167 return;
168 }
169 } elseif ( $params['excluderights'] ) {
170 $excludeGroups = true;
171 foreach ( $params['excluderights'] as $r ) {
172 $limitGroups = array_merge( $limitGroups, $this->getPermissionManager()
173 ->getGroupsWithPermission( $r ) );
174 }
175 }
176
177 if ( $limitGroups ) {
178 $limitGroups = array_unique( $limitGroups );
179 $this->addTables( 'user_groups' );
180 $this->addJoinConds( [ 'user_groups' => [
181 $excludeGroups ? 'LEFT JOIN' : 'JOIN',
182 [
183 'ug_user=' . $revQuery['fields']['rev_user'],
184 'ug_group' => $limitGroups,
185 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
186 ]
187 ] ] );
188 $this->addWhereIf( 'ug_user IS NULL', $excludeGroups );
189 }
190
191 if ( $params['continue'] !== null ) {
192 $cont = explode( '|', $params['continue'] );
193 $this->dieContinueUsageIf( count( $cont ) != 2 );
194 $cont_page = (int)$cont[0];
195 $cont_id = (int)$cont[1];
196 $this->addWhere(
197 "$pageField > $cont_page OR " .
198 "($pageField = $cont_page AND " .
199 "$idField >= $cont_id)"
200 );
201 }
202
203 $res = $this->select( __METHOD__ );
204 $count = 0;
205 foreach ( $res as $row ) {
206 if ( ++$count > $params['limit'] ) {
207 // We've reached the one extra which shows that
208 // there are additional pages to be had. Stop here...
209 $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
210 return;
211 }
212
213 $fit = $this->addPageSubItem( $row->page,
214 [ 'userid' => (int)$row->userid, 'name' => $row->username ],
215 'user'
216 );
217 if ( !$fit ) {
218 $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
219 return;
220 }
221 }
222
223 if ( $continuePages !== null ) {
224 $this->setContinueEnumParameter( 'continue', $continuePages );
225 }
226 }
227
228 public function getCacheMode( $params ) {
229 return 'public';
230 }
231
232 public function getAllowedParams() {
233 $userGroups = User::getAllGroups();
234 $userRights = $this->getPermissionManager()->getAllPermissions();
235
236 return [
237 'group' => [
238 ApiBase::PARAM_TYPE => $userGroups,
239 ApiBase::PARAM_ISMULTI => true,
240 ],
241 'excludegroup' => [
242 ApiBase::PARAM_TYPE => $userGroups,
243 ApiBase::PARAM_ISMULTI => true,
244 ],
245 'rights' => [
246 ApiBase::PARAM_TYPE => $userRights,
247 ApiBase::PARAM_ISMULTI => true,
248 ],
249 'excluderights' => [
250 ApiBase::PARAM_TYPE => $userRights,
251 ApiBase::PARAM_ISMULTI => true,
252 ],
253 'limit' => [
254 ApiBase::PARAM_DFLT => 10,
255 ApiBase::PARAM_TYPE => 'limit',
256 ApiBase::PARAM_MIN => 1,
257 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
258 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
259 ],
260 'continue' => [
261 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
262 ],
263 ];
264 }
265
266 protected function getExamplesMessages() {
267 return [
268 'action=query&prop=contributors&titles=Main_Page'
269 => 'apihelp-query+contributors-example-simple',
270 ];
271 }
272
273 public function getHelpUrls() {
274 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Contributors';
275 }
276 }