summaryrefslogtreecommitdiff
path: root/includes/content/MessageContent.php
blob: b36b670c7863b5abac59f24e9402961be2a2e35b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
 * Wrapper content object allowing to handle a system message as a Content object.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @since 1.21
 *
 * @file
 * @ingroup Content
 *
 * @author Daniel Kinzler
 */

/**
 * 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.
 *
 * @ingroup Content
 */
class MessageContent extends AbstractContent {

	/**
	 * @var Message
	 */
	protected $mMessage;

	/**
	 * @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_string( $msg ) ) {
			$this->mMessage = wfMessage( $msg );
		} else {
			$this->mMessage = clone $msg;
		}

		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();
	}

	/**
	 * 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;
	}

	/**
	 * @see Content::getTextForSearchIndex
	 */
	public function getTextForSearchIndex() {
		return $this->mMessage->plain();
	}

	/**
	 * @see Content::getWikitextForTransclusion
	 */
	public function getWikitextForTransclusion() {
		return $this->getWikitext();
	}

	/**
	 * @see Content::getTextForSummary
	 */
	public function getTextForSummary( $maxlength = 250 ) {
		return substr( $this->mMessage->plain(), 0, $maxlength );
	}

	/**
	 * @see Content::getSize
	 *
	 * @return int
	 */
	public function getSize() {
		return strlen( $this->mMessage->plain() );
	}

	/**
	 * @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;
	}
}