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
61 public function copy() {
62 return $this; # NOTE: this is ok since TextContent are immutable.
63 }
64
72 public function getTextForSummary( $maxlength = 250 ) {
73 $text = $this->getText();
74
75 $truncatedtext = MediaWikiServices::getInstance()->getContentLanguage()->
76 truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
77
78 return $truncatedtext;
79 }
80
88 public function getSize() {
89 $text = $this->getText();
90
91 return strlen( $text );
92 }
93
105 public function isCountable( $hasLinks = null ) {
106 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()->get(
108
109 if ( $this->isRedirect() ) {
110 return false;
111 }
112
113 if ( $articleCountMethod === 'any' ) {
114 return true;
115 }
116
117 return false;
118 }
119
127 public function getNativeData() {
128 return $this->getText();
129 }
130
141 public function getText() {
142 return $this->mText;
143 }
144
152 public function getTextForSearchIndex() {
153 return $this->getText();
154 }
155
166 public function getWikitextForTransclusion() {
168 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
169 '@phan-var WikitextContent $wikitext';
170
171 if ( $wikitext ) {
172 return $wikitext->getText();
173 } else {
174 return false;
175 }
176 }
177
191 public static function normalizeLineEndings( $text ) {
192 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
193 }
194
208 public function diff( Content $that, ?Language $lang = null ) {
209 $this->checkModelID( $that->getModel() );
211 '@phan-var self $that';
212 // @todo could implement this in DifferenceEngine and just delegate here?
213
214 if ( !$lang ) {
215 $lang = MediaWikiServices::getInstance()->getContentLanguage();
216 }
217
218 $otext = $this->getText();
219 $ntext = $that->getText();
220
221 # Note: Use native PHP diff, external engines don't give us abstract output
222 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
223 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
224
225 $diff = new Diff( $ota, $nta );
226
227 return $diff;
228 }
229
246 public function convert( $toModel, $lossy = '' ) {
247 $converted = parent::convert( $toModel, $lossy );
248
249 if ( $converted !== false ) {
250 return $converted;
251 }
252
253 $toHandler = $this->getContentHandlerFactory()->getContentHandler( $toModel );
254
255 if ( $toHandler instanceof TextContentHandler ) {
256 // NOTE: ignore content serialization format - it's just text anyway.
257 $text = $this->getText();
258 $converted = $toHandler->unserializeContent( $text );
259 }
260
261 return $converted;
262 }
263
264}
266class_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.
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:69
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.