ApiFormatRaw.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright © 2009 Roan Kattouw "<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. * Formatter that spits out anything you like with any desired MIME type
  24. * @ingroup API
  25. */
  26. class ApiFormatRaw extends ApiFormatBase {
  27. private $errorFallback;
  28. private $mFailWithHTTPError = false;
  29. /**
  30. * @param ApiMain $main
  31. * @param ApiFormatBase|null $errorFallback Object to fall back on for errors
  32. */
  33. public function __construct( ApiMain $main, ApiFormatBase $errorFallback = null ) {
  34. parent::__construct( $main, 'raw' );
  35. $this->errorFallback = $errorFallback ?:
  36. $main->createPrinterByName( $main->getParameter( 'format' ) );
  37. }
  38. public function getMimeType() {
  39. $data = $this->getResult()->getResultData();
  40. if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
  41. return $this->errorFallback->getMimeType();
  42. }
  43. if ( !isset( $data['mime'] ) ) {
  44. ApiBase::dieDebug( __METHOD__, 'No MIME type set for raw formatter' );
  45. }
  46. return $data['mime'];
  47. }
  48. public function getFilename() {
  49. $data = $this->getResult()->getResultData();
  50. if ( isset( $data['error'] ) ) {
  51. return $this->errorFallback->getFilename();
  52. } elseif ( !isset( $data['filename'] ) || $this->getIsWrappedHtml() || $this->getIsHtml() ) {
  53. return parent::getFilename();
  54. } else {
  55. return $data['filename'];
  56. }
  57. }
  58. public function initPrinter( $unused = false ) {
  59. $data = $this->getResult()->getResultData();
  60. if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
  61. $this->errorFallback->initPrinter( $unused );
  62. if ( $this->mFailWithHTTPError ) {
  63. $this->getMain()->getRequest()->response()->statusHeader( 400 );
  64. }
  65. } else {
  66. parent::initPrinter( $unused );
  67. }
  68. }
  69. public function closePrinter() {
  70. $data = $this->getResult()->getResultData();
  71. if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
  72. $this->errorFallback->closePrinter();
  73. } else {
  74. parent::closePrinter();
  75. }
  76. }
  77. public function execute() {
  78. $data = $this->getResult()->getResultData();
  79. if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
  80. $this->errorFallback->execute();
  81. return;
  82. }
  83. if ( !isset( $data['text'] ) ) {
  84. ApiBase::dieDebug( __METHOD__, 'No text given for raw formatter' );
  85. }
  86. $this->printText( $data['text'] );
  87. }
  88. /**
  89. * Output HTTP error code 400 when if an error is encountered
  90. *
  91. * The purpose is for output formats where the user-agent will
  92. * not be able to interpret the validity of the content in any
  93. * other way. For example subtitle files read by browser video players.
  94. *
  95. * @param bool $fail
  96. */
  97. public function setFailWithHTTPError( $fail ) {
  98. $this->mFailWithHTTPError = $fail;
  99. }
  100. }