4 * Content object implementation for representing flat text.
6 * TextContent instances are immutable
10 class TextContent
extends AbstractContent
{
12 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT
) {
13 parent
::__construct( $model_id );
15 if ( !is_string( $text ) ) {
16 throw new MWException( "TextContent expects a string in the constructor." );
22 public function copy() {
23 return $this; # NOTE: this is ok since TextContent are immutable.
26 public function getTextForSummary( $maxlength = 250 ) {
29 $text = $this->getNativeData();
31 $truncatedtext = $wgContLang->truncate(
32 preg_replace( "/[\n\r]/", ' ', $text ),
33 max( 0, $maxlength ) );
35 return $truncatedtext;
39 * returns the text's size in bytes.
41 * @return int The size
43 public function getSize( ) {
44 $text = $this->getNativeData( );
45 return strlen( $text );
49 * Returns true if this content is not a redirect, and $wgArticleCountMethod
52 * @param $hasLinks Bool: if it is known whether this content contains links,
53 * provide this information here, to avoid redundant parsing to find out.
55 * @return bool True if the content is countable
57 public function isCountable( $hasLinks = null ) {
58 global $wgArticleCountMethod;
60 if ( $this->isRedirect( ) ) {
64 if ( $wgArticleCountMethod === 'any' ) {
72 * Returns the text represented by this Content object, as a string.
76 public function getNativeData( ) {
82 * Returns the text represented by this Content object, as a string.
86 public function getTextForSearchIndex( ) {
87 return $this->getNativeData();
91 * Returns the text represented by this Content object, as a string.
95 public function getWikitextForTransclusion( ) {
96 return $this->getNativeData();
100 * Diff this content object with another content object..
104 * @param $that Content the other content object to compare this content object to
105 * @param $lang Language the language object to use for text segmentation.
106 * If not given, $wgContentLang is used.
108 * @return DiffResult a diff representing the changes that would have to be
109 * made to this content object to make it equal to $that.
111 public function diff( Content
$that, Language
$lang = null ) {
114 $this->checkModelID( $that->getModel() );
116 # @todo: could implement this in DifferenceEngine and just delegate here?
118 if ( !$lang ) $lang = $wgContLang;
120 $otext = $this->getNativeData();
121 $ntext = $this->getNativeData();
123 # Note: Use native PHP diff, external engines don't give us abstract output
124 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
125 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
127 $diff = new Diff( $ota, $nta );
133 * Returns a generic ParserOutput object, wrapping the HTML returned by
136 * @param $title Title Context title for parsing
137 * @param $revId int|null Revision ID (for {{REVISIONID}})
138 * @param $options ParserOptions|null Parser options
139 * @param $generateHtml bool Whether or not to generate HTML
141 * @return ParserOutput representing the HTML form of the text
143 public function getParserOutput( Title
$title,
145 ParserOptions
$options = null, $generateHtml = true
147 # Generic implementation, relying on $this->getHtml()
149 if ( $generateHtml ) {
150 $html = $this->getHtml();
155 $po = new ParserOutput( $html );
160 * Generates an HTML version of the content, for display. Used by
161 * getParserOutput() to construct a ParserOutput object.
163 * This default implementation just calls getHighlightHtml(). Content
164 * models that have another mapping to HTML (as is the case for markup
165 * languages like wikitext) should override this method to generate the
168 * @return string An HTML representation of the content
170 protected function getHtml() {
171 return $this->getHighlightHtml();
175 * Generates a syntax-highlighted version of the content, as HTML.
176 * Used by the default implementation of getHtml().
178 * @return string an HTML representation of the content's markup
180 protected function getHighlightHtml( ) {
181 # TODO: make Highlighter interface, use highlighter here, if available
182 return htmlspecialchars( $this->getNativeData() );