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