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