3 * Content object implementation for representing flat text.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * @author Daniel Kinzler
28 use MediaWiki\MediaWikiServices
;
31 * Content object implementation for representing flat text.
33 * TextContent instances are immutable
37 class TextContent
extends AbstractContent
{
46 * @param string $model_id
49 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT
) {
50 parent
::__construct( $model_id );
52 if ( $text === null ||
$text === false ) {
53 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
54 . "This may indicate an error in the caller's scope.", 2 );
59 if ( !is_string( $text ) ) {
60 throw new MWException( "TextContent expects a string in the constructor." );
67 * @note Mutable subclasses MUST override this to return a copy!
69 * @return Content $this
71 public function copy() {
72 return $this; # NOTE: this is ok since TextContent are immutable.
75 public function getTextForSummary( $maxlength = 250 ) {
76 $text = $this->getText();
78 $truncatedtext = MediaWikiServices
::getInstance()->getContentLanguage()->
79 truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
81 return $truncatedtext;
85 * Returns the text's size in bytes.
89 public function getSize() {
90 $text = $this->getText();
92 return strlen( $text );
96 * Returns true if this content is not a redirect, and $wgArticleCountMethod
99 * @param bool|null $hasLinks If it is known whether this content contains links,
100 * provide this information here, to avoid redundant parsing to find out.
104 public function isCountable( $hasLinks = null ) {
105 global $wgArticleCountMethod;
107 if ( $this->isRedirect() ) {
111 if ( $wgArticleCountMethod === 'any' ) {
119 * Returns the text represented by this Content object, as a string.
121 * @deprecated since 1.33 use getText() instead.
123 * @return string The raw text. Subclasses may guarantee a specific syntax here.
125 public function getNativeData() {
126 return $this->getText();
130 * Returns the text represented by this Content object, as a string.
134 * @return string The raw text.
136 public function getText() {
141 * Returns the text represented by this Content object, as a string.
143 * @return string The raw text.
145 public function getTextForSearchIndex() {
146 return $this->getText();
150 * Returns attempts to convert this content object to wikitext,
151 * and then returns the text string. The conversion may be lossy.
153 * @note this allows any text-based content to be transcluded as if it was wikitext.
155 * @return string|bool The raw text, or false if the conversion failed.
157 public function getWikitextForTransclusion() {
158 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT
, 'lossy' );
161 return $wikitext->getText();
168 * Do a "\r\n" -> "\n" and "\r" -> "\n" transformation
169 * as well as trim trailing whitespace
171 * This was formerly part of Parser::preSaveTransform, but
172 * for non-wikitext content models they probably still want
173 * to normalize line endings without all of the other PST
177 * @param string $text
180 public static function normalizeLineEndings( $text ) {
181 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
185 * Returns a Content object with pre-save transformations applied.
187 * At a minimum, subclasses should make sure to call TextContent::normalizeLineEndings()
188 * either directly or part of Parser::preSaveTransform().
190 * @param Title $title
192 * @param ParserOptions $popts
196 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
197 $text = $this->getText();
198 $pst = self
::normalizeLineEndings( $text );
200 return ( $text === $pst ) ?
$this : new static( $pst, $this->getModel() );
204 * Diff this content object with another content object.
208 * @param Content $that The other content object to compare this content object to.
209 * @param Language|null $lang The language object to use for text segmentation.
210 * If not given, the content language is used.
212 * @return Diff A diff representing the changes that would have to be
213 * made to this content object to make it equal to $that.
215 public function diff( Content
$that, Language
$lang = null ) {
216 $this->checkModelID( $that->getModel() );
218 // @todo could implement this in DifferenceEngine and just delegate here?
221 $lang = MediaWikiServices
::getInstance()->getContentLanguage();
224 $otext = $this->getText();
225 $ntext = $that->getText();
227 # Note: Use native PHP diff, external engines don't give us abstract output
228 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
229 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
231 $diff = new Diff( $ota, $nta );
237 * Fills the provided ParserOutput object with information derived from the content.
238 * Unless $generateHtml was false, this includes an HTML representation of the content
239 * provided by getHtml().
241 * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki
242 * wikitext parser on the text to extract any (wikitext) links, magic words, etc.
244 * Subclasses may override this to provide custom content processing.
245 * For custom HTML generation alone, it is sufficient to override getHtml().
247 * @param Title $title Context title for parsing
248 * @param int $revId Revision ID (for {{REVISIONID}})
249 * @param ParserOptions $options
250 * @param bool $generateHtml Whether or not to generate HTML
251 * @param ParserOutput &$output The output object to fill (reference).
253 protected function fillParserOutput( Title
$title, $revId,
254 ParserOptions
$options, $generateHtml, ParserOutput
&$output
256 global $wgTextModelsToParse;
258 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
259 // parse just to get links etc into the database, HTML is replaced below.
260 $output = MediaWikiServices
::getInstance()->getParser()
261 ->parse( $this->getText(), $title, $options, true, true, $revId );
264 if ( $generateHtml ) {
265 $html = $this->getHtml();
270 $output->clearWrapperDivClass();
271 $output->setText( $html );
275 * Generates an HTML version of the content, for display. Used by
276 * fillParserOutput() to provide HTML for the ParserOutput object.
278 * Subclasses may override this to provide a custom HTML rendering.
279 * If further information is to be derived from the content (such as
280 * categories), the fillParserOutput() method can be overridden instead.
282 * For backwards-compatibility, this default implementation just calls
283 * getHighlightHtml().
285 * @return string An HTML representation of the content
287 protected function getHtml() {
288 return $this->getHighlightHtml();
292 * Generates an HTML version of the content, for display.
294 * This default implementation returns an HTML-escaped version
295 * of the raw text content.
297 * @note The functionality of this method should really be implemented
298 * in getHtml(), and subclasses should override getHtml() if needed.
299 * getHighlightHtml() is kept around for backward compatibility with
300 * extensions that already override it.
302 * @deprecated since 1.24. Use getHtml() instead. In particular, subclasses overriding
303 * getHighlightHtml() should override getHtml() instead.
305 * @return string An HTML representation of the content
307 protected function getHighlightHtml() {
308 return htmlspecialchars( $this->getText() );
312 * This implementation provides lossless conversion between content models based
315 * @param string $toModel The desired content model, use the CONTENT_MODEL_XXX flags.
316 * @param string $lossy Flag, set to "lossy" to allow lossy conversion. If lossy conversion is not
317 * allowed, full round-trip conversion is expected to work without losing information.
319 * @return Content|bool A content object with the content model $toModel, or false if that
320 * conversion is not supported.
322 * @see Content::convert()
324 public function convert( $toModel, $lossy = '' ) {
325 $converted = parent
::convert( $toModel, $lossy );
327 if ( $converted !== false ) {
331 $toHandler = ContentHandler
::getForModelID( $toModel );
333 if ( $toHandler instanceof TextContentHandler
) {
334 // NOTE: ignore content serialization format - it's just text anyway.
335 $text = $this->getText();
336 $converted = $toHandler->unserializeContent( $text );