4 * Content object implementation for representing flat text.
6 * TextContent instances are immutable
10 abstract class TextContent
extends AbstractContent
{
12 public function __construct( $text, $model_id = null ) {
13 parent
::__construct( $model_id );
18 public function copy() {
19 return $this; # NOTE: this is ok since TextContent are immutable.
22 public function getTextForSummary( $maxlength = 250 ) {
25 $text = $this->getNativeData();
27 $truncatedtext = $wgContLang->truncate(
28 preg_replace( "/[\n\r]/", ' ', $text ),
29 max( 0, $maxlength ) );
31 return $truncatedtext;
35 * returns the text's size in bytes.
37 * @return int The size
39 public function getSize( ) {
40 $text = $this->getNativeData( );
41 return strlen( $text );
45 * Returns true if this content is not a redirect, and $wgArticleCountMethod
48 * @param $hasLinks Bool: if it is known whether this content contains links,
49 * provide this information here, to avoid redundant parsing to find out.
51 * @return bool True if the content is countable
53 public function isCountable( $hasLinks = null ) {
54 global $wgArticleCountMethod;
56 if ( $this->isRedirect( ) ) {
60 if ( $wgArticleCountMethod === 'any' ) {
68 * Returns the text represented by this Content object, as a string.
72 public function getNativeData( ) {
78 * Returns the text represented by this Content object, as a string.
82 public function getTextForSearchIndex( ) {
83 return $this->getNativeData();
87 * Returns the text represented by this Content object, as a string.
91 public function getWikitextForTransclusion( ) {
92 return $this->getNativeData();
96 * Diff this content object with another content object..
100 * @param $that Content the other content object to compare this content object to
101 * @param $lang Language the language object to use for text segmentation.
102 * If not given, $wgContentLang is used.
104 * @return DiffResult a diff representing the changes that would have to be
105 * made to this content object to make it equal to $that.
107 public function diff( Content
$that, Language
$lang = null ) {
110 $this->checkModelID( $that->getModel() );
112 # @todo: could implement this in DifferenceEngine and just delegate here?
114 if ( !$lang ) $lang = $wgContLang;
116 $otext = $this->getNativeData();
117 $ntext = $this->getNativeData();
119 # Note: Use native PHP diff, external engines don't give us abstract output
120 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
121 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
123 $diff = new Diff( $ota, $nta );
129 * Returns a generic ParserOutput object, wrapping the HTML returned by
132 * @param $title Title Context title for parsing
133 * @param $revId int|null Revision ID (for {{REVISIONID}})
134 * @param $options ParserOptions|null Parser options
135 * @param $generateHtml bool Whether or not to generate HTML
137 * @return ParserOutput representing the HTML form of the text
139 public function getParserOutput( Title
$title,
141 ParserOptions
$options = null, $generateHtml = true
143 # Generic implementation, relying on $this->getHtml()
145 if ( $generateHtml ) {
146 $html = $this->getHtml();
151 $po = new ParserOutput( $html );
156 * Generates an HTML version of the content, for display. Used by
157 * getParserOutput() to construct a ParserOutput object.
159 * This default implementation just calls getHighlightHtml(). Content
160 * models that have another mapping to HTML (as is the case for markup
161 * languages like wikitext) should override this method to generate the
164 * @return string An HTML representation of the content
166 protected function getHtml() {
167 return $this->getHighlightHtml();
171 * Generates a syntax-highlighted version of the content, as HTML.
172 * Used by the default implementation of getHtml().
174 * @return string an HTML representation of the content's markup
176 protected function getHighlightHtml( ) {
177 # TODO: make Highlighter interface, use highlighter here, if available
178 return htmlspecialchars( $this->getNativeData() );