ErrorPageError.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * An error page which can definitely be safely rendered using the OutputPage.
  22. *
  23. * @since 1.7
  24. * @ingroup Exception
  25. */
  26. class ErrorPageError extends MWException implements ILocalizedException {
  27. const SEND_OUTPUT = 0;
  28. const STAGE_OUTPUT = 1;
  29. public $title, $msg, $params;
  30. /**
  31. * Note: these arguments are keys into wfMessage(), not text!
  32. *
  33. * @param string|Message $title Message key (string) for page title, or a Message object
  34. * @param string|Message $msg Message key (string) for error text, or a Message object
  35. * @param array $params Array with parameters to wfMessage()
  36. */
  37. public function __construct( $title, $msg, $params = [] ) {
  38. $this->title = $title;
  39. $this->msg = $msg;
  40. $this->params = $params;
  41. // T46111: Messages in the log files should be in English and not
  42. // customized by the local wiki. So get the default English version for
  43. // passing to the parent constructor. Our overridden report() below
  44. // makes sure that the page shown to the user is not forced to English.
  45. $enMsg = $this->getMessageObject();
  46. $enMsg->inLanguage( 'en' )->useDatabase( false );
  47. parent::__construct( $enMsg->text() );
  48. }
  49. /**
  50. * Return a Message object for this exception
  51. * @since 1.29
  52. * @return Message
  53. */
  54. public function getMessageObject() {
  55. if ( $this->msg instanceof Message ) {
  56. return clone $this->msg;
  57. }
  58. return wfMessage( $this->msg, $this->params );
  59. }
  60. public function report( $action = self::SEND_OUTPUT ) {
  61. if ( self::isCommandLine() || defined( 'MW_API' ) ) {
  62. parent::report();
  63. } else {
  64. global $wgOut;
  65. $wgOut->showErrorPage( $this->title, $this->msg, $this->params );
  66. // Allow skipping of the final output step, so that web-based page views
  67. // from MediaWiki.php, can inspect the staged OutputPage state, and perform
  68. // graceful shutdown via doPreOutputCommit first, just like for regular
  69. // output when there isn't an error page.
  70. if ( $action === self::SEND_OUTPUT ) {
  71. $wgOut->output();
  72. }
  73. }
  74. }
  75. }