WikiError.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * MediaWiki error classes
  4. * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
  5. * http://www.mediawiki.org/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. */
  23. /**
  24. * Since PHP4 doesn't have exceptions, here's some error objects
  25. * loosely modeled on the standard PEAR_Error model...
  26. * @ingroup Exception
  27. */
  28. class WikiError {
  29. /**
  30. * @param $message string
  31. */
  32. function __construct( $message ) {
  33. $this->mMessage = $message;
  34. }
  35. /**
  36. * @return string Plaintext error message to display
  37. */
  38. function getMessage() {
  39. return $this->mMessage;
  40. }
  41. /**
  42. * In following PEAR_Error model this could be formatted differently,
  43. * but so far it's not.
  44. * @return string
  45. */
  46. function toString() {
  47. return $this->getMessage();
  48. }
  49. /**
  50. * Returns true if the given object is a WikiError-descended
  51. * error object, false otherwise.
  52. *
  53. * @param $object mixed
  54. * @return bool
  55. */
  56. public static function isError( $object ) {
  57. return $object instanceof WikiError;
  58. }
  59. }
  60. /**
  61. * Localized error message object
  62. * @ingroup Exception
  63. */
  64. class WikiErrorMsg extends WikiError {
  65. /**
  66. * @param $message String: wiki message name
  67. * @param ... parameters to pass to wfMsg()
  68. */
  69. function WikiErrorMsg( $message/*, ... */ ) {
  70. $args = func_get_args();
  71. array_shift( $args );
  72. $this->mMessage = wfMsgReal( $message, $args, true );
  73. $this->mMsgKey = $message;
  74. $this->mMsgArgs = $args;
  75. }
  76. function getMessageKey() {
  77. return $this->mMsgKey;
  78. }
  79. function getMessageArgs() {
  80. return $this->mMsgArgs;
  81. }
  82. }
  83. /**
  84. * Error class designed to handle errors involved with
  85. * XML parsing
  86. * @ingroup Exception
  87. */
  88. class WikiXmlError extends WikiError {
  89. /**
  90. * @param $parser resource
  91. * @param $message string
  92. * @param $context
  93. * @param $offset Int
  94. */
  95. function WikiXmlError( $parser, $message = 'XML parsing error', $context = null, $offset = 0 ) {
  96. $this->mXmlError = xml_get_error_code( $parser );
  97. $this->mColumn = xml_get_current_column_number( $parser );
  98. $this->mLine = xml_get_current_line_number( $parser );
  99. $this->mByte = xml_get_current_byte_index( $parser );
  100. $this->mContext = $this->_extractContext( $context, $offset );
  101. $this->mMessage = $message;
  102. xml_parser_free( $parser );
  103. wfDebug( "WikiXmlError: " . $this->getMessage() . "\n" );
  104. }
  105. /** @return string */
  106. function getMessage() {
  107. // '$1 at line $2, col $3 (byte $4): $5',
  108. return wfMsgHtml( 'xml-error-string',
  109. $this->mMessage,
  110. $this->mLine,
  111. $this->mColumn,
  112. $this->mByte . $this->mContext,
  113. xml_error_string( $this->mXmlError ) );
  114. }
  115. function _extractContext( $context, $offset ) {
  116. if( is_null( $context ) ) {
  117. return null;
  118. } else {
  119. // Hopefully integer overflow will be handled transparently here
  120. $inlineOffset = $this->mByte - $offset;
  121. return '; "' . substr( $context, $inlineOffset, 16 ) . '"';
  122. }
  123. }
  124. }