Merge "mw.Feedback: If the message is posted remotely, link the title correctly"
[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 /**
27 * A query module to show contributors to a page
28 *
29 * @ingroup API
30 * @since 1.23
31 */
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).
36 */
37 const MAX_PAGES = 100;
38
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' );
43 }
44
45 public function execute() {
46 $db = $this->getDB();
47 $params = $this->extractRequestParams();
48 $this->requireMaxOneParameter( $params, 'group', 'excludegroup', 'rights', 'excluderights' );
49
50 // Only operate on existing pages
51 $pages = array_keys( $this->getPageSet()->getGoodTitles() );
52
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;
60 } );
61 }
62 if ( !count( $pages ) ) {
63 // Nothing to do
64 return;
65 }
66
67 // Apply MAX_PAGES, leaving any over the limit for a continue.
68 sort( $pages );
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 );
73 }
74
75 $result = $this->getResult();
76
77 // First, count anons
78 $this->addTables( 'revision' );
79 $this->addFields( [
80 'page' => 'rev_page',
81 'anons' => 'COUNT(DISTINCT rev_user_text)',
82 ] );
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
91 );
92 if ( !$fit ) {
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'
98 );
99
100 return;
101 }
102 }
103
104 // Next, add logged-in users
105 $this->resetQueryParams();
106 $this->addTables( 'revision' );
107 $this->addFields( [
108 'page' => 'rev_page',
109 'user' => 'rev_user',
110 'username' => 'MAX(rev_user_text)', // Non-MySQL databases don't like partial group-by
111 ] );
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 );
117
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' );
122 } else {
123 $this->addOption( 'ORDER BY', 'rev_user' );
124 }
125
126 $limitGroups = [];
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 ) );
137 }
138
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 );
145 }
146
147 return;
148 }
149 } elseif ( $params['excluderights'] ) {
150 $excludeGroups = true;
151 foreach ( $params['excluderights'] as $r ) {
152 $limitGroups = array_merge( $limitGroups, User::getGroupsWithPermission( $r ) );
153 }
154 }
155
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',
161 [
162 'ug_user=rev_user',
163 'ug_group' => $limitGroups,
164 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
165 ]
166 ] ] );
167 $this->addWhereIf( 'ug_user IS NULL', $excludeGroups );
168 }
169
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];
175 $this->addWhere(
176 "rev_page > $cont_page OR " .
177 "(rev_page = $cont_page AND " .
178 "rev_user >= $cont_user)"
179 );
180 }
181
182 $res = $this->select( __METHOD__ );
183 $count = 0;
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 );
189
190 return;
191 }
192
193 $fit = $this->addPageSubItem( $row->page,
194 [ 'userid' => (int)$row->user, 'name' => $row->username ],
195 'user'
196 );
197 if ( !$fit ) {
198 $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->user );
199
200 return;
201 }
202 }
203
204 if ( $continuePages !== null ) {
205 $this->setContinueEnumParameter( 'continue', $continuePages );
206 }
207 }
208
209 public function getCacheMode( $params ) {
210 return 'public';
211 }
212
213 public function getAllowedParams() {
214 $userGroups = User::getAllGroups();
215 $userRights = User::getAllRights();
216
217 return [
218 'group' => [
219 ApiBase::PARAM_TYPE => $userGroups,
220 ApiBase::PARAM_ISMULTI => true,
221 ],
222 'excludegroup' => [
223 ApiBase::PARAM_TYPE => $userGroups,
224 ApiBase::PARAM_ISMULTI => true,
225 ],
226 'rights' => [
227 ApiBase::PARAM_TYPE => $userRights,
228 ApiBase::PARAM_ISMULTI => true,
229 ],
230 'excluderights' => [
231 ApiBase::PARAM_TYPE => $userRights,
232 ApiBase::PARAM_ISMULTI => true,
233 ],
234 'limit' => [
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
240 ],
241 'continue' => [
242 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
243 ],
244 ];
245 }
246
247 protected function getExamplesMessages() {
248 return [
249 'action=query&prop=contributors&titles=Main_Page'
250 => 'apihelp-query+contributors-example-simple',
251 ];
252 }
253
254 public function getHelpUrls() {
255 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Contributors';
256 }
257 }