Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiFormatFeedWrapper
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 6
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setResult
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getMimeType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 canPrintErrors
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 initPrinter
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 execute
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23use MediaWiki\Feed\ChannelFeed;
24use MediaWiki\Feed\FeedItem;
25
26/**
27 * This printer is used to wrap an instance of the Feed class
28 * @ingroup API
29 */
30class ApiFormatFeedWrapper extends ApiFormatBase {
31
32    public function __construct( ApiMain $main ) {
33        parent::__construct( $main, 'feed' );
34    }
35
36    /**
37     * Call this method to initialize output data. See execute()
38     * @param ApiResult $result
39     * @param FeedItem $feed An instance of one of the $wgFeedClasses classes
40     * @param FeedItem[] $feedItems
41     */
42    public static function setResult( $result, $feed, $feedItems ) {
43        // Store output in the Result data.
44        // This way we can check during execution if any error has occurred
45        // Disable size checking for this because we can't continue
46        // cleanly; size checking would cause more problems than it'd
47        // solve
48        $result->addValue( null, '_feed', $feed, ApiResult::NO_VALIDATE );
49        $result->addValue( null, '_feeditems', $feedItems, ApiResult::NO_VALIDATE );
50    }
51
52    /**
53     * Feed does its own headers
54     *
55     * @return null
56     */
57    public function getMimeType() {
58        return null;
59    }
60
61    /**
62     * MediaWiki\Feed\ChannelFeed doesn't give us a method to print errors in a friendly
63     * manner, so just punt errors to the default printer.
64     * @return bool
65     */
66    public function canPrintErrors() {
67        return false;
68    }
69
70    /**
71     * This class expects the result data to be in a custom format set by self::setResult()
72     * $result['_feed'] - an instance of one of the $wgFeedClasses classes
73     * $result['_feeditems'] - an array of MediaWiki\Feed\FeedItem instances
74     * @param bool $unused
75     */
76    public function initPrinter( $unused = false ) {
77        parent::initPrinter( $unused );
78
79        if ( $this->isDisabled() ) {
80            return;
81        }
82
83        $data = $this->getResult()->getResultData();
84        if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
85            /** @var ChannelFeed $feed */
86            $feed = $data['_feed'];
87            $feed->httpHeaders();
88        } else {
89            // Error has occurred, print something useful
90            ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
91        }
92    }
93
94    /**
95     * This class expects the result data to be in a custom format set by self::setResult()
96     * $result['_feed'] - an instance of one of the $wgFeedClasses classes
97     * $result['_feeditems'] - an array of MediaWiki\Feed\FeedItem instances
98     */
99    public function execute() {
100        $data = $this->getResult()->getResultData();
101        if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
102            /** @var ChannelFeed $feed */
103            $feed = $data['_feed'];
104            $items = $data['_feeditems'];
105
106            // execute() needs to pass strings to $this->printText, not produce output itself.
107            ob_start();
108            $feed->outHeader();
109            foreach ( $items as & $item ) {
110                $feed->outItem( $item );
111            }
112            $feed->outFooter();
113            $this->printText( ob_get_clean() );
114        } else {
115            // Error has occurred, print something useful
116            ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
117        }
118    }
119}