Prevent the following strict-standards warnings - i.e. when running with error_loggin...
[lhc/web/wiklou.git] / includes / WikiError.php
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 * @package MediaWiki
23 */
24
25 /**
26 * Since PHP4 doesn't have exceptions, here's some error objects
27 * loosely modeled on the standard PEAR_Error model...
28 * @package MediaWiki
29 */
30 class WikiError {
31 /**
32 * @param string $message
33 */
34 function WikiError( $message ) {
35 $this->mMessage = $message;
36 }
37
38 /**
39 * @return string Plaintext error message to display
40 */
41 function getMessage() {
42 return $this->mMessage;
43 }
44
45 /**
46 * In following PEAR_Error model this could be formatted differently,
47 * but so far it's not.
48 * @return string
49 */
50 function toString() {
51 return $this->getMessage();
52 }
53
54 /**
55 * Returns true if the given object is a WikiError-descended
56 * error object, false otherwise.
57 *
58 * @param mixed $object
59 * @return bool
60 * @static
61 */
62 public static function isError( &$object ) {
63 return $object instanceof WikiError;
64 }
65 }
66
67 /**
68 * Localized error message object
69 * @package MediaWiki
70 */
71 class WikiErrorMsg extends WikiError {
72 /**
73 * @param string $message Wiki message name
74 * @param ... parameters to pass to wfMsg()
75 */
76 function WikiErrorMsg( $message/*, ... */ ) {
77 $args = func_get_args();
78 array_shift( $args );
79 $this->mMessage = wfMsgReal( $message, $args, true );
80 }
81 }
82
83 /**
84 * @package MediaWiki
85 * @todo document
86 */
87 class WikiXmlError extends WikiError {
88 /**
89 * @param resource $parser
90 * @param string $message
91 */
92 function WikiXmlError( $parser, $message = 'XML parsing error', $context = null, $offset = 0 ) {
93 $this->mXmlError = xml_get_error_code( $parser );
94 $this->mColumn = xml_get_current_column_number( $parser );
95 $this->mLine = xml_get_current_line_number( $parser );
96 $this->mByte = xml_get_current_byte_index( $parser );
97 $this->mContext = $this->_extractContext( $context, $offset );
98 $this->mMessage = $message;
99 xml_parser_free( $parser );
100 wfDebug( "WikiXmlError: " . $this->getMessage() . "\n" );
101 }
102
103 /** @return string */
104 function getMessage() {
105 return sprintf( '%s at line %d, col %d (byte %d%s): %s',
106 $this->mMessage,
107 $this->mLine,
108 $this->mColumn,
109 $this->mByte,
110 $this->mContext,
111 xml_error_string( $this->mXmlError ) );
112 }
113
114 function _extractContext( $context, $offset ) {
115 if( is_null( $context ) ) {
116 return null;
117 } else {
118 // Hopefully integer overflow will be handled transparently here
119 $inlineOffset = $this->mByte - $offset;
120 return '; "' . substr( $context, $inlineOffset, 16 ) . '"';
121 }
122 }
123 }
124
125 ?>