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