Merge "Set namespaces for dtp"
[mediawiki.git] / includes / api / ApiFormatFeedWrapper.php
blob205fed23132a0be0852564ab3d329e6ff6b0bc0d
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\Feed\ChannelFeed;
26 use MediaWiki\Feed\FeedItem;
28 /**
29 * This printer is used to wrap an instance of the Feed class
30 * @ingroup API
32 class ApiFormatFeedWrapper extends ApiFormatBase {
34 public function __construct( ApiMain $main ) {
35 parent::__construct( $main, 'feed' );
38 /**
39 * Call this method to initialize output data. See execute()
40 * @param ApiResult $result
41 * @param FeedItem $feed An instance of one of the $wgFeedClasses classes
42 * @param FeedItem[] $feedItems
44 public static function setResult( $result, $feed, $feedItems ) {
45 // Store output in the Result data.
46 // This way we can check during execution if any error has occurred
47 // Disable size checking for this because we can't continue
48 // cleanly; size checking would cause more problems than it'd
49 // solve
50 $result->addValue( null, '_feed', $feed, ApiResult::NO_VALIDATE );
51 $result->addValue( null, '_feeditems', $feedItems, ApiResult::NO_VALIDATE );
54 /**
55 * Feed does its own headers
57 * @return null
59 public function getMimeType() {
60 return null;
63 /**
64 * MediaWiki\Feed\ChannelFeed doesn't give us a method to print errors in a friendly
65 * manner, so just punt errors to the default printer.
66 * @return bool
68 public function canPrintErrors() {
69 return false;
72 /**
73 * This class expects the result data to be in a custom format set by self::setResult()
74 * $result['_feed'] - an instance of one of the $wgFeedClasses classes
75 * $result['_feeditems'] - an array of MediaWiki\Feed\FeedItem instances
76 * @param bool $unused
78 public function initPrinter( $unused = false ) {
79 parent::initPrinter( $unused );
81 if ( $this->isDisabled() ) {
82 return;
85 $data = $this->getResult()->getResultData();
86 if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
87 /** @var ChannelFeed $feed */
88 $feed = $data['_feed'];
89 $feed->httpHeaders();
90 } else {
91 // Error has occurred, print something useful
92 ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
96 /**
97 * This class expects the result data to be in a custom format set by self::setResult()
98 * $result['_feed'] - an instance of one of the $wgFeedClasses classes
99 * $result['_feeditems'] - an array of MediaWiki\Feed\FeedItem instances
101 public function execute() {
102 $data = $this->getResult()->getResultData();
103 if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
104 /** @var ChannelFeed $feed */
105 $feed = $data['_feed'];
106 $items = $data['_feeditems'];
108 // execute() needs to pass strings to $this->printText, not produce output itself.
109 ob_start();
110 $feed->outHeader();
111 foreach ( $items as & $item ) {
112 $feed->outItem( $item );
114 $feed->outFooter();
115 $this->printText( ob_get_clean() );
116 } else {
117 // Error has occurred, print something useful
118 ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
123 /** @deprecated class alias since 1.43 */
124 class_alias( ApiFormatFeedWrapper::class, 'ApiFormatFeedWrapper' );