ApiQueryGeneratorBase.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © 2006 Yuri Astrakhan "<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. /**
  23. * @ingroup API
  24. */
  25. abstract class ApiQueryGeneratorBase extends ApiQueryBase {
  26. private $mGeneratorPageSet = null;
  27. /**
  28. * Switch this module to generator mode. By default, generator mode is
  29. * switched off and the module acts like a normal query module.
  30. * @since 1.21 requires pageset parameter
  31. * @param ApiPageSet $generatorPageSet ApiPageSet object that the module will get
  32. * by calling getPageSet() when in generator mode.
  33. */
  34. public function setGeneratorMode( ApiPageSet $generatorPageSet ) {
  35. if ( $generatorPageSet === null ) {
  36. ApiBase::dieDebug( __METHOD__, 'Required parameter missing - $generatorPageSet' );
  37. }
  38. $this->mGeneratorPageSet = $generatorPageSet;
  39. }
  40. /**
  41. * Indicate whether the module is in generator mode
  42. * @since 1.28
  43. * @return bool
  44. */
  45. public function isInGeneratorMode() {
  46. return $this->mGeneratorPageSet !== null;
  47. }
  48. /**
  49. * Get the PageSet object to work on.
  50. * If this module is generator, the pageSet object is different from other module's
  51. * @return ApiPageSet
  52. */
  53. protected function getPageSet() {
  54. if ( $this->mGeneratorPageSet !== null ) {
  55. return $this->mGeneratorPageSet;
  56. }
  57. return parent::getPageSet();
  58. }
  59. /**
  60. * Overrides ApiBase to prepend 'g' to every generator parameter
  61. * @param string $paramName Parameter name
  62. * @return string Prefixed parameter name
  63. */
  64. public function encodeParamName( $paramName ) {
  65. if ( $this->mGeneratorPageSet !== null ) {
  66. return 'g' . parent::encodeParamName( $paramName );
  67. } else {
  68. return parent::encodeParamName( $paramName );
  69. }
  70. }
  71. /**
  72. * Overridden to set the generator param if in generator mode
  73. * @param string $paramName Parameter name
  74. * @param string|array $paramValue Parameter value
  75. */
  76. protected function setContinueEnumParameter( $paramName, $paramValue ) {
  77. if ( $this->mGeneratorPageSet !== null ) {
  78. $this->getContinuationManager()->addGeneratorContinueParam( $this, $paramName, $paramValue );
  79. } else {
  80. parent::setContinueEnumParameter( $paramName, $paramValue );
  81. }
  82. }
  83. /** @inheritDoc */
  84. protected function getHelpFlags() {
  85. // Corresponding messages: api-help-flag-generator
  86. $flags = parent::getHelpFlags();
  87. $flags[] = 'generator';
  88. return $flags;
  89. }
  90. /**
  91. * Execute this module as a generator
  92. * @param ApiPageSet $resultPageSet All output should be appended to this object
  93. */
  94. abstract public function executeGenerator( $resultPageSet );
  95. }