Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
74.63% |
50 / 67 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CargoFeedFormat | |
74.63% |
50 / 67 |
|
0.00% |
0 / 4 |
23.29 | |
0.00% |
0 / 1 |
allowedParameters | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
queryAndDisplay | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
7.60 | |||
getFeedType | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
outputFeed | |
95.24% |
40 / 42 |
|
0.00% |
0 / 1 |
8 |
1 | <?php |
2 | /** |
3 | * @ingroup Cargo |
4 | * @file |
5 | */ |
6 | |
7 | use MediaWiki\Feed\FeedItem; |
8 | use MediaWiki\Html\Html; |
9 | use MediaWiki\MediaWikiServices; |
10 | use MediaWiki\Title\Title; |
11 | |
12 | /** |
13 | * Handle the feed export format. |
14 | * @since 3.5 |
15 | */ |
16 | class CargoFeedFormat extends CargoDeferredFormat { |
17 | |
18 | public static function allowedParameters() { |
19 | return [ |
20 | 'feed type' => [ 'values' => [ 'atom', 'rss' ] ], |
21 | 'link text' => [ 'type' => 'string' ], |
22 | 'feed title' => [ 'type' => 'string' ], |
23 | 'feed description' => [ 'type' => 'string' ], |
24 | ]; |
25 | } |
26 | |
27 | /** |
28 | * @param CargoSQLQuery[] $sqlQueries |
29 | * @param string[] $displayParams Unused |
30 | * @param string[]|null $querySpecificParams Unused |
31 | * @return string An HTML link to Special:CargoExport with the required query string. |
32 | */ |
33 | public function queryAndDisplay( $sqlQueries, $displayParams, $querySpecificParams = null ) { |
34 | $queryParams = $this->sqlQueriesToQueryParams( $sqlQueries ); |
35 | $queryParams['format'] = 'feed'; |
36 | |
37 | // Feed type. Defaults to the first one set in $wgAdvertisedFeedTypes. |
38 | $queryParams['feed type'] = $this->getFeedType( $displayParams['feed type'] ?? null ); |
39 | |
40 | // Feed title. |
41 | if ( isset( $displayParams['feed title'] ) && trim( $displayParams['feed title'] ) != '' ) { |
42 | $queryParams['feed title'] = $displayParams['feed title']; |
43 | } |
44 | |
45 | // Feed description. |
46 | if ( isset( $displayParams['feed description'] ) && trim( $displayParams['feed description'] ) !== '' ) { |
47 | $queryParams['feed description'] = $displayParams['feed description']; |
48 | } |
49 | |
50 | // Link. |
51 | if ( isset( $displayParams['link text'] ) && $displayParams['link text'] ) { |
52 | $linkText = $displayParams['link text']; |
53 | } else { |
54 | // The following messages can be used here: |
55 | // * feed-rss |
56 | // * feed-atom |
57 | $feedName = wfMessage( 'feed-' . $queryParams['feed type'] ); |
58 | $linkText = wfMessage( 'cargo-viewfeed', [ $feedName ] )->parse(); |
59 | } |
60 | |
61 | // Output full anchor element. The array_filter is to avoid empty params. |
62 | $export = SpecialPage::getTitleFor( 'CargoExport' ); |
63 | return Html::element( 'a', [ 'href' => $export->getFullURL( array_filter( $queryParams ) ) ], $linkText ); |
64 | } |
65 | |
66 | /** |
67 | * Determine the feed type (Atom or RSS). |
68 | * @param string|null $in The user-provided value. |
69 | * @return string Either 'atom' or 'rss'. |
70 | */ |
71 | private function getFeedType( ?string $in = null ): string { |
72 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
73 | $types = array_keys( $config->get( 'FeedClasses' ) ); |
74 | |
75 | // User-provided (if it's valid). |
76 | $inType = strtolower( trim( $in ?? '' ) ); |
77 | if ( in_array( $inType, $types ) ) { |
78 | return $inType; |
79 | } |
80 | |
81 | // Otherwise, fall back to Atom. |
82 | return 'atom'; |
83 | } |
84 | |
85 | /** |
86 | * Get the iCalendar format output. |
87 | * @param WebRequest $request |
88 | * @param CargoSQLQuery[] $sqlQueries |
89 | * @return string |
90 | */ |
91 | public function outputFeed( WebRequest $request, $sqlQueries ) { |
92 | $queryParams = $this->sqlQueriesToQueryParams( $sqlQueries ); |
93 | $queryParams['format'] = 'feed'; |
94 | $feedType = $this->getFeedType( $request->getText( 'feed_type' ) ); |
95 | $title = $request->getText( 'feed_title', 'News feed' ); |
96 | $description = $request->getText( 'feed_description', '' ); |
97 | |
98 | $feedClasses = MediaWikiServices::getInstance()->getMainConfig()->get( 'FeedClasses' ); |
99 | /** @var RSSFeed|AtomFeed $feed */ |
100 | $feed = new $feedClasses[$feedType]( $title, $description, $request->getFullRequestURL() ); |
101 | |
102 | $services = MediaWikiServices::getInstance(); |
103 | $parser = $services->getParser(); |
104 | $pageTitle = $parser->getTitle(); |
105 | $parserOptions = ParserOptions::newFromAnon(); |
106 | if ( method_exists( $parserOptions, 'setSuppressSectionEditLinks' ) ) { |
107 | // MW 1.42+ |
108 | $parserOptions->setSuppressSectionEditLinks(); |
109 | } |
110 | $contentRenderer = $services->getContentRenderer(); |
111 | $items = []; |
112 | foreach ( $sqlQueries as $sqlQuery ) { |
113 | $dateFields = $sqlQuery->getMainStartAndEndDateFields(); |
114 | |
115 | $queryResults = $sqlQuery->run(); |
116 | foreach ( $queryResults as $queryResult ) { |
117 | $title = Title::newFromText( $queryResult['_pageName'] ); |
118 | if ( isset( $queryResult['description'] ) ) { |
119 | $description = $queryResult['description']; |
120 | $parserOutput = $parser->parse( $description, $pageTitle, $parserOptions ); |
121 | } else { |
122 | $wikiPage = new WikiPage( $title ); |
123 | $parserOutput = $contentRenderer->getParserOutput( $wikiPage->getContent(), $title, null, $parserOptions ); |
124 | } |
125 | $description = $parserOutput->getText(); |
126 | $item = new FeedItem( |
127 | $queryResult['title'] ?? $queryResult['_pageName'] ?? '', |
128 | $description, |
129 | $queryResult['url'] ?? $title->getCanonicalURL(), |
130 | $queryResult[$dateFields[0]] ?? '', |
131 | $queryResult['author'] ?? '', |
132 | $queryResult['comments'] ?? '' |
133 | ); |
134 | if ( isset( $queryResult['id'] ) && !empty( $queryResult['id'] ) ) { |
135 | $item->setUniqueId( $queryResult['id'] ); |
136 | } else { |
137 | $item->setUniqueId( $title->getCanonicalURL(), true ); |
138 | } |
139 | $items[$queryResult[$dateFields[0]]] = $item; |
140 | } |
141 | } |
142 | |
143 | // Output feed content. |
144 | $feed->outHeader(); |
145 | foreach ( $items as $item ) { |
146 | $feed->outItem( $item ); |
147 | } |
148 | $feed->outFooter(); |
149 | } |
150 | } |