MediaWiki master
TextContent.php
Go to the documentation of this file.
1<?php
28namespace MediaWiki\Content;
29
30use InvalidArgumentException;
36
47
51 protected $mText;
52
58 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
59 parent::__construct( $model_id );
60
61 if ( $text === null || $text === false ) {
62 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
63 . "This may indicate an error in the caller's scope.", 2 );
64
65 $text = '';
66 }
67
68 if ( !is_string( $text ) ) {
69 throw new InvalidArgumentException( "TextContent expects a string in the constructor." );
70 }
71
72 $this->mText = $text;
73 }
74
80 public function copy() {
81 return $this; # NOTE: this is ok since TextContent are immutable.
82 }
83
91 public function getTextForSummary( $maxlength = 250 ) {
92 $text = $this->getText();
93
94 $truncatedtext = MediaWikiServices::getInstance()->getContentLanguage()->
95 truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
96
97 return $truncatedtext;
98 }
99
107 public function getSize() {
108 $text = $this->getText();
109
110 return strlen( $text );
111 }
112
124 public function isCountable( $hasLinks = null ) {
125 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()->get(
127
128 if ( $this->isRedirect() ) {
129 return false;
130 }
131
132 if ( $articleCountMethod === 'any' ) {
133 return true;
134 }
135
136 return false;
137 }
138
146 public function getNativeData() {
147 return $this->getText();
148 }
149
160 public function getText() {
161 return $this->mText;
162 }
163
171 public function getTextForSearchIndex() {
172 return $this->getText();
173 }
174
185 public function getWikitextForTransclusion() {
187 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
188 '@phan-var WikitextContent $wikitext';
189
190 if ( $wikitext ) {
191 return $wikitext->getText();
192 } else {
193 return false;
194 }
195 }
196
210 public static function normalizeLineEndings( $text ) {
211 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
212 }
213
227 public function diff( Content $that, ?Language $lang = null ) {
228 $this->checkModelID( $that->getModel() );
230 '@phan-var self $that';
231 // @todo could implement this in DifferenceEngine and just delegate here?
232
233 if ( !$lang ) {
234 $lang = MediaWikiServices::getInstance()->getContentLanguage();
235 }
236
237 $otext = $this->getText();
238 $ntext = $that->getText();
239
240 # Note: Use native PHP diff, external engines don't give us abstract output
241 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
242 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
243
244 $diff = new Diff( $ota, $nta );
245
246 return $diff;
247 }
248
265 public function convert( $toModel, $lossy = '' ) {
266 $converted = parent::convert( $toModel, $lossy );
267
268 if ( $converted !== false ) {
269 return $converted;
270 }
271
272 $toHandler = $this->getContentHandlerFactory()->getContentHandler( $toModel );
273
274 if ( $toHandler instanceof TextContentHandler ) {
275 // NOTE: ignore content serialization format - it's just text anyway.
276 $text = $this->getText();
277 $converted = $toHandler->unserializeContent( $text );
278 }
279
280 return $converted;
281 }
282
283}
285class_alias( TextContent::class, 'TextContent' );
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:228
const CONTENT_MODEL_TEXT
Definition Defines.php:231
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Exception thrown when an unregistered content model is requested.
Base implementation for content objects.
string $model_id
Name of the content model this Content object represents.
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".
Base class for language-specific code.
Definition Language.php:81
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:34
Base interface for representing page content.
Definition Content.php:39
getModel()
Returns the ID of the content model used by this Content object.