ApiFormatFeedWrapper.php 3.5 KB

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