Minor bugfix to IP::prettifyIP.
[lhc/web/wiklou.git] / includes / WikiError.php
1 <?php
2 /**
3 * MediaWiki error classes
4 *
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
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
30 */
31 class WikiError {
32 /**
33 * @param $message string
34 *
35 * @deprecated since 1.17
36 */
37 function __construct( $message ) {
38 wfDeprecated( __METHOD__, '1.17' );
39 $this->mMessage = $message;
40 }
41
42 /**
43 * @return string Plaintext error message to display
44 */
45 function getMessage() {
46 return $this->mMessage;
47 }
48
49 /**
50 * In following PEAR_Error model this could be formatted differently,
51 * but so far it's not.
52 * @return string
53 */
54 function toString() {
55 return $this->getMessage();
56 }
57
58 /**
59 * Returns true if the given object is a WikiError-descended
60 * error object, false otherwise.
61 *
62 * @param $object mixed
63 * @return bool
64 *
65 * @deprecated since 1.17
66 */
67 public static function isError( $object ) {
68 wfDeprecated( __METHOD__, '1.17' );
69 if ( $object instanceof WikiError ) {
70 return true;
71 } elseif ( $object instanceof Status ) {
72 return !$object->isOK();
73 } else {
74 return false;
75 }
76 }
77 }
78
79 /**
80 * Localized error message object
81 * @ingroup Exception
82 */
83 class WikiErrorMsg extends WikiError {
84 /**
85 * @param string $message wiki message name
86 * @param ... parameters to pass to wfMsg()
87 *
88 * @deprecated since 1.17
89 */
90 function __construct( $message/*, ... */ ) {
91 wfDeprecated( __METHOD__, '1.17' );
92 $args = func_get_args();
93 array_shift( $args );
94 $this->mMessage = wfMessage( $message )->rawParams( $args )->text();
95 $this->mMsgKey = $message;
96 $this->mMsgArgs = $args;
97 }
98
99 function getMessageKey() {
100 return $this->mMsgKey;
101 }
102
103 function getMessageArgs() {
104 return $this->mMsgArgs;
105 }
106 }
107
108 /**
109 * Error class designed to handle errors involved with
110 * XML parsing
111 * @ingroup Exception
112 */
113 class WikiXmlError extends WikiError {
114 /**
115 * @param $parser resource
116 * @param $message string
117 * @param $context
118 * @param $offset Int
119 *
120 * @deprecated since 1.17
121 */
122 function __construct( $parser, $message = 'XML parsing error', $context = null, $offset = 0 ) {
123 wfDeprecated( __METHOD__, '1.17' );
124 $this->mXmlError = xml_get_error_code( $parser );
125 $this->mColumn = xml_get_current_column_number( $parser );
126 $this->mLine = xml_get_current_line_number( $parser );
127 $this->mByte = xml_get_current_byte_index( $parser );
128 $this->mContext = $this->_extractContext( $context, $offset );
129 $this->mMessage = $message;
130 xml_parser_free( $parser );
131 wfDebug( "WikiXmlError: " . $this->getMessage() . "\n" );
132 }
133
134 /** @return string */
135 function getMessage() {
136 // '$1 at line $2, col $3 (byte $4): $5',
137 return wfMessage( 'xml-error-string',
138 $this->mMessage,
139 $this->mLine,
140 $this->mColumn,
141 $this->mByte . $this->mContext,
142 xml_error_string( $this->mXmlError ) )->escaped();
143 }
144
145 function _extractContext( $context, $offset ) {
146 if ( is_null( $context ) ) {
147 return null;
148 } else {
149 // Hopefully integer overflow will be handled transparently here
150 $inlineOffset = $this->mByte - $offset;
151 return '; "' . substr( $context, $inlineOffset, 16 ) . '"';
152 }
153 }
154 }