wiki.techinc.nl/includes/content/MessageContent.php
Thiemo Mättig 6806400129 Added and updated Doxygen comments in content handler and diff namespaces
Reasons for touching this are:
* "@param type $var" were mixed in a lot of places. Both works but the
  MediaWiki coding conventions suggest that specific order.
* Things like String and Bool aren't objects and shouldn't be uppercase.
* Tried to fill missing types in "@param $var".
* Tried to fill missing descriptions in "@return type" when I could.
* Removed duplicate descriptions if a @see is sufficend.
* Removed useless descriptions ("isUsefull returns true if usefull").
* Removed useless @return void.
* Replaces mixed[] with array (does have the exact same meaning).
* Tried to find better replacements for "varargs", phpDocumentor
  suggest $var,...
* Order should be @since, @param, @throws, @return, @see. This is the
  order Doxygen renders this.

There is always more to do but I think this is already much better
than before. Please feel free to put more change sets on top of mine
or request more changes by adding comments.

Change-Id: I05262ce06caabf79f68772302524ac91bbead1c6
2014-03-06 11:17:41 +01:00

172 lines
4.1 KiB
PHP

<?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 string[] $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 );
}
}
/**
* Fully parse the text from wikitext to HTML.
*
* @return string Parsed HTML.
*/
public function getHtml() {
return $this->mMessage->parse();
}
/**
* Returns the message text. {{-transformation is done.
*
* @return string Unescaped message text.
*/
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;
}
/**
* @return string
*
* @see Content::getTextForSearchIndex
*/
public function getTextForSearchIndex() {
return $this->mMessage->plain();
}
/**
* @return string
*
* @see Content::getWikitextForTransclusion
*/
public function getWikitextForTransclusion() {
return $this->getWikitext();
}
/**
* @param int $maxLength Maximum length of the summary text, defaults to 250.
*
* @return string The summary text.
*
* @see Content::getTextForSummary
*/
public function getTextForSummary( $maxlength = 250 ) {
return substr( $this->mMessage->plain(), 0, $maxlength );
}
/**
* @return int
*
* @see Content::getSize
*/
public function getSize() {
return strlen( $this->mMessage->plain() );
}
/**
* @return Content. A copy of this object
*
* @see Content::copy
*/
public function copy() {
// MessageContent is immutable (because getNativeData() returns a clone of the Message object)
return $this;
}
/**
* @param bool $hasLinks
*
* @return bool Always false.
*
* @see Content::isCountable
*/
public function isCountable( $hasLinks = null ) {
return false;
}
/**
* @param Title $title Unused.
* @param int $revId Unused.
* @param ParserOptions $options Unused.
* @param bool $generateHtml Whether to generate HTML (default: true).
*
* @return ParserOutput
*
* @see Content::getParserOutput
*/
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;
}
}