MediaWiki  master
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 ) {
119  global $wgArticleCountMethod;
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 
221  public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
222  $text = $this->getText();
223  $pst = self::normalizeLineEndings( $text );
224 
225  return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() );
226  }
227 
241  public function diff( Content $that, Language $lang = null ) {
242  $this->checkModelID( $that->getModel() );
244  '@phan-var self $that';
245  // @todo could implement this in DifferenceEngine and just delegate here?
246 
247  if ( !$lang ) {
248  $lang = MediaWikiServices::getInstance()->getContentLanguage();
249  }
250 
251  $otext = $this->getText();
252  $ntext = $that->getText();
253 
254  # Note: Use native PHP diff, external engines don't give us abstract output
255  $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
256  $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
257 
258  $diff = new Diff( $ota, $nta );
259 
260  return $diff;
261  }
262 
282  protected function fillParserOutput( Title $title, $revId,
283  ParserOptions $options, $generateHtml, ParserOutput &$output
284  ) {
285  global $wgTextModelsToParse;
286 
287  if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
288  // parse just to get links etc into the database, HTML is replaced below.
289  $output = MediaWikiServices::getInstance()->getParser()
290  ->parse( $this->getText(), $title, $options, true, true, $revId );
291  }
292 
293  if ( $generateHtml ) {
294  $html = $this->getHtml();
295  } else {
296  $html = '';
297  }
298 
299  $output->clearWrapperDivClass();
300  $output->setText( $html );
301  }
302 
315  protected function getHtml() {
316  // TODO: Remove in MediaWiki 1.36
317  if ( method_exists( $this, 'getHighlightHtml' ) ) {
318  wfDeprecated( 'getHighlightHtml', '1.24' );
319  throw new Exception( 'getHighlightHtml() is not called any more!' );
320  }
321  return htmlspecialchars( $this->getText() );
322  }
323 
340  public function convert( $toModel, $lossy = '' ) {
341  $converted = parent::convert( $toModel, $lossy );
342 
343  if ( $converted !== false ) {
344  return $converted;
345  }
346 
347  $toHandler = $this->getContentHandlerFactory()->getContentHandler( $toModel );
348 
349  if ( $toHandler instanceof TextContentHandler ) {
350  // NOTE: ignore content serialization format - it's just text anyway.
351  $text = $this->getText();
352  $converted = $toHandler->unserializeContent( $text );
353  }
354 
355  return $converted;
356  }
357 
358 }
TextContent\__construct
__construct( $text, $model_id=CONTENT_MODEL_TEXT)
Stable to call.
Definition: TextContent.php:52
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:44
TextContent\getTextForSearchIndex
getTextForSearchIndex()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:164
ParserOutput
Definition: ParserOutput.php:25
AbstractContent\isRedirect
isRedirect()
Definition: AbstractContent.php:378
TextContent\getWikitextForTransclusion
getWikitextForTransclusion()
Returns attempts to convert this content object to wikitext, and then returns the text string.
Definition: TextContent.php:178
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
AbstractContent\getContentHandlerFactory
getContentHandlerFactory()
Definition: AbstractContent.php:102
TextContent\getHtml
getHtml()
Generates an HTML version of the content, for display.
Definition: TextContent.php:315
TextContent\$mText
string $mText
Definition: TextContent.php:44
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:224
TextContent\getText
getText()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:153
TextContent\getTextForSummary
getTextForSummary( $maxlength=250)
Stable to override.
Definition: TextContent.php:85
MWException
MediaWiki exception.
Definition: MWException.php:29
AbstractContent\checkModelID
checkModelID( $modelId)
Definition: AbstractContent.php:79
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1026
TextContent\copy
copy()
Definition: TextContent.php:74
$title
$title
Definition: testCompression.php:38
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:701
TextContent\preSaveTransform
preSaveTransform(Title $title, User $user, ParserOptions $popts)
Returns a Content object with pre-save transformations applied.
Definition: TextContent.php:221
ParserOutput\clearWrapperDivClass
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
Definition: ParserOutput.php:457
TextContent\getSize
getSize()
Returns the text's size in bytes.
Definition: TextContent.php:101
AbstractContent
Base implementation for content objects.
Definition: AbstractContent.php:39
TextContent\isCountable
isCountable( $hasLinks=null)
Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
Definition: TextContent.php:118
TextContentHandler
Base content handler implementation for flat text contents.
Definition: TextContentHandler.php:31
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:39
Content
Base interface for content objects.
Definition: Content.php:35
AbstractContent\getModel
getModel()
Definition: AbstractContent.php:67
AbstractContent\$model_id
string $model_id
Name of the content model this Content object represents.
Definition: AbstractContent.php:48
Title
Represents a title within MediaWiki.
Definition: Title.php:42
Content\getModel
getModel()
Returns the ID of the content model used by this Content object.
TextContent\normalizeLineEndings
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
Definition: TextContent.php:203
$wgTextModelsToParse
$wgTextModelsToParse
Determines which types of text are parsed as wikitext.
Definition: DefaultSettings.php:9105
wfWarn
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Definition: GlobalFunctions.php:1073
$wgArticleCountMethod
$wgArticleCountMethod
Method used to determine if a page in a content namespace should be counted as a valid article.
Definition: DefaultSettings.php:4774
TextContent\diff
diff(Content $that, Language $lang=null)
Diff this content object with another content object.
Definition: TextContent.php:241
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:227
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:41
TextContent\fillParserOutput
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
Definition: TextContent.php:282
TextContent\convert
convert( $toModel, $lossy='')
This implementation provides lossless conversion between content models based on TextContent.
Definition: TextContent.php:340
TextContent\getNativeData
getNativeData()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:139
Diff
Class representing a 'diff' between two sequences of strings.
Definition: Diff.php:32