ApiQueryAllCategories.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Query module to enumerate all categories, even the ones that don't have
  24. * category pages.
  25. *
  26. * @ingroup API
  27. */
  28. class ApiQueryAllCategories extends ApiQueryGeneratorBase {
  29. public function __construct( ApiQuery $query, $moduleName ) {
  30. parent::__construct( $query, $moduleName, 'ac' );
  31. }
  32. public function execute() {
  33. $this->run();
  34. }
  35. public function getCacheMode( $params ) {
  36. return 'public';
  37. }
  38. public function executeGenerator( $resultPageSet ) {
  39. $this->run( $resultPageSet );
  40. }
  41. /**
  42. * @param ApiPageSet $resultPageSet
  43. */
  44. private function run( $resultPageSet = null ) {
  45. $db = $this->getDB();
  46. $params = $this->extractRequestParams();
  47. $this->addTables( 'category' );
  48. $this->addFields( 'cat_title' );
  49. if ( !is_null( $params['continue'] ) ) {
  50. $cont = explode( '|', $params['continue'] );
  51. $this->dieContinueUsageIf( count( $cont ) != 1 );
  52. $op = $params['dir'] == 'descending' ? '<' : '>';
  53. $cont_from = $db->addQuotes( $cont[0] );
  54. $this->addWhere( "cat_title $op= $cont_from" );
  55. }
  56. $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
  57. $from = ( $params['from'] === null
  58. ? null
  59. : $this->titlePartToKey( $params['from'], NS_CATEGORY ) );
  60. $to = ( $params['to'] === null
  61. ? null
  62. : $this->titlePartToKey( $params['to'], NS_CATEGORY ) );
  63. $this->addWhereRange( 'cat_title', $dir, $from, $to );
  64. $min = $params['min'];
  65. $max = $params['max'];
  66. if ( $dir == 'newer' ) {
  67. $this->addWhereRange( 'cat_pages', 'newer', $min, $max );
  68. } else {
  69. $this->addWhereRange( 'cat_pages', 'older', $max, $min );
  70. }
  71. if ( isset( $params['prefix'] ) ) {
  72. $this->addWhere( 'cat_title' . $db->buildLike(
  73. $this->titlePartToKey( $params['prefix'], NS_CATEGORY ),
  74. $db->anyString() ) );
  75. }
  76. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  77. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  78. $this->addOption( 'ORDER BY', 'cat_title' . $sort );
  79. $prop = array_flip( $params['prop'] );
  80. $this->addFieldsIf( [ 'cat_pages', 'cat_subcats', 'cat_files' ], isset( $prop['size'] ) );
  81. if ( isset( $prop['hidden'] ) ) {
  82. $this->addTables( [ 'page', 'page_props' ] );
  83. $this->addJoinConds( [
  84. 'page' => [ 'LEFT JOIN', [
  85. 'page_namespace' => NS_CATEGORY,
  86. 'page_title=cat_title' ] ],
  87. 'page_props' => [ 'LEFT JOIN', [
  88. 'pp_page=page_id',
  89. 'pp_propname' => 'hiddencat' ] ],
  90. ] );
  91. $this->addFields( [ 'cat_hidden' => 'pp_propname' ] );
  92. }
  93. $res = $this->select( __METHOD__ );
  94. $pages = [];
  95. $result = $this->getResult();
  96. $count = 0;
  97. foreach ( $res as $row ) {
  98. if ( ++$count > $params['limit'] ) {
  99. // We've reached the one extra which shows that there are
  100. // additional cats to be had. Stop here...
  101. $this->setContinueEnumParameter( 'continue', $row->cat_title );
  102. break;
  103. }
  104. // Normalize titles
  105. $titleObj = Title::makeTitle( NS_CATEGORY, $row->cat_title );
  106. if ( !is_null( $resultPageSet ) ) {
  107. $pages[] = $titleObj;
  108. } else {
  109. $item = [];
  110. ApiResult::setContentValue( $item, 'category', $titleObj->getText() );
  111. if ( isset( $prop['size'] ) ) {
  112. $item['size'] = (int)$row->cat_pages;
  113. $item['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
  114. $item['files'] = (int)$row->cat_files;
  115. $item['subcats'] = (int)$row->cat_subcats;
  116. }
  117. if ( isset( $prop['hidden'] ) ) {
  118. $item['hidden'] = (bool)$row->cat_hidden;
  119. }
  120. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $item );
  121. if ( !$fit ) {
  122. $this->setContinueEnumParameter( 'continue', $row->cat_title );
  123. break;
  124. }
  125. }
  126. }
  127. if ( is_null( $resultPageSet ) ) {
  128. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'c' );
  129. } else {
  130. $resultPageSet->populateFromTitles( $pages );
  131. }
  132. }
  133. public function getAllowedParams() {
  134. return [
  135. 'from' => null,
  136. 'continue' => [
  137. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  138. ],
  139. 'to' => null,
  140. 'prefix' => null,
  141. 'dir' => [
  142. ApiBase::PARAM_DFLT => 'ascending',
  143. ApiBase::PARAM_TYPE => [
  144. 'ascending',
  145. 'descending'
  146. ],
  147. ],
  148. 'min' => [
  149. ApiBase::PARAM_TYPE => 'integer'
  150. ],
  151. 'max' => [
  152. ApiBase::PARAM_TYPE => 'integer'
  153. ],
  154. 'limit' => [
  155. ApiBase::PARAM_DFLT => 10,
  156. ApiBase::PARAM_TYPE => 'limit',
  157. ApiBase::PARAM_MIN => 1,
  158. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  159. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  160. ],
  161. 'prop' => [
  162. ApiBase::PARAM_TYPE => [ 'size', 'hidden' ],
  163. ApiBase::PARAM_DFLT => '',
  164. ApiBase::PARAM_ISMULTI => true,
  165. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  166. ],
  167. ];
  168. }
  169. protected function getExamplesMessages() {
  170. return [
  171. 'action=query&list=allcategories&acprop=size'
  172. => 'apihelp-query+allcategories-example-size',
  173. 'action=query&generator=allcategories&gacprefix=List&prop=info'
  174. => 'apihelp-query+allcategories-example-generator',
  175. ];
  176. }
  177. public function getHelpUrls() {
  178. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allcategories';
  179. }
  180. }