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