summaryrefslogtreecommitdiff
path: root/includes/content/TextContent.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/content/TextContent.php')
-rw-r--r--includes/content/TextContent.php137
1 files changed, 78 insertions, 59 deletions
diff --git a/includes/content/TextContent.php b/includes/content/TextContent.php
index f66dacd7..c479f202 100644
--- a/includes/content/TextContent.php
+++ b/includes/content/TextContent.php
@@ -34,12 +34,16 @@
*/
class TextContent extends AbstractContent {
+ /**
+ * @param string $text
+ * @param string $model_id
+ */
public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
parent::__construct( $model_id );
if ( $text === null || $text === false ) {
wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
- . "This may indicate an error in the caller's scope." );
+ . "This may indicate an error in the caller's scope.", 2 );
$text = '';
}
@@ -51,6 +55,11 @@ class TextContent extends AbstractContent {
$this->mText = $text;
}
+ /**
+ * @note Mutable subclasses MUST override this to return a copy!
+ *
+ * @return Content $this
+ */
public function copy() {
return $this; # NOTE: this is ok since TextContent are immutable.
}
@@ -68,12 +77,13 @@ class TextContent extends AbstractContent {
}
/**
- * returns the text's size in bytes.
+ * Returns the text's size in bytes.
*
- * @return int The size
+ * @return int
*/
public function getSize() {
$text = $this->getNativeData();
+
return strlen( $text );
}
@@ -81,10 +91,10 @@ class TextContent extends AbstractContent {
* Returns true if this content is not a redirect, and $wgArticleCountMethod
* is "any".
*
- * @param bool $hasLinks if it is known whether this content contains links,
+ * @param bool $hasLinks If it is known whether this content contains links,
* provide this information here, to avoid redundant parsing to find out.
*
- * @return bool True if the content is countable
+ * @return bool
*/
public function isCountable( $hasLinks = null ) {
global $wgArticleCountMethod;
@@ -103,17 +113,16 @@ class TextContent extends AbstractContent {
/**
* Returns the text represented by this Content object, as a string.
*
- * @return string: the raw text
+ * @return string The raw text.
*/
public function getNativeData() {
- $text = $this->mText;
- return $text;
+ return $this->mText;
}
/**
* Returns the text represented by this Content object, as a string.
*
- * @return string: the raw text
+ * @return string The raw text.
*/
public function getTextForSearchIndex() {
return $this->getNativeData();
@@ -123,9 +132,9 @@ class TextContent extends AbstractContent {
* Returns attempts to convert this content object to wikitext,
* and then returns the text string. The conversion may be lossy.
*
- * @note: this allows any text-based content to be transcluded as if it was wikitext.
+ * @note this allows any text-based content to be transcluded as if it was wikitext.
*
- * @return string|false: the raw text, or null if the conversion failed
+ * @return string|bool The raw text, or false if the conversion failed.
*/
public function getWikitextForTransclusion() {
$wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
@@ -141,29 +150,29 @@ class TextContent extends AbstractContent {
* Returns a Content object with pre-save transformations applied.
* This implementation just trims trailing whitespace.
*
- * @param $title Title
- * @param $user User
- * @param $popts ParserOptions
+ * @param Title $title
+ * @param User $user
+ * @param ParserOptions $popts
+ *
* @return Content
*/
public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
$text = $this->getNativeData();
$pst = rtrim( $text );
- return ( $text === $pst ) ? $this : new WikitextContent( $pst );
+ return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() );
}
/**
* Diff this content object with another content object.
*
- * @since 1.21diff
+ * @since 1.21
*
- * @param $that Content: The other content object to compare this content
- * object to.
- * @param $lang Language: The language object to use for text segmentation.
+ * @param Content $that The other content object to compare this content object to.
+ * @param Language $lang The language object to use for text segmentation.
* If not given, $wgContentLang is used.
*
- * @return DiffResult: A diff representing the changes that would have to be
+ * @return Diff A diff representing the changes that would have to be
* made to this content object to make it equal to $that.
*/
public function diff( Content $that, Language $lang = null ) {
@@ -178,43 +187,42 @@ class TextContent extends AbstractContent {
}
$otext = $this->getNativeData();
- $ntext = $this->getNativeData();
+ $ntext = $that->getNativeData();
# Note: Use native PHP diff, external engines don't give us abstract output
$ota = explode( "\n", $lang->segmentForDiff( $otext ) );
$nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
$diff = new Diff( $ota, $nta );
+
return $diff;
}
/**
- * Returns a generic ParserOutput object, wrapping the HTML returned by
- * getHtml().
+ * Fills the provided ParserOutput object with information derived from the content.
+ * Unless $generateHtml was false, this includes an HTML representation of the content
+ * provided by getHtml().
*
- * @param $title Title Context title for parsing
- * @param int|null $revId Revision ID (for {{REVISIONID}})
- * @param $options ParserOptions|null Parser options
- * @param bool $generateHtml Whether or not to generate HTML
+ * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki
+ * wikitext parser on the text to extract any (wikitext) links, magic words, etc.
*
- * @return ParserOutput representing the HTML form of the text
+ * Subclasses may override this to provide custom content processing.
+ * For custom HTML generation alone, it is sufficient to override getHtml().
+ *
+ * @param Title $title Context title for parsing
+ * @param int $revId Revision ID (for {{REVISIONID}})
+ * @param ParserOptions $options Parser options
+ * @param bool $generateHtml Whether or not to generate HTML
+ * @param ParserOutput $output The output object to fill (reference).
*/
- public function getParserOutput( Title $title,
- $revId = null,
- ParserOptions $options = null, $generateHtml = true
+ protected function fillParserOutput( Title $title, $revId,
+ ParserOptions $options, $generateHtml, ParserOutput &$output
) {
global $wgParser, $wgTextModelsToParse;
- if ( !$options ) {
- //NOTE: use canonical options per default to produce cacheable output
- $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
- }
-
if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
- // parse just to get links etc into the database
- $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
- } else {
- $po = new ParserOutput();
+ // parse just to get links etc into the database, HTML is replaced below.
+ $output = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
}
if ( $generateHtml ) {
@@ -223,18 +231,19 @@ class TextContent extends AbstractContent {
$html = '';
}
- $po->setText( $html );
- return $po;
+ $output->setText( $html );
}
/**
* Generates an HTML version of the content, for display. Used by
- * getParserOutput() to construct a ParserOutput object.
+ * fillParserOutput() to provide HTML for the ParserOutput object.
+ *
+ * Subclasses may override this to provide a custom HTML rendering.
+ * If further information is to be derived from the content (such as
+ * categories), the fillParserOutput() method can be overridden instead.
*
- * This default implementation just calls getHighlightHtml(). Content
- * models that have another mapping to HTML (as is the case for markup
- * languages like wikitext) should override this method to generate the
- * appropriate HTML.
+ * For backwards-compatibility, this default implementation just calls
+ * getHighlightHtml().
*
* @return string An HTML representation of the content
*/
@@ -243,28 +252,37 @@ class TextContent extends AbstractContent {
}
/**
- * Generates a syntax-highlighted version of the content, as HTML.
- * Used by the default implementation of getHtml().
+ * Generates an HTML version of the content, for display.
+ *
+ * This default implementation returns an HTML-escaped version
+ * of the raw text content.
+ *
+ * @note The functionality of this method should really be implemented
+ * in getHtml(), and subclasses should override getHtml() if needed.
+ * getHighlightHtml() is kept around for backward compatibility with
+ * extensions that already override it.
+ *
+ * @deprecated since 1.24. Use getHtml() instead. In particular, subclasses overriding
+ * getHighlightHtml() should override getHtml() instead.
*
- * @return string an HTML representation of the content's markup
+ * @return string An HTML representation of the content
*/
protected function getHighlightHtml() {
- # TODO: make Highlighter interface, use highlighter here, if available
return htmlspecialchars( $this->getNativeData() );
}
/**
- * @see Content::convert()
- *
* This implementation provides lossless conversion between content models based
* on TextContent.
*
- * @param string $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
- * @param string $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
- * not allowed, full round-trip conversion is expected to work without losing information.
+ * @param string $toModel The desired content model, use the CONTENT_MODEL_XXX flags.
+ * @param string $lossy Flag, set to "lossy" to allow lossy conversion. If lossy conversion is not
+ * allowed, full round-trip conversion is expected to work without losing information.
*
- * @return Content|bool A content object with the content model $toModel, or false if
- * that conversion is not supported.
+ * @return Content|bool A content object with the content model $toModel, or false if that
+ * conversion is not supported.
+ *
+ * @see Content::convert()
*/
public function convert( $toModel, $lossy = '' ) {
$converted = parent::convert( $toModel, $lossy );
@@ -276,11 +294,12 @@ class TextContent extends AbstractContent {
$toHandler = ContentHandler::getForModelID( $toModel );
if ( $toHandler instanceof TextContentHandler ) {
- //NOTE: ignore content serialization format - it's just text anyway.
+ // NOTE: ignore content serialization format - it's just text anyway.
$text = $this->getNativeData();
$converted = $toHandler->unserializeContent( $text );
}
return $converted;
}
+
}