MediaWiki master
TextContent.php
Go to the documentation of this file.
1<?php
28namespace MediaWiki\Content;
29
31use Content;
32use InvalidArgumentException;
33use Language;
39
50
54 protected $mText;
55
61 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
62 parent::__construct( $model_id );
63
64 if ( $text === null || $text === false ) {
65 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
66 . "This may indicate an error in the caller's scope.", 2 );
67
68 $text = '';
69 }
70
71 if ( !is_string( $text ) ) {
72 throw new InvalidArgumentException( "TextContent expects a string in the constructor." );
73 }
74
75 $this->mText = $text;
76 }
77
83 public function copy() {
84 return $this; # NOTE: this is ok since TextContent are immutable.
85 }
86
94 public function getTextForSummary( $maxlength = 250 ) {
95 $text = $this->getText();
96
97 $truncatedtext = MediaWikiServices::getInstance()->getContentLanguage()->
98 truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
99
100 return $truncatedtext;
101 }
102
110 public function getSize() {
111 $text = $this->getText();
112
113 return strlen( $text );
114 }
115
127 public function isCountable( $hasLinks = null ) {
128 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()->get(
130
131 if ( $this->isRedirect() ) {
132 return false;
133 }
134
135 if ( $articleCountMethod === 'any' ) {
136 return true;
137 }
138
139 return false;
140 }
141
149 public function getNativeData() {
150 return $this->getText();
151 }
152
163 public function getText() {
164 return $this->mText;
165 }
166
174 public function getTextForSearchIndex() {
175 return $this->getText();
176 }
177
188 public function getWikitextForTransclusion() {
190 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
191 '@phan-var WikitextContent $wikitext';
192
193 if ( $wikitext ) {
194 return $wikitext->getText();
195 } else {
196 return false;
197 }
198 }
199
213 public static function normalizeLineEndings( $text ) {
214 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
215 }
216
230 public function diff( Content $that, Language $lang = null ) {
231 $this->checkModelID( $that->getModel() );
233 '@phan-var self $that';
234 // @todo could implement this in DifferenceEngine and just delegate here?
235
236 if ( !$lang ) {
237 $lang = MediaWikiServices::getInstance()->getContentLanguage();
238 }
239
240 $otext = $this->getText();
241 $ntext = $that->getText();
242
243 # Note: Use native PHP diff, external engines don't give us abstract output
244 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
245 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
246
247 $diff = new Diff( $ota, $nta );
248
249 return $diff;
250 }
251
268 public function convert( $toModel, $lossy = '' ) {
269 $converted = parent::convert( $toModel, $lossy );
270
271 if ( $converted !== false ) {
272 return $converted;
273 }
274
275 $toHandler = $this->getContentHandlerFactory()->getContentHandler( $toModel );
276
277 if ( $toHandler instanceof TextContentHandler ) {
278 // NOTE: ignore content serialization format - it's just text anyway.
279 $text = $this->getText();
280 $converted = $toHandler->unserializeContent( $text );
281 }
282
283 return $converted;
284 }
285
286}
288class_alias( TextContent::class, 'TextContent' );
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:222
const CONTENT_MODEL_TEXT
Definition Defines.php:225
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 implementation for content objects.
checkModelID( $modelId)
string $model_id
Name of the content model this Content object represents.
Base class for language-specific code.
Definition Language.php:66
Exception thrown when an unregistered content model is requested.
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".
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
Content object for wiki text pages.
Base interface for representing page content.
Definition Content.php:37
getModel()
Returns the ID of the content model used by this Content object.