Add two new debug log groups
[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 programatically.
33 *
34 * @ingroup Content
35 */
36 class MessageContent extends AbstractContent {
37 /**
38 * @var Message
39 */
40 protected $mMessage;
41
42 /**
43 * @param Message|String $msg A Message object, or a message key
44 * @param array|null $params An optional array of message parameters
45 */
46 public function __construct( $msg, $params = null ) {
47 # XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
48 parent::__construct( CONTENT_MODEL_WIKITEXT );
49
50 if ( is_string( $msg ) ) {
51 $this->mMessage = wfMessage( $msg );
52 } else {
53 $this->mMessage = clone $msg;
54 }
55
56 if ( $params ) {
57 $this->mMessage = $this->mMessage->params( $params );
58 }
59 }
60
61 /**
62 * Returns the message as rendered HTML
63 *
64 * @return string The message text, parsed into html
65 */
66 public function getHtml() {
67 return $this->mMessage->parse();
68 }
69
70 /**
71 * Returns the message as rendered HTML
72 *
73 * @return string The message text, parsed into html
74 */
75 public function getWikitext() {
76 return $this->mMessage->text();
77 }
78
79 /**
80 * Returns the message object, with any parameters already substituted.
81 *
82 * @return Message The message object.
83 */
84 public function getNativeData() {
85 //NOTE: Message objects are mutable. Cloning here makes MessageContent immutable.
86 return clone $this->mMessage;
87 }
88
89 /**
90 * @see Content::getTextForSearchIndex
91 */
92 public function getTextForSearchIndex() {
93 return $this->mMessage->plain();
94 }
95
96 /**
97 * @see Content::getWikitextForTransclusion
98 */
99 public function getWikitextForTransclusion() {
100 return $this->getWikitext();
101 }
102
103 /**
104 * @see Content::getTextForSummary
105 */
106 public function getTextForSummary( $maxlength = 250 ) {
107 return substr( $this->mMessage->plain(), 0, $maxlength );
108 }
109
110 /**
111 * @see Content::getSize
112 *
113 * @return int
114 */
115 public function getSize() {
116 return strlen( $this->mMessage->plain() );
117 }
118
119 /**
120 * @see Content::copy
121 *
122 * @return Content. A copy of this object
123 */
124 public function copy() {
125 // MessageContent is immutable (because getNativeData() returns a clone of the Message object)
126 return $this;
127 }
128
129 /**
130 * @see Content::isCountable
131 *
132 * @param bool $hasLinks
133 * @return bool false
134 */
135 public function isCountable( $hasLinks = null ) {
136 return false;
137 }
138
139 /**
140 * @see Content::getParserOutput
141 *
142 * @param Title $title
143 * @param int $revId Optional revision ID
144 * @param ParserOptions $options
145 * @param bool $generateHtml Wether to generate HTML
146 * @return ParserOutput
147 */
148 public function getParserOutput(
149 Title $title, $revId = null,
150 ParserOptions $options = null, $generateHtml = true
151 ) {
152
153 if ( $generateHtml ) {
154 $html = $this->getHtml();
155 } else {
156 $html = '';
157 }
158
159 $po = new ParserOutput( $html );
160
161 return $po;
162 }
163 }