MediaWiki REL1_37
TextContent.php
Go to the documentation of this file.
1<?php
29
40
44 protected $mText;
45
52 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
53 parent::__construct( $model_id );
54
55 if ( $text === null || $text === false ) {
56 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
57 . "This may indicate an error in the caller's scope.", 2 );
58
59 $text = '';
60 }
61
62 if ( !is_string( $text ) ) {
63 throw new MWException( "TextContent expects a string in the constructor." );
64 }
65
66 $this->mText = $text;
67 }
68
74 public function copy() {
75 return $this; # NOTE: this is ok since TextContent are immutable.
76 }
77
85 public function getTextForSummary( $maxlength = 250 ) {
86 $text = $this->getText();
87
88 $truncatedtext = MediaWikiServices::getInstance()->getContentLanguage()->
89 truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
90
91 return $truncatedtext;
92 }
93
101 public function getSize() {
102 $text = $this->getText();
103
104 return strlen( $text );
105 }
106
118 public function isCountable( $hasLinks = null ) {
120
121 if ( $this->isRedirect() ) {
122 return false;
123 }
124
125 if ( $wgArticleCountMethod === 'any' ) {
126 return true;
127 }
128
129 return false;
130 }
131
139 public function getNativeData() {
140 return $this->getText();
141 }
142
153 public function getText() {
154 return $this->mText;
155 }
156
164 public function getTextForSearchIndex() {
165 return $this->getText();
166 }
167
178 public function getWikitextForTransclusion() {
180 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
181 '@phan-var WikitextContent $wikitext';
182
183 if ( $wikitext ) {
184 return $wikitext->getText();
185 } else {
186 return false;
187 }
188 }
189
203 public static function normalizeLineEndings( $text ) {
204 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
205 }
206
220 public function diff( Content $that, Language $lang = null ) {
221 $this->checkModelID( $that->getModel() );
223 '@phan-var self $that';
224 // @todo could implement this in DifferenceEngine and just delegate here?
225
226 if ( !$lang ) {
227 $lang = MediaWikiServices::getInstance()->getContentLanguage();
228 }
229
230 $otext = $this->getText();
231 $ntext = $that->getText();
232
233 # Note: Use native PHP diff, external engines don't give us abstract output
234 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
235 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
236
237 $diff = new Diff( $ota, $nta );
238
239 return $diff;
240 }
241
261 protected function fillParserOutput( Title $title, $revId,
262 ParserOptions $options, $generateHtml, ParserOutput &$output
263 ) {
265
266 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
267 // parse just to get links etc into the database, HTML is replaced below.
268 $output = MediaWikiServices::getInstance()->getParser()
269 ->parse( $this->getText(), $title, $options, true, true, $revId );
270 }
271
272 if ( $generateHtml ) {
273 $html = $this->getHtml();
274 } else {
275 $html = '';
276 }
277
278 $output->clearWrapperDivClass();
279 $output->setText( $html );
280 }
281
294 protected function getHtml() {
295 return htmlspecialchars( $this->getText() );
296 }
297
314 public function convert( $toModel, $lossy = '' ) {
315 $converted = parent::convert( $toModel, $lossy );
316
317 if ( $converted !== false ) {
318 return $converted;
319 }
320
321 $toHandler = $this->getContentHandlerFactory()->getContentHandler( $toModel );
322
323 if ( $toHandler instanceof TextContentHandler ) {
324 // NOTE: ignore content serialization format - it's just text anyway.
325 $text = $this->getText();
326 $converted = $toHandler->unserializeContent( $text );
327 }
328
329 return $converted;
330 }
331
332}
$wgArticleCountMethod
Method used to determine if a page in a content namespace should be counted as a valid article.
$wgTextModelsToParse
Determines which types of text are parsed as wikitext.
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:208
const CONTENT_MODEL_TEXT
Definition Defines.php:211
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.
Class representing a 'diff' between two sequences of strings.
Definition Diff.php:32
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:42
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Set options of the Parser.
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
Base content handler implementation for flat text contents.
Content object implementation for representing flat text.
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
isCountable( $hasLinks=null)
Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
getSize()
Returns the text's size in bytes.
__construct( $text, $model_id=CONTENT_MODEL_TEXT)
getWikitextForTransclusion()
Returns attempts to convert this content object to wikitext, and then returns the text string.
convert( $toModel, $lossy='')
This implementation provides lossless conversion between content models based on TextContent.
string $mText
getText()
Returns the text represented by this Content object, as a string.
getHtml()
Generates an HTML version of the content, for display.
getNativeData()
Returns the text represented by this Content object, as a string.
diff(Content $that, Language $lang=null)
Diff this content object with another content object.
getTextForSearchIndex()
Returns the text represented by this Content object, as a string.
getTextForSummary( $maxlength=250)
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
Represents a title within MediaWiki.
Definition Title.php:48
Base interface for content objects.
Definition Content.php:35
getModel()
Returns the ID of the content model used by this Content object.
if(!isset( $args[0])) $lang