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
128 public function getText() {
129 return $this->mText;
130 }
131
139 public function getTextForSearchIndex() {
140 return $this->getText();
141 }
142
153 public function getWikitextForTransclusion() {
155 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
156 '@phan-var WikitextContent $wikitext';
157
158 if ( $wikitext ) {
159 return $wikitext->getText();
160 } else {
161 return false;
162 }
163 }
164
178 public static function normalizeLineEndings( $text ) {
179 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
180 }
181
195 public function diff( Content $that, ?Language $lang = null ) {
196 $this->checkModelID( $that->getModel() );
198 '@phan-var self $that';
199 // @todo could implement this in DifferenceEngine and just delegate here?
200
201 if ( !$lang ) {
202 $lang = MediaWikiServices::getInstance()->getContentLanguage();
203 }
204
205 $otext = $this->getText();
206 $ntext = $that->getText();
207
208 # Note: Use native PHP diff, external engines don't give us abstract output
209 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
210 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
211
212 $diff = new Diff( $ota, $nta );
213
214 return $diff;
215 }
216
233 public function convert( $toModel, $lossy = '' ) {
234 $converted = parent::convert( $toModel, $lossy );
235
236 if ( $converted !== false ) {
237 return $converted;
238 }
239
240 $toHandler = $this->getContentHandlerFactory()->getContentHandler( $toModel );
241
242 if ( $toHandler instanceof TextContentHandler ) {
243 // NOTE: ignore content serialization format - it's just text anyway.
244 $text = $this->getText();
245 $converted = $toHandler->unserializeContent( $text );
246 }
247
248 return $converted;
249 }
250
251}
253class_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.
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.