ApiQueryContributors.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. use MediaWiki\MediaWikiServices;
  26. use MediaWiki\Revision\RevisionRecord;
  27. /**
  28. * A query module to show contributors to a page
  29. *
  30. * @ingroup API
  31. * @since 1.23
  32. */
  33. class ApiQueryContributors extends ApiQueryBase {
  34. /** We don't want to process too many pages at once (it hits cold
  35. * database pages too heavily), so only do the first MAX_PAGES input pages
  36. * in each API call (leaving the rest for continuation).
  37. */
  38. const MAX_PAGES = 100;
  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. public function execute() {
  45. $db = $this->getDB();
  46. $params = $this->extractRequestParams();
  47. $this->requireMaxOneParameter( $params, 'group', 'excludegroup', 'rights', 'excluderights' );
  48. // Only operate on existing pages
  49. $pages = array_keys( $this->getPageSet()->getGoodTitles() );
  50. // Filter out already-processed pages
  51. if ( $params['continue'] !== null ) {
  52. $cont = explode( '|', $params['continue'] );
  53. $this->dieContinueUsageIf( count( $cont ) != 2 );
  54. $cont_page = (int)$cont[0];
  55. $pages = array_filter( $pages, function ( $v ) use ( $cont_page ) {
  56. return $v >= $cont_page;
  57. } );
  58. }
  59. if ( $pages === [] ) {
  60. // Nothing to do
  61. return;
  62. }
  63. // Apply MAX_PAGES, leaving any over the limit for a continue.
  64. sort( $pages );
  65. $continuePages = null;
  66. if ( count( $pages ) > self::MAX_PAGES ) {
  67. $continuePages = $pages[self::MAX_PAGES] . '|0';
  68. $pages = array_slice( $pages, 0, self::MAX_PAGES );
  69. }
  70. $result = $this->getResult();
  71. $revQuery = MediaWikiServices::getInstance()->getRevisionStore()->getQueryInfo();
  72. // Target indexes on the revision_actor_temp table.
  73. $pageField = 'revactor_page';
  74. $idField = 'revactor_actor';
  75. $countField = 'revactor_actor';
  76. // First, count anons
  77. $this->addTables( $revQuery['tables'] );
  78. $this->addJoinConds( $revQuery['joins'] );
  79. $this->addFields( [
  80. 'page' => $pageField,
  81. 'anons' => "COUNT(DISTINCT $countField)",
  82. ] );
  83. $this->addWhereFld( $pageField, $pages );
  84. $this->addWhere( ActorMigration::newMigration()->isAnon( $revQuery['fields']['rev_user'] ) );
  85. $this->addWhere( $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' );
  86. $this->addOption( 'GROUP BY', $pageField );
  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'] ?? '0|0'
  98. );
  99. return;
  100. }
  101. }
  102. // Next, add logged-in users
  103. $this->resetQueryParams();
  104. $this->addTables( $revQuery['tables'] );
  105. $this->addJoinConds( $revQuery['joins'] );
  106. $this->addFields( [
  107. 'page' => $pageField,
  108. 'id' => $idField,
  109. // Non-MySQL databases don't like partial group-by
  110. 'userid' => 'MAX(' . $revQuery['fields']['rev_user'] . ')',
  111. 'username' => 'MAX(' . $revQuery['fields']['rev_user_text'] . ')',
  112. ] );
  113. $this->addWhereFld( $pageField, $pages );
  114. $this->addWhere( ActorMigration::newMigration()->isNotAnon( $revQuery['fields']['rev_user'] ) );
  115. $this->addWhere( $db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' );
  116. $this->addOption( 'GROUP BY', [ $pageField, $idField ] );
  117. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  118. // Force a sort order to ensure that properties are grouped by page
  119. // But only if rev_page is not constant in the WHERE clause.
  120. if ( count( $pages ) > 1 ) {
  121. $this->addOption( 'ORDER BY', [ 'page', 'id' ] );
  122. } else {
  123. $this->addOption( 'ORDER BY', 'id' );
  124. }
  125. $limitGroups = [];
  126. if ( $params['group'] ) {
  127. $excludeGroups = false;
  128. $limitGroups = $params['group'];
  129. } elseif ( $params['excludegroup'] ) {
  130. $excludeGroups = true;
  131. $limitGroups = $params['excludegroup'];
  132. } elseif ( $params['rights'] ) {
  133. $excludeGroups = false;
  134. foreach ( $params['rights'] as $r ) {
  135. $limitGroups = array_merge( $limitGroups, $this->getPermissionManager()
  136. ->getGroupsWithPermission( $r ) );
  137. }
  138. // If no group has the rights requested, no need to query
  139. if ( !$limitGroups ) {
  140. if ( $continuePages !== null ) {
  141. // But we still need to continue for the next page's worth
  142. // of anoncontributors
  143. $this->setContinueEnumParameter( 'continue', $continuePages );
  144. }
  145. return;
  146. }
  147. } elseif ( $params['excluderights'] ) {
  148. $excludeGroups = true;
  149. foreach ( $params['excluderights'] as $r ) {
  150. $limitGroups = array_merge( $limitGroups, $this->getPermissionManager()
  151. ->getGroupsWithPermission( $r ) );
  152. }
  153. }
  154. if ( $limitGroups ) {
  155. $limitGroups = array_unique( $limitGroups );
  156. $this->addTables( 'user_groups' );
  157. $this->addJoinConds( [ 'user_groups' => [
  158. $excludeGroups ? 'LEFT JOIN' : 'JOIN',
  159. [
  160. 'ug_user=' . $revQuery['fields']['rev_user'],
  161. 'ug_group' => $limitGroups,
  162. 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
  163. ]
  164. ] ] );
  165. $this->addWhereIf( 'ug_user IS NULL', $excludeGroups );
  166. }
  167. if ( $params['continue'] !== null ) {
  168. $cont = explode( '|', $params['continue'] );
  169. $this->dieContinueUsageIf( count( $cont ) != 2 );
  170. $cont_page = (int)$cont[0];
  171. $cont_id = (int)$cont[1];
  172. $this->addWhere(
  173. "$pageField > $cont_page OR " .
  174. "($pageField = $cont_page AND " .
  175. "$idField >= $cont_id)"
  176. );
  177. }
  178. $res = $this->select( __METHOD__ );
  179. $count = 0;
  180. foreach ( $res as $row ) {
  181. if ( ++$count > $params['limit'] ) {
  182. // We've reached the one extra which shows that
  183. // there are additional pages to be had. Stop here...
  184. $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
  185. return;
  186. }
  187. $fit = $this->addPageSubItem( $row->page,
  188. [ 'userid' => (int)$row->userid, 'name' => $row->username ],
  189. 'user'
  190. );
  191. if ( !$fit ) {
  192. $this->setContinueEnumParameter( 'continue', $row->page . '|' . $row->id );
  193. return;
  194. }
  195. }
  196. if ( $continuePages !== null ) {
  197. $this->setContinueEnumParameter( 'continue', $continuePages );
  198. }
  199. }
  200. public function getCacheMode( $params ) {
  201. return 'public';
  202. }
  203. public function getAllowedParams() {
  204. $userGroups = User::getAllGroups();
  205. $userRights = $this->getPermissionManager()->getAllPermissions();
  206. return [
  207. 'group' => [
  208. ApiBase::PARAM_TYPE => $userGroups,
  209. ApiBase::PARAM_ISMULTI => true,
  210. ],
  211. 'excludegroup' => [
  212. ApiBase::PARAM_TYPE => $userGroups,
  213. ApiBase::PARAM_ISMULTI => true,
  214. ],
  215. 'rights' => [
  216. ApiBase::PARAM_TYPE => $userRights,
  217. ApiBase::PARAM_ISMULTI => true,
  218. ],
  219. 'excluderights' => [
  220. ApiBase::PARAM_TYPE => $userRights,
  221. ApiBase::PARAM_ISMULTI => true,
  222. ],
  223. 'limit' => [
  224. ApiBase::PARAM_DFLT => 10,
  225. ApiBase::PARAM_TYPE => 'limit',
  226. ApiBase::PARAM_MIN => 1,
  227. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  228. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  229. ],
  230. 'continue' => [
  231. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  232. ],
  233. ];
  234. }
  235. protected function getExamplesMessages() {
  236. return [
  237. 'action=query&prop=contributors&titles=Main_Page'
  238. => 'apihelp-query+contributors-example-simple',
  239. ];
  240. }
  241. public function getHelpUrls() {
  242. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Contributors';
  243. }
  244. }