ApiQueryPageProps.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Aug 7, 2010
  6. *
  7. * Copyright © 2010 soxred93, Bryan Tong Minh
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. /**
  27. * A query module to show basic page information.
  28. *
  29. * @ingroup API
  30. */
  31. class ApiQueryPageProps extends ApiQueryBase {
  32. private $params;
  33. public function __construct( ApiQuery $query, $moduleName ) {
  34. parent::__construct( $query, $moduleName, 'pp' );
  35. }
  36. public function execute() {
  37. # Only operate on existing pages
  38. $pages = $this->getPageSet()->getGoodTitles();
  39. $this->params = $this->extractRequestParams();
  40. if ( $this->params['continue'] ) {
  41. $continueValue = intval( $this->params['continue'] );
  42. $this->dieContinueUsageIf( strval( $continueValue ) !== $this->params['continue'] );
  43. $filteredPages = [];
  44. foreach ( $pages as $id => $page ) {
  45. if ( $id >= $continueValue ) {
  46. $filteredPages[$id] = $page;
  47. }
  48. }
  49. $pages = $filteredPages;
  50. }
  51. if ( !count( $pages ) ) {
  52. # Nothing to do
  53. return;
  54. }
  55. $pageProps = PageProps::getInstance();
  56. $result = $this->getResult();
  57. if ( $this->params['prop'] ) {
  58. $propnames = $this->params['prop'];
  59. $properties = $pageProps->getProperties( $pages, $propnames );
  60. } else {
  61. $properties = $pageProps->getAllProperties( $pages );
  62. }
  63. ksort( $properties );
  64. foreach ( $properties as $page => $props ) {
  65. if ( !$this->addPageProps( $result, $page, $props ) ) {
  66. break;
  67. }
  68. }
  69. }
  70. /**
  71. * Add page properties to an ApiResult, adding a continue
  72. * parameter if it doesn't fit.
  73. *
  74. * @param ApiResult $result
  75. * @param int $page
  76. * @param array $props
  77. * @return bool True if it fits in the result
  78. */
  79. private function addPageProps( $result, $page, $props ) {
  80. ApiResult::setArrayType( $props, 'assoc' );
  81. $fit = $result->addValue( [ 'query', 'pages', $page ], 'pageprops', $props );
  82. if ( !$fit ) {
  83. $this->setContinueEnumParameter( 'continue', $page );
  84. }
  85. return $fit;
  86. }
  87. public function getCacheMode( $params ) {
  88. return 'public';
  89. }
  90. public function getAllowedParams() {
  91. return [
  92. 'continue' => [
  93. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  94. ],
  95. 'prop' => [
  96. ApiBase::PARAM_ISMULTI => true,
  97. ],
  98. ];
  99. }
  100. protected function getExamplesMessages() {
  101. return [
  102. 'action=query&prop=pageprops&titles=Main%20Page|MediaWiki'
  103. => 'apihelp-query+pageprops-example-simple',
  104. ];
  105. }
  106. public function getHelpUrls() {
  107. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageprops';
  108. }
  109. }