ApiQueryCategoryMembers.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /*
  3. * Created on June 14, 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 pages that belong to a category.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'cm');
  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. $params = $this->extractRequestParams();
  45. if ( !isset($params['title']) || is_null($params['title']) )
  46. $this->dieUsage("The cmtitle parameter is required", 'notitle');
  47. $categoryTitle = Title::newFromText($params['title']);
  48. if ( is_null( $categoryTitle ) || $categoryTitle->getNamespace() != NS_CATEGORY )
  49. $this->dieUsage("The category name you entered is not valid", 'invalidcategory');
  50. $prop = array_flip($params['prop']);
  51. $fld_ids = isset($prop['ids']);
  52. $fld_title = isset($prop['title']);
  53. $fld_sortkey = isset($prop['sortkey']);
  54. $fld_timestamp = isset($prop['timestamp']);
  55. if (is_null($resultPageSet)) {
  56. $this->addFields(array('cl_from', 'cl_sortkey', 'page_namespace', 'page_title'));
  57. $this->addFieldsIf('page_id', $fld_ids);
  58. } else {
  59. $this->addFields($resultPageSet->getPageTableFields()); // will include page_ id, ns, title
  60. $this->addFields(array('cl_from', 'cl_sortkey'));
  61. }
  62. $this->addFieldsIf('cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp');
  63. $this->addTables(array('page','categorylinks')); // must be in this order for 'USE INDEX'
  64. // Not needed after bug 10280 is applied to servers
  65. if($params['sort'] == 'timestamp')
  66. $this->addOption('USE INDEX', 'cl_timestamp');
  67. else
  68. $this->addOption('USE INDEX', 'cl_sortkey');
  69. $this->addWhere('cl_from=page_id');
  70. $this->setContinuation($params['continue'], $params['dir']);
  71. $this->addWhereFld('cl_to', $categoryTitle->getDBkey());
  72. $this->addWhereFld('page_namespace', $params['namespace']);
  73. if($params['sort'] == 'timestamp')
  74. $this->addWhereRange('cl_timestamp', ($params['dir'] == 'asc' ? 'newer' : 'older'), $params['start'], $params['end']);
  75. else
  76. {
  77. $this->addWhereRange('cl_sortkey', ($params['dir'] == 'asc' ? 'newer' : 'older'), $params['startsortkey'], $params['endsortkey']);
  78. $this->addWhereRange('cl_from', ($params['dir'] == 'asc' ? 'newer' : 'older'), null, null);
  79. }
  80. $limit = $params['limit'];
  81. $this->addOption('LIMIT', $limit +1);
  82. $db = $this->getDB();
  83. $data = array ();
  84. $count = 0;
  85. $lastSortKey = null;
  86. $res = $this->select(__METHOD__);
  87. while ($row = $db->fetchObject($res)) {
  88. if (++ $count > $limit) {
  89. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  90. // TODO: Security issue - if the user has no right to view next title, it will still be shown
  91. if ($params['sort'] == 'timestamp')
  92. $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->cl_timestamp));
  93. else
  94. $this->setContinueEnumParameter('continue', $this->getContinueStr($row, $lastSortKey));
  95. break;
  96. }
  97. if (is_null($resultPageSet)) {
  98. $vals = array();
  99. if ($fld_ids)
  100. $vals['pageid'] = intval($row->page_id);
  101. if ($fld_title) {
  102. $title = Title :: makeTitle($row->page_namespace, $row->page_title);
  103. ApiQueryBase::addTitleInfo($vals, $title);
  104. }
  105. if ($fld_sortkey)
  106. $vals['sortkey'] = $row->cl_sortkey;
  107. if ($fld_timestamp)
  108. $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->cl_timestamp);
  109. $fit = $this->getResult()->addValue(array('query', $this->getModuleName()),
  110. null, $vals);
  111. if(!$fit)
  112. {
  113. if ($params['sort'] == 'timestamp')
  114. $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->cl_timestamp));
  115. else
  116. $this->setContinueEnumParameter('continue', $this->getContinueStr($row, $lastSortKey));
  117. break;
  118. }
  119. } else {
  120. $resultPageSet->processDbRow($row);
  121. }
  122. $lastSortKey = $row->cl_sortkey; // detect duplicate sortkeys
  123. }
  124. $db->freeResult($res);
  125. if (is_null($resultPageSet)) {
  126. $this->getResult()->setIndexedTagName_internal(
  127. array('query', $this->getModuleName()), 'cm');
  128. }
  129. }
  130. private function getContinueStr($row, $lastSortKey) {
  131. $ret = $row->cl_sortkey . '|';
  132. if ($row->cl_sortkey == $lastSortKey) // duplicate sort key, add cl_from
  133. $ret .= $row->cl_from;
  134. return $ret;
  135. }
  136. /**
  137. * Add DB WHERE clause to continue previous query based on 'continue' parameter
  138. */
  139. private function setContinuation($continue, $dir) {
  140. if (is_null($continue))
  141. return; // This is not a continuation request
  142. $pos = strrpos($continue, '|');
  143. $sortkey = substr($continue, 0, $pos);
  144. $fromstr = substr($continue, $pos + 1);
  145. $from = intval($fromstr);
  146. if ($from == 0 && strlen($fromstr) > 0)
  147. $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
  148. $encSortKey = $this->getDB()->addQuotes($sortkey);
  149. $encFrom = $this->getDB()->addQuotes($from);
  150. $op = ($dir == 'desc' ? '<' : '>');
  151. if ($from != 0) {
  152. // Duplicate sort key continue
  153. $this->addWhere( "cl_sortkey$op$encSortKey OR (cl_sortkey=$encSortKey AND cl_from$op=$encFrom)" );
  154. } else {
  155. $this->addWhere( "cl_sortkey$op=$encSortKey" );
  156. }
  157. }
  158. public function getAllowedParams() {
  159. return array (
  160. 'title' => null,
  161. 'prop' => array (
  162. ApiBase :: PARAM_DFLT => 'ids|title',
  163. ApiBase :: PARAM_ISMULTI => true,
  164. ApiBase :: PARAM_TYPE => array (
  165. 'ids',
  166. 'title',
  167. 'sortkey',
  168. 'timestamp',
  169. )
  170. ),
  171. 'namespace' => array (
  172. ApiBase :: PARAM_ISMULTI => true,
  173. ApiBase :: PARAM_TYPE => 'namespace',
  174. ),
  175. 'continue' => null,
  176. 'limit' => array (
  177. ApiBase :: PARAM_TYPE => 'limit',
  178. ApiBase :: PARAM_DFLT => 10,
  179. ApiBase :: PARAM_MIN => 1,
  180. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  181. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  182. ),
  183. 'sort' => array(
  184. ApiBase :: PARAM_DFLT => 'sortkey',
  185. ApiBase :: PARAM_TYPE => array(
  186. 'sortkey',
  187. 'timestamp'
  188. )
  189. ),
  190. 'dir' => array(
  191. ApiBase :: PARAM_DFLT => 'asc',
  192. ApiBase :: PARAM_TYPE => array(
  193. 'asc',
  194. 'desc'
  195. )
  196. ),
  197. 'start' => array(
  198. ApiBase :: PARAM_TYPE => 'timestamp'
  199. ),
  200. 'end' => array(
  201. ApiBase :: PARAM_TYPE => 'timestamp'
  202. ),
  203. 'startsortkey' => null,
  204. 'endsortkey' => null,
  205. );
  206. }
  207. public function getParamDescription() {
  208. return array (
  209. 'title' => 'Which category to enumerate (required). Must include Category: prefix',
  210. 'prop' => 'What pieces of information to include',
  211. 'namespace' => 'Only include pages in these namespaces',
  212. 'sort' => 'Property to sort by',
  213. 'dir' => 'In which direction to sort',
  214. 'start' => 'Timestamp to start listing from. Can only be used with cmsort=timestamp',
  215. 'end' => 'Timestamp to end listing at. Can only be used with cmsort=timestamp',
  216. 'startsortkey' => 'Sortkey to start listing from. Can only be used with cmsort=sortkey',
  217. 'endsortkey' => 'Sortkey to end listing at. Can only be used with cmsort=sortkey',
  218. 'continue' => 'For large categories, give the value retured from previous query',
  219. 'limit' => 'The maximum number of pages to return.',
  220. );
  221. }
  222. public function getDescription() {
  223. return 'List all pages in a given category';
  224. }
  225. protected function getExamples() {
  226. return array (
  227. "Get first 10 pages in [[Category:Physics]]:",
  228. " api.php?action=query&list=categorymembers&cmtitle=Category:Physics",
  229. "Get page info about first 10 pages in [[Category:Physics]]:",
  230. " api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info",
  231. );
  232. }
  233. public function getVersion() {
  234. return __CLASS__ . ': $Id: ApiQueryCategoryMembers.php 47865 2009-02-27 16:03:01Z catrope $';
  235. }
  236. }