PageRenderTime 43ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/api/ApiQueryAllpages.php

https://github.com/daevid/MWFork
PHP | 335 lines | 257 code | 33 blank | 45 comment | 33 complexity | e1bd7bb0f6eecbab01eddaa33044817d MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Sep 25, 2006
  6. *
  7. * Copyright © 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. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. if ( !defined( 'MEDIAWIKI' ) ) {
  27. // Eclipse helper - will be ignored in production
  28. require_once( 'ApiQueryBase.php' );
  29. }
  30. /**
  31. * Query module to enumerate all available pages.
  32. *
  33. * @ingroup API
  34. */
  35. class ApiQueryAllpages extends ApiQueryGeneratorBase {
  36. public function __construct( $query, $moduleName ) {
  37. parent::__construct( $query, $moduleName, 'ap' );
  38. }
  39. public function execute() {
  40. $this->run();
  41. }
  42. public function getCacheMode( $params ) {
  43. return 'public';
  44. }
  45. /**
  46. * @param $resultPageSet ApiPageSet
  47. * @return void
  48. */
  49. public function executeGenerator( $resultPageSet ) {
  50. if ( $resultPageSet->isResolvingRedirects() ) {
  51. $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
  52. }
  53. $this->run( $resultPageSet );
  54. }
  55. /**
  56. * @param $resultPageSet ApiPageSet
  57. * @return void
  58. */
  59. private function run( $resultPageSet = null ) {
  60. $db = $this->getDB();
  61. $params = $this->extractRequestParams();
  62. // Page filters
  63. $this->addTables( 'page' );
  64. if ( $params['filterredir'] == 'redirects' ) {
  65. $this->addWhereFld( 'page_is_redirect', 1 );
  66. } elseif ( $params['filterredir'] == 'nonredirects' ) {
  67. $this->addWhereFld( 'page_is_redirect', 0 );
  68. }
  69. $this->addWhereFld( 'page_namespace', $params['namespace'] );
  70. $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
  71. $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
  72. $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
  73. $this->addWhereRange( 'page_title', $dir, $from, $to );
  74. if ( isset( $params['prefix'] ) ) {
  75. $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
  76. }
  77. if ( is_null( $resultPageSet ) ) {
  78. $selectFields = array(
  79. 'page_namespace',
  80. 'page_title',
  81. 'page_id'
  82. );
  83. } else {
  84. $selectFields = $resultPageSet->getPageTableFields();
  85. }
  86. $this->addFields( $selectFields );
  87. $forceNameTitleIndex = true;
  88. if ( isset( $params['minsize'] ) ) {
  89. $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
  90. $forceNameTitleIndex = false;
  91. }
  92. if ( isset( $params['maxsize'] ) ) {
  93. $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
  94. $forceNameTitleIndex = false;
  95. }
  96. // Page protection filtering
  97. if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) {
  98. $this->addTables( 'page_restrictions' );
  99. $this->addWhere( 'page_id=pr_page' );
  100. $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
  101. if ( count( $params['prtype'] ) ) {
  102. $this->addWhereFld( 'pr_type', $params['prtype'] );
  103. if ( isset( $params['prlevel'] ) ) {
  104. // Remove the empty string and '*' from the prlevel array
  105. $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
  106. if ( count( $prlevel ) ) {
  107. $this->addWhereFld( 'pr_level', $prlevel );
  108. }
  109. }
  110. if ( $params['prfiltercascade'] == 'cascading' ) {
  111. $this->addWhereFld( 'pr_cascade', 1 );
  112. } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
  113. $this->addWhereFld( 'pr_cascade', 0 );
  114. }
  115. $this->addOption( 'DISTINCT' );
  116. }
  117. $forceNameTitleIndex = false;
  118. if ( $params['prexpiry'] == 'indefinite' ) {
  119. $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
  120. } elseif ( $params['prexpiry'] == 'definite' ) {
  121. $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
  122. }
  123. } elseif ( isset( $params['prlevel'] ) ) {
  124. $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
  125. }
  126. if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
  127. $this->addTables( 'langlinks' );
  128. $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
  129. $this->addWhere( 'll_from IS NULL' );
  130. $forceNameTitleIndex = false;
  131. } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
  132. $this->addTables( 'langlinks' );
  133. $this->addWhere( 'page_id=ll_from' );
  134. $this->addOption( 'STRAIGHT_JOIN' );
  135. // We have to GROUP BY all selected fields to stop
  136. // PostgreSQL from whining
  137. $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
  138. $forceNameTitleIndex = false;
  139. }
  140. if ( $forceNameTitleIndex ) {
  141. $this->addOption( 'USE INDEX', 'name_title' );
  142. }
  143. $limit = $params['limit'];
  144. $this->addOption( 'LIMIT', $limit + 1 );
  145. $res = $this->select( __METHOD__ );
  146. $count = 0;
  147. $result = $this->getResult();
  148. foreach ( $res as $row ) {
  149. if ( ++ $count > $limit ) {
  150. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  151. // TODO: Security issue - if the user has no right to view next title, it will still be shown
  152. $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
  153. break;
  154. }
  155. if ( is_null( $resultPageSet ) ) {
  156. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  157. $vals = array(
  158. 'pageid' => intval( $row->page_id ),
  159. 'ns' => intval( $title->getNamespace() ),
  160. 'title' => $title->getPrefixedText()
  161. );
  162. $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
  163. if ( !$fit ) {
  164. $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
  165. break;
  166. }
  167. } else {
  168. $resultPageSet->processDbRow( $row );
  169. }
  170. }
  171. if ( is_null( $resultPageSet ) ) {
  172. $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
  173. }
  174. }
  175. public function getAllowedParams() {
  176. global $wgRestrictionLevels;
  177. return array(
  178. 'from' => null,
  179. 'to' => null,
  180. 'prefix' => null,
  181. 'namespace' => array(
  182. ApiBase::PARAM_DFLT => 0,
  183. ApiBase::PARAM_TYPE => 'namespace',
  184. ),
  185. 'filterredir' => array(
  186. ApiBase::PARAM_DFLT => 'all',
  187. ApiBase::PARAM_TYPE => array(
  188. 'all',
  189. 'redirects',
  190. 'nonredirects'
  191. )
  192. ),
  193. 'minsize' => array(
  194. ApiBase::PARAM_TYPE => 'integer',
  195. ),
  196. 'maxsize' => array(
  197. ApiBase::PARAM_TYPE => 'integer',
  198. ),
  199. 'prtype' => array(
  200. ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
  201. ApiBase::PARAM_ISMULTI => true
  202. ),
  203. 'prlevel' => array(
  204. ApiBase::PARAM_TYPE => $wgRestrictionLevels,
  205. ApiBase::PARAM_ISMULTI => true
  206. ),
  207. 'prfiltercascade' => array(
  208. ApiBase::PARAM_DFLT => 'all',
  209. ApiBase::PARAM_TYPE => array(
  210. 'cascading',
  211. 'noncascading',
  212. 'all'
  213. ),
  214. ),
  215. 'limit' => array(
  216. ApiBase::PARAM_DFLT => 10,
  217. ApiBase::PARAM_TYPE => 'limit',
  218. ApiBase::PARAM_MIN => 1,
  219. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  220. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  221. ),
  222. 'dir' => array(
  223. ApiBase::PARAM_DFLT => 'ascending',
  224. ApiBase::PARAM_TYPE => array(
  225. 'ascending',
  226. 'descending'
  227. )
  228. ),
  229. 'filterlanglinks' => array(
  230. ApiBase::PARAM_TYPE => array(
  231. 'withlanglinks',
  232. 'withoutlanglinks',
  233. 'all'
  234. ),
  235. ApiBase::PARAM_DFLT => 'all'
  236. ),
  237. 'prexpiry' => array(
  238. ApiBase::PARAM_TYPE => array(
  239. 'indefinite',
  240. 'definite',
  241. 'all'
  242. ),
  243. ApiBase::PARAM_DFLT => 'all'
  244. ),
  245. );
  246. }
  247. public function getParamDescription() {
  248. $p = $this->getModulePrefix();
  249. return array(
  250. 'from' => 'The page title to start enumerating from',
  251. 'to' => 'The page title to stop enumerating at',
  252. 'prefix' => 'Search for all page titles that begin with this value',
  253. 'namespace' => 'The namespace to enumerate',
  254. 'filterredir' => 'Which pages to list',
  255. 'dir' => 'The direction in which to list',
  256. 'minsize' => 'Limit to pages with at least this many bytes',
  257. 'maxsize' => 'Limit to pages with at most this many bytes',
  258. 'prtype' => 'Limit to protected pages only',
  259. 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
  260. 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
  261. 'filterlanglinks' => 'Filter based on whether a page has langlinks',
  262. 'limit' => 'How many total pages to return.',
  263. 'prexpiry' => array(
  264. 'Which protection expiry to filter the page on',
  265. ' indefinite - Get only pages with indefinite protection expiry',
  266. ' definite - Get only pages with a definite (specific) protection expiry',
  267. ' all - Get pages with any protections expiry'
  268. ),
  269. );
  270. }
  271. public function getDescription() {
  272. return 'Enumerate all pages sequentially in a given namespace';
  273. }
  274. public function getPossibleErrors() {
  275. return array_merge( parent::getPossibleErrors(), array(
  276. array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
  277. array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
  278. ) );
  279. }
  280. protected function getExamples() {
  281. return array(
  282. 'Simple Use',
  283. ' Show a list of pages starting at the letter "B"',
  284. ' api.php?action=query&list=allpages&apfrom=B',
  285. 'Using as Generator',
  286. ' Show info about 4 pages starting at the letter "T"',
  287. ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
  288. ' Show content of first 2 non-redirect pages begining at "Re"',
  289. ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
  290. );
  291. }
  292. public function getHelpUrls() {
  293. return 'http://www.mediawiki.org/wiki/API:Allpages';
  294. }
  295. public function getVersion() {
  296. return __CLASS__ . ': $Id: ApiQueryAllpages.php 92401 2011-07-17 17:02:06Z reedy $';
  297. }
  298. }