ApiQueryCategoryInfo.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * Created on May 13, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ("ApiQueryBase.php");
  27. }
  28. /**
  29. * This query adds the <categories> subelement to all pages with the list of categories the page is in
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryCategoryInfo extends ApiQueryBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'ci');
  36. }
  37. public function execute() {
  38. $params = $this->extractRequestParams();
  39. $alltitles = $this->getPageSet()->getAllTitlesByNamespace();
  40. if ( empty( $alltitles[NS_CATEGORY] ) ) {
  41. return;
  42. }
  43. $categories = $alltitles[NS_CATEGORY];
  44. $titles = $this->getPageSet()->getGoodTitles() +
  45. $this->getPageSet()->getMissingTitles();
  46. $cattitles = array();
  47. foreach($categories as $c)
  48. {
  49. $t = $titles[$c];
  50. $cattitles[$c] = $t->getDBKey();
  51. }
  52. $this->addTables(array('category', 'page', 'page_props'));
  53. $this->addJoinConds(array(
  54. 'page' => array('LEFT JOIN', array(
  55. 'page_namespace' => NS_CATEGORY,
  56. 'page_title=cat_title')),
  57. 'page_props' => array('LEFT JOIN', array(
  58. 'pp_page=page_id',
  59. 'pp_propname' => 'hiddencat')),
  60. ));
  61. $this->addFields(array('cat_title', 'cat_pages', 'cat_subcats', 'cat_files', 'pp_propname AS cat_hidden'));
  62. $this->addWhere(array('cat_title' => $cattitles));
  63. if(!is_null($params['continue']))
  64. {
  65. $title = $this->getDB()->addQuotes($params['continue']);
  66. $this->addWhere("cat_title >= $title");
  67. }
  68. $this->addOption('ORDER BY', 'cat_title');
  69. $db = $this->getDB();
  70. $res = $this->select(__METHOD__);
  71. $catids = array_flip($cattitles);
  72. while($row = $db->fetchObject($res))
  73. {
  74. $vals = array();
  75. $vals['size'] = intval($row->cat_pages);
  76. $vals['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
  77. $vals['files'] = intval($row->cat_files);
  78. $vals['subcats'] = intval($row->cat_subcats);
  79. if($row->cat_hidden)
  80. $vals['hidden'] = '';
  81. $fit = $this->addPageSubItems($catids[$row->cat_title], $vals);
  82. if(!$fit)
  83. {
  84. $this->setContinueEnumParameter('continue', $row->cat_title);
  85. break;
  86. }
  87. }
  88. $db->freeResult($res);
  89. }
  90. public function getAllowedParams() {
  91. return array (
  92. 'continue' => null,
  93. );
  94. }
  95. public function getParamDescription() {
  96. return array (
  97. 'continue' => 'When more results are available, use this to continue',
  98. );
  99. }
  100. public function getDescription() {
  101. return 'Returns information about the given categories';
  102. }
  103. protected function getExamples() {
  104. return "api.php?action=query&prop=categoryinfo&titles=Category:Foo|Category:Bar";
  105. }
  106. public function getVersion() {
  107. return __CLASS__ . ': $Id: ApiQueryCategoryInfo.php 47865 2009-02-27 16:03:01Z catrope $';
  108. }
  109. }