Merge "Fix method declaration in UploadFromStash"
[mediawiki.git] / includes / content / TextContent.php
blob872738b4fea7233720bfb48e56377ac8c2449833
1 <?php
3 /**
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
23 * @since 1.21
25 * @file
26 * @ingroup Content
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 ) {
36 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
37 . "This may indicate an error in the caller's scope." );
39 $text = '';
42 if ( !is_string( $text ) ) {
43 throw new MWException( "TextContent expects a string in the constructor." );
46 $this->mText = $text;
49 public function copy() {
50 return $this; # NOTE: this is ok since TextContent are immutable.
53 public function getTextForSummary( $maxlength = 250 ) {
54 global $wgContLang;
56 $text = $this->getNativeData();
58 $truncatedtext = $wgContLang->truncate(
59 preg_replace( "/[\n\r]/", ' ', $text ),
60 max( 0, $maxlength ) );
62 return $truncatedtext;
65 /**
66 * returns the text's size in bytes.
68 * @return int The size
70 public function getSize( ) {
71 $text = $this->getNativeData( );
72 return strlen( $text );
75 /**
76 * Returns true if this content is not a redirect, and $wgArticleCountMethod
77 * is "any".
79 * @param $hasLinks Bool: if it is known whether this content contains links,
80 * provide this information here, to avoid redundant parsing to find out.
82 * @return bool True if the content is countable
84 public function isCountable( $hasLinks = null ) {
85 global $wgArticleCountMethod;
87 if ( $this->isRedirect( ) ) {
88 return false;
91 if ( $wgArticleCountMethod === 'any' ) {
92 return true;
95 return false;
98 /**
99 * Returns the text represented by this Content object, as a string.
101 * @return string: the raw text
103 public function getNativeData( ) {
104 $text = $this->mText;
105 return $text;
109 * Returns the text represented by this Content object, as a string.
111 * @return string: the raw text
113 public function getTextForSearchIndex( ) {
114 return $this->getNativeData();
118 * Returns attempts to convert this content object to wikitext,
119 * and then returns the text string. The conversion may be lossy.
121 * @note: this allows any text-based content to be transcluded as if it was wikitext.
123 * @return string|false: the raw text, or null if the conversion failed
125 public function getWikitextForTransclusion( ) {
126 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
128 if ( $wikitext ) {
129 return $wikitext->getNativeData();
130 } else {
131 return false;
136 * Returns a Content object with pre-save transformations applied.
137 * This implementation just trims trailing whitespace.
139 * @param $title Title
140 * @param $user User
141 * @param $popts ParserOptions
142 * @return Content
144 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
145 $text = $this->getNativeData();
146 $pst = rtrim( $text );
148 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
152 * Diff this content object with another content object.
154 * @since 1.21diff
156 * @param $that Content: The other content object to compare this content
157 * object to.
158 * @param $lang Language: The language object to use for text segmentation.
159 * If not given, $wgContentLang is used.
161 * @return DiffResult: A diff representing the changes that would have to be
162 * made to this content object to make it equal to $that.
164 public function diff( Content $that, Language $lang = null ) {
165 global $wgContLang;
167 $this->checkModelID( $that->getModel() );
169 # @todo: could implement this in DifferenceEngine and just delegate here?
171 if ( !$lang ) $lang = $wgContLang;
173 $otext = $this->getNativeData();
174 $ntext = $this->getNativeData();
176 # Note: Use native PHP diff, external engines don't give us abstract output
177 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
178 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
180 $diff = new Diff( $ota, $nta );
181 return $diff;
186 * Returns a generic ParserOutput object, wrapping the HTML returned by
187 * getHtml().
189 * @param $title Title Context title for parsing
190 * @param $revId int|null Revision ID (for {{REVISIONID}})
191 * @param $options ParserOptions|null Parser options
192 * @param $generateHtml bool Whether or not to generate HTML
194 * @return ParserOutput representing the HTML form of the text
196 public function getParserOutput( Title $title,
197 $revId = null,
198 ParserOptions $options = null, $generateHtml = true
200 global $wgParser, $wgTextModelsToParse;
202 if ( !$options ) {
203 //NOTE: use canonical options per default to produce cacheable output
204 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
207 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
208 // parse just to get links etc into the database
209 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
210 } else {
211 $po = new ParserOutput();
214 if ( $generateHtml ) {
215 $html = $this->getHtml();
216 } else {
217 $html = '';
220 $po->setText( $html );
221 return $po;
225 * Generates an HTML version of the content, for display. Used by
226 * getParserOutput() to construct a ParserOutput object.
228 * This default implementation just calls getHighlightHtml(). Content
229 * models that have another mapping to HTML (as is the case for markup
230 * languages like wikitext) should override this method to generate the
231 * appropriate HTML.
233 * @return string An HTML representation of the content
235 protected function getHtml() {
236 return $this->getHighlightHtml();
240 * Generates a syntax-highlighted version of the content, as HTML.
241 * Used by the default implementation of getHtml().
243 * @return string an HTML representation of the content's markup
245 protected function getHighlightHtml( ) {
246 # TODO: make Highlighter interface, use highlighter here, if available
247 return htmlspecialchars( $this->getNativeData() );
251 * @see Content::convert()
253 * This implementation provides lossless conversion between content models based
254 * on TextContent.
256 * @param String $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
257 * @param String $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
258 * not allowed, full round-trip conversion is expected to work without losing information.
260 * @return Content|bool A content object with the content model $toModel, or false if
261 * that conversion is not supported.
263 public function convert( $toModel, $lossy = '' ) {
264 $converted = parent::convert( $toModel, $lossy );
266 if ( $converted !== false ) {
267 return $converted;
270 $toHandler = ContentHandler::getForModelID( $toModel );
272 if ( $toHandler instanceof TextContentHandler ) {
273 //NOTE: ignore content serialization format - it's just text anyway.
274 $text = $this->getNativeData();
275 $converted = $toHandler->unserializeContent( $text );
278 return $converted;