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