ApiQueryPagesWithProp.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © 2012 Wikimedia Foundation and contributors
  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. * @since 1.21
  22. */
  23. /**
  24. * A query module to enumerate pages that use a particular prop
  25. *
  26. * @ingroup API
  27. * @since 1.21
  28. */
  29. class ApiQueryPagesWithProp extends ApiQueryGeneratorBase {
  30. public function __construct( ApiQuery $query, $moduleName ) {
  31. parent::__construct( $query, $moduleName, 'pwp' );
  32. }
  33. public function execute() {
  34. $this->run();
  35. }
  36. public function getCacheMode( $params ) {
  37. return 'public';
  38. }
  39. public function executeGenerator( $resultPageSet ) {
  40. $this->run( $resultPageSet );
  41. }
  42. /**
  43. * @param ApiPageSet $resultPageSet
  44. * @return void
  45. */
  46. private function run( $resultPageSet = null ) {
  47. $params = $this->extractRequestParams();
  48. $prop = array_flip( $params['prop'] );
  49. $fld_ids = isset( $prop['ids'] );
  50. $fld_title = isset( $prop['title'] );
  51. $fld_value = isset( $prop['value'] );
  52. if ( $resultPageSet === null ) {
  53. $this->addFields( [ 'page_id' ] );
  54. $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
  55. $this->addFieldsIf( 'pp_value', $fld_value );
  56. } else {
  57. $this->addFields( $resultPageSet->getPageTableFields() );
  58. }
  59. $this->addTables( [ 'page_props', 'page' ] );
  60. $this->addWhere( 'pp_page=page_id' );
  61. $this->addWhereFld( 'pp_propname', $params['propname'] );
  62. $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older';
  63. if ( $params['continue'] ) {
  64. $cont = explode( '|', $params['continue'] );
  65. $this->dieContinueUsageIf( count( $cont ) != 1 );
  66. // Add a WHERE clause
  67. $from = (int)$cont[0];
  68. $this->addWhereRange( 'pp_page', $dir, $from, null );
  69. }
  70. $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' );
  71. $this->addOption( 'ORDER BY', 'pp_page' . $sort );
  72. $limit = $params['limit'];
  73. $this->addOption( 'LIMIT', $limit + 1 );
  74. $result = $this->getResult();
  75. $count = 0;
  76. foreach ( $this->select( __METHOD__ ) as $row ) {
  77. if ( ++$count > $limit ) {
  78. // We've reached the one extra which shows that there are
  79. // additional pages to be had. Stop here...
  80. $this->setContinueEnumParameter( 'continue', $row->page_id );
  81. break;
  82. }
  83. if ( $resultPageSet === null ) {
  84. $vals = [
  85. ApiResult::META_TYPE => 'assoc',
  86. ];
  87. if ( $fld_ids ) {
  88. $vals['pageid'] = (int)$row->page_id;
  89. }
  90. if ( $fld_title ) {
  91. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  92. ApiQueryBase::addTitleInfo( $vals, $title );
  93. }
  94. if ( $fld_value ) {
  95. $vals['value'] = $row->pp_value;
  96. }
  97. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  98. if ( !$fit ) {
  99. $this->setContinueEnumParameter( 'continue', $row->page_id );
  100. break;
  101. }
  102. } else {
  103. $resultPageSet->processDbRow( $row );
  104. }
  105. }
  106. if ( $resultPageSet === null ) {
  107. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
  108. }
  109. }
  110. public function getAllowedParams() {
  111. return [
  112. 'propname' => [
  113. ApiBase::PARAM_TYPE => 'string',
  114. ApiBase::PARAM_REQUIRED => true,
  115. ],
  116. 'prop' => [
  117. ApiBase::PARAM_DFLT => 'ids|title',
  118. ApiBase::PARAM_ISMULTI => true,
  119. ApiBase::PARAM_TYPE => [
  120. 'ids',
  121. 'title',
  122. 'value',
  123. ],
  124. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  125. ],
  126. 'continue' => [
  127. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  128. ],
  129. 'limit' => [
  130. ApiBase::PARAM_TYPE => 'limit',
  131. ApiBase::PARAM_DFLT => 10,
  132. ApiBase::PARAM_MIN => 1,
  133. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  134. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  135. ],
  136. 'dir' => [
  137. ApiBase::PARAM_DFLT => 'ascending',
  138. ApiBase::PARAM_TYPE => [
  139. 'ascending',
  140. 'descending',
  141. ]
  142. ],
  143. ];
  144. }
  145. protected function getExamplesMessages() {
  146. return [
  147. 'action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value'
  148. => 'apihelp-query+pageswithprop-example-simple',
  149. 'action=query&generator=pageswithprop&gpwppropname=notoc&prop=info'
  150. => 'apihelp-query+pageswithprop-example-generator',
  151. ];
  152. }
  153. public function getHelpUrls() {
  154. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';
  155. }
  156. }