From 504e7e45cc044e9590fcd27dbad2ed38cecec54b Mon Sep 17 00:00:00 2001 From: daniel Date: Wed, 29 Aug 2012 19:13:27 +0200 Subject: [PATCH] make MessageContent use a Message object Change-Id: I1eff1293acc540ffe3b6d6189ed78560b63b9672 --- includes/Content.php | 130 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 106 insertions(+), 24 deletions(-) diff --git a/includes/Content.php b/includes/Content.php index 8dfcd1c6e0..b3d55060a3 100644 --- a/includes/Content.php +++ b/includes/Content.php @@ -1275,52 +1275,134 @@ class WikitextContent extends TextContent { } /** + * Wrapper allowing us to handle a system message as a Content object. Note that this is generally *not* used + * to represent content from the MediaWiki namespace, and that there is no MessageContentHandler. MessageContent + * is just intended as glue for wrapping a message programatically. + * * @since WD.1 */ -class MessageContent extends TextContent { - public function __construct( $msg_key, $params = null, $options = null ) { - # XXX: messages may be wikitext, html or plain text! and maybe even - # something else entirely. - parent::__construct( null, CONTENT_MODEL_WIKITEXT ); +class MessageContent extends AbstractContent { - $this->mMessageKey = $msg_key; + /** + * @var Message + */ + protected $mMessage; - $this->mParameters = $params; + /** + * @param Message|String $msg A Message object, or a message key + * @param array|null $params An optional array of message parameters + */ + public function __construct( $msg, $params = null ) { + # XXX: messages may be wikitext, html or plain text! and maybe even something else entirely. + parent::__construct( CONTENT_MODEL_WIKITEXT ); - if ( is_null( $options ) ) { - $options = array(); + if ( is_string( $msg ) ) { + $this->mMessage = wfMessage( $msg ); + } else { + $this->mMessage = clone $msg; } - elseif ( is_string( $options ) ) { - $options = array( $options ); + + if ( $params ) { + $this->mMessage = $this->mMessage->params( $params ); } + } + + /** + * Returns the message as rendered HTML + * + * @return string The message text, parsed into html + */ + public function getHtml() { + return $this->mMessage->parse(); + } + + /** + * Returns the message as rendered HTML + * + * @return string The message text, parsed into html + */ + public function getWikitext() { + return $this->mMessage->text(); + } - $this->mOptions = $options; + /** + * Returns the message object, with any parameters already substituted. + * + * @return Message The message object. + */ + public function getNativeData() { + //NOTE: Message objects are mutable. Cloning here makes MessageContent immutable. + return clone $this->mMessage; } /** - * Returns the message as rendered HTML, using the options supplied to the - * constructor plus "parse". - * @param the message text, parsed + * @see Content::getTextForSearchIndex */ - public function getHtml( ) { - $opt = array_merge( $this->mOptions, array( 'parse' ) ); + public function getTextForSearchIndex() { + return $this->mMessage->plain(); + } - return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt ); + /** + * @see Content::getWikitextForTransclusion + */ + public function getWikitextForTransclusion() { + return $this->getWikitext(); } + /** + * @see Content::getTextForSummary + */ + public function getTextForSummary( $maxlength = 250 ) { + return substr( $this->mMessage->plain(), 0, $maxlength ); + } /** - * Returns the message as raw text, using the options supplied to the - * constructor minus "parse" and "parseinline". + * @see Content::getSize * - * @param the message text, unparsed. + * @return int */ - public function getNativeData( ) { - $opt = array_diff( $this->mOptions, array( 'parse', 'parseinline' ) ); + public function getSize() { + return strlen( $this->mMessage->plain() ); + } - return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt ); + /** + * @see Content::copy + * + * @return Content. A copy of this object + */ + public function copy() { + // MessageContent is immutable (because getNativeData() returns a clone of the Message object) + return $this; } + /** + * @see Content::isCountable + * + * @return bool false + */ + public function isCountable( $hasLinks = null ) { + return false; + } + + /** + * @see Content::getParserOutput + * + * @return ParserOutput + */ + public function getParserOutput( + Title $title, $revId = null, + ParserOptions $options = null, $generateHtml = true + ) { + + if ( $generateHtml ) { + $html = $this->getHtml(); + } else { + $html = ''; + } + + $po = new ParserOutput( $html ); + return $po; + } } /** -- 2.20.1