ApiQueryCategoryInfo.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * This query adds the "<categories>" subelement to all pages with the list of
  24. * categories the page is in.
  25. *
  26. * @ingroup API
  27. */
  28. class ApiQueryCategoryInfo extends ApiQueryBase {
  29. public function __construct( ApiQuery $query, $moduleName ) {
  30. parent::__construct( $query, $moduleName, 'ci' );
  31. }
  32. public function execute() {
  33. $params = $this->extractRequestParams();
  34. $alltitles = $this->getPageSet()->getGoodAndMissingTitlesByNamespace();
  35. if ( empty( $alltitles[NS_CATEGORY] ) ) {
  36. return;
  37. }
  38. $categories = $alltitles[NS_CATEGORY];
  39. $titles = $this->getPageSet()->getGoodAndMissingTitles();
  40. $cattitles = [];
  41. foreach ( $categories as $c ) {
  42. /** @var Title $t */
  43. $t = $titles[$c];
  44. $cattitles[$c] = $t->getDBkey();
  45. }
  46. $this->addTables( [ 'category', 'page', 'page_props' ] );
  47. $this->addJoinConds( [
  48. 'page' => [ 'LEFT JOIN', [
  49. 'page_namespace' => NS_CATEGORY,
  50. 'page_title=cat_title' ] ],
  51. 'page_props' => [ 'LEFT JOIN', [
  52. 'pp_page=page_id',
  53. 'pp_propname' => 'hiddencat' ] ],
  54. ] );
  55. $this->addFields( [
  56. 'cat_title',
  57. 'cat_pages',
  58. 'cat_subcats',
  59. 'cat_files',
  60. 'cat_hidden' => 'pp_propname'
  61. ] );
  62. $this->addWhere( [ 'cat_title' => $cattitles ] );
  63. if ( !is_null( $params['continue'] ) ) {
  64. $title = $this->getDB()->addQuotes( $params['continue'] );
  65. $this->addWhere( "cat_title >= $title" );
  66. }
  67. $this->addOption( 'ORDER BY', 'cat_title' );
  68. $res = $this->select( __METHOD__ );
  69. $catids = array_flip( $cattitles );
  70. foreach ( $res as $row ) {
  71. $vals = [];
  72. $vals['size'] = (int)$row->cat_pages;
  73. $vals['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
  74. $vals['files'] = (int)$row->cat_files;
  75. $vals['subcats'] = (int)$row->cat_subcats;
  76. $vals['hidden'] = (bool)$row->cat_hidden;
  77. $fit = $this->addPageSubItems( $catids[$row->cat_title], $vals );
  78. if ( !$fit ) {
  79. $this->setContinueEnumParameter( 'continue', $row->cat_title );
  80. break;
  81. }
  82. }
  83. }
  84. public function getCacheMode( $params ) {
  85. return 'public';
  86. }
  87. public function getAllowedParams() {
  88. return [
  89. 'continue' => [
  90. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  91. ],
  92. ];
  93. }
  94. protected function getExamplesMessages() {
  95. return [
  96. 'action=query&prop=categoryinfo&titles=Category:Foo|Category:Bar'
  97. => 'apihelp-query+categoryinfo-example-simple',
  98. ];
  99. }
  100. public function getHelpUrls() {
  101. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categoryinfo';
  102. }
  103. }