ImagePage.php: add missing braces + other code style tweaks
[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 function __construct( $message ) {
36 wfDeprecated( __METHOD__ );
37 $this->mMessage = $message;
38 }
39
40 /**
41 * @return string Plaintext error message to display
42 */
43 function getMessage() {
44 return $this->mMessage;
45 }
46
47 /**
48 * In following PEAR_Error model this could be formatted differently,
49 * but so far it's not.
50 * @return string
51 */
52 function toString() {
53 return $this->getMessage();
54 }
55
56 /**
57 * Returns true if the given object is a WikiError-descended
58 * error object, false otherwise.
59 *
60 * @param $object mixed
61 * @return bool
62 */
63 public static function isError( $object ) {
64 wfDeprecated( __METHOD__ );
65 if ( $object instanceof WikiError ) {
66 return true;
67 } elseif ( $object instanceof Status ) {
68 return !$object->isOK();
69 } else {
70 return false;
71 }
72 }
73 }
74
75 /**
76 * Localized error message object
77 * @ingroup Exception
78 */
79 class WikiErrorMsg extends WikiError {
80 /**
81 * @param $message String: wiki message name
82 * @param ... parameters to pass to wfMsg()
83 */
84 function __construct( $message/*, ... */ ) {
85 wfDeprecated( __METHOD__ );
86 $args = func_get_args();
87 array_shift( $args );
88 $this->mMessage = wfMsgReal( $message, $args, true );
89 $this->mMsgKey = $message;
90 $this->mMsgArgs = $args;
91 }
92
93 function getMessageKey() {
94 return $this->mMsgKey;
95 }
96
97 function getMessageArgs() {
98 return $this->mMsgArgs;
99 }
100 }
101
102 /**
103 * Error class designed to handle errors involved with
104 * XML parsing
105 * @ingroup Exception
106 */
107 class WikiXmlError extends WikiError {
108 /**
109 * @param $parser resource
110 * @param $message string
111 * @param $context
112 * @param $offset Int
113 */
114 function __construct( $parser, $message = 'XML parsing error', $context = null, $offset = 0 ) {
115 wfDeprecated( __METHOD__ );
116 $this->mXmlError = xml_get_error_code( $parser );
117 $this->mColumn = xml_get_current_column_number( $parser );
118 $this->mLine = xml_get_current_line_number( $parser );
119 $this->mByte = xml_get_current_byte_index( $parser );
120 $this->mContext = $this->_extractContext( $context, $offset );
121 $this->mMessage = $message;
122 xml_parser_free( $parser );
123 wfDebug( "WikiXmlError: " . $this->getMessage() . "\n" );
124 }
125
126 /** @return string */
127 function getMessage() {
128 // '$1 at line $2, col $3 (byte $4): $5',
129 return wfMsgHtml( 'xml-error-string',
130 $this->mMessage,
131 $this->mLine,
132 $this->mColumn,
133 $this->mByte . $this->mContext,
134 xml_error_string( $this->mXmlError ) );
135 }
136
137 function _extractContext( $context, $offset ) {
138 if( is_null( $context ) ) {
139 return null;
140 } else {
141 // Hopefully integer overflow will be handled transparently here
142 $inlineOffset = $this->mByte - $offset;
143 return '; "' . substr( $context, $inlineOffset, 16 ) . '"';
144 }
145 }
146 }