summaryrefslogtreecommitdiff
path: root/includes/api/ApiFormatFeedWrapper.php
blob: d2bfd48d33c7870ab079df4a394c172eb5bbfcfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
 *
 *
 * Created on Sep 19, 2006
 *
 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

/**
 * This printer is used to wrap an instance of the Feed class
 * @ingroup API
 */
class ApiFormatFeedWrapper extends ApiFormatBase {

	public function __construct( ApiMain $main ) {
		parent::__construct( $main, 'feed' );
	}

	/**
	 * Call this method to initialize output data. See execute()
	 * @param ApiResult $result
	 * @param object $feed An instance of one of the $wgFeedClasses classes
	 * @param array $feedItems Array of FeedItem objects
	 */
	public static function setResult( $result, $feed, $feedItems ) {
		// Store output in the Result data.
		// This way we can check during execution if any error has occurred
		// Disable size checking for this because we can't continue
		// cleanly; size checking would cause more problems than it'd
		// solve
		$result->addValue( null, '_feed', $feed, ApiResult::NO_VALIDATE );
		$result->addValue( null, '_feeditems', $feedItems, ApiResult::NO_VALIDATE );
	}

	/**
	 * Feed does its own headers
	 *
	 * @return null
	 */
	public function getMimeType() {
		return null;
	}

	/**
	 * ChannelFeed doesn't give us a method to print errors in a friendly
	 * manner, so just punt errors to the default printer.
	 * @return bool
	 */
	public function canPrintErrors() {
		return false;
	}

	/**
	 * This class expects the result data to be in a custom format set by self::setResult()
	 * $result['_feed'] - an instance of one of the $wgFeedClasses classes
	 * $result['_feeditems'] - an array of FeedItem instances
	 */
	public function initPrinter( $unused = false ) {
		parent::initPrinter( $unused );

		if ( $this->isDisabled() ) {
			return;
		}

		$data = $this->getResult()->getResultData();
		if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
			$data['_feed']->httpHeaders();
		} else {
			// Error has occurred, print something useful
			ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
		}
	}

	/**
	 * This class expects the result data to be in a custom format set by self::setResult()
	 * $result['_feed'] - an instance of one of the $wgFeedClasses classes
	 * $result['_feeditems'] - an array of FeedItem instances
	 */
	public function execute() {
		$data = $this->getResult()->getResultData();
		if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
			$feed = $data['_feed'];
			$items = $data['_feeditems'];

			// execute() needs to pass strings to $this->printText, not produce output itself.
			ob_start();
			$feed->outHeader();
			foreach ( $items as & $item ) {
				$feed->outItem( $item );
			}
			$feed->outFooter();
			$this->printText( ob_get_clean() );
		} else {
			// Error has occurred, print something useful
			ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
		}
	}
}