ApiQueryAllpages.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /*
  3. * Created on Sep 25, 2006
  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. * Query module to enumerate all available pages.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryAllpages extends ApiQueryGeneratorBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'ap');
  36. }
  37. public function execute() {
  38. $this->run();
  39. }
  40. public function executeGenerator($resultPageSet) {
  41. if ($resultPageSet->isResolvingRedirects())
  42. $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
  43. $this->run($resultPageSet);
  44. }
  45. private function run($resultPageSet = null) {
  46. $db = $this->getDB();
  47. $params = $this->extractRequestParams();
  48. // Page filters
  49. $this->addTables('page');
  50. if (!$this->addWhereIf('page_is_redirect = 1', $params['filterredir'] === 'redirects'))
  51. $this->addWhereIf('page_is_redirect = 0', $params['filterredir'] === 'nonredirects');
  52. $this->addWhereFld('page_namespace', $params['namespace']);
  53. $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
  54. $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
  55. $this->addWhereRange('page_title', $dir, $from, null);
  56. if (isset ($params['prefix']))
  57. $this->addWhere("page_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
  58. if (is_null($resultPageSet)) {
  59. $selectFields = array (
  60. 'page_namespace',
  61. 'page_title',
  62. 'page_id'
  63. );
  64. } else {
  65. $selectFields = $resultPageSet->getPageTableFields();
  66. }
  67. $this->addFields($selectFields);
  68. $forceNameTitleIndex = true;
  69. if (isset ($params['minsize'])) {
  70. $this->addWhere('page_len>=' . intval($params['minsize']));
  71. $forceNameTitleIndex = false;
  72. }
  73. if (isset ($params['maxsize'])) {
  74. $this->addWhere('page_len<=' . intval($params['maxsize']));
  75. $forceNameTitleIndex = false;
  76. }
  77. // Page protection filtering
  78. if (!empty ($params['prtype'])) {
  79. $this->addTables('page_restrictions');
  80. $this->addWhere('page_id=pr_page');
  81. $this->addWhere('pr_expiry>' . $db->addQuotes($db->timestamp()));
  82. $this->addWhereFld('pr_type', $params['prtype']);
  83. // Remove the empty string and '*' from the prlevel array
  84. $prlevel = array_diff($params['prlevel'], array('', '*'));
  85. if (!empty($prlevel))
  86. $this->addWhereFld('pr_level', $prlevel);
  87. if ($params['prfiltercascade'] == 'cascading')
  88. $this->addWhereFld('pr_cascade', 1);
  89. if ($params['prfiltercascade'] == 'noncascading')
  90. $this->addWhereFld('pr_cascade', 0);
  91. $this->addOption('DISTINCT');
  92. $forceNameTitleIndex = false;
  93. } else if (isset ($params['prlevel'])) {
  94. $this->dieUsage('prlevel may not be used without prtype', 'params');
  95. }
  96. if($params['filterlanglinks'] == 'withoutlanglinks') {
  97. $this->addTables('langlinks');
  98. $this->addJoinConds(array('langlinks' => array('LEFT JOIN', 'page_id=ll_from')));
  99. $this->addWhere('ll_from IS NULL');
  100. $forceNameTitleIndex = false;
  101. } else if($params['filterlanglinks'] == 'withlanglinks') {
  102. $this->addTables('langlinks');
  103. $this->addWhere('page_id=ll_from');
  104. $this->addOption('STRAIGHT_JOIN');
  105. // We have to GROUP BY all selected fields to stop
  106. // PostgreSQL from whining
  107. $this->addOption('GROUP BY', implode(', ', $selectFields));
  108. $forceNameTitleIndex = false;
  109. }
  110. if ($forceNameTitleIndex)
  111. $this->addOption('USE INDEX', 'name_title');
  112. $limit = $params['limit'];
  113. $this->addOption('LIMIT', $limit+1);
  114. $res = $this->select(__METHOD__);
  115. $count = 0;
  116. $result = $this->getResult();
  117. while ($row = $db->fetchObject($res)) {
  118. if (++ $count > $limit) {
  119. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  120. // TODO: Security issue - if the user has no right to view next title, it will still be shown
  121. $this->setContinueEnumParameter('from', $this->keyToTitle($row->page_title));
  122. break;
  123. }
  124. if (is_null($resultPageSet)) {
  125. $title = Title :: makeTitle($row->page_namespace, $row->page_title);
  126. $vals = array(
  127. 'pageid' => intval($row->page_id),
  128. 'ns' => intval($title->getNamespace()),
  129. 'title' => $title->getPrefixedText());
  130. $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
  131. if(!$fit)
  132. {
  133. $this->setContinueEnumParameter('from', $this->keyToTitle($row->page_title));
  134. break;
  135. }
  136. } else {
  137. $resultPageSet->processDbRow($row);
  138. }
  139. }
  140. $db->freeResult($res);
  141. if (is_null($resultPageSet)) {
  142. $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
  143. }
  144. }
  145. public function getAllowedParams() {
  146. global $wgRestrictionTypes, $wgRestrictionLevels;
  147. return array (
  148. 'from' => null,
  149. 'prefix' => null,
  150. 'namespace' => array (
  151. ApiBase :: PARAM_DFLT => 0,
  152. ApiBase :: PARAM_TYPE => 'namespace',
  153. ),
  154. 'filterredir' => array (
  155. ApiBase :: PARAM_DFLT => 'all',
  156. ApiBase :: PARAM_TYPE => array (
  157. 'all',
  158. 'redirects',
  159. 'nonredirects'
  160. )
  161. ),
  162. 'minsize' => array (
  163. ApiBase :: PARAM_TYPE => 'integer',
  164. ),
  165. 'maxsize' => array (
  166. ApiBase :: PARAM_TYPE => 'integer',
  167. ),
  168. 'prtype' => array (
  169. ApiBase :: PARAM_TYPE => $wgRestrictionTypes,
  170. ApiBase :: PARAM_ISMULTI => true
  171. ),
  172. 'prlevel' => array (
  173. ApiBase :: PARAM_TYPE => $wgRestrictionLevels,
  174. ApiBase :: PARAM_ISMULTI => true
  175. ),
  176. 'prfiltercascade' => array (
  177. ApiBase :: PARAM_DFLT => 'all',
  178. ApiBase :: PARAM_TYPE => array (
  179. 'cascading',
  180. 'noncascading',
  181. 'all'
  182. ),
  183. ),
  184. 'limit' => array (
  185. ApiBase :: PARAM_DFLT => 10,
  186. ApiBase :: PARAM_TYPE => 'limit',
  187. ApiBase :: PARAM_MIN => 1,
  188. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  189. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  190. ),
  191. 'dir' => array (
  192. ApiBase :: PARAM_DFLT => 'ascending',
  193. ApiBase :: PARAM_TYPE => array (
  194. 'ascending',
  195. 'descending'
  196. )
  197. ),
  198. 'filterlanglinks' => array(
  199. ApiBase :: PARAM_TYPE => array(
  200. 'withlanglinks',
  201. 'withoutlanglinks',
  202. 'all'
  203. ),
  204. ApiBase :: PARAM_DFLT => 'all'
  205. )
  206. );
  207. }
  208. public function getParamDescription() {
  209. return array (
  210. 'from' => 'The page title to start enumerating from.',
  211. 'prefix' => 'Search for all page titles that begin with this value.',
  212. 'namespace' => 'The namespace to enumerate.',
  213. 'filterredir' => 'Which pages to list.',
  214. 'dir' => 'The direction in which to list',
  215. 'minsize' => 'Limit to pages with at least this many bytes',
  216. 'maxsize' => 'Limit to pages with at most this many bytes',
  217. 'prtype' => 'Limit to protected pages only',
  218. 'prlevel' => 'The protection level (must be used with apprtype= parameter)',
  219. 'prfiltercascade' => 'Filter protections based on cascadingness (ignored when apprtype isn\'t set)',
  220. 'filterlanglinks' => 'Filter based on whether a page has langlinks',
  221. 'limit' => 'How many total pages to return.'
  222. );
  223. }
  224. public function getDescription() {
  225. return 'Enumerate all pages sequentially in a given namespace';
  226. }
  227. protected function getExamples() {
  228. return array (
  229. 'Simple Use',
  230. ' Show a list of pages starting at the letter "B"',
  231. ' api.php?action=query&list=allpages&apfrom=B',
  232. 'Using as Generator',
  233. ' Show info about 4 pages starting at the letter "T"',
  234. ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
  235. ' Show content of first 2 non-redirect pages begining at "Re"',
  236. ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
  237. );
  238. }
  239. public function getVersion() {
  240. return __CLASS__ . ': $Id: ApiQueryAllpages.php 46845 2009-02-05 14:30:59Z catrope $';
  241. }
  242. }