Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ApiCentralNoticeCdnCacheUpdateBanner | |
0.00% |
0 / 33 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
| getAllowedParams | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | use MediaWiki\Api\ApiBase; |
| 4 | use MediaWiki\Api\ApiMain; |
| 5 | use MediaWiki\Deferred\DeferredUpdates; |
| 6 | use MediaWiki\Language\LanguageNameUtils; |
| 7 | use Wikimedia\ParamValidator\ParamValidator; |
| 8 | |
| 9 | /** |
| 10 | * Module for the centralnoticecdncacheupdatebanner Web API. Used by a background call |
| 11 | * via JS from Special:CentralNoticeBanners, to purge banner content from the front-end |
| 12 | * cache, for a user-specified language. |
| 13 | */ |
| 14 | class ApiCentralNoticeCdnCacheUpdateBanner extends ApiBase { |
| 15 | |
| 16 | public function __construct( |
| 17 | ApiMain $mainModule, |
| 18 | string $moduleName, |
| 19 | private readonly LanguageNameUtils $languageNameUtils, |
| 20 | ) { |
| 21 | parent::__construct( $mainModule, $moduleName ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @inheritDoc |
| 26 | */ |
| 27 | public function execute() { |
| 28 | if ( !$this->getUser()->isAllowed( 'centralnotice-admin' ) ) { |
| 29 | $this->dieWithError( 'apierror-centralnotice-cdn-permissions-error' ); |
| 30 | } |
| 31 | |
| 32 | $params = $this->extractRequestParams(); |
| 33 | $langCode = $params[ 'language' ]; |
| 34 | |
| 35 | if ( !$this->languageNameUtils->isValidCode( $langCode ) ) { |
| 36 | $this->dieWithError( 'apierror-centralnotice-cdn-lang-code-error' ); |
| 37 | } |
| 38 | |
| 39 | $bannerName = $params[ 'banner' ]; |
| 40 | |
| 41 | if ( !Banner::isValidBannerName( $bannerName ) ) { |
| 42 | $this->dieWithError( 'apierror-centralnotice-cdn-banner-name-error' ); |
| 43 | } |
| 44 | |
| 45 | // Get the banner object |
| 46 | $banner = Banner::fromName( $bannerName ); |
| 47 | if ( !$banner->exists() ) { |
| 48 | $this->dieWithError( 'apierror-centralnotice-cdn-banner-not-found' ); |
| 49 | } |
| 50 | |
| 51 | // Deferred update to purge CDN caches for banner content |
| 52 | DeferredUpdates::addUpdate( |
| 53 | new CdnCacheUpdateBannerLoader( $langCode, $banner ), |
| 54 | DeferredUpdates::PRESEND |
| 55 | ); |
| 56 | |
| 57 | $this->getResult()->addValue( null, $this->getModuleName(), 'update_requested' ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @inheritDoc |
| 62 | */ |
| 63 | public function getAllowedParams() { |
| 64 | return [ |
| 65 | 'banner' => [ |
| 66 | ParamValidator::PARAM_TYPE => 'string', |
| 67 | ParamValidator::PARAM_REQUIRED => true, |
| 68 | ], |
| 69 | 'language' => [ |
| 70 | ParamValidator::PARAM_TYPE => 'string', |
| 71 | ParamValidator::PARAM_REQUIRED => true, |
| 72 | ] |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @inheritDoc |
| 78 | */ |
| 79 | public function needsToken() { |
| 80 | return 'csrf'; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @inheritDoc |
| 85 | */ |
| 86 | protected function getExamplesMessages() { |
| 87 | return [ |
| 88 | 'action=centralnoticecdncacheupdatebanner&token=ABC123&banner=Banner1&language=en' |
| 89 | => 'apihelp-centralnoticecdncacheupdatebanner-example-1' |
| 90 | ]; |
| 91 | } |
| 92 | } |