ApiQueryPagePropNames.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © 2013 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 list used page props
  25. *
  26. * @ingroup API
  27. * @since 1.21
  28. */
  29. class ApiQueryPagePropNames extends ApiQueryBase {
  30. public function __construct( ApiQuery $query, $moduleName ) {
  31. parent::__construct( $query, $moduleName, 'ppn' );
  32. }
  33. public function getCacheMode( $params ) {
  34. return 'public';
  35. }
  36. public function execute() {
  37. $params = $this->extractRequestParams();
  38. $this->addTables( 'page_props' );
  39. $this->addFields( 'pp_propname' );
  40. $this->addOption( 'DISTINCT' );
  41. $this->addOption( 'ORDER BY', 'pp_propname' );
  42. if ( $params['continue'] ) {
  43. $cont = explode( '|', $params['continue'] );
  44. $this->dieContinueUsageIf( count( $cont ) != 1 );
  45. // Add a WHERE clause
  46. $this->addWhereRange( 'pp_propname', 'newer', $cont[0], null );
  47. }
  48. $limit = $params['limit'];
  49. // mysql has issues with limit in loose index T115825
  50. if ( $this->getDB()->getType() !== 'mysql' ) {
  51. $this->addOption( 'LIMIT', $limit + 1 );
  52. }
  53. $result = $this->getResult();
  54. $count = 0;
  55. foreach ( $this->select( __METHOD__ ) as $row ) {
  56. if ( ++$count > $limit ) {
  57. // We've reached the one extra which shows that there are
  58. // additional pages to be had. Stop here...
  59. $this->setContinueEnumParameter( 'continue', $row->pp_propname );
  60. break;
  61. }
  62. $vals = [];
  63. $vals['propname'] = $row->pp_propname;
  64. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  65. if ( !$fit ) {
  66. $this->setContinueEnumParameter( 'continue', $row->pp_propname );
  67. break;
  68. }
  69. }
  70. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'p' );
  71. }
  72. public function getAllowedParams() {
  73. return [
  74. 'continue' => [
  75. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  76. ],
  77. 'limit' => [
  78. ApiBase::PARAM_TYPE => 'limit',
  79. ApiBase::PARAM_DFLT => 10,
  80. ApiBase::PARAM_MIN => 1,
  81. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  82. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  83. ],
  84. ];
  85. }
  86. protected function getExamplesMessages() {
  87. return [
  88. 'action=query&list=pagepropnames'
  89. => 'apihelp-query+pagepropnames-example-simple',
  90. ];
  91. }
  92. public function getHelpUrls() {
  93. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pagepropnames';
  94. }
  95. }