ApiQueryQueryPage.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Copyright © 2010 Roan Kattouw "<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. use MediaWiki\Special\SpecialPageFactory;
  24. /**
  25. * Query module to get the results of a QueryPage-based special page
  26. *
  27. * @ingroup API
  28. */
  29. class ApiQueryQueryPage extends ApiQueryGeneratorBase {
  30. /**
  31. * @var string[] list of special page names
  32. */
  33. private $queryPages;
  34. /**
  35. * @var SpecialPageFactory
  36. */
  37. private $specialPageFactory;
  38. public function __construct( ApiQuery $query, $moduleName ) {
  39. parent::__construct( $query, $moduleName, 'qp' );
  40. $this->queryPages = array_values( array_diff(
  41. array_column( QueryPage::getPages(), 1 ), // [ class, name ]
  42. $this->getConfig()->get( 'APIUselessQueryPages' )
  43. ) );
  44. $this->specialPageFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
  45. }
  46. public function execute() {
  47. $this->run();
  48. }
  49. public function executeGenerator( $resultPageSet ) {
  50. $this->run( $resultPageSet );
  51. }
  52. /**
  53. * @param string $name
  54. * @return QueryPage
  55. */
  56. private function getSpecialPage( $name ) : QueryPage {
  57. $qp = $this->specialPageFactory->getPage( $name );
  58. if ( !$qp ) {
  59. self::dieDebug(
  60. __METHOD__,
  61. 'SpecialPageFactory failed to create special page ' . $name
  62. );
  63. }
  64. if ( !( $qp instanceof QueryPage ) ) {
  65. self::dieDebug(
  66. __METHOD__,
  67. 'Special page ' . $name . ' is not a QueryPage'
  68. );
  69. }
  70. return $qp;
  71. }
  72. /**
  73. * @param ApiPageSet|null $resultPageSet
  74. */
  75. public function run( $resultPageSet = null ) {
  76. $params = $this->extractRequestParams();
  77. $result = $this->getResult();
  78. $qp = $this->getSpecialPage( $params['page'] );
  79. if ( !$qp->userCanExecute( $this->getUser() ) ) {
  80. $this->dieWithError( 'apierror-specialpage-cantexecute' );
  81. }
  82. $r = [ 'name' => $params['page'] ];
  83. if ( $qp->isCached() ) {
  84. if ( !$qp->isCacheable() ) {
  85. $r['disabled'] = true;
  86. } else {
  87. $r['cached'] = true;
  88. $ts = $qp->getCachedTimestamp();
  89. if ( $ts ) {
  90. $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
  91. }
  92. $r['maxresults'] = $this->getConfig()->get( 'QueryCacheLimit' );
  93. }
  94. }
  95. $result->addValue( [ 'query' ], $this->getModuleName(), $r );
  96. if ( $qp->isCached() && !$qp->isCacheable() ) {
  97. // Disabled query page, don't run the query
  98. return;
  99. }
  100. $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
  101. $count = 0;
  102. $titles = [];
  103. foreach ( $res as $row ) {
  104. if ( ++$count > $params['limit'] ) {
  105. // We've had enough
  106. $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
  107. break;
  108. }
  109. $title = Title::makeTitle( $row->namespace, $row->title );
  110. if ( is_null( $resultPageSet ) ) {
  111. $data = [];
  112. if ( isset( $row->value ) ) {
  113. $data['value'] = $row->value;
  114. if ( $qp->usesTimestamps() ) {
  115. $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
  116. }
  117. }
  118. self::addTitleInfo( $data, $title );
  119. foreach ( $row as $field => $value ) {
  120. if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
  121. $data['databaseResult'][$field] = $value;
  122. }
  123. }
  124. $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
  125. if ( !$fit ) {
  126. $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
  127. break;
  128. }
  129. } else {
  130. $titles[] = $title;
  131. }
  132. }
  133. if ( is_null( $resultPageSet ) ) {
  134. $result->addIndexedTagName(
  135. [ 'query', $this->getModuleName(), 'results' ],
  136. 'page'
  137. );
  138. } else {
  139. $resultPageSet->populateFromTitles( $titles );
  140. }
  141. }
  142. public function getCacheMode( $params ) {
  143. $qp = $this->getSpecialPage( $params['page'] );
  144. if ( $qp->getRestriction() != '' ) {
  145. return 'private';
  146. }
  147. return 'public';
  148. }
  149. public function getAllowedParams() {
  150. return [
  151. 'page' => [
  152. ApiBase::PARAM_TYPE => $this->queryPages,
  153. ApiBase::PARAM_REQUIRED => true
  154. ],
  155. 'offset' => [
  156. ApiBase::PARAM_DFLT => 0,
  157. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  158. ],
  159. 'limit' => [
  160. ApiBase::PARAM_DFLT => 10,
  161. ApiBase::PARAM_TYPE => 'limit',
  162. ApiBase::PARAM_MIN => 1,
  163. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  164. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  165. ],
  166. ];
  167. }
  168. protected function getExamplesMessages() {
  169. return [
  170. 'action=query&list=querypage&qppage=Ancientpages'
  171. => 'apihelp-query+querypage-example-ancientpages',
  172. ];
  173. }
  174. public function getHelpUrls() {
  175. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage';
  176. }
  177. }