MediaWiki master
TextContent.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
9use InvalidArgumentException;
14
27
31 protected $mText;
32
38 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
39 parent::__construct( $model_id );
40
41 if ( $text === null || $text === false ) {
42 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
43 . "This may indicate an error in the caller's scope.", 2 );
44
45 $text = '';
46 }
47
48 if ( !is_string( $text ) ) {
49 throw new InvalidArgumentException( "TextContent expects a string in the constructor." );
50 }
51
52 $this->mText = $text;
53 }
54
59 public function copy() {
60 return $this; # NOTE: this is ok since TextContent are immutable.
61 }
62
70 public function getTextForSummary( $maxlength = 250 ) {
71 $text = $this->getText();
72
73 $truncatedtext = MediaWikiServices::getInstance()->getContentLanguage()->
74 truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
75
76 return $truncatedtext;
77 }
78
86 public function getSize() {
87 $text = $this->getText();
88
89 return strlen( $text );
90 }
91
103 public function isCountable( $hasLinks = null ) {
104 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()->get(
106
107 if ( $this->isRedirect() ) {
108 return false;
109 }
110
111 if ( $articleCountMethod === 'any' ) {
112 return true;
113 }
114
115 return false;
116 }
117
125 public function getNativeData() {
126 wfDeprecated( __METHOD__, '1.33' );
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.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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".
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.