Merge "(bug 16020) Fix race condition in User::addToDatabase()"
[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 * @since 1.21
9 */
10 class MessageContent extends AbstractContent {
11
12 /**
13 * @var Message
14 */
15 protected $mMessage;
16
17 /**
18 * @param Message|String $msg A Message object, or a message key
19 * @param array|null $params An optional array of message parameters
20 */
21 public function __construct( $msg, $params = null ) {
22 # XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
23 parent::__construct( CONTENT_MODEL_WIKITEXT );
24
25 if ( is_string( $msg ) ) {
26 $this->mMessage = wfMessage( $msg );
27 } else {
28 $this->mMessage = clone $msg;
29 }
30
31 if ( $params ) {
32 $this->mMessage = $this->mMessage->params( $params );
33 }
34 }
35
36 /**
37 * Returns the message as rendered HTML
38 *
39 * @return string The message text, parsed into html
40 */
41 public function getHtml() {
42 return $this->mMessage->parse();
43 }
44
45 /**
46 * Returns the message as rendered HTML
47 *
48 * @return string The message text, parsed into html
49 */
50 public function getWikitext() {
51 return $this->mMessage->text();
52 }
53
54 /**
55 * Returns the message object, with any parameters already substituted.
56 *
57 * @return Message The message object.
58 */
59 public function getNativeData() {
60 //NOTE: Message objects are mutable. Cloning here makes MessageContent immutable.
61 return clone $this->mMessage;
62 }
63
64 /**
65 * @see Content::getTextForSearchIndex
66 */
67 public function getTextForSearchIndex() {
68 return $this->mMessage->plain();
69 }
70
71 /**
72 * @see Content::getWikitextForTransclusion
73 */
74 public function getWikitextForTransclusion() {
75 return $this->getWikitext();
76 }
77
78 /**
79 * @see Content::getTextForSummary
80 */
81 public function getTextForSummary( $maxlength = 250 ) {
82 return substr( $this->mMessage->plain(), 0, $maxlength );
83 }
84
85 /**
86 * @see Content::getSize
87 *
88 * @return int
89 */
90 public function getSize() {
91 return strlen( $this->mMessage->plain() );
92 }
93
94 /**
95 * @see Content::copy
96 *
97 * @return Content. A copy of this object
98 */
99 public function copy() {
100 // MessageContent is immutable (because getNativeData() returns a clone of the Message object)
101 return $this;
102 }
103
104 /**
105 * @see Content::isCountable
106 *
107 * @return bool false
108 */
109 public function isCountable( $hasLinks = null ) {
110 return false;
111 }
112
113 /**
114 * @see Content::getParserOutput
115 *
116 * @return ParserOutput
117 */
118 public function getParserOutput(
119 Title $title, $revId = null,
120 ParserOptions $options = null, $generateHtml = true
121 ) {
122
123 if ( $generateHtml ) {
124 $html = $this->getHtml();
125 } else {
126 $html = '';
127 }
128
129 $po = new ParserOutput( $html );
130 return $po;
131 }
132 }