Add comments to Linker::formatLinksInComment()
[lhc/web/wiklou.git] / includes / content / MessageContent.php
1 <?php
2
3 /**
4 * Wrapper allowing us to handle a system message as a Content object. Note that this is generally *not* used
5 * to represent content from the MediaWiki namespace, and that there is no MessageContentHandler. MessageContent
6 * is just intended as glue for wrapping a message programatically.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @since 1.21
24 *
25 * @file
26 * @ingroup Content
27 *
28 * @author Daniel Kinzler
29 */
30 class MessageContent extends AbstractContent {
31
32 /**
33 * @var Message
34 */
35 protected $mMessage;
36
37 /**
38 * @param Message|String $msg A Message object, or a message key
39 * @param array|null $params An optional array of message parameters
40 */
41 public function __construct( $msg, $params = null ) {
42 # XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
43 parent::__construct( CONTENT_MODEL_WIKITEXT );
44
45 if ( is_string( $msg ) ) {
46 $this->mMessage = wfMessage( $msg );
47 } else {
48 $this->mMessage = clone $msg;
49 }
50
51 if ( $params ) {
52 $this->mMessage = $this->mMessage->params( $params );
53 }
54 }
55
56 /**
57 * Returns the message as rendered HTML
58 *
59 * @return string The message text, parsed into html
60 */
61 public function getHtml() {
62 return $this->mMessage->parse();
63 }
64
65 /**
66 * Returns the message as rendered HTML
67 *
68 * @return string The message text, parsed into html
69 */
70 public function getWikitext() {
71 return $this->mMessage->text();
72 }
73
74 /**
75 * Returns the message object, with any parameters already substituted.
76 *
77 * @return Message The message object.
78 */
79 public function getNativeData() {
80 //NOTE: Message objects are mutable. Cloning here makes MessageContent immutable.
81 return clone $this->mMessage;
82 }
83
84 /**
85 * @see Content::getTextForSearchIndex
86 */
87 public function getTextForSearchIndex() {
88 return $this->mMessage->plain();
89 }
90
91 /**
92 * @see Content::getWikitextForTransclusion
93 */
94 public function getWikitextForTransclusion() {
95 return $this->getWikitext();
96 }
97
98 /**
99 * @see Content::getTextForSummary
100 */
101 public function getTextForSummary( $maxlength = 250 ) {
102 return substr( $this->mMessage->plain(), 0, $maxlength );
103 }
104
105 /**
106 * @see Content::getSize
107 *
108 * @return int
109 */
110 public function getSize() {
111 return strlen( $this->mMessage->plain() );
112 }
113
114 /**
115 * @see Content::copy
116 *
117 * @return Content. A copy of this object
118 */
119 public function copy() {
120 // MessageContent is immutable (because getNativeData() returns a clone of the Message object)
121 return $this;
122 }
123
124 /**
125 * @see Content::isCountable
126 *
127 * @return bool false
128 */
129 public function isCountable( $hasLinks = null ) {
130 return false;
131 }
132
133 /**
134 * @see Content::getParserOutput
135 *
136 * @return ParserOutput
137 */
138 public function getParserOutput(
139 Title $title, $revId = null,
140 ParserOptions $options = null, $generateHtml = true
141 ) {
142
143 if ( $generateHtml ) {
144 $html = $this->getHtml();
145 } else {
146 $html = '';
147 }
148
149 $po = new ParserOutput( $html );
150 return $po;
151 }
152 }