ApiQueryCategories.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Copyright © 2006 Yuri Astrakhan "<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. * A query module to enumerate categories the set of pages belong to.
  24. *
  25. * @ingroup API
  26. */
  27. class ApiQueryCategories extends ApiQueryGeneratorBase {
  28. public function __construct( ApiQuery $query, $moduleName ) {
  29. parent::__construct( $query, $moduleName, 'cl' );
  30. }
  31. public function execute() {
  32. $this->run();
  33. }
  34. public function getCacheMode( $params ) {
  35. return 'public';
  36. }
  37. public function executeGenerator( $resultPageSet ) {
  38. $this->run( $resultPageSet );
  39. }
  40. /**
  41. * @param ApiPageSet $resultPageSet
  42. */
  43. private function run( $resultPageSet = null ) {
  44. if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  45. return; // nothing to do
  46. }
  47. $params = $this->extractRequestParams();
  48. $prop = array_flip( (array)$params['prop'] );
  49. $show = array_flip( (array)$params['show'] );
  50. $this->addFields( [
  51. 'cl_from',
  52. 'cl_to'
  53. ] );
  54. $this->addFieldsIf( [ 'cl_sortkey', 'cl_sortkey_prefix' ], isset( $prop['sortkey'] ) );
  55. $this->addFieldsIf( 'cl_timestamp', isset( $prop['timestamp'] ) );
  56. $this->addTables( 'categorylinks' );
  57. $this->addWhereFld( 'cl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  58. if ( $params['categories'] ) {
  59. $cats = [];
  60. foreach ( $params['categories'] as $cat ) {
  61. $title = Title::newFromText( $cat );
  62. if ( !$title || $title->getNamespace() != NS_CATEGORY ) {
  63. $this->addWarning( [ 'apiwarn-invalidcategory', wfEscapeWikiText( $cat ) ] );
  64. } else {
  65. $cats[] = $title->getDBkey();
  66. }
  67. }
  68. if ( !$cats ) {
  69. // No titles so no results
  70. return;
  71. }
  72. $this->addWhereFld( 'cl_to', $cats );
  73. }
  74. if ( !is_null( $params['continue'] ) ) {
  75. $cont = explode( '|', $params['continue'] );
  76. $this->dieContinueUsageIf( count( $cont ) != 2 );
  77. $op = $params['dir'] == 'descending' ? '<' : '>';
  78. $clfrom = (int)$cont[0];
  79. $clto = $this->getDB()->addQuotes( $cont[1] );
  80. $this->addWhere(
  81. "cl_from $op $clfrom OR " .
  82. "(cl_from = $clfrom AND " .
  83. "cl_to $op= $clto)"
  84. );
  85. }
  86. if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) {
  87. $this->dieWithError( 'apierror-show' );
  88. }
  89. if ( isset( $show['hidden'] ) || isset( $show['!hidden'] ) || isset( $prop['hidden'] ) ) {
  90. $this->addOption( 'STRAIGHT_JOIN' );
  91. $this->addTables( [ 'page', 'page_props' ] );
  92. $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) );
  93. $this->addJoinConds( [
  94. 'page' => [ 'LEFT JOIN', [
  95. 'page_namespace' => NS_CATEGORY,
  96. 'page_title = cl_to' ] ],
  97. 'page_props' => [ 'LEFT JOIN', [
  98. 'pp_page=page_id',
  99. 'pp_propname' => 'hiddencat' ] ]
  100. ] );
  101. if ( isset( $show['hidden'] ) ) {
  102. $this->addWhere( [ 'pp_propname IS NOT NULL' ] );
  103. } elseif ( isset( $show['!hidden'] ) ) {
  104. $this->addWhere( [ 'pp_propname IS NULL' ] );
  105. }
  106. }
  107. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  108. // Don't order by cl_from if it's constant in the WHERE clause
  109. if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
  110. $this->addOption( 'ORDER BY', 'cl_to' . $sort );
  111. } else {
  112. $this->addOption( 'ORDER BY', [
  113. 'cl_from' . $sort,
  114. 'cl_to' . $sort
  115. ] );
  116. }
  117. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  118. $res = $this->select( __METHOD__ );
  119. $count = 0;
  120. if ( is_null( $resultPageSet ) ) {
  121. foreach ( $res as $row ) {
  122. if ( ++$count > $params['limit'] ) {
  123. // We've reached the one extra which shows that
  124. // there are additional pages to be had. Stop here...
  125. $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
  126. break;
  127. }
  128. $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
  129. $vals = [];
  130. ApiQueryBase::addTitleInfo( $vals, $title );
  131. if ( isset( $prop['sortkey'] ) ) {
  132. $vals['sortkey'] = bin2hex( $row->cl_sortkey );
  133. $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
  134. }
  135. if ( isset( $prop['timestamp'] ) ) {
  136. $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
  137. }
  138. if ( isset( $prop['hidden'] ) ) {
  139. $vals['hidden'] = !is_null( $row->pp_propname );
  140. }
  141. $fit = $this->addPageSubItem( $row->cl_from, $vals );
  142. if ( !$fit ) {
  143. $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
  144. break;
  145. }
  146. }
  147. } else {
  148. $titles = [];
  149. foreach ( $res as $row ) {
  150. if ( ++$count > $params['limit'] ) {
  151. // We've reached the one extra which shows that
  152. // there are additional pages to be had. Stop here...
  153. $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
  154. break;
  155. }
  156. $titles[] = Title::makeTitle( NS_CATEGORY, $row->cl_to );
  157. }
  158. $resultPageSet->populateFromTitles( $titles );
  159. }
  160. }
  161. public function getAllowedParams() {
  162. return [
  163. 'prop' => [
  164. ApiBase::PARAM_ISMULTI => true,
  165. ApiBase::PARAM_TYPE => [
  166. 'sortkey',
  167. 'timestamp',
  168. 'hidden',
  169. ],
  170. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  171. ],
  172. 'show' => [
  173. ApiBase::PARAM_ISMULTI => true,
  174. ApiBase::PARAM_TYPE => [
  175. 'hidden',
  176. '!hidden',
  177. ]
  178. ],
  179. 'limit' => [
  180. ApiBase::PARAM_DFLT => 10,
  181. ApiBase::PARAM_TYPE => 'limit',
  182. ApiBase::PARAM_MIN => 1,
  183. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  184. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  185. ],
  186. 'continue' => [
  187. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  188. ],
  189. 'categories' => [
  190. ApiBase::PARAM_ISMULTI => true,
  191. ],
  192. 'dir' => [
  193. ApiBase::PARAM_DFLT => 'ascending',
  194. ApiBase::PARAM_TYPE => [
  195. 'ascending',
  196. 'descending'
  197. ]
  198. ],
  199. ];
  200. }
  201. protected function getExamplesMessages() {
  202. return [
  203. 'action=query&prop=categories&titles=Albert%20Einstein'
  204. => 'apihelp-query+categories-example-simple',
  205. 'action=query&generator=categories&titles=Albert%20Einstein&prop=info'
  206. => 'apihelp-query+categories-example-generator',
  207. ];
  208. }
  209. public function getHelpUrls() {
  210. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categories';
  211. }
  212. }