74c5a882cda8852079d1fc108fa4c346f4ff11bb
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 ( !is_string( $text ) ) {
36 throw new MWException( "TextContent expects a string in the constructor." );
42 public function copy() {
43 return $this; # NOTE: this is ok since TextContent are immutable.
46 public function getTextForSummary( $maxlength = 250 ) {
49 $text = $this->getNativeData();
51 $truncatedtext = $wgContLang->truncate(
52 preg_replace( "/[\n\r]/", ' ', $text ),
53 max( 0, $maxlength ) );
55 return $truncatedtext;
59 * returns the text's size in bytes.
61 * @return int The size
63 public function getSize( ) {
64 $text = $this->getNativeData( );
65 return strlen( $text );
69 * Returns true if this content is not a redirect, and $wgArticleCountMethod
72 * @param $hasLinks Bool: if it is known whether this content contains links,
73 * provide this information here, to avoid redundant parsing to find out.
75 * @return bool True if the content is countable
77 public function isCountable( $hasLinks = null ) {
78 global $wgArticleCountMethod;
80 if ( $this->isRedirect( ) ) {
84 if ( $wgArticleCountMethod === 'any' ) {
92 * Returns the text represented by this Content object, as a string.
94 * @return string: the raw text
96 public function getNativeData( ) {
102 * Returns the text represented by this Content object, as a string.
104 * @return string: the raw text
106 public function getTextForSearchIndex( ) {
107 return $this->getNativeData();
111 * Returns the text represented by this Content object, as a string.
113 * @return string: the raw text
115 public function getWikitextForTransclusion( ) {
116 return $this->getNativeData();
120 * Returns a Content object with pre-save transformations applied.
121 * This implementation just trims trailing whitespace.
123 * @param $title Title
125 * @param $popts ParserOptions
128 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
129 $text = $this->getNativeData();
130 $pst = rtrim( $text );
132 return ( $text === $pst ) ?
$this : new WikitextContent( $pst );
136 * Diff this content object with another content object.
140 * @param $that Content: The other content object to compare this content
142 * @param $lang Language: The language object to use for text segmentation.
143 * If not given, $wgContentLang is used.
145 * @return DiffResult: A diff representing the changes that would have to be
146 * made to this content object to make it equal to $that.
148 public function diff( Content
$that, Language
$lang = null ) {
151 $this->checkModelID( $that->getModel() );
153 # @todo: could implement this in DifferenceEngine and just delegate here?
155 if ( !$lang ) $lang = $wgContLang;
157 $otext = $this->getNativeData();
158 $ntext = $this->getNativeData();
160 # Note: Use native PHP diff, external engines don't give us abstract output
161 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
162 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
164 $diff = new Diff( $ota, $nta );
170 * Returns a generic ParserOutput object, wrapping the HTML returned by
173 * @param $title Title Context title for parsing
174 * @param $revId int|null Revision ID (for {{REVISIONID}})
175 * @param $options ParserOptions|null Parser options
176 * @param $generateHtml bool Whether or not to generate HTML
178 * @return ParserOutput representing the HTML form of the text
180 public function getParserOutput( Title
$title,
182 ParserOptions
$options = null, $generateHtml = true
184 global $wgParser, $wgTextModelsToParse;
187 //NOTE: use canonical options per default to produce cacheable output
188 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
191 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
192 // parse just to get links etc into the database
193 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
195 $po = new ParserOutput();
198 if ( $generateHtml ) {
199 $html = $this->getHtml();
204 $po->setText( $html );
209 * Generates an HTML version of the content, for display. Used by
210 * getParserOutput() to construct a ParserOutput object.
212 * This default implementation just calls getHighlightHtml(). Content
213 * models that have another mapping to HTML (as is the case for markup
214 * languages like wikitext) should override this method to generate the
217 * @return string An HTML representation of the content
219 protected function getHtml() {
220 return $this->getHighlightHtml();
224 * Generates a syntax-highlighted version of the content, as HTML.
225 * Used by the default implementation of getHtml().
227 * @return string an HTML representation of the content's markup
229 protected function getHighlightHtml( ) {
230 # TODO: make Highlighter interface, use highlighter here, if available
231 return htmlspecialchars( $this->getNativeData() );