MediaWiki master
TextContent.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
9use InvalidArgumentException;
15
28
32 protected $mText;
33
39 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
40 parent::__construct( $model_id );
41
42 if ( $text === null || $text === false ) {
43 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
44 . "This may indicate an error in the caller's scope.", 2 );
45
46 $text = '';
47 }
48
49 if ( !is_string( $text ) ) {
50 throw new InvalidArgumentException( "TextContent expects a string in the constructor." );
51 }
52
53 $this->mText = $text;
54 }
55
60 public function copy() {
61 return $this; # NOTE: this is ok since TextContent are immutable.
62 }
63
71 public function getTextForSummary( $maxlength = 250 ) {
72 $text = $this->getText();
73
74 $truncatedtext = MediaWikiServices::getInstance()->getContentLanguage()->
75 truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
76
77 return $truncatedtext;
78 }
79
87 public function getSize() {
88 $text = $this->getText();
89
90 return strlen( $text );
91 }
92
104 public function isCountable( $hasLinks = null ) {
105 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()->get(
107
108 if ( $this->isRedirect() ) {
109 return false;
110 }
111
112 if ( $articleCountMethod === 'any' ) {
113 return true;
114 }
115
116 return false;
117 }
118
126 public function getNativeData() {
127 return $this->getText();
128 }
129
140 public function getText() {
141 return $this->mText;
142 }
143
151 public function getTextForSearchIndex() {
152 return $this->getText();
153 }
154
165 public function getWikitextForTransclusion() {
167 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
168 '@phan-var WikitextContent $wikitext';
169
170 if ( $wikitext ) {
171 return $wikitext->getText();
172 } else {
173 return false;
174 }
175 }
176
190 public static function normalizeLineEndings( $text ) {
191 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
192 }
193
207 public function diff( Content $that, ?Language $lang = null ) {
208 $this->checkModelID( $that->getModel() );
210 '@phan-var self $that';
211 // @todo could implement this in DifferenceEngine and just delegate here?
212
213 if ( !$lang ) {
214 $lang = MediaWikiServices::getInstance()->getContentLanguage();
215 }
216
217 $otext = $this->getText();
218 $ntext = $that->getText();
219
220 # Note: Use native PHP diff, external engines don't give us abstract output
221 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
222 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
223
224 $diff = new Diff( $ota, $nta );
225
226 return $diff;
227 }
228
245 public function convert( $toModel, $lossy = '' ) {
246 $converted = parent::convert( $toModel, $lossy );
247
248 if ( $converted !== false ) {
249 return $converted;
250 }
251
252 $toHandler = $this->getContentHandlerFactory()->getContentHandler( $toModel );
253
254 if ( $toHandler instanceof TextContentHandler ) {
255 // NOTE: ignore content serialization format - it's just text anyway.
256 $text = $this->getText();
257 $converted = $toHandler->unserializeContent( $text );
258 }
259
260 return $converted;
261 }
262
263}
265class_alias( TextContent::class, 'TextContent' );
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:235
const CONTENT_MODEL_TEXT
Definition Defines.php:238
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Base class for all Content objects.
checkModelID( $modelId)
Helper for subclasses.
Base content handler implementation for flat text contents.
Content object implementation for representing flat text.
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
__construct( $text, $model_id=CONTENT_MODEL_TEXT)
getWikitextForTransclusion()
Returns attempts to convert this content object to wikitext, and then returns the text string.
copy()
Create a copy of this Content object.The following must be true for the returned object,...
diff(Content $that, ?Language $lang=null)
Diff this content object with another content object.
getNativeData()
Returns the text represented by this Content object, as a string.
getTextForSummary( $maxlength=250)
getText()
Returns the text represented by this Content object, as a string.
getTextForSearchIndex()
Returns the text represented by this Content object, as a string.
getSize()
Returns the text's size in bytes.
convert( $toModel, $lossy='')
This implementation provides lossless conversion between content models based on TextContent.
isCountable( $hasLinks=null)
Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
Exception thrown when an unregistered content model is requested.
Base class for language-specific code.
Definition Language.php:65
A class containing constants representing the names of configuration variables.
const ArticleCountMethod
Name constant for the ArticleCountMethod setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Class representing a 'diff' between two sequences of strings.
Definition Diff.php:20
Content objects represent page content, e.g.
Definition Content.php:28
getModel()
Get the content model ID.