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