0f6437a38c5800b8206d66039ad406f2fe79a924
3 * OBS!!! *EXPERIMENTAL* This class is still under discussion.
5 * This class provides methods for fetching interface messages and
6 * processing them into variety of formats that are needed in MediaWiki.
8 * It is intented to replace the old wfMsg* functions that over time grew
12 * Fetching a message text for interface message
13 * $button = Xml::button( Message::key( 'submit' )->text() );
15 * Messages can have parameters:
16 * Message::key( 'welcome-to' )->params( $wgSitename )->text();
17 * {{GRAMMAR}} and friends work correctly
18 * Message::key( 'are-friends', $user, $friend );
19 * Message::key( 'bad-message' )->rawParams( '<script>...</script>' )->escaped();
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()
24 * Checking if message exists:
25 * Message::key( 'mysterious-message' )->exists()
27 * If you want to use a different language:
28 * Message::key( 'email-header' )->inLanguage( $user->getOption( 'language' ) )->plain()
29 * Note that you cannot parse the text except in the content or interface
34 * Comparison with old wfMsg* functions:
37 * wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
38 * === Message::key( 'key', 'apple' )->parse();
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.
43 * Places where html cannot be used. {{-transformation is done.
44 * wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
45 * === Message::key( 'key', 'apple', 'pear' )->text();
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();
54 * - test, can we have tests?
55 * - sort out the details marked with fixme
56 * - should we have _m() or similar global wrapper?
62 * In which language to get this message. True, which is the default,
63 * means the current interface language, false content language.
65 protected $interface = true;
68 * In which language to get this message. Overrides the $interface
71 protected $language = null;
79 * List of parameters which will be substituted into the message.
81 protected $parameters = array();
84 * Format for the message.
85 * Supported formats are:
87 * * escaped (transform+htmlspecialchars)
92 protected $format = 'parse';
95 * Whether database can be used.
97 protected $useDatabase = true;
101 * @param $key String: message key
102 * @param $params Array message parameters
103 * @return Message: $this
105 public function __construct( $key, $params = array() ) {
107 $this->parameters
= array_values( $params );
111 * Factory function that is just wrapper for the real constructor. It is
112 * intented to be used instead of the real constructor, because it allows
113 * chaining method calls, while new objects don't.
114 * @param $key String: message key
115 * @param Varargs: parameters as Strings
116 * @return Message: $this
118 public static function key( $key /*...*/ ) {
119 $params = func_get_args();
120 array_shift( $params );
121 return new self( $key, $params );
125 * Adds parameters to the parameter list of this message.
126 * @param Varargs: parameters as Strings
127 * @return Message: $this
129 public function params( /*...*/ ) {
130 $this->parameters
= array_merge( $this->parameters
, array_values( func_get_args() ) );
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
142 public function rawParams( /*...*/ ) {
143 $params = func_get_args();
144 foreach( $params as $param ) {
145 $this->parameters
[] = self
::rawParam( $param );
151 * Request the message in any language that is supported.
152 * As a side effect interface message status is unconditionally
154 * @param $lang Mixed: language code or Language object.
155 * @return Message: $this
157 public function inLanguage( $lang ) {
158 if( $lang instanceof Language
){
159 $this->language
= $lang;
160 } elseif ( is_string( $lang ) ) {
161 $this->language
= Language
::factory( $lang );
163 $type = gettype( $lang );
164 throw new MWException( __METHOD__
. " must be "
165 . "passed a String or Language object; $type given"
168 $this->interface = false;
173 * Request the message in the wiki's content language.
174 * @return Message: $this
176 public function inContentLanguage() {
177 $this->interface = false;
178 $this->language
= null;
183 * Enable or disable database use.
184 * @param $value Boolean
185 * @return Message: $this
187 public function useDatabase( $value ) {
188 $this->useDatabase
= (bool) $value;
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
202 public function toString() {
203 $string = $this->getMessageText();
205 # Replace parameters before text parsing
206 $string = $this->replaceParameters( $string, 'before' );
208 # Maybe transform using the full parser
209 if( $this->format
=== 'parse' ) {
210 $string = $this->parseText( $string );
212 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
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 );
225 # Raw parameter replacement
226 $string = $this->replaceParameters( $string, 'after' );
232 * Fully parse the text from wikitext to HTML
233 * @return String parsed HTML
235 public function parse() {
236 $this->format
= 'parse';
237 return $this->toString();
241 * Returns the message text. {{-transformation is done.
242 * @return String: Unescaped message text.
244 public function text() {
245 $this->format
= 'text';
246 return $this->toString();
250 * Returns the message text as-is, only parameters are subsituted.
251 * @return String: Unescaped untransformed message text.
253 public function plain() {
254 $this->format
= 'plain';
255 return $this->toString();
259 * Returns the parsed message text which is always surrounded by a block element.
260 * @return String: HTML
262 public function parseAsBlock() {
263 $this->format
= 'block-parse';
264 return $this->toString();
268 * Returns the message text. {{-transformation is done and the result
269 * is excaped excluding any raw parameters.
270 * @return String: Escaped message text.
272 public function escaped() {
273 $this->format
= 'escaped';
274 return $this->toString();
278 * Check whether a message key has been defined currently.
279 * @return Bool: true if it is and false if not.
281 public function exists() {
282 return $this->fetchMessage() !== false;
285 public static function rawParam( $value ) {
286 return array( 'raw' => $value );
290 * Substitutes any paramaters into the message text.
291 * @param $message String, the message text
292 * @param $type String: either before or after
295 protected function replaceParameters( $message, $type = 'before' ) {
296 $replacementKeys = array();
297 foreach( $this->parameters
as $n => $param ) {
298 if ( $type === 'before' && !is_array( $param ) ) {
299 $replacementKeys['$' . ($n +
1)] = $param;
300 } elseif ( $type === 'after' && isset( $param['raw'] ) ) {
301 $replacementKeys['$' . ($n +
1)] = $param['raw'];
304 $message = strtr( $message, $replacementKeys );
309 * Wrapper for what ever method we use to parse wikitext.
310 * @param $string String: Wikitext message contents
311 * @return Wikitext parsed into HTML
313 protected function parseText( $string ) {
315 if ( $this->language
!== null ) {
316 # FIXME: remove this limitation
317 throw new MWException( 'Can only parse in interface or content language' );
319 return $wgOut->parse( $string, /*linestart*/true, $this->interface );
323 * Wrapper for what ever method we use to {{-transform wikitext.
324 * @param $string String: Wikitext message contents
325 * @return Wikitext with {{-constructs replaced with their values.
327 protected function transformText( $string ) {
328 global $wgMessageCache;
329 return $wgMessageCache->transform( $string, $this->interface, $this->language
);
333 * Returns the textual value for the message.
334 * @return Message contents or placeholder
336 protected function getMessageText() {
337 $message = $this->fetchMessage();
338 if ( $message === false ) {
339 return '<' . htmlspecialchars( $this->key
) . '>';
346 * Wrapper for what ever method we use to get message contents
348 protected function fetchMessage() {
349 if ( !isset( $this->message
) ) {
350 global $wgMessageCache;
351 $this->message
= $wgMessageCache->get( $this->key
, $this->useDatabase
, $this->language
);
353 return $this->message
;