Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / content / MessageContent.php
blob4b589893717a1ebc4632a0584891d5e493a48828
1 <?php
2 /**
3 * Wrapper content object allowing to handle a system message as a Content object.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @since 1.21
22 * @file
23 * @ingroup Content
25 * @author Daniel Kinzler
28 /**
29 * Wrapper allowing us to handle a system message as a Content object.
30 * Note that this is generally *not* used to represent content from the
31 * MediaWiki namespace, and that there is no MessageContentHandler.
32 * MessageContent is just intended as glue for wrapping a message programmatically.
34 * @ingroup Content
36 class MessageContent extends AbstractContent {
38 /**
39 * @var Message
41 protected $mMessage;
43 /**
44 * @param Message|string $msg A Message object, or a message key.
45 * @param string[] $params An optional array of message parameters.
47 public function __construct( $msg, $params = null ) {
48 # XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
49 parent::__construct( CONTENT_MODEL_WIKITEXT );
51 if ( is_string( $msg ) ) {
52 $this->mMessage = wfMessage( $msg );
53 } else {
54 $this->mMessage = clone $msg;
57 if ( $params ) {
58 $this->mMessage = $this->mMessage->params( $params );
62 /**
63 * Fully parse the text from wikitext to HTML.
65 * @return string Parsed HTML.
67 public function getHtml() {
68 return $this->mMessage->parse();
71 /**
72 * Returns the message text. {{-transformation is done.
74 * @return string Unescaped message text.
76 public function getWikitext() {
77 return $this->mMessage->text();
80 /**
81 * Returns the message object, with any parameters already substituted.
83 * @return Message The message object.
85 public function getNativeData() {
86 // NOTE: Message objects are mutable. Cloning here makes MessageContent immutable.
87 return clone $this->mMessage;
90 /**
91 * @return string
93 * @see Content::getTextForSearchIndex
95 public function getTextForSearchIndex() {
96 return $this->mMessage->plain();
99 /**
100 * @return string
102 * @see Content::getWikitextForTransclusion
104 public function getWikitextForTransclusion() {
105 return $this->getWikitext();
109 * @param int $maxlength Maximum length of the summary text, defaults to 250.
111 * @return string The summary text.
113 * @see Content::getTextForSummary
115 public function getTextForSummary( $maxlength = 250 ) {
116 return substr( $this->mMessage->plain(), 0, $maxlength );
120 * @return int
122 * @see Content::getSize
124 public function getSize() {
125 return strlen( $this->mMessage->plain() );
129 * @return Content A copy of this object
131 * @see Content::copy
133 public function copy() {
134 // MessageContent is immutable (because getNativeData() returns a clone of the Message object)
135 return $this;
139 * @param bool|null $hasLinks
141 * @return bool Always false.
143 * @see Content::isCountable
145 public function isCountable( $hasLinks = null ) {
146 return false;
150 * @param Title $title Unused.
151 * @param int $revId Unused.
152 * @param ParserOptions $options Unused.
153 * @param bool $generateHtml Whether to generate HTML (default: true).
155 * @return ParserOutput
157 * @see Content::getParserOutput
159 public function getParserOutput( Title $title, $revId = null,
160 ParserOptions $options = null, $generateHtml = true ) {
161 if ( $generateHtml ) {
162 $html = $this->getHtml();
163 } else {
164 $html = '';
167 $po = new ParserOutput( $html );
168 // Message objects are in the user language.
169 $po->recordOption( 'userlang' );
171 return $po;