MediaWiki  master
TextContent.php
Go to the documentation of this file.
1 <?php
30 
41 
45  protected $mText;
46 
53  public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
54  parent::__construct( $model_id );
55 
56  if ( $text === null || $text === false ) {
57  wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
58  . "This may indicate an error in the caller's scope.", 2 );
59 
60  $text = '';
61  }
62 
63  if ( !is_string( $text ) ) {
64  throw new MWException( "TextContent expects a string in the constructor." );
65  }
66 
67  $this->mText = $text;
68  }
69 
75  public function copy() {
76  return $this; # NOTE: this is ok since TextContent are immutable.
77  }
78 
86  public function getTextForSummary( $maxlength = 250 ) {
87  $text = $this->getText();
88 
89  $truncatedtext = MediaWikiServices::getInstance()->getContentLanguage()->
90  truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
91 
92  return $truncatedtext;
93  }
94 
102  public function getSize() {
103  $text = $this->getText();
104 
105  return strlen( $text );
106  }
107 
119  public function isCountable( $hasLinks = null ) {
120  $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()->get(
121  MainConfigNames::ArticleCountMethod );
122 
123  if ( $this->isRedirect() ) {
124  return false;
125  }
126 
127  if ( $articleCountMethod === 'any' ) {
128  return true;
129  }
130 
131  return false;
132  }
133 
141  public function getNativeData() {
142  return $this->getText();
143  }
144 
155  public function getText() {
156  return $this->mText;
157  }
158 
166  public function getTextForSearchIndex() {
167  return $this->getText();
168  }
169 
180  public function getWikitextForTransclusion() {
182  $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
183  '@phan-var WikitextContent $wikitext';
184 
185  if ( $wikitext ) {
186  return $wikitext->getText();
187  } else {
188  return false;
189  }
190  }
191 
205  public static function normalizeLineEndings( $text ) {
206  return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
207  }
208 
222  public function diff( Content $that, Language $lang = null ) {
223  $this->checkModelID( $that->getModel() );
225  '@phan-var self $that';
226  // @todo could implement this in DifferenceEngine and just delegate here?
227 
228  if ( !$lang ) {
229  $lang = MediaWikiServices::getInstance()->getContentLanguage();
230  }
231 
232  $otext = $this->getText();
233  $ntext = $that->getText();
234 
235  # Note: Use native PHP diff, external engines don't give us abstract output
236  $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
237  $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
238 
239  $diff = new Diff( $ota, $nta );
240 
241  return $diff;
242  }
243 
260  public function convert( $toModel, $lossy = '' ) {
261  $converted = parent::convert( $toModel, $lossy );
262 
263  if ( $converted !== false ) {
264  return $converted;
265  }
266 
267  $toHandler = $this->getContentHandlerFactory()->getContentHandler( $toModel );
268 
269  if ( $toHandler instanceof TextContentHandler ) {
270  // NOTE: ignore content serialization format - it's just text anyway.
271  $text = $this->getText();
272  $converted = $toHandler->unserializeContent( $text );
273  }
274 
275  return $converted;
276  }
277 
278 }
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:211
const CONTENT_MODEL_TEXT
Definition: Defines.php:214
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
Base class for language-specific code.
Definition: Language.php:53
MediaWiki exception.
Definition: MWException.php:29
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Base content handler implementation for flat text contents.
Content object implementation for representing flat text.
Definition: TextContent.php:40
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)
Definition: TextContent.php:53
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
Definition: TextContent.php:45
getText()
Returns the text represented by this Content object, as a string.
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)
Definition: TextContent.php:86
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
Base interface for representing page content.
Definition: Content.php:34
getModel()
Returns the ID of the content model used by this Content object.
if(!isset( $args[0])) $lang