| 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 | |
| 23 | namespace MediaWiki\Api; |
| 24 | |
| 25 | use MediaWiki\Feed\ChannelFeed; |
| 26 | use MediaWiki\Feed\FeedItem; |
| 27 | |
| 28 | /** |
| 29 | * This printer is used to wrap an instance of the Feed class |
| 30 | * @ingroup API |
| 31 | */ |
| 32 | class ApiFormatFeedWrapper extends ApiFormatBase { |
| 33 | |
| 34 | public function __construct( ApiMain $main ) { |
| 35 | parent::__construct( $main, 'feed' ); |
| 36 | } |
| 37 | |
| 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 |
| 43 | */ |
| 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 ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Feed does its own headers |
| 56 | * |
| 57 | * @return null |
| 58 | */ |
| 59 | public function getMimeType() { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 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 |
| 67 | */ |
| 68 | public function canPrintErrors() { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 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 |
| 77 | */ |
| 78 | public function initPrinter( $unused = false ) { |
| 79 | parent::initPrinter( $unused ); |
| 80 | |
| 81 | if ( $this->isDisabled() ) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 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' ); |
| 93 | } |
| 94 | } |
| 95 | |
| 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 |
| 100 | */ |
| 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']; |
| 107 | |
| 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 ); |
| 113 | } |
| 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' ); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** @deprecated class alias since 1.43 */ |
| 124 | class_alias( ApiFormatFeedWrapper::class, 'ApiFormatFeedWrapper' ); |