MediaWiki  master
AbstractContent.php
Go to the documentation of this file.
1 <?php
33 
41 abstract class AbstractContent implements Content {
50  protected $model_id;
51 
59  public function __construct( $modelId = null ) {
60  $this->model_id = $modelId;
61  }
62 
69  public function getModel() {
70  return $this->model_id;
71  }
72 
81  protected function checkModelID( $modelId ) {
82  if ( $modelId !== $this->model_id ) {
83  throw new MWException(
84  "Bad content model: " .
85  "expected {$this->model_id} " .
86  "but got $modelId."
87  );
88  }
89  }
90 
97  public function getContentHandler() {
98  return $this->getContentHandlerFactory()->getContentHandler( $this->getModel() );
99  }
100 
105  return MediaWikiServices::getInstance()->getContentHandlerFactory();
106  }
107 
114  public function getDefaultFormat() {
115  return $this->getContentHandler()->getDefaultFormat();
116  }
117 
124  public function getSupportedFormats() {
125  return $this->getContentHandler()->getSupportedFormats();
126  }
127 
137  public function isSupportedFormat( $format ) {
138  if ( !$format ) {
139  return true; // this means "use the default"
140  }
141 
142  return $this->getContentHandler()->isSupportedFormat( $format );
143  }
144 
152  protected function checkFormat( $format ) {
153  if ( !$this->isSupportedFormat( $format ) ) {
154  throw new MWException(
155  "Format $format is not supported for content model " .
156  $this->getModel()
157  );
158  }
159  }
160 
171  public function serialize( $format = null ) {
172  return $this->getContentHandler()->serializeContent( $this, $format );
173  }
174 
183  public function isEmpty() {
184  return $this->getSize() === 0;
185  }
186 
197  public function isValid() {
198  return true;
199  }
200 
222  public function equals( Content $that = null ) {
223  if ( $that === null ) {
224  return false;
225  }
226 
227  if ( $that === $this ) {
228  return true;
229  }
230 
231  if ( $that->getModel() !== $this->getModel() ) {
232  return false;
233  }
234 
235  // For type safety. Needed for odd cases like MessageContent using CONTENT_MODEL_WIKITEXT
236  if ( get_class( $that ) !== get_class( $this ) ) {
237  return false;
238  }
239 
240  return $this->equalsInternal( $that );
241  }
242 
263  protected function equalsInternal( Content $that ) {
264  return $this->serialize() === $that->serialize();
265  }
266 
274  public function getRedirectChain() {
275  global $wgMaxRedirects;
276  $title = $this->getRedirectTarget();
277  if ( $title === null ) {
278  return null;
279  }
280  $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
281  // recursive check to follow double redirects
282  $recurse = $wgMaxRedirects;
283  $titles = [ $title ];
284  while ( --$recurse > 0 ) {
285  if ( $title->isRedirect() ) {
286  $page = $wikiPageFactory->newFromTitle( $title );
287  $newtitle = $page->getRedirectTarget();
288  } else {
289  break;
290  }
291  // Redirects to some special pages are not permitted
292  if ( $newtitle instanceof Title && $newtitle->isValidRedirectTarget() ) {
293  // The new title passes the checks, so make that our current
294  // title so that further recursion can be checked
295  $title = $newtitle;
296  $titles[] = $newtitle;
297  } else {
298  break;
299  }
300  }
301 
302  return $titles;
303  }
304 
315  public function getRedirectTarget() {
316  return null;
317  }
318 
328  public function getUltimateRedirectTarget() {
329  $titles = $this->getRedirectChain();
330 
331  return $titles ? array_pop( $titles ) : null;
332  }
333 
341  public function isRedirect() {
342  return $this->getRedirectTarget() !== null;
343  }
344 
358  public function updateRedirect( Title $target ) {
359  return $this;
360  }
361 
371  public function getSection( $sectionId ) {
372  return null;
373  }
374 
386  public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
387  return null;
388  }
389 
403  public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
404  wfDeprecated( __METHOD__, '1.37' );
405  $pstParams = new PreSaveTransformParamsValue( $title, $user, $popts );
406  return $this->getContentHandler()->preSaveTransform(
407  $this,
408  $pstParams
409  );
410  }
411 
421  public function addSectionHeader( $header ) {
422  return $this;
423  }
424 
436  public function preloadTransform( Title $title, ParserOptions $popts, $params = [] ) {
437  wfDeprecated( __METHOD__, '1.37' );
438  $pltParams = new PreloadTransformParamsValue( $title, $popts, $params );
439  return $this->getContentHandler()->preloadTransform(
440  $this,
441  $pltParams
442  );
443  }
444 
457  public function prepareSave( WikiPage $page, $flags, $parentRevId, User $user ) {
458  if ( $this->isValid() ) {
459  return Status::newGood();
460  } else {
461  return Status::newFatal( "invalid-content-data" );
462  }
463  }
464 
478  public function matchMagicWord( MagicWord $word ) {
479  return false;
480  }
481 
495  public function convert( $toModel, $lossy = '' ) {
496  if ( $this->getModel() === $toModel ) {
497  // nothing to do, shorten out.
498  return $this;
499  }
500 
501  $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
502  $result = false;
503 
504  Hooks::runner()->onConvertContent( $this, $toModel, $lossy, $result );
505 
506  return $result;
507  }
508 
531  public function getParserOutput( Title $title, $revId = null,
532  ParserOptions $options = null, $generateHtml = true
533  ) {
534  if ( $options === null ) {
535  $options = ParserOptions::newCanonical( 'canonical' );
536  }
537 
538  $output = new ParserOutput();
539  $options->registerWatcher( [ $output, 'recordOption' ] );
540 
541  if ( Hooks::runner()->onContentGetParserOutput(
542  $this, $title, $revId, $options, $generateHtml, $output )
543  ) {
544  // Save and restore the old value, just in case something is reusing
545  // the ParserOptions object in some weird way.
546  $oldRedir = $options->getRedirectTarget();
547  $options->setRedirectTarget( $this->getRedirectTarget() );
548  $this->fillParserOutput( $title, $revId, $options, $generateHtml, $output );
549  MediaWikiServices::getInstance()->get( '_ParserObserver' )->notifyParse(
550  $title,
551  $revId,
552  $options,
553  $output
554  );
555  $options->setRedirectTarget( $oldRedir );
556  }
557 
558  Hooks::runner()->onContentAlterParserOutput( $this, $title, $output );
559  $options->registerWatcher( null );
560 
561  return $output;
562  }
563 
587  protected function fillParserOutput( Title $title, $revId,
588  ParserOptions $options, $generateHtml, ParserOutput &$output
589  ) {
590  // Don't make abstract, so subclasses that override getParserOutput() directly don't fail.
591  throw new MWException( 'Subclasses of AbstractContent must override fillParserOutput!' );
592  }
593 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:45
AbstractContent\addSectionHeader
addSectionHeader( $header)
Definition: AbstractContent.php:421
Content\serialize
serialize( $format=null)
Convenience method for serializing this Content object.
ParserOutput
Definition: ParserOutput.php:31
AbstractContent\isRedirect
isRedirect()
Definition: AbstractContent.php:341
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
AbstractContent\fillParserOutput
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Fills the provided ParserOutput with information derived from the content.
Definition: AbstractContent.php:587
AbstractContent\convert
convert( $toModel, $lossy='')
This base implementation calls the hook ConvertContent to enable custom conversions.
Definition: AbstractContent.php:495
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
AbstractContent\replaceSection
replaceSection( $sectionId, Content $with, $sectionTitle='')
Definition: AbstractContent.php:386
AbstractContent\checkFormat
checkFormat( $format)
Definition: AbstractContent.php:152
AbstractContent\getParserOutput
getParserOutput(Title $title, $revId=null, ParserOptions $options=null, $generateHtml=true)
Returns a ParserOutput object containing information derived from this content.
Definition: AbstractContent.php:531
AbstractContent\getContentHandlerFactory
getContentHandlerFactory()
Definition: AbstractContent.php:104
AbstractContent\getRedirectTarget
getRedirectTarget()
Subclasses that implement redirects should override this.
Definition: AbstractContent.php:315
AbstractContent\updateRedirect
updateRedirect(Title $target)
This default implementation always returns $this.
Definition: AbstractContent.php:358
MediaWiki\Content\Transform\PreSaveTransformParamsValue
Definition: PreSaveTransformParamsValue.php:12
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:60
AbstractContent\getRedirectChain
getRedirectChain()
Definition: AbstractContent.php:274
AbstractContent\equals
equals(Content $that=null)
Decides whether two Content objects are equal.
Definition: AbstractContent.php:222
AbstractContent\isEmpty
isEmpty()
Definition: AbstractContent.php:183
AbstractContent\getUltimateRedirectTarget
getUltimateRedirectTarget()
Definition: AbstractContent.php:328
Content\getSize
getSize()
Returns the content's nominal size in "bogo-bytes".
AbstractContent\matchMagicWord
matchMagicWord(MagicWord $word)
This default implementation always returns false.
Definition: AbstractContent.php:478
AbstractContent\preSaveTransform
preSaveTransform(Title $title, User $user, ParserOptions $popts)
Definition: AbstractContent.php:403
MediaWiki\Content\Transform\PreloadTransformParamsValue
Definition: PreloadTransformParamsValue.php:11
AbstractContent\getDefaultFormat
getDefaultFormat()
Definition: AbstractContent.php:114
MWException
MediaWiki exception.
Definition: MWException.php:29
AbstractContent\checkModelID
checkModelID( $modelId)
Definition: AbstractContent.php:81
AbstractContent\getContentHandler
getContentHandler()
Definition: AbstractContent.php:97
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
AbstractContent\isSupportedFormat
isSupportedFormat( $format)
Definition: AbstractContent.php:137
AbstractContent\serialize
serialize( $format=null)
Definition: AbstractContent.php:171
Title\isValidRedirectTarget
isValidRedirectTarget()
Check if this Title is a valid redirect target.
Definition: Title.php:3887
AbstractContent\prepareSave
prepareSave(WikiPage $page, $flags, $parentRevId, User $user)
Definition: AbstractContent.php:457
ParserOptions\newCanonical
static newCanonical( $context, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1134
$title
$title
Definition: testCompression.php:38
AbstractContent\__construct
__construct( $modelId=null)
Definition: AbstractContent.php:59
MagicWord
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition: MagicWord.php:60
AbstractContent\preloadTransform
preloadTransform(Title $title, ParserOptions $popts, $params=[])
Definition: AbstractContent.php:436
AbstractContent\equalsInternal
equalsInternal(Content $that)
Checks whether $that is logically equal to this Content object.
Definition: AbstractContent.php:263
MediaWiki\Content\IContentHandlerFactory
Definition: IContentHandlerFactory.php:10
$header
$header
Definition: updateCredits.php:37
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
AbstractContent
Base implementation for content objects.
Definition: AbstractContent.php:41
AbstractContent\getSection
getSection( $sectionId)
Definition: AbstractContent.php:371
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
Content
Base interface for content objects.
Definition: Content.php:35
AbstractContent\getModel
getModel()
Definition: AbstractContent.php:69
AbstractContent\$model_id
string $model_id
Name of the content model this Content object represents.
Definition: AbstractContent.php:50
Title
Represents a title within MediaWiki.
Definition: Title.php:48
$wgMaxRedirects
$wgMaxRedirects
Max number of redirects to follow when resolving redirects.
Definition: DefaultSettings.php:4726
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
AbstractContent\getSupportedFormats
getSupportedFormats()
Definition: AbstractContent.php:124
AbstractContent\isValid
isValid()
Subclasses may override this to implement (light weight) validation.
Definition: AbstractContent.php:197