Merge "Fix user talk checkbox enabling/disabling on Special:Block"
[lhc/web/wiklou.git] / includes / content / MessageContent.php
1 <?php
2 /**
3 * Wrapper content object allowing to handle a system message as a Content object.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @since 1.21
21 *
22 * @file
23 * @ingroup Content
24 *
25 * @author Daniel Kinzler
26 */
27
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.
33 *
34 * @ingroup Content
35 */
36 class MessageContent extends AbstractContent {
37
38 /**
39 * @var Message
40 */
41 protected $mMessage;
42
43 /**
44 * @param Message|string $msg A Message object, or a message key.
45 * @param string[]|null $params An optional array of message parameters.
46 */
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 );
50
51 if ( is_string( $msg ) ) {
52 $this->mMessage = wfMessage( $msg );
53 } else {
54 $this->mMessage = clone $msg;
55 }
56
57 if ( $params ) {
58 $this->mMessage = $this->mMessage->params( $params );
59 }
60 }
61
62 /**
63 * Fully parse the text from wikitext to HTML.
64 *
65 * @return string Parsed HTML.
66 */
67 public function getHtml() {
68 return $this->mMessage->parse();
69 }
70
71 /**
72 * Returns the message text. {{-transformation is done.
73 *
74 * @return string Unescaped message text.
75 */
76 public function getWikitext() {
77 return $this->mMessage->text();
78 }
79
80 /**
81 * Returns the message object, with any parameters already substituted.
82 *
83 * @deprecated since 1.33 use getMessage() instead.
84 *
85 * @return Message The message object.
86 */
87 public function getNativeData() {
88 return $this->getMessage();
89 }
90
91 /**
92 * Returns the message object, with any parameters already substituted.
93 *
94 * @since 1.33
95 *
96 * @return Message The message object.
97 */
98 public function getMessage() {
99 // NOTE: Message objects are mutable. Cloning here makes MessageContent immutable.
100 return clone $this->mMessage;
101 }
102
103 /**
104 * @return string
105 *
106 * @see Content::getTextForSearchIndex
107 */
108 public function getTextForSearchIndex() {
109 return $this->mMessage->plain();
110 }
111
112 /**
113 * @return string
114 *
115 * @see Content::getWikitextForTransclusion
116 */
117 public function getWikitextForTransclusion() {
118 return $this->getWikitext();
119 }
120
121 /**
122 * @param int $maxlength Maximum length of the summary text, defaults to 250.
123 *
124 * @return string The summary text.
125 *
126 * @see Content::getTextForSummary
127 */
128 public function getTextForSummary( $maxlength = 250 ) {
129 return substr( $this->mMessage->plain(), 0, $maxlength );
130 }
131
132 /**
133 * @return int
134 *
135 * @see Content::getSize
136 */
137 public function getSize() {
138 return strlen( $this->mMessage->plain() );
139 }
140
141 /**
142 * @return Content A copy of this object
143 *
144 * @see Content::copy
145 */
146 public function copy() {
147 // MessageContent is immutable (because getNativeData() and getMessage()
148 // returns a clone of the Message object)
149 return $this;
150 }
151
152 /**
153 * @param bool|null $hasLinks
154 *
155 * @return bool Always false.
156 *
157 * @see Content::isCountable
158 */
159 public function isCountable( $hasLinks = null ) {
160 return false;
161 }
162
163 /**
164 * @param Title $title Unused.
165 * @param int|null $revId Unused.
166 * @param ParserOptions|null $options Unused.
167 * @param bool $generateHtml Whether to generate HTML (default: true).
168 *
169 * @return ParserOutput
170 *
171 * @see Content::getParserOutput
172 */
173 public function getParserOutput( Title $title, $revId = null,
174 ParserOptions $options = null, $generateHtml = true ) {
175 if ( $generateHtml ) {
176 $html = $this->getHtml();
177 } else {
178 $html = '';
179 }
180
181 $po = new ParserOutput( $html );
182 // Message objects are in the user language.
183 $po->recordOption( 'userlang' );
184
185 return $po;
186 }
187
188 }