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