ApiQueryCategories.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * A query module to enumerate categories the set of pages belong to.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryCategories extends ApiQueryGeneratorBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'cl');
  36. }
  37. public function execute() {
  38. $this->run();
  39. }
  40. public function executeGenerator($resultPageSet) {
  41. $this->run($resultPageSet);
  42. }
  43. private function run($resultPageSet = null) {
  44. if ($this->getPageSet()->getGoodTitleCount() == 0)
  45. return; // nothing to do
  46. $params = $this->extractRequestParams();
  47. $prop = $params['prop'];
  48. $show = array_flip((array)$params['show']);
  49. $this->addFields(array (
  50. 'cl_from',
  51. 'cl_to'
  52. ));
  53. $fld_sortkey = $fld_timestamp = false;
  54. if (!is_null($prop)) {
  55. foreach($prop as $p) {
  56. switch ($p) {
  57. case 'sortkey':
  58. $this->addFields('cl_sortkey');
  59. $fld_sortkey = true;
  60. break;
  61. case 'timestamp':
  62. $this->addFields('cl_timestamp');
  63. $fld_timestamp = true;
  64. break;
  65. default :
  66. ApiBase :: dieDebug(__METHOD__, "Unknown prop=$p");
  67. }
  68. }
  69. }
  70. $this->addTables('categorylinks');
  71. $this->addWhereFld('cl_from', array_keys($this->getPageSet()->getGoodTitles()));
  72. if(!is_null($params['categories']))
  73. {
  74. $cats = array();
  75. foreach($params['categories'] as $cat)
  76. {
  77. $title = Title::newFromText($cat);
  78. if(!$title || $title->getNamespace() != NS_CATEGORY)
  79. $this->setWarning("``$cat'' is not a category");
  80. else
  81. $cats[] = $title->getDBkey();
  82. }
  83. $this->addWhereFld('cl_to', $cats);
  84. }
  85. if(!is_null($params['continue'])) {
  86. $cont = explode('|', $params['continue']);
  87. if(count($cont) != 2)
  88. $this->dieUsage("Invalid continue param. You should pass the " .
  89. "original value returned by the previous query", "_badcontinue");
  90. $clfrom = intval($cont[0]);
  91. $clto = $this->getDB()->strencode($this->titleToKey($cont[1]));
  92. $this->addWhere("cl_from > $clfrom OR ".
  93. "(cl_from = $clfrom AND ".
  94. "cl_to >= '$clto')");
  95. }
  96. if(isset($show['hidden']) && isset($show['!hidden']))
  97. $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
  98. if(isset($show['hidden']) || isset($show['!hidden']))
  99. {
  100. $this->addOption('STRAIGHT_JOIN');
  101. $this->addTables(array('page', 'page_props'));
  102. $this->addJoinConds(array(
  103. 'page' => array('LEFT JOIN', array(
  104. 'page_namespace' => NS_CATEGORY,
  105. 'page_title = cl_to')),
  106. 'page_props' => array('LEFT JOIN', array(
  107. 'pp_page=page_id',
  108. 'pp_propname' => 'hiddencat'))
  109. ));
  110. if(isset($show['hidden']))
  111. $this->addWhere(array('pp_propname IS NOT NULL'));
  112. else
  113. $this->addWhere(array('pp_propname IS NULL'));
  114. }
  115. $this->addOption('USE INDEX', array('categorylinks' => 'cl_from'));
  116. # Don't order by cl_from if it's constant in the WHERE clause
  117. if(count($this->getPageSet()->getGoodTitles()) == 1)
  118. $this->addOption('ORDER BY', 'cl_to');
  119. else
  120. $this->addOption('ORDER BY', "cl_from, cl_to");
  121. $db = $this->getDB();
  122. $res = $this->select(__METHOD__);
  123. if (is_null($resultPageSet)) {
  124. $count = 0;
  125. while ($row = $db->fetchObject($res)) {
  126. if (++$count > $params['limit']) {
  127. // We've reached the one extra which shows that
  128. // there are additional pages to be had. Stop here...
  129. $this->setContinueEnumParameter('continue', $row->cl_from .
  130. '|' . $this->keyToTitle($row->cl_to));
  131. break;
  132. }
  133. $title = Title :: makeTitle(NS_CATEGORY, $row->cl_to);
  134. $vals = array();
  135. ApiQueryBase :: addTitleInfo($vals, $title);
  136. if ($fld_sortkey)
  137. $vals['sortkey'] = $row->cl_sortkey;
  138. if ($fld_timestamp)
  139. $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->cl_timestamp);
  140. $fit = $this->addPageSubItem($row->cl_from, $vals);
  141. if(!$fit)
  142. {
  143. $this->setContinueEnumParameter('continue', $row->cl_from .
  144. '|' . $this->keyToTitle($row->cl_to));
  145. break;
  146. }
  147. }
  148. } else {
  149. $titles = array();
  150. while ($row = $db->fetchObject($res)) {
  151. if (++$count > $params['limit']) {
  152. // We've reached the one extra which shows that
  153. // there are additional pages to be had. Stop here...
  154. $this->setContinueEnumParameter('continue', $row->cl_from .
  155. '|' . $this->keyToTitle($row->cl_to));
  156. break;
  157. }
  158. $titles[] = Title :: makeTitle(NS_CATEGORY, $row->cl_to);
  159. }
  160. $resultPageSet->populateFromTitles($titles);
  161. }
  162. $db->freeResult($res);
  163. }
  164. public function getAllowedParams() {
  165. return array (
  166. 'prop' => array (
  167. ApiBase :: PARAM_ISMULTI => true,
  168. ApiBase :: PARAM_TYPE => array (
  169. 'sortkey',
  170. 'timestamp',
  171. )
  172. ),
  173. 'show' => array(
  174. ApiBase :: PARAM_ISMULTI => true,
  175. ApiBase :: PARAM_TYPE => array(
  176. 'hidden',
  177. '!hidden',
  178. )
  179. ),
  180. 'limit' => array(
  181. ApiBase :: PARAM_DFLT => 10,
  182. ApiBase :: PARAM_TYPE => 'limit',
  183. ApiBase :: PARAM_MIN => 1,
  184. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  185. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  186. ),
  187. 'continue' => null,
  188. 'categories' => array(
  189. ApiBase :: PARAM_ISMULTI => true,
  190. ),
  191. );
  192. }
  193. public function getParamDescription() {
  194. return array (
  195. 'prop' => 'Which additional properties to get for each category.',
  196. 'limit' => 'How many categories to return',
  197. 'show' => 'Which kind of categories to show',
  198. 'continue' => 'When more results are available, use this to continue',
  199. 'categories' => 'Only list these categories. Useful for checking whether a certain page is in a certain category',
  200. );
  201. }
  202. public function getDescription() {
  203. return 'List all categories the page(s) belong to';
  204. }
  205. protected function getExamples() {
  206. return array (
  207. "Get a list of categories [[Albert Einstein]] belongs to:",
  208. " api.php?action=query&prop=categories&titles=Albert%20Einstein",
  209. "Get information about all categories used in the [[Albert Einstein]]:",
  210. " api.php?action=query&generator=categories&titles=Albert%20Einstein&prop=info"
  211. );
  212. }
  213. public function getVersion() {
  214. return __CLASS__ . ': $Id: ApiQueryCategories.php 50097 2009-05-01 06:35:57Z tstarling $';
  215. }
  216. }