| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Push\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiModuleManager; |
| 7 |
use MediaWiki\Api\ApiUsageException; |
| 8 |
use MediaWiki\MediaWikiServices; |
| 9 |
use Wikimedia\ParamValidator\ParamValidator; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class ApiEchoPushSubscriptions extends ApiBase { |
| 17 |
|
| 18 |
|
| 19 |
private const SUBMODULES = [ |
| 20 |
'create' => ApiEchoPushSubscriptionsCreate::class, |
| 21 |
'delete' => ApiEchoPushSubscriptionsDelete::class, |
| 22 |
]; |
| 23 |
|
| 24 |
|
| 25 |
private $moduleManager; |
| 26 |
|
| 27 |
|
| 28 |
public function execute(): void { |
| 29 |
$this->checkLoginState(); |
| 30 |
$this->checkUserRightsAny( 'editmyprivateinfo' ); |
| 31 |
$command = $this->getParameter( 'command' ); |
| 32 |
$module = $this->moduleManager->getModule( $command, 'command' ); |
| 33 |
$module->execute(); |
| 34 |
$module->getResult()->addValue( |
| 35 |
null, |
| 36 |
$module->getModuleName(), |
| 37 |
[ 'result' => 'Success' ] |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
public function getModuleManager(): ApiModuleManager { |
| 43 |
if ( !$this->moduleManager ) { |
| 44 |
$submodules = array_map( static function ( $class ) { |
| 45 |
return [ |
| 46 |
'class' => $class, |
| 47 |
'factory' => "$class::factory", |
| 48 |
]; |
| 49 |
}, self::SUBMODULES ); |
| 50 |
$this->moduleManager = new ApiModuleManager( |
| 51 |
$this, |
| 52 |
MediaWikiServices::getInstance()->getObjectFactory() |
| 53 |
); |
| 54 |
$this->moduleManager->addModules( $submodules, 'command' ); |
| 55 |
} |
| 56 |
return $this->moduleManager; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
protected function getAllowedParams(): array { |
| 61 |
return [ |
| 62 |
'command' => [ |
| 63 |
ParamValidator::PARAM_TYPE => 'submodule', |
| 64 |
ParamValidator::PARAM_REQUIRED => true, |
| 65 |
], |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
private function checkLoginState(): void { |
| 74 |
if ( !$this->getUser()->isRegistered() ) { |
| 75 |
$this->dieWithError( |
| 76 |
[ 'apierror-mustbeloggedin', $this->msg( 'action-editmyprivateinfo' ) ], |
| 77 |
'notloggedin' |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public function getHelpUrls(): string { |
| 84 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:Echo#API'; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
public function isWriteMode(): bool { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
public function needsToken(): string { |
| 94 |
return 'csrf'; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public function isInternal(): bool { |
| 99 |
|
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
} |