summaryrefslogtreecommitdiff
path: root/includes/Message.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/Message.php')
-rw-r--r--includes/Message.php95
1 files changed, 74 insertions, 21 deletions
diff --git a/includes/Message.php b/includes/Message.php
index 10f9d3e1..b0147b9a 100644
--- a/includes/Message.php
+++ b/includes/Message.php
@@ -1,12 +1,34 @@
<?php
/**
+ * Fetching and processing of interface messages.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Niklas Laxström
+ */
+
+/**
* The Message class provides methods which fullfil two basic services:
* - fetching interface messages
* - processing messages into a variety of formats
*
* First implemented with MediaWiki 1.17, the Message class is intented to
* replace the old wfMsg* functions that over time grew unusable.
- * @see https://www.mediawiki.org/wiki/New_messages_API for equivalences
+ * @see https://www.mediawiki.org/wiki/Manual:Messages_API for equivalences
* between old and new functions.
*
* You should use the wfMessage() global function which acts as a wrapper for
@@ -90,8 +112,7 @@
* ->plain();
* @endcode
*
- * @note You cannot parse the text except in the content or interface
- * @note languages
+ * @note You can parse the text only in the content or interface languages
*
* @section message_compare_old Comparison with old wfMsg* functions:
*
@@ -134,7 +155,6 @@
* @see https://www.mediawiki.org/wiki/Localisation
*
* @since 1.17
- * @author Niklas Laxström
*/
class Message {
/**
@@ -189,6 +209,7 @@ class Message {
/**
* Constructor.
+ * @since 1.17
* @param $key: message key, or array of message keys to try and use the first non-empty message for
* @param $params Array message parameters
* @return Message: $this
@@ -204,6 +225,7 @@ class Message {
* Factory function that is just wrapper for the real constructor. It is
* intented to be used instead of the real constructor, because it allows
* chaining method calls, while new objects don't.
+ * @since 1.17
* @param $key String: message key
* @param Varargs: parameters as Strings
* @return Message: $this
@@ -218,6 +240,7 @@ class Message {
* Factory function accepting multiple message keys and returning a message instance
* for the first message which is non-empty. If all messages are empty then an
* instance of the first message key is returned.
+ * @since 1.18
* @param Varargs: message keys (or first arg as an array of all the message keys)
* @return Message: $this
*/
@@ -237,6 +260,7 @@ class Message {
/**
* Adds parameters to the parameter list of this message.
+ * @since 1.17
* @param Varargs: parameters as Strings, or a single argument that is an array of Strings
* @return Message: $this
*/
@@ -255,6 +279,7 @@ class Message {
* In other words the parsing process cannot access the contents
* of this type of parameter, and you need to make sure it is
* sanitized beforehand. The parser will see "$n", instead.
+ * @since 1.17
* @param Varargs: raw parameters as Strings (or single argument that is an array of raw parameters)
* @return Message: $this
*/
@@ -272,6 +297,7 @@ class Message {
/**
* Add parameters that are numeric and will be passed through
* Language::formatNum before substitution
+ * @since 1.18
* @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
* @return Message: $this
*/
@@ -288,13 +314,14 @@ class Message {
/**
* Set the language and the title from a context object
- *
+ * @since 1.19
* @param $context IContextSource
* @return Message: $this
*/
public function setContext( IContextSource $context ) {
$this->inLanguage( $context->getLanguage() );
$this->title( $context->getTitle() );
+ $this->interface = true;
return $this;
}
@@ -303,6 +330,7 @@ class Message {
* Request the message in any language that is supported.
* As a side effect interface message status is unconditionally
* turned off.
+ * @since 1.17
* @param $lang Mixed: language code or Language object.
* @return Message: $this
*/
@@ -326,6 +354,7 @@ class Message {
/**
* Request the message in the wiki's content language,
* unless it is disabled for this message.
+ * @since 1.17
* @see $wgForceUIMsgAsContentMsg
* @return Message: $this
*/
@@ -342,7 +371,20 @@ class Message {
}
/**
+ * Allows manipulating the interface message flag directly.
+ * Can be used to restore the flag after setting a language.
+ * @param $value bool
+ * @return Message: $this
+ * @since 1.20
+ */
+ public function setInterfaceMessageFlag( $value ) {
+ $this->interface = (bool) $value;
+ return $this;
+ }
+
+ /**
* Enable or disable database use.
+ * @since 1.17
* @param $value Boolean
* @return Message: $this
*/
@@ -353,7 +395,7 @@ class Message {
/**
* Set the Title object to use as context when transforming the message
- *
+ * @since 1.18
* @param $title Title object
* @return Message: $this
*/
@@ -364,10 +406,19 @@ class Message {
/**
* Returns the message parsed from wikitext to HTML.
+ * @since 1.17
* @return String: HTML
*/
public function toString() {
- $string = $this->getMessageText();
+ $string = $this->fetchMessage();
+
+ if ( $string === false ) {
+ $key = htmlspecialchars( is_array( $this->key ) ? $this->key[0] : $this->key );
+ if ( $this->format === 'plain' ) {
+ return '<' . $key . '>';
+ }
+ return '&lt;' . $key . '&gt;';
+ }
# Replace parameters before text parsing
$string = $this->replaceParameters( $string, 'before' );
@@ -398,6 +449,7 @@ class Message {
* Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg:
* $foo = Message::get($key);
* $string = "<abbr>$foo</abbr>";
+ * @since 1.18
* @return String
*/
public function __toString() {
@@ -406,6 +458,7 @@ class Message {
/**
* Fully parse the text from wikitext to HTML
+ * @since 1.17
* @return String parsed HTML
*/
public function parse() {
@@ -415,6 +468,7 @@ class Message {
/**
* Returns the message text. {{-transformation is done.
+ * @since 1.17
* @return String: Unescaped message text.
*/
public function text() {
@@ -424,6 +478,7 @@ class Message {
/**
* Returns the message text as-is, only parameters are subsituted.
+ * @since 1.17
* @return String: Unescaped untransformed message text.
*/
public function plain() {
@@ -433,6 +488,7 @@ class Message {
/**
* Returns the parsed message text which is always surrounded by a block element.
+ * @since 1.17
* @return String: HTML
*/
public function parseAsBlock() {
@@ -443,6 +499,7 @@ class Message {
/**
* Returns the message text. {{-transformation is done and the result
* is escaped excluding any raw parameters.
+ * @since 1.17
* @return String: Escaped message text.
*/
public function escaped() {
@@ -452,6 +509,7 @@ class Message {
/**
* Check whether a message key has been defined currently.
+ * @since 1.17
* @return Bool: true if it is and false if not.
*/
public function exists() {
@@ -460,6 +518,7 @@ class Message {
/**
* Check whether a message does not exist, or is an empty string
+ * @since 1.18
* @return Bool: true if is is and false if not
* @todo FIXME: Merge with isDisabled()?
*/
@@ -470,6 +529,7 @@ class Message {
/**
* Check whether a message does not exist, is an empty string, or is "-"
+ * @since 1.18
* @return Bool: true if is is and false if not
*/
public function isDisabled() {
@@ -478,6 +538,7 @@ class Message {
}
/**
+ * @since 1.17
* @param $value
* @return array
*/
@@ -486,6 +547,7 @@ class Message {
}
/**
+ * @since 1.18
* @param $value
* @return array
*/
@@ -495,6 +557,7 @@ class Message {
/**
* Substitutes any paramaters into the message text.
+ * @since 1.17
* @param $message String: the message text
* @param $type String: either before or after
* @return String
@@ -513,6 +576,7 @@ class Message {
/**
* Extracts the parameter type and preprocessed the value if needed.
+ * @since 1.18
* @param $param String|Array: Parameter as defined in this class.
* @return Tuple(type, value)
*/
@@ -536,6 +600,7 @@ class Message {
/**
* Wrapper for what ever method we use to parse wikitext.
+ * @since 1.17
* @param $string String: Wikitext message contents
* @return string Wikitext parsed into HTML
*/
@@ -545,6 +610,7 @@ class Message {
/**
* Wrapper for what ever method we use to {{-transform wikitext.
+ * @since 1.17
* @param $string String: Wikitext message contents
* @return string Wikitext with {{-constructs replaced with their values.
*/
@@ -553,21 +619,8 @@ class Message {
}
/**
- * Returns the textual value for the message.
- * @return Message contents or placeholder
- */
- protected function getMessageText() {
- $message = $this->fetchMessage();
- if ( $message === false ) {
- return '&lt;' . htmlspecialchars( is_array($this->key) ? $this->key[0] : $this->key ) . '&gt;';
- } else {
- return $message;
- }
- }
-
- /**
* Wrapper for what ever method we use to get message contents
- *
+ * @since 1.17
* @return string
*/
protected function fetchMessage() {