Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
34 / 34 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
GetFormattedTimeHandler | |
100.00% |
34 / 34 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
run | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
2 | |||
getParamSettings | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare( strict_types=1 ); |
4 | |
5 | namespace MediaWiki\Extension\CampaignEvents\Rest; |
6 | |
7 | use Exception; |
8 | use MediaWiki\Languages\LanguageFactory; |
9 | use MediaWiki\Rest\Handler; |
10 | use MediaWiki\Rest\HttpException; |
11 | use MediaWiki\Rest\Response; |
12 | use MediaWiki\Rest\SimpleHandler; |
13 | use Wikimedia\ParamValidator\ParamValidator; |
14 | |
15 | /** |
16 | * Helper endpoint to format date strings on the client side with all the features / formats supported by |
17 | * {@see Language::sprintfDate}. |
18 | * |
19 | * @internal For use by this extension only. |
20 | */ |
21 | class GetFormattedTimeHandler extends SimpleHandler { |
22 | private LanguageFactory $languageFactory; |
23 | |
24 | public function __construct( LanguageFactory $languageFactory ) { |
25 | $this->languageFactory = $languageFactory; |
26 | } |
27 | |
28 | protected function run( string $languageCode, string $startTS, string $endTS ): Response { |
29 | $language = $this->languageFactory->getLanguage( $languageCode ); |
30 | $user = $this->getAuthority()->getUser(); |
31 | // Time correction is applied in JavaScript. |
32 | $options = [ 'timecorrection' => false ]; |
33 | |
34 | try { |
35 | return $this->getResponseFactory()->createJson( [ |
36 | 'startTime' => $language->userTime( $startTS, $user, $options ), |
37 | 'startDate' => $language->userDate( $startTS, $user, $options ), |
38 | 'startDateTime' => $language->userTimeAndDate( $startTS, $user, $options ), |
39 | 'endTime' => $language->userTime( $endTS, $user, $options ), |
40 | 'endDate' => $language->userDate( $endTS, $user, $options ), |
41 | 'endDateTime' => $language->userTimeAndDate( $endTS, $user, $options ), |
42 | ] ); |
43 | } catch ( Exception $e ) { |
44 | // Probably an invalid timestamp. The Language::user* methods don't give us a narrow exception class |
45 | // to catch, so just catch everything. No need to localise errors as the module is internal, and errors |
46 | // are just ignored on the client side. |
47 | throw new HttpException( |
48 | "Invalid input timestamp ($startTS or $endTS): {$e->getMessage()}", |
49 | 400 |
50 | ); |
51 | } |
52 | } |
53 | |
54 | /** |
55 | * @inheritDoc |
56 | */ |
57 | public function getParamSettings(): array { |
58 | return [ |
59 | 'languageCode' => [ |
60 | Handler::PARAM_SOURCE => 'path', |
61 | ParamValidator::PARAM_TYPE => 'string', |
62 | ParamValidator::PARAM_REQUIRED => true, |
63 | ], |
64 | 'start' => [ |
65 | Handler::PARAM_SOURCE => 'path', |
66 | ParamValidator::PARAM_TYPE => 'string', |
67 | ParamValidator::PARAM_REQUIRED => true, |
68 | ], |
69 | 'end' => [ |
70 | Handler::PARAM_SOURCE => 'path', |
71 | ParamValidator::PARAM_TYPE => 'string', |
72 | ParamValidator::PARAM_REQUIRED => true, |
73 | ], |
74 | ]; |
75 | } |
76 | } |