* (bug 8918) Enable PLURAL option for MediaWiki:showingresults and
[mediawiki.git] / includes / WikiError.php
blob1b3f7f0aa9675178ce7aad1454e2549e03f759be
1 <?php
2 /**
3 * MediaWiki error classes
4 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
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.
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.
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
24 /**
25 * Since PHP4 doesn't have exceptions, here's some error objects
26 * loosely modeled on the standard PEAR_Error model...
28 class WikiError {
29 /**
30 * @param string $message
32 function __construct( $message ) {
33 $this->mMessage = $message;
36 /**
37 * @return string Plaintext error message to display
39 function getMessage() {
40 return $this->mMessage;
43 /**
44 * In following PEAR_Error model this could be formatted differently,
45 * but so far it's not.
46 * @return string
48 function toString() {
49 return $this->getMessage();
52 /**
53 * Returns true if the given object is a WikiError-descended
54 * error object, false otherwise.
56 * @param mixed $object
57 * @return bool
58 * @static
60 public static function isError( $object ) {
61 return $object instanceof WikiError;
65 /**
66 * Localized error message object
68 class WikiErrorMsg extends WikiError {
69 /**
70 * @param string $message Wiki message name
71 * @param ... parameters to pass to wfMsg()
73 function WikiErrorMsg( $message/*, ... */ ) {
74 $args = func_get_args();
75 array_shift( $args );
76 $this->mMessage = wfMsgReal( $message, $args, true );
80 /**
81 * @todo document
83 class WikiXmlError extends WikiError {
84 /**
85 * @param resource $parser
86 * @param string $message
88 function WikiXmlError( $parser, $message = 'XML parsing error', $context = null, $offset = 0 ) {
89 $this->mXmlError = xml_get_error_code( $parser );
90 $this->mColumn = xml_get_current_column_number( $parser );
91 $this->mLine = xml_get_current_line_number( $parser );
92 $this->mByte = xml_get_current_byte_index( $parser );
93 $this->mContext = $this->_extractContext( $context, $offset );
94 $this->mMessage = $message;
95 xml_parser_free( $parser );
96 wfDebug( "WikiXmlError: " . $this->getMessage() . "\n" );
99 /** @return string */
100 function getMessage() {
101 return sprintf( '%s at line %d, col %d (byte %d%s): %s',
102 $this->mMessage,
103 $this->mLine,
104 $this->mColumn,
105 $this->mByte,
106 $this->mContext,
107 xml_error_string( $this->mXmlError ) );
110 function _extractContext( $context, $offset ) {
111 if( is_null( $context ) ) {
112 return null;
113 } else {
114 // Hopefully integer overflow will be handled transparently here
115 $inlineOffset = $this->mByte - $offset;
116 return '; "' . substr( $context, $inlineOffset, 16 ) . '"';