ApiQueryPagesWithProp.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Created on December 31, 2012
  4. *
  5. * Copyright © 2012 Wikimedia Foundation and contributors
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @since 1.21
  24. */
  25. /**
  26. * A query module to enumerate pages that use a particular prop
  27. *
  28. * @ingroup API
  29. * @since 1.21
  30. */
  31. class ApiQueryPagesWithProp extends ApiQueryGeneratorBase {
  32. public function __construct( ApiQuery $query, $moduleName ) {
  33. parent::__construct( $query, $moduleName, 'pwp' );
  34. }
  35. public function execute() {
  36. $this->run();
  37. }
  38. public function getCacheMode( $params ) {
  39. return 'public';
  40. }
  41. public function executeGenerator( $resultPageSet ) {
  42. $this->run( $resultPageSet );
  43. }
  44. /**
  45. * @param ApiPageSet $resultPageSet
  46. * @return void
  47. */
  48. private function run( $resultPageSet = null ) {
  49. $params = $this->extractRequestParams();
  50. $prop = array_flip( $params['prop'] );
  51. $fld_ids = isset( $prop['ids'] );
  52. $fld_title = isset( $prop['title'] );
  53. $fld_value = isset( $prop['value'] );
  54. if ( $resultPageSet === null ) {
  55. $this->addFields( [ 'page_id' ] );
  56. $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
  57. $this->addFieldsIf( 'pp_value', $fld_value );
  58. } else {
  59. $this->addFields( $resultPageSet->getPageTableFields() );
  60. }
  61. $this->addTables( [ 'page_props', 'page' ] );
  62. $this->addWhere( 'pp_page=page_id' );
  63. $this->addWhereFld( 'pp_propname', $params['propname'] );
  64. $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older';
  65. if ( $params['continue'] ) {
  66. $cont = explode( '|', $params['continue'] );
  67. $this->dieContinueUsageIf( count( $cont ) != 1 );
  68. // Add a WHERE clause
  69. $from = (int)$cont[0];
  70. $this->addWhereRange( 'pp_page', $dir, $from, null );
  71. }
  72. $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' );
  73. $this->addOption( 'ORDER BY', 'pp_page' . $sort );
  74. $limit = $params['limit'];
  75. $this->addOption( 'LIMIT', $limit + 1 );
  76. $result = $this->getResult();
  77. $count = 0;
  78. foreach ( $this->select( __METHOD__ ) as $row ) {
  79. if ( ++$count > $limit ) {
  80. // We've reached the one extra which shows that there are
  81. // additional pages to be had. Stop here...
  82. $this->setContinueEnumParameter( 'continue', $row->page_id );
  83. break;
  84. }
  85. if ( $resultPageSet === null ) {
  86. $vals = [
  87. ApiResult::META_TYPE => 'assoc',
  88. ];
  89. if ( $fld_ids ) {
  90. $vals['pageid'] = (int)$row->page_id;
  91. }
  92. if ( $fld_title ) {
  93. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  94. ApiQueryBase::addTitleInfo( $vals, $title );
  95. }
  96. if ( $fld_value ) {
  97. $vals['value'] = $row->pp_value;
  98. }
  99. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  100. if ( !$fit ) {
  101. $this->setContinueEnumParameter( 'continue', $row->page_id );
  102. break;
  103. }
  104. } else {
  105. $resultPageSet->processDbRow( $row );
  106. }
  107. }
  108. if ( $resultPageSet === null ) {
  109. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
  110. }
  111. }
  112. public function getAllowedParams() {
  113. return [
  114. 'propname' => [
  115. ApiBase::PARAM_TYPE => 'string',
  116. ApiBase::PARAM_REQUIRED => true,
  117. ],
  118. 'prop' => [
  119. ApiBase::PARAM_DFLT => 'ids|title',
  120. ApiBase::PARAM_ISMULTI => true,
  121. ApiBase::PARAM_TYPE => [
  122. 'ids',
  123. 'title',
  124. 'value',
  125. ],
  126. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  127. ],
  128. 'continue' => [
  129. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  130. ],
  131. 'limit' => [
  132. ApiBase::PARAM_TYPE => 'limit',
  133. ApiBase::PARAM_DFLT => 10,
  134. ApiBase::PARAM_MIN => 1,
  135. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  136. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  137. ],
  138. 'dir' => [
  139. ApiBase::PARAM_DFLT => 'ascending',
  140. ApiBase::PARAM_TYPE => [
  141. 'ascending',
  142. 'descending',
  143. ]
  144. ],
  145. ];
  146. }
  147. protected function getExamplesMessages() {
  148. return [
  149. 'action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value'
  150. => 'apihelp-query+pageswithprop-example-simple',
  151. 'action=query&generator=pageswithprop&gpwppropname=notoc&prop=info'
  152. => 'apihelp-query+pageswithprop-example-generator',
  153. ];
  154. }
  155. public function getHelpUrls() {
  156. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';
  157. }
  158. }