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