ApiFormatFeedWrapper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * This printer is used to wrap an instance of the Feed class
  24. * @ingroup API
  25. */
  26. class ApiFormatFeedWrapper extends ApiFormatBase {
  27. public function __construct( ApiMain $main ) {
  28. parent::__construct( $main, 'feed' );
  29. }
  30. /**
  31. * Call this method to initialize output data. See execute()
  32. * @param ApiResult $result
  33. * @param object $feed An instance of one of the $wgFeedClasses classes
  34. * @param array $feedItems Array of FeedItem objects
  35. */
  36. public static function setResult( $result, $feed, $feedItems ) {
  37. // Store output in the Result data.
  38. // This way we can check during execution if any error has occurred
  39. // Disable size checking for this because we can't continue
  40. // cleanly; size checking would cause more problems than it'd
  41. // solve
  42. $result->addValue( null, '_feed', $feed, ApiResult::NO_VALIDATE );
  43. $result->addValue( null, '_feeditems', $feedItems, ApiResult::NO_VALIDATE );
  44. }
  45. /**
  46. * Feed does its own headers
  47. *
  48. * @return null
  49. */
  50. public function getMimeType() {
  51. return null;
  52. }
  53. /**
  54. * ChannelFeed doesn't give us a method to print errors in a friendly
  55. * manner, so just punt errors to the default printer.
  56. * @return bool
  57. */
  58. public function canPrintErrors() {
  59. return false;
  60. }
  61. /**
  62. * This class expects the result data to be in a custom format set by self::setResult()
  63. * $result['_feed'] - an instance of one of the $wgFeedClasses classes
  64. * $result['_feeditems'] - an array of FeedItem instances
  65. * @param bool $unused
  66. */
  67. public function initPrinter( $unused = false ) {
  68. parent::initPrinter( $unused );
  69. if ( $this->isDisabled() ) {
  70. return;
  71. }
  72. $data = $this->getResult()->getResultData();
  73. if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
  74. /** @var ChannelFeed $feed */
  75. $feed = $data['_feed'];
  76. $feed->httpHeaders();
  77. } else {
  78. // Error has occurred, print something useful
  79. ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
  80. }
  81. }
  82. /**
  83. * This class expects the result data to be in a custom format set by self::setResult()
  84. * $result['_feed'] - an instance of one of the $wgFeedClasses classes
  85. * $result['_feeditems'] - an array of FeedItem instances
  86. */
  87. public function execute() {
  88. $data = $this->getResult()->getResultData();
  89. if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
  90. /** @var ChannelFeed $feed */
  91. $feed = $data['_feed'];
  92. $items = $data['_feeditems'];
  93. // execute() needs to pass strings to $this->printText, not produce output itself.
  94. ob_start();
  95. $feed->outHeader();
  96. foreach ( $items as & $item ) {
  97. $feed->outItem( $item );
  98. }
  99. $feed->outFooter();
  100. $this->printText( ob_get_clean() );
  101. } else {
  102. // Error has occurred, print something useful
  103. ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
  104. }
  105. }
  106. }