MessageContent.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * Wrapper allowing us to handle a system message as a Content object.
  29. * Note that this is generally *not* used to represent content from the
  30. * MediaWiki namespace, and that there is no MessageContentHandler.
  31. * MessageContent is just intended as glue for wrapping a message programmatically.
  32. *
  33. * @ingroup Content
  34. */
  35. class MessageContent extends AbstractContent {
  36. /**
  37. * @var Message
  38. */
  39. protected $mMessage;
  40. /**
  41. * @param Message|string $msg A Message object, or a message key.
  42. * @param string[]|null $params An optional array of message parameters.
  43. */
  44. public function __construct( $msg, $params = null ) {
  45. # XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
  46. parent::__construct( CONTENT_MODEL_WIKITEXT );
  47. if ( is_string( $msg ) ) {
  48. $this->mMessage = wfMessage( $msg );
  49. } else {
  50. $this->mMessage = clone $msg;
  51. }
  52. if ( $params ) {
  53. $this->mMessage = $this->mMessage->params( $params );
  54. }
  55. }
  56. /**
  57. * Fully parse the text from wikitext to HTML.
  58. *
  59. * @return string Parsed HTML.
  60. */
  61. public function getHtml() {
  62. return $this->mMessage->parse();
  63. }
  64. /**
  65. * Returns the message text. {{-transformation is done.
  66. *
  67. * @return string Unescaped message text.
  68. */
  69. public function getWikitext() {
  70. return $this->mMessage->text();
  71. }
  72. /**
  73. * Returns the message object, with any parameters already substituted.
  74. *
  75. * @return Message The message object.
  76. */
  77. public function getNativeData() {
  78. // NOTE: Message objects are mutable. Cloning here makes MessageContent immutable.
  79. return clone $this->mMessage;
  80. }
  81. /**
  82. * @return string
  83. *
  84. * @see Content::getTextForSearchIndex
  85. */
  86. public function getTextForSearchIndex() {
  87. return $this->mMessage->plain();
  88. }
  89. /**
  90. * @return string
  91. *
  92. * @see Content::getWikitextForTransclusion
  93. */
  94. public function getWikitextForTransclusion() {
  95. return $this->getWikitext();
  96. }
  97. /**
  98. * @param int $maxlength Maximum length of the summary text, defaults to 250.
  99. *
  100. * @return string The summary text.
  101. *
  102. * @see Content::getTextForSummary
  103. */
  104. public function getTextForSummary( $maxlength = 250 ) {
  105. return substr( $this->mMessage->plain(), 0, $maxlength );
  106. }
  107. /**
  108. * @return int
  109. *
  110. * @see Content::getSize
  111. */
  112. public function getSize() {
  113. return strlen( $this->mMessage->plain() );
  114. }
  115. /**
  116. * @return Content A copy of this object
  117. *
  118. * @see Content::copy
  119. */
  120. public function copy() {
  121. // MessageContent is immutable (because getNativeData() returns a clone of the Message object)
  122. return $this;
  123. }
  124. /**
  125. * @param bool|null $hasLinks
  126. *
  127. * @return bool Always false.
  128. *
  129. * @see Content::isCountable
  130. */
  131. public function isCountable( $hasLinks = null ) {
  132. return false;
  133. }
  134. /**
  135. * @param Title $title Unused.
  136. * @param int|null $revId Unused.
  137. * @param ParserOptions|null $options Unused.
  138. * @param bool $generateHtml Whether to generate HTML (default: true).
  139. *
  140. * @return ParserOutput
  141. *
  142. * @see Content::getParserOutput
  143. */
  144. public function getParserOutput( Title $title, $revId = null,
  145. ParserOptions $options = null, $generateHtml = true ) {
  146. if ( $generateHtml ) {
  147. $html = $this->getHtml();
  148. } else {
  149. $html = '';
  150. }
  151. $po = new ParserOutput( $html );
  152. // Message objects are in the user language.
  153. $po->recordOption( 'userlang' );
  154. return $po;
  155. }
  156. }