Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 44 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ApiFeaturedFeeds | |
0.00% |
0 / 44 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getCustomPrinter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
getAllowedParams | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\FeaturedFeeds; |
4 | |
5 | use MediaWiki\Api\ApiBase; |
6 | use MediaWiki\Api\ApiFormatFeedWrapper; |
7 | use MediaWiki\Api\ApiMain; |
8 | use MediaWiki\Languages\LanguageNameUtils; |
9 | use MediaWiki\MainConfigNames; |
10 | use MediaWiki\Title\Title; |
11 | use Wikimedia\ParamValidator\ParamValidator; |
12 | |
13 | class ApiFeaturedFeeds extends ApiBase { |
14 | private LanguageNameUtils $languageNameUtils; |
15 | |
16 | public function __construct( |
17 | ApiMain $main, |
18 | string $action, |
19 | LanguageNameUtils $languageNameUtils |
20 | ) { |
21 | parent::__construct( $main, $action ); |
22 | $this->languageNameUtils = $languageNameUtils; |
23 | } |
24 | |
25 | /** |
26 | * This module uses a custom feed wrapper printer. |
27 | * |
28 | * @return ApiFormatFeedWrapper |
29 | */ |
30 | public function getCustomPrinter() { |
31 | return new ApiFormatFeedWrapper( $this->getMain() ); |
32 | } |
33 | |
34 | public function execute() { |
35 | $params = $this->extractRequestParams(); |
36 | |
37 | $feedClasses = $this->getConfig()->get( MainConfigNames::FeedClasses ); |
38 | |
39 | if ( !isset( $feedClasses[$params['feedformat']] ) ) { |
40 | $this->dieWithError( 'feed-invalid' ); |
41 | } |
42 | |
43 | $language = $params['language'] ?? false; |
44 | if ( $language !== false && |
45 | !$this->languageNameUtils->isValidCode( $language ) |
46 | ) { |
47 | $language = false; |
48 | } |
49 | $feeds = FeaturedFeeds::getFeeds( $language ); |
50 | $ourFeed = $feeds[$params['feed']]; |
51 | |
52 | $feedClass = new $feedClasses[$params['feedformat']] ( |
53 | $ourFeed->title, |
54 | $ourFeed->description, |
55 | wfExpandUrl( Title::newMainPage()->getFullURL() ) |
56 | ); |
57 | |
58 | ApiFormatFeedWrapper::setResult( $this->getResult(), $feedClass, $ourFeed->getFeedItems() ); |
59 | |
60 | // Cache stuff in squids |
61 | $this->getMain()->setCacheMode( 'public' ); |
62 | $this->getMain()->setCacheMaxAge( FeaturedFeeds::getMaxAge() ); |
63 | } |
64 | |
65 | /** @inheritDoc */ |
66 | public function getAllowedParams() { |
67 | $feedFormatNames = array_keys( $this->getConfig()->get( MainConfigNames::FeedClasses ) ); |
68 | $availableFeeds = array_keys( FeaturedFeeds::getFeeds( false ) ); |
69 | return [ |
70 | 'feedformat' => [ |
71 | ParamValidator::PARAM_DEFAULT => 'rss', |
72 | ParamValidator::PARAM_TYPE => $feedFormatNames |
73 | ], |
74 | 'feed' => [ |
75 | ParamValidator::PARAM_TYPE => $availableFeeds, |
76 | ParamValidator::PARAM_REQUIRED => true, |
77 | ], |
78 | 'language' => [ |
79 | ParamValidator::PARAM_TYPE => 'string', |
80 | ] |
81 | ]; |
82 | } |
83 | |
84 | /** |
85 | * @see ApiBase::getExamplesMessages() |
86 | * @return array |
87 | */ |
88 | protected function getExamplesMessages() { |
89 | // attempt to find a valid feed name |
90 | // if none available, just use an example value |
91 | $availableFeeds = array_keys( FeaturedFeeds::getFeeds( false ) ); |
92 | $feed = reset( $availableFeeds ); |
93 | if ( !$feed ) { |
94 | $feed = 'featured'; |
95 | } |
96 | |
97 | return [ |
98 | "action=featuredfeed&feed=$feed" |
99 | => [ 'apihelp-featuredfeed-example-1', $feed ], |
100 | ]; |
101 | } |
102 | } |