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