MediaWiki  master
ApiQueryContributors.php
Go to the documentation of this file.
1 <?php
30 
42  private const MAX_PAGES = 100;
43 
45  private $revisionStore;
46 
48  private $actorMigration;
49 
52 
55 
64  public function __construct(
65  ApiQuery $query,
66  $moduleName,
71  ) {
72  // "pc" is short for "page contributors", "co" was already taken by the
73  // GeoData extension's prop=coordinates.
74  parent::__construct( $query, $moduleName, 'pc' );
75  $this->revisionStore = $revisionStore;
76  $this->actorMigration = $actorMigration;
77  $this->userGroupManager = $userGroupManager;
78  $this->groupPermissionsLookup = $groupPermissionsLookup;
79  }
80 
81  public function execute() {
82  $db = $this->getDB();
83  $params = $this->extractRequestParams();
84  $this->requireMaxOneParameter( $params, 'group', 'excludegroup', 'rights', 'excluderights' );
85 
86  // Only operate on existing pages
87  $pages = array_keys( $this->getPageSet()->getGoodPages() );
88 
89  // Filter out already-processed pages
90  if ( $params['continue'] !== null ) {
91  $cont = explode( '|', $params['continue'] );
92  $this->dieContinueUsageIf( count( $cont ) != 2 );
93  $cont_page = (int)$cont[0];
94  $pages = array_filter( $pages, static function ( $v ) use ( $cont_page ) {
95  return $v >= $cont_page;
96  } );
97  }
98  if ( $pages === [] ) {
99  // Nothing to do
100  return;
101  }
102 
103  // Apply MAX_PAGES, leaving any over the limit for a continue.
104  sort( $pages );
105  $continuePages = null;
106  if ( count( $pages ) > self::MAX_PAGES ) {
107  $continuePages = $pages[self::MAX_PAGES] . '|0';
108  $pages = array_slice( $pages, 0, self::MAX_PAGES );
109  }
110 
111  $result = $this->getResult();
112  $revQuery = $this->revisionStore->getQueryInfo();
113  $pageField = 'rev_page';
114  $idField = 'rev_actor';
115  $countField = 'rev_actor';
116 
117  // First, count anons
118  $this->addTables( $revQuery['tables'] );
119  $this->addJoinConds( $revQuery['joins'] );
120  $this->addFields( [
121  'page' => $pageField,
122  'anons' => "COUNT(DISTINCT $countField)",
123  ] );
124  $this->addWhereFld( $pageField, $pages );
125  $this->addWhere( $this->actorMigration->isAnon( $revQuery['fields']['rev_user'] ) );
126  $this->addWhere( $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' );
127  $this->addOption( 'GROUP BY', $pageField );
128  $res = $this->select( __METHOD__ );
129  foreach ( $res as $row ) {
130  $fit = $result->addValue( [ 'query', 'pages', $row->page ],
131  'anoncontributors', (int)$row->anons
132  );
133  if ( !$fit ) {
134  // This not fitting isn't reasonable, so it probably means that
135  // some other module used up all the space. Just set a dummy
136  // continue and hope it works next time.
137  $this->setContinueEnumParameter( 'continue',
138  $params['continue'] ?? '0|0'
139  );
140 
141  return;
142  }
143  }
144 
145  // Next, add logged-in users
146  $this->resetQueryParams();
147  $this->addTables( $revQuery['tables'] );
148  $this->addJoinConds( $revQuery['joins'] );
149  $this->addFields( [
150  'page' => $pageField,
151  'id' => $idField,
152  // Non-MySQL databases don't like partial group-by
153  'userid' => 'MAX(' . $revQuery['fields']['rev_user'] . ')',
154  'username' => 'MAX(' . $revQuery['fields']['rev_user_text'] . ')',
155  ] );
156  $this->addWhereFld( $pageField, $pages );
157  $this->addWhere( $this->actorMigration->isNotAnon( $revQuery['fields']['rev_user'] ) );
158  $this->addWhere( $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' );
159  $this->addOption( 'GROUP BY', [ $pageField, $idField ] );
160  $this->addOption( 'LIMIT', $params['limit'] + 1 );
161 
162  // Force a sort order to ensure that properties are grouped by page
163  // But only if rev_page is not constant in the WHERE clause.
164  if ( count( $pages ) > 1 ) {
165  $this->addOption( 'ORDER BY', [ 'page', 'id' ] );
166  } else {
167  $this->addOption( 'ORDER BY', 'id' );
168  }
169 
170  $limitGroups = [];
171  if ( $params['group'] ) {
172  $excludeGroups = false;
173  $limitGroups = $params['group'];
174  } elseif ( $params['excludegroup'] ) {
175  $excludeGroups = true;
176  $limitGroups = $params['excludegroup'];
177  } elseif ( $params['rights'] ) {
178  $excludeGroups = false;
179  foreach ( $params['rights'] as $r ) {
180  $limitGroups = array_merge( $limitGroups,
181  $this->groupPermissionsLookup->getGroupsWithPermission( $r ) );
182  }
183 
184  // If no group has the rights requested, no need to query
185  if ( !$limitGroups ) {
186  if ( $continuePages !== null ) {
187  // But we still need to continue for the next page's worth
188  // of anoncontributors
189  $this->setContinueEnumParameter( 'continue', $continuePages );
190  }
191 
192  return;
193  }
194  } elseif ( $params['excluderights'] ) {
195  $excludeGroups = true;
196  foreach ( $params['excluderights'] as $r ) {
197  $limitGroups = array_merge( $limitGroups,
198  $this->groupPermissionsLookup->getGroupsWithPermission( $r ) );
199  }
200  }
201 
202  if ( $limitGroups ) {
203  $limitGroups = array_unique( $limitGroups );
204  $this->addTables( 'user_groups' );
205  $this->addJoinConds( [ 'user_groups' => [
206  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable excludeGroups declared when limitGroups set
207  $excludeGroups ? 'LEFT JOIN' : 'JOIN',
208  [
209  'ug_user=' . $revQuery['fields']['rev_user'],
210  'ug_group' => $limitGroups,
211  'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
212  ]
213  ] ] );
214  // @phan-suppress-next-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable
215  // excludeGroups declared when limitGroups set
216  $this->addWhereIf( 'ug_user IS NULL', $excludeGroups );
217  }
218 
219  if ( $params['continue'] !== null ) {
220  $cont = explode( '|', $params['continue'] );
221  $this->dieContinueUsageIf( count( $cont ) != 2 );
222  $cont_page = (int)$cont[0];
223  $cont_id = (int)$cont[1];
224  $this->addWhere(
225  "$pageField > $cont_page OR " .
226  "($pageField = $cont_page AND " .
227  "$idField >= $cont_id)"
228  );
229  }
230 
231  $res = $this->select( __METHOD__ );
232  $count = 0;
233  foreach ( $res as $row ) {
234  if ( ++$count > $params['limit'] ) {
235  // We've reached the one extra which shows that
236  // there are additional pages to be had. Stop here...
237  $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
238  return;
239  }
240 
241  $fit = $this->addPageSubItem( $row->page,
242  [ 'userid' => (int)$row->userid, 'name' => $row->username ],
243  'user'
244  );
245  if ( !$fit ) {
246  $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
247  return;
248  }
249  }
250 
251  if ( $continuePages !== null ) {
252  $this->setContinueEnumParameter( 'continue', $continuePages );
253  }
254  }
255 
256  public function getCacheMode( $params ) {
257  return 'public';
258  }
259 
260  public function getAllowedParams( $flags = 0 ) {
261  $userGroups = $this->userGroupManager->listAllGroups();
262  $userRights = $this->getPermissionManager()->getAllPermissions();
263 
264  if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
265  sort( $userGroups );
266  }
267 
268  return [
269  'group' => [
270  ApiBase::PARAM_TYPE => $userGroups,
271  ApiBase::PARAM_ISMULTI => true,
272  ],
273  'excludegroup' => [
274  ApiBase::PARAM_TYPE => $userGroups,
275  ApiBase::PARAM_ISMULTI => true,
276  ],
277  'rights' => [
278  ApiBase::PARAM_TYPE => $userRights,
279  ApiBase::PARAM_ISMULTI => true,
280  ],
281  'excluderights' => [
282  ApiBase::PARAM_TYPE => $userRights,
283  ApiBase::PARAM_ISMULTI => true,
284  ],
285  'limit' => [
286  ApiBase::PARAM_DFLT => 10,
287  ApiBase::PARAM_TYPE => 'limit',
288  ApiBase::PARAM_MIN => 1,
291  ],
292  'continue' => [
293  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
294  ],
295  ];
296  }
297 
298  protected function getExamplesMessages() {
299  return [
300  'action=query&prop=contributors&titles=Main_Page'
301  => 'apihelp-query+contributors-example-simple',
302  ];
303  }
304 
305  public function getHelpUrls() {
306  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Contributors';
307  }
308 }
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
const PARAM_MAX2
Definition: ApiBase.php:90
const PARAM_MAX
Definition: ApiBase.php:86
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1638
const PARAM_TYPE
Definition: ApiBase.php:82
const PARAM_DFLT
Definition: ApiBase.php:74
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:686
const PARAM_MIN
Definition: ApiBase.php:94
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:221
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:938
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:163
const GET_VALUES_FOR_HELP
getAllowedParams() flag: When set, the result could take longer to generate, but should be more thoro...
Definition: ApiBase.php:234
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:223
const PARAM_ISMULTI
Definition: ApiBase.php:78
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
resetQueryParams()
Blank the internal arrays with query parameters.
addWhereIf( $value, $condition)
Same as addWhere(), but add the WHERE clauses only if a condition is met.
addFields( $value)
Add a set of fields to select to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
getDB()
Get the Query database connection (read-only)
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
getPageSet()
Get the PageSet object to work on.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
A query module to show contributors to a page.
GroupPermissionsLookup $groupPermissionsLookup
UserGroupManager $userGroupManager
ActorMigration $actorMigration
getExamplesMessages()
Returns usage examples for this module.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
__construct(ApiQuery $query, $moduleName, RevisionStore $revisionStore, ActorMigration $actorMigration, UserGroupManager $userGroupManager, GroupPermissionsLookup $groupPermissionsLookup)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.
const MAX_PAGES
We don't want to process too many pages at once (it hits cold database pages too heavily),...
This is the main query class.
Definition: ApiQuery.php:40
Page revision base class.
Service for looking up page revisions.
$revQuery