4 * Content object implementation for representing flat text.
6 * TextContent instances are immutable
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
28 * @author Daniel Kinzler
30 class TextContent
extends AbstractContent
{
32 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT
) {
33 parent
::__construct( $model_id );
35 if ( $text === null ||
$text === false ) {
39 if ( !is_string( $text ) ) {
40 throw new MWException( "TextContent expects a string in the constructor." );
46 public function copy() {
47 return $this; # NOTE: this is ok since TextContent are immutable.
50 public function getTextForSummary( $maxlength = 250 ) {
53 $text = $this->getNativeData();
55 $truncatedtext = $wgContLang->truncate(
56 preg_replace( "/[\n\r]/", ' ', $text ),
57 max( 0, $maxlength ) );
59 return $truncatedtext;
63 * returns the text's size in bytes.
65 * @return int The size
67 public function getSize( ) {
68 $text = $this->getNativeData( );
69 return strlen( $text );
73 * Returns true if this content is not a redirect, and $wgArticleCountMethod
76 * @param $hasLinks Bool: if it is known whether this content contains links,
77 * provide this information here, to avoid redundant parsing to find out.
79 * @return bool True if the content is countable
81 public function isCountable( $hasLinks = null ) {
82 global $wgArticleCountMethod;
84 if ( $this->isRedirect( ) ) {
88 if ( $wgArticleCountMethod === 'any' ) {
96 * Returns the text represented by this Content object, as a string.
98 * @return string: the raw text
100 public function getNativeData( ) {
101 $text = $this->mText
;
106 * Returns the text represented by this Content object, as a string.
108 * @return string: the raw text
110 public function getTextForSearchIndex( ) {
111 return $this->getNativeData();
115 * Returns the text represented by this Content object, as a string.
117 * @return string: the raw text
119 public function getWikitextForTransclusion( ) {
120 return $this->getNativeData();
124 * Returns a Content object with pre-save transformations applied.
125 * This implementation just trims trailing whitespace.
127 * @param $title Title
129 * @param $popts ParserOptions
132 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
133 $text = $this->getNativeData();
134 $pst = rtrim( $text );
136 return ( $text === $pst ) ?
$this : new WikitextContent( $pst );
140 * Diff this content object with another content object.
144 * @param $that Content: The other content object to compare this content
146 * @param $lang Language: The language object to use for text segmentation.
147 * If not given, $wgContentLang is used.
149 * @return DiffResult: A diff representing the changes that would have to be
150 * made to this content object to make it equal to $that.
152 public function diff( Content
$that, Language
$lang = null ) {
155 $this->checkModelID( $that->getModel() );
157 # @todo: could implement this in DifferenceEngine and just delegate here?
159 if ( !$lang ) $lang = $wgContLang;
161 $otext = $this->getNativeData();
162 $ntext = $this->getNativeData();
164 # Note: Use native PHP diff, external engines don't give us abstract output
165 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
166 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
168 $diff = new Diff( $ota, $nta );
174 * Returns a generic ParserOutput object, wrapping the HTML returned by
177 * @param $title Title Context title for parsing
178 * @param $revId int|null Revision ID (for {{REVISIONID}})
179 * @param $options ParserOptions|null Parser options
180 * @param $generateHtml bool Whether or not to generate HTML
182 * @return ParserOutput representing the HTML form of the text
184 public function getParserOutput( Title
$title,
186 ParserOptions
$options = null, $generateHtml = true
188 global $wgParser, $wgTextModelsToParse;
191 //NOTE: use canonical options per default to produce cacheable output
192 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
195 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
196 // parse just to get links etc into the database
197 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
199 $po = new ParserOutput();
202 if ( $generateHtml ) {
203 $html = $this->getHtml();
208 $po->setText( $html );
213 * Generates an HTML version of the content, for display. Used by
214 * getParserOutput() to construct a ParserOutput object.
216 * This default implementation just calls getHighlightHtml(). Content
217 * models that have another mapping to HTML (as is the case for markup
218 * languages like wikitext) should override this method to generate the
221 * @return string An HTML representation of the content
223 protected function getHtml() {
224 return $this->getHighlightHtml();
228 * Generates a syntax-highlighted version of the content, as HTML.
229 * Used by the default implementation of getHtml().
231 * @return string an HTML representation of the content's markup
233 protected function getHighlightHtml( ) {
234 # TODO: make Highlighter interface, use highlighter here, if available
235 return htmlspecialchars( $this->getNativeData() );