ApiQueryAllPages.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * Query module to enumerate all available pages.
  25. *
  26. * @ingroup API
  27. */
  28. class ApiQueryAllPages extends ApiQueryGeneratorBase {
  29. public function __construct( ApiQuery $query, $moduleName ) {
  30. parent::__construct( $query, $moduleName, 'ap' );
  31. }
  32. public function execute() {
  33. $this->run();
  34. }
  35. public function getCacheMode( $params ) {
  36. return 'public';
  37. }
  38. /**
  39. * @param ApiPageSet $resultPageSet
  40. * @return void
  41. */
  42. public function executeGenerator( $resultPageSet ) {
  43. if ( $resultPageSet->isResolvingRedirects() ) {
  44. $this->dieWithError( 'apierror-allpages-generator-redirects', 'params' );
  45. }
  46. $this->run( $resultPageSet );
  47. }
  48. /**
  49. * @param ApiPageSet $resultPageSet
  50. * @return void
  51. */
  52. private function run( $resultPageSet = null ) {
  53. $db = $this->getDB();
  54. $params = $this->extractRequestParams();
  55. // Page filters
  56. $this->addTables( 'page' );
  57. if ( !is_null( $params['continue'] ) ) {
  58. $cont = explode( '|', $params['continue'] );
  59. $this->dieContinueUsageIf( count( $cont ) != 1 );
  60. $op = $params['dir'] == 'descending' ? '<' : '>';
  61. $cont_from = $db->addQuotes( $cont[0] );
  62. $this->addWhere( "page_title $op= $cont_from" );
  63. }
  64. $miserMode = $this->getConfig()->get( 'MiserMode' );
  65. if ( !$miserMode ) {
  66. if ( $params['filterredir'] == 'redirects' ) {
  67. $this->addWhereFld( 'page_is_redirect', 1 );
  68. } elseif ( $params['filterredir'] == 'nonredirects' ) {
  69. $this->addWhereFld( 'page_is_redirect', 0 );
  70. }
  71. }
  72. $this->addWhereFld( 'page_namespace', $params['namespace'] );
  73. $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
  74. $from = ( $params['from'] === null
  75. ? null
  76. : $this->titlePartToKey( $params['from'], $params['namespace'] ) );
  77. $to = ( $params['to'] === null
  78. ? null
  79. : $this->titlePartToKey( $params['to'], $params['namespace'] ) );
  80. $this->addWhereRange( 'page_title', $dir, $from, $to );
  81. if ( isset( $params['prefix'] ) ) {
  82. $this->addWhere( 'page_title' . $db->buildLike(
  83. $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
  84. $db->anyString() ) );
  85. }
  86. if ( is_null( $resultPageSet ) ) {
  87. $selectFields = [
  88. 'page_namespace',
  89. 'page_title',
  90. 'page_id'
  91. ];
  92. } else {
  93. $selectFields = $resultPageSet->getPageTableFields();
  94. }
  95. $miserModeFilterRedirValue = null;
  96. $miserModeFilterRedir = $miserMode && $params['filterredir'] !== 'all';
  97. if ( $miserModeFilterRedir ) {
  98. $selectFields[] = 'page_is_redirect';
  99. if ( $params['filterredir'] == 'redirects' ) {
  100. $miserModeFilterRedirValue = 1;
  101. } elseif ( $params['filterredir'] == 'nonredirects' ) {
  102. $miserModeFilterRedirValue = 0;
  103. }
  104. }
  105. $this->addFields( $selectFields );
  106. $forceNameTitleIndex = true;
  107. if ( isset( $params['minsize'] ) ) {
  108. $this->addWhere( 'page_len>=' . (int)$params['minsize'] );
  109. $forceNameTitleIndex = false;
  110. }
  111. if ( isset( $params['maxsize'] ) ) {
  112. $this->addWhere( 'page_len<=' . (int)$params['maxsize'] );
  113. $forceNameTitleIndex = false;
  114. }
  115. // Page protection filtering
  116. if ( $params['prtype'] || $params['prexpiry'] != 'all' ) {
  117. $this->addTables( 'page_restrictions' );
  118. $this->addWhere( 'page_id=pr_page' );
  119. $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
  120. if ( $params['prtype'] ) {
  121. $this->addWhereFld( 'pr_type', $params['prtype'] );
  122. if ( isset( $params['prlevel'] ) ) {
  123. // Remove the empty string and '*' from the prlevel array
  124. $prlevel = array_diff( $params['prlevel'], [ '', '*' ] );
  125. if ( count( $prlevel ) ) {
  126. $this->addWhereFld( 'pr_level', $prlevel );
  127. }
  128. }
  129. if ( $params['prfiltercascade'] == 'cascading' ) {
  130. $this->addWhereFld( 'pr_cascade', 1 );
  131. } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
  132. $this->addWhereFld( 'pr_cascade', 0 );
  133. }
  134. }
  135. $forceNameTitleIndex = false;
  136. if ( $params['prexpiry'] == 'indefinite' ) {
  137. $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
  138. } elseif ( $params['prexpiry'] == 'definite' ) {
  139. $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
  140. }
  141. $this->addOption( 'DISTINCT' );
  142. } elseif ( isset( $params['prlevel'] ) ) {
  143. $this->dieWithError(
  144. [ 'apierror-invalidparammix-mustusewith', 'prlevel', 'prtype' ], 'invalidparammix'
  145. );
  146. }
  147. if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
  148. $this->addTables( 'langlinks' );
  149. $this->addJoinConds( [ 'langlinks' => [ 'LEFT JOIN', 'page_id=ll_from' ] ] );
  150. $this->addWhere( 'll_from IS NULL' );
  151. $forceNameTitleIndex = false;
  152. } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
  153. $this->addTables( 'langlinks' );
  154. $this->addWhere( 'page_id=ll_from' );
  155. $this->addOption( 'STRAIGHT_JOIN' );
  156. // MySQL filesorts if we use a GROUP BY that works with the rules
  157. // in the 1992 SQL standard (it doesn't like having the
  158. // constant-in-WHERE page_namespace column in there). Using the
  159. // 1999 rules works fine, but that breaks other DBs. Sigh.
  160. /// @todo Once we drop support for 1992-rule DBs, we can simplify this.
  161. $dbType = $db->getType();
  162. if ( $dbType === 'mysql' || $dbType === 'sqlite' ) {
  163. // Ignore the rules, or 1999 rules if you count unique keys
  164. // over non-NULL columns as satisfying the requirement for
  165. // "functional dependency" and don't require including
  166. // constant-in-WHERE columns in the GROUP BY.
  167. $this->addOption( 'GROUP BY', [ 'page_title' ] );
  168. } elseif ( $dbType === 'postgres' && $db->getServerVersion() >= 9.1 ) {
  169. // 1999 rules only counting primary keys
  170. $this->addOption( 'GROUP BY', [ 'page_title', 'page_id' ] );
  171. } else {
  172. // 1992 rules
  173. $this->addOption( 'GROUP BY', $selectFields );
  174. }
  175. $forceNameTitleIndex = false;
  176. }
  177. if ( $forceNameTitleIndex ) {
  178. $this->addOption( 'USE INDEX', 'name_title' );
  179. }
  180. $limit = $params['limit'];
  181. $this->addOption( 'LIMIT', $limit + 1 );
  182. $res = $this->select( __METHOD__ );
  183. // Get gender information
  184. $services = MediaWikiServices::getInstance();
  185. if ( $services->getNamespaceInfo()->hasGenderDistinction( $params['namespace'] ) ) {
  186. $users = [];
  187. foreach ( $res as $row ) {
  188. $users[] = $row->page_title;
  189. }
  190. $services->getGenderCache()->doQuery( $users, __METHOD__ );
  191. $res->rewind(); // reset
  192. }
  193. $count = 0;
  194. $result = $this->getResult();
  195. foreach ( $res as $row ) {
  196. if ( ++$count > $limit ) {
  197. // We've reached the one extra which shows that there are
  198. // additional pages to be had. Stop here...
  199. $this->setContinueEnumParameter( 'continue', $row->page_title );
  200. break;
  201. }
  202. if ( $miserModeFilterRedir && (int)$row->page_is_redirect !== $miserModeFilterRedirValue ) {
  203. // Filter implemented in PHP due to being in Miser Mode
  204. continue;
  205. }
  206. if ( is_null( $resultPageSet ) ) {
  207. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  208. $vals = [
  209. 'pageid' => (int)$row->page_id,
  210. 'ns' => (int)$title->getNamespace(),
  211. 'title' => $title->getPrefixedText()
  212. ];
  213. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  214. if ( !$fit ) {
  215. $this->setContinueEnumParameter( 'continue', $row->page_title );
  216. break;
  217. }
  218. } else {
  219. $resultPageSet->processDbRow( $row );
  220. }
  221. }
  222. if ( is_null( $resultPageSet ) ) {
  223. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'p' );
  224. }
  225. }
  226. public function getAllowedParams() {
  227. $ret = [
  228. 'from' => null,
  229. 'continue' => [
  230. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  231. ],
  232. 'to' => null,
  233. 'prefix' => null,
  234. 'namespace' => [
  235. ApiBase::PARAM_DFLT => NS_MAIN,
  236. ApiBase::PARAM_TYPE => 'namespace',
  237. ],
  238. 'filterredir' => [
  239. ApiBase::PARAM_DFLT => 'all',
  240. ApiBase::PARAM_TYPE => [
  241. 'all',
  242. 'redirects',
  243. 'nonredirects'
  244. ]
  245. ],
  246. 'minsize' => [
  247. ApiBase::PARAM_TYPE => 'integer',
  248. ],
  249. 'maxsize' => [
  250. ApiBase::PARAM_TYPE => 'integer',
  251. ],
  252. 'prtype' => [
  253. ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
  254. ApiBase::PARAM_ISMULTI => true
  255. ],
  256. 'prlevel' => [
  257. ApiBase::PARAM_TYPE => $this->getConfig()->get( 'RestrictionLevels' ),
  258. ApiBase::PARAM_ISMULTI => true
  259. ],
  260. 'prfiltercascade' => [
  261. ApiBase::PARAM_DFLT => 'all',
  262. ApiBase::PARAM_TYPE => [
  263. 'cascading',
  264. 'noncascading',
  265. 'all'
  266. ],
  267. ],
  268. 'limit' => [
  269. ApiBase::PARAM_DFLT => 10,
  270. ApiBase::PARAM_TYPE => 'limit',
  271. ApiBase::PARAM_MIN => 1,
  272. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  273. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  274. ],
  275. 'dir' => [
  276. ApiBase::PARAM_DFLT => 'ascending',
  277. ApiBase::PARAM_TYPE => [
  278. 'ascending',
  279. 'descending'
  280. ]
  281. ],
  282. 'filterlanglinks' => [
  283. ApiBase::PARAM_TYPE => [
  284. 'withlanglinks',
  285. 'withoutlanglinks',
  286. 'all'
  287. ],
  288. ApiBase::PARAM_DFLT => 'all'
  289. ],
  290. 'prexpiry' => [
  291. ApiBase::PARAM_TYPE => [
  292. 'indefinite',
  293. 'definite',
  294. 'all'
  295. ],
  296. ApiBase::PARAM_DFLT => 'all'
  297. ],
  298. ];
  299. if ( $this->getConfig()->get( 'MiserMode' ) ) {
  300. $ret['filterredir'][ApiBase::PARAM_HELP_MSG_APPEND] = [ 'api-help-param-limited-in-miser-mode' ];
  301. }
  302. return $ret;
  303. }
  304. protected function getExamplesMessages() {
  305. return [
  306. 'action=query&list=allpages&apfrom=B'
  307. => 'apihelp-query+allpages-example-b',
  308. 'action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info'
  309. => 'apihelp-query+allpages-example-generator',
  310. 'action=query&generator=allpages&gaplimit=2&' .
  311. 'gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
  312. => 'apihelp-query+allpages-example-generator-revisions',
  313. ];
  314. }
  315. public function getHelpUrls() {
  316. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allpages';
  317. }
  318. }