ApiQueryAllCategories.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. * Created on December 12, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
  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. * Query module to enumerate all categories, even the ones that don't have
  30. * category pages.
  31. *
  32. * @ingroup API
  33. */
  34. class ApiQueryAllCategories extends ApiQueryGeneratorBase {
  35. public function __construct($query, $moduleName) {
  36. parent :: __construct($query, $moduleName, 'ac');
  37. }
  38. public function execute() {
  39. $this->run();
  40. }
  41. public function executeGenerator($resultPageSet) {
  42. $this->run($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. $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
  50. $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
  51. $this->addWhereRange('cat_title', $dir, $from, null);
  52. if (isset ($params['prefix']))
  53. $this->addWhere("cat_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
  54. $this->addOption('LIMIT', $params['limit']+1);
  55. $this->addOption('ORDER BY', 'cat_title' . ($params['dir'] == 'descending' ? ' DESC' : ''));
  56. $prop = array_flip($params['prop']);
  57. $this->addFieldsIf( array( 'cat_pages', 'cat_subcats', 'cat_files' ), isset($prop['size']) );
  58. if(isset($prop['hidden']))
  59. {
  60. $this->addTables(array('page', 'page_props'));
  61. $this->addJoinConds(array(
  62. 'page' => array('LEFT JOIN', array(
  63. 'page_namespace' => NS_CATEGORY,
  64. 'page_title=cat_title')),
  65. 'page_props' => array('LEFT JOIN', array(
  66. 'pp_page=page_id',
  67. 'pp_propname' => 'hiddencat')),
  68. ));
  69. $this->addFields('pp_propname AS cat_hidden');
  70. }
  71. $res = $this->select(__METHOD__);
  72. $pages = array();
  73. $categories = array();
  74. $result = $this->getResult();
  75. $count = 0;
  76. while ($row = $db->fetchObject($res)) {
  77. if (++ $count > $params['limit']) {
  78. // We've reached the one extra which shows that there are additional cats to be had. Stop here...
  79. // TODO: Security issue - if the user has no right to view next title, it will still be shown
  80. $this->setContinueEnumParameter('from', $this->keyToTitle($row->cat_title));
  81. break;
  82. }
  83. // Normalize titles
  84. $titleObj = Title::makeTitle(NS_CATEGORY, $row->cat_title);
  85. if(!is_null($resultPageSet))
  86. $pages[] = $titleObj->getPrefixedText();
  87. else {
  88. $item = array();
  89. $result->setContent( $item, $titleObj->getText() );
  90. if( isset( $prop['size'] ) ) {
  91. $item['size'] = intval($row->cat_pages);
  92. $item['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
  93. $item['files'] = intval($row->cat_files);
  94. $item['subcats'] = intval($row->cat_subcats);
  95. }
  96. if( isset( $prop['hidden'] ) && $row->cat_hidden )
  97. $item['hidden'] = '';
  98. $fit = $result->addValue(array('query', $this->getModuleName()), null, $item);
  99. if(!$fit)
  100. {
  101. $this->setContinueEnumParameter('from', $this->keyToTitle($row->cat_title));
  102. break;
  103. }
  104. }
  105. }
  106. $db->freeResult($res);
  107. if (is_null($resultPageSet)) {
  108. $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'c');
  109. } else {
  110. $resultPageSet->populateFromTitles($pages);
  111. }
  112. }
  113. public function getAllowedParams() {
  114. return array (
  115. 'from' => null,
  116. 'prefix' => null,
  117. 'dir' => array(
  118. ApiBase :: PARAM_DFLT => 'ascending',
  119. ApiBase :: PARAM_TYPE => array(
  120. 'ascending',
  121. 'descending'
  122. ),
  123. ),
  124. 'limit' => array (
  125. ApiBase :: PARAM_DFLT => 10,
  126. ApiBase :: PARAM_TYPE => 'limit',
  127. ApiBase :: PARAM_MIN => 1,
  128. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  129. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  130. ),
  131. 'prop' => array (
  132. ApiBase :: PARAM_TYPE => array( 'size', 'hidden' ),
  133. ApiBase :: PARAM_DFLT => '',
  134. ApiBase :: PARAM_ISMULTI => true
  135. ),
  136. );
  137. }
  138. public function getParamDescription() {
  139. return array (
  140. 'from' => 'The category to start enumerating from.',
  141. 'prefix' => 'Search for all category titles that begin with this value.',
  142. 'dir' => 'Direction to sort in.',
  143. 'limit' => 'How many categories to return.',
  144. 'prop' => 'Which properties to get',
  145. );
  146. }
  147. public function getDescription() {
  148. return 'Enumerate all categories';
  149. }
  150. protected function getExamples() {
  151. return array (
  152. 'api.php?action=query&list=allcategories&acprop=size',
  153. 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
  154. );
  155. }
  156. public function getVersion() {
  157. return __CLASS__ . ': $Id: ApiQueryAllCategories.php 47865 2009-02-27 16:03:01Z catrope $';
  158. }
  159. }