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