Remove options array and move message fetching one level down.
[lhc/web/wiklou.git] / includes / Message.php
1 <?php
2 /**
3 * OBS!!! *EXPERIMENTAL* This class is still under discussion.
4 *
5 * This class provides methods for fetching interface messages and
6 * processing them into variety of formats that are needed in MediaWiki.
7 *
8 * It is intented to replace the old wfMsg* functions that over time grew
9 * unusable.
10 *
11 * Examples:
12 * Fetching a message text for interface message
13 * $button = Xml::button( Message::key( 'submit' )->text() );
14 * </pre>
15 * Messages can have parameters:
16 * Message::key( 'welcome-to' )->params( $wgSitename )->text();
17 * {{GRAMMAR}} and friends work correctly
18 * Message::key( 'are-friends' )->params( $user, $friend );
19 * Message::key( 'bad-message' )->rawParams( '<script>...</script>' )->escaped()
20 * </pre>
21 * Sometimes the message text ends up in the database, so content language is needed.
22 * Message::key( 'file-log' )->params( $user, $filename )->inContentLanguage()->text()
23 * </pre>
24 * Checking if message exists:
25 * Message::key( 'mysterious-message' )->exists()
26 * </pre>
27 * If you want to use a different language:
28 * Message::key( 'email-header' )->language( $user->getOption( 'language' ) )->plain()
29 * Note that you cannot parse the text except in the content or interface
30 * languages
31 * </pre>
32 *
33 *
34 * Comparison with old wfMsg* functions:
35 *
36 * Use full parsing.
37 * wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
38 * === Message::key( 'key' )->params( 'apple' )->parse();
39 * </pre>
40 * Parseinline is used because it is more useful when pre-building html.
41 * In normal use it is better to use OutputPage::(add|wrap)WikiMsg.
42 *
43 * Places where html cannot be used. {{-transformation is done.
44 * wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
45 * === Message::key( 'key' )->params( 'apple', 'pear' )->text();
46 * </pre>
47 *
48 * Shortcut for escaping the message too, similar to wfMsgHTML, but
49 * parameters are not replaced after escaping by default.
50 * $escaped = Message::key( 'key' )->rawParams( 'apple' )->escaped();
51 * </pre>
52 *
53 * TODO:
54 * - test, can we have tests?
55 * - sort out the details marked with fixme
56 * - should we have _m() or similar global wrapper?
57 *
58 * @since 1.17
59 */
60 class Message {
61 /**
62 * In which language to get this message. True, which is the default,
63 * means the current interface language, false content language.
64 */
65 protected $interface = true;
66
67 /**
68 * In which language to get this message. Overrides the $interface
69 * variable.
70 */
71 protected $language = null;
72
73 /**
74 * The message key.
75 */
76 protected $key;
77
78 /**
79 * List of parameters which will be substituted into the message.
80 */
81 protected $parameters = array();
82
83 /**
84 * Format for the message.
85 * Supported formats are:
86 * * text (transform)
87 * * escaped (transform+htmlspecialchars)
88 * * block-parse
89 * * parse (default)
90 * * plain
91 */
92 protected $format = 'parse';
93
94 /**
95 * Whether database can be used.
96 */
97 protected $useDatabase = true;
98
99 /**
100 * Constructor.
101 * @param $key String: message key
102 * @param $params Array message parameters
103 * @return Message: $this
104 */
105 public function __construct( $key, $params = array() ) {
106 $this->key = $key;
107 foreach( $params as $param ){
108 $this->params( $param );
109 }
110 }
111
112 /**
113 * Factory function that is just wrapper for the real constructor. It is
114 * intented to be used instead of the real constructor, because it allows
115 * chaining method calls, while new objects don't.
116 * //FIXME: key or get or something else?
117 * @param $key String: message key
118 * @return Message: $this
119 */
120 public static function key( $key ) {
121 return new Message( $key );
122 }
123
124 /**
125 * Adds parameters to the parameter list of this message.
126 * @param Vargars: parameters as Strings
127 * @return Message: $this
128 */
129 public function params( /*...*/ ) {
130 $this->parameters += array_values( func_get_args() );
131 return $this;
132 }
133
134 /**
135 * Add parameters that are substituted after parsing or escaping.
136 * In other words the parsing process cannot access the contents
137 * of this type of parameter, and you need to make sure it is
138 * sanitized beforehand. The parser will see "$n", instead.
139 * @param Varargs: raw parameters as Strings
140 * @return Message: $this
141 */
142 public function rawParams( /*...*/ ) {
143 $params = func_get_args();
144 foreach( $params as $param ){
145 $this->parameters[] = array( 'raw' => $param );
146 }
147 return $this;
148 }
149
150 /**
151 * Request the message in any language that is supported.
152 * As a side effect interface message status is unconditionally
153 * turned off.
154 * @param $lang Mixed: langauge code or language object.
155 * @return Message: $this
156 */
157 public function language( $lang ) {
158 if( $lang instanceof Language ){
159 $this->language = $lang;
160 } elseif ( is_string( $lang ) ) {
161 $this->language = Language::factory( $lang );
162 } else {
163 $type = gettype( $lang );
164 throw new MWException( __METHOD__ . " must be "
165 . "passed a String or Language object; $type given"
166 );
167 }
168 $this->interface = false;
169 return $this;
170 }
171
172 /**
173 * Request the message in the wiki's content language.
174 * @return Message: $this
175 */
176 public function inContentLanguage() {
177 $this->interface = false;
178 $this->language = null;
179 return $this;
180 }
181
182 /**
183 * Enable or disable database use.
184 * @param $value Boolean
185 * @return Message: $this
186 */
187 public function useDatabase( $value ) {
188 $this->useDatabase = (bool) $value;
189 return $this;
190 }
191
192 /**
193 * Returns the message parsed from wikitext to HTML.
194 * TODO: in PHP >= 5.2.0, we can make this a magic method,
195 * and then we can do, eg:
196 * $foo = Message::get($key);
197 * $string = "<abbr>$foo</abbr>";
198 * But we shouldn't implement that while MediaWiki still supports
199 * PHP < 5.2; or people will start using it...
200 * @return String: HTML
201 */
202 public function toString() {
203 $string = $this->getMessageText();
204
205 # Replace parameters before text parsing
206 $string = $this->replaceParameters( $string, 'before' );
207
208 # Maybe transform using the full parser
209 if( $this->format === 'parse' ) {
210 $string = $this->parseText( $string );
211 $m = array();
212 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
213 $string = $m[1];
214 }
215 } elseif( $this->format === 'block-parse' ){
216 $string = $this->parseText( $string );
217 } elseif( $this->format === 'text' ){
218 $string = $this->transformText( $string );
219 } elseif( $this->format === 'escaped' ){
220 # FIXME: Sanitizer method here?
221 $string = $this->transformText( $string );
222 $string = htmlspecialchars( $string );
223 }
224
225 # Raw parameter replacement
226 $string = $this->replaceParameters( $string, 'after' );
227
228 return $string;
229 }
230
231 /**
232 * Fully parse the text from wikitext to HTML
233 * @return String parsed HTML
234 */
235 public function parse(){
236 $this->format = 'parse';
237 return $this->tostring();
238 }
239
240 /**
241 * Returns the message text. {{-transformation is done.
242 * @return String: Unescaped message text.
243 */
244 public function text() {
245 $this->format = 'text';
246 return $this->tostring();
247 }
248
249 /**
250 * Returns the message text as-is, only parameters are subsituted.
251 * @return String: Unescaped untransformed message text.
252 */
253 public function plain() {
254 $this->format = 'plain';
255 return $this->tostring();
256 }
257
258 /**
259 * Returns the parsed message text which is always surrounded by a block element.
260 * @return String: HTML
261 */
262 public function parseAsBlock() {
263 $this->format = 'block-parse';
264 return $this->tostring();
265 }
266
267 /**
268 * Returns the message text. {{-transformation is done and the result
269 * is excaped excluding any raw parameters.
270 * @return String: Escaped message text.
271 */
272 public function escaped() {
273 $this->format = 'escaped';
274 return $this->tostring();
275 }
276
277 /**
278 * Check whether a message key has been defined currently.
279 * @return Bool: true if it is and false if not.
280 */
281 public function exists() {
282 return $this->fetchMessage() === false;
283 }
284
285 /**
286 * Substitutes any paramaters into the message text.
287 * @param $message String, the message text
288 * @param $type String: either before or after
289 * @return String
290 */
291 protected function replaceParameters( $message, $type = 'before' ) {
292 $replacementKeys = array();
293 foreach( $this->parameters as $n => $param ) {
294 if ( $type === 'before' && !is_array( $param ) ) {
295 $replacementKeys['$' . ($n + 1)] = $param;
296 } elseif ( $type === 'after' && isset( $param['raw'] ) ) {
297 $replacementKeys['$' . ($n + 1)] = $param['raw'];
298 }
299 }
300 $message = strtr( $message, $replacementKeys );
301 return $message;
302 }
303
304 /**
305 * Wrapper for what ever method we use to parse wikitext.
306 * @param $string String: Wikitext message contents
307 * @return Wikitext parsed into HTML
308 */
309 protected function parseText( $string ) {
310 global $wgOut;
311 if ( $this->language !== null ) {
312 # FIXME: remove this limitation
313 throw new MWException( 'Can only parse in interface or content language' );
314 }
315 return $wgOut->parse( $string, /*linestart*/true, $this->interface );
316 }
317
318 /**
319 * Wrapper for what ever method we use to {{-transform wikitext.
320 * @param $string String: Wikitext message contents
321 * @return Wikitext with {{-constructs replaced with their values.
322 */
323 protected function transformText( $string ) {
324 global $wgMessageCache;
325 return $wgMessageCache->transform( $string, $this->interface, $this->language );
326 }
327
328 /**
329 * Returns the textual value for the message.
330 * @return Message contents or placeholder
331 */
332 protected function getMessageText() {
333 $message = $this->fetchMessage();
334 if ( $message === false ) {
335 return '&lt;' . htmlspecialchars( $this->key ) . '&gt;';
336 } else {
337 return $message;
338 }
339 }
340
341 /**
342 * Wrapper for what ever method we use to get message contents
343 */
344 protected function fetchMessage() {
345 if ( !isset( $this->message ) ) {
346 global $wgMessageCache;
347 $this->message = $wgMessageCache->get( $this->key, $this->useDatabase, $this->language );
348 }
349 return $this->message;
350 }
351
352 }