MediaWiki master
MediaWiki\Content\Content Interface Reference

Content objects represent page content, e.g. More...

Inherited by MediaWiki\Content\AbstractContent.

Public Member Functions

 addSectionHeader ( $header)
 Create a derived WikitextContent with a prepended section heading, or return $this.
 
 convert ( $toModel, $lossy='')
 Converts this content object into another content object with the given content model, if that is possible.
 
 copy ()
 Create a copy of this Content object.
 
 equals (?Content $that=null)
 Whether this Content object is conceptually equivalent to another one.
 
 getContentHandler ()
 Convenience method that returns the ContentHandler singleton for handling the content model that this Content object uses.
 
 getDefaultFormat ()
 Get the default serialization format.
 
 getModel ()
 Get the content model ID.
 
 getNativeData ()
 Get native representation of the data.
 
 getRedirectTarget ()
 Get the redirect destination or null if this content doesn't represent a redirect.
 
 getSection ( $sectionId)
 Create a derived Content for the portion of text in the specified section.
 
 getSize ()
 Get the content's nominal size in "bogo-bytes".
 
 getSupportedFormats ()
 Get the list of supported serialization formats.
 
 getTextForSearchIndex ()
 
 getTextForSummary ( $maxLength=250)
 Get a textual representation of the content, suitable for use in edit summaries and log messages.
 
 getWikitextForTransclusion ()
 
 isCountable ( $hasLinks=null)
 Whether this content may count towards a "real" wiki page.
 
 isEmpty ()
 Whether this Content object is considered empty.
 
 isRedirect ()
 Whether this Content represents a redirect.
 
 isSupportedFormat ( $format)
 Whether a given format is a supported serialization format for this Content.
 
 isValid ()
 Whether the content is valid.
 
 matchMagicWord (MagicWord $word)
 Returns true if this Content object matches the given magic word.
 
 replaceSection ( $sectionId, Content $with, $sectionTitle='')
 Create a derived Content with the specified section added or replaced.
 
 serialize ( $format=null)
 Serialize this Content object.
 
 updateRedirect (Title $target)
 Create a derived Content with a replaced redirect destination.
 

Detailed Description

Content objects represent page content, e.g.

the text shown on a page.

Content objects must have no knowledge of what page they belong to, or how they relate to pages in general. This information belongs in page and revision records instead, flowing down to the content, instead of up in the other direction.

NOTE: This interface must NOT be implemented directly by extensions as it may grow in backwards incompatible ways. Extend the AbstractContent class instead.

Stability: stable
to type
Since
1.21
Author
Daniel Kinzler

Definition at line 42 of file Content.php.

Member Function Documentation

◆ addSectionHeader()

MediaWiki\Content\Content::addSectionHeader ( $header)

Create a derived WikitextContent with a prepended section heading, or return $this.

If the content model does not support section headings, the same Content object is returned unmodified. The default AbstractContent implementation returns $this unmodified, ignoring the section header.

Since
1.21
Parameters
string$header
Returns
Content

Implemented in MediaWiki\Content\AbstractContent, and MediaWiki\Content\WikitextContent.

◆ convert()

MediaWiki\Content\Content::convert ( $toModel,
$lossy = '' )

Converts this content object into another content object with the given content model, if that is possible.

Parameters
string$toModelThe desired content model, use the CONTENT_MODEL_XXX flags.
string$lossyOptional flag, set to "lossy" to allow lossy conversion. If lossy conversion is not allowed, full round-trip conversion is expected to work without losing information.
Returns
Content|false A content object with the content model $toModel, or false if that conversion is not supported.

Implemented in MediaWiki\Content\AbstractContent, MediaWiki\Content\FallbackContent, and MediaWiki\Content\TextContent.

◆ copy()

MediaWiki\Content\Content::copy ( )

Create a copy of this Content object.

The following must be true for the returned object, given $copy = $original->copy():

  • get_class( $original ) === get_class( $copy )
  • $original->getModel() === $copy->getModel()
  • $original->equals( $copy )

If and only if the Content object is immutable, the copy() method can and should return $this. That is, $copy === $original may be true, but only for fully immutable value objects.

Since
1.21
Returns
Content

Implemented in MediaWiki\Content\FallbackContent, and MediaWiki\Content\TextContent.

◆ equals()

MediaWiki\Content\Content::equals ( ?Content $that = null)

Whether this Content object is conceptually equivalent to another one.

Contract:

  • MUST return false if $that is null.
  • MUST return true if $that === $this.
  • MUST return false if $that does not share the same content model, i.e. $that->getModel() !== $this->getModel().
  • MUST return false if get_class( $that ) !== get_class( $this ).
  • MUST return true if $that->getModel() == $this->getModel() and the contents are considered semantically equivalent according to the data model defined by $this->getModel().

Two Content objects that are equal SHOULD have the same serialization.

Implementations should be careful to make equals() transitive and reflexive:

  • $a->equals( $b ) <=> $b->equals( $a )
  • $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )

This default AbstractContent::equals implementation fulfills the above and relies on Contenet::serialize() (via AbstractContent::equalsInternal) to determine whether Content objects are logically equivalent. Subclasses that only need to implement a custom equality check should consider overriding AbstractContent::equalsInternal().

Since
1.21
Parameters
Content | null$thatThe Content object to compare to.
Returns
bool True if this Content object is equal to $that, false otherwise.

Implemented in MediaWiki\Content\AbstractContent.

Referenced by MediaWiki\Content\ContentHandler\getUndoContent(), and MediaWiki\Content\TextContentHandler\merge3().

◆ getContentHandler()

MediaWiki\Content\Content::getContentHandler ( )

Convenience method that returns the ContentHandler singleton for handling the content model that this Content object uses.

Shorthand for ContentHandler::getForContent( $this )

Since
1.21
Returns
ContentHandler

Implemented in MediaWiki\Content\AbstractContent, and MediaWiki\Content\WikitextContent.

Referenced by DifferenceEngine\generateContentDiffBody().

◆ getDefaultFormat()

MediaWiki\Content\Content::getDefaultFormat ( )

Get the default serialization format.

Convenience method as shorthand for $this->getContentHandler()->getDefaultFormat().

See also
ContentHandler::getDefaultFormat
Since
1.21
Returns
string

Implemented in MediaWiki\Content\AbstractContent.

Referenced by WikiRevision\setContent().

◆ getModel()

◆ getNativeData()

MediaWiki\Content\Content::getNativeData ( )

Get native representation of the data.

Note
Caller must be aware of content model. Interpretation must consider the model from getModel() or the handler from getContentHandler().
Deprecated
since 1.33 Use getText() for TextContent instances. For other content models, use specialized getters.
Since
1.21
Returns
mixed The native representation of the content. Could be a string, nested array structure, object, binary blob, anything really.

Implemented in MediaWiki\Content\AbstractContent, MediaWiki\Content\FallbackContent, and MediaWiki\Content\TextContent.

◆ getRedirectTarget()

MediaWiki\Content\Content::getRedirectTarget ( )

Get the redirect destination or null if this content doesn't represent a redirect.

Since
1.21
Returns
Title|null

Implemented in MediaWiki\Content\AbstractContent, MediaWiki\Content\CssContent, MediaWiki\Content\JavaScriptContent, and MediaWiki\Content\WikitextContent.

Referenced by MediaWiki\Content\ContentHandler\getParserOutput().

◆ getSection()

MediaWiki\Content\Content::getSection ( $sectionId)

Create a derived Content for the portion of text in the specified section.

Since
1.21
Parameters
string | int$sectionIdSection identifier as a number or string (e.g. 0, 1 or 'T-1'). The ID "0" retrieves the section before the first heading, "1" the text between the first heading (included) and the second heading (excluded), etc.
Returns
Content|false|null The requested section's content, or false if no such section exist, or null if sections are not supported for this content model.

Implemented in MediaWiki\Content\AbstractContent, and MediaWiki\Content\WikitextContent.

◆ getSize()

MediaWiki\Content\Content::getSize ( )

Get the content's nominal size in "bogo-bytes".

Since
1.21
Returns
int

Implemented in MediaWiki\Content\FallbackContent, and MediaWiki\Content\TextContent.

◆ getSupportedFormats()

MediaWiki\Content\Content::getSupportedFormats ( )

Get the list of supported serialization formats.

Shorthand for $this->getContentHandler()->getSupportedFormats().

See also
ContentHandler::getSupportedFormats
Since
1.21
Returns
string[] List of supported serialization formats

Implemented in MediaWiki\Content\AbstractContent.

◆ getTextForSearchIndex()

MediaWiki\Content\Content::getTextForSearchIndex ( )
Since
1.21
See also
ContentHandler::getDataForSearchIndex
Returns
string A string representing the content in a way useful for building a full text search index. If no useful representation exists, this method returns an empty string.

Implemented in MediaWiki\Content\FallbackContent, and MediaWiki\Content\TextContent.

◆ getTextForSummary()

MediaWiki\Content\Content::getTextForSummary ( $maxLength = 250)

Get a textual representation of the content, suitable for use in edit summaries and log messages.

Since
1.21
Parameters
int$maxLengthMaximum length of the summary text, in bytes. Usually implemented using Language::truncateForDatabase().
Returns
string The summary text.

Implemented in MediaWiki\Content\FallbackContent, MediaWiki\Content\TextContent, and MediaWiki\Content\WikitextContent.

◆ getWikitextForTransclusion()

MediaWiki\Content\Content::getWikitextForTransclusion ( )
Todo

Allow native handling, bypassing wikitext representation, like for includable special pages.

Allow transclusion into other content models than Wikitext!

Used in WikiPage and MessageCache to get message text. Not so nice. What should we use instead?!

Since
1.21
Returns
string|false The wikitext to include when another page includes this content, or false if the content is not includable in a wikitext page.

Implemented in MediaWiki\Content\FallbackContent, and MediaWiki\Content\TextContent.

◆ isCountable()

MediaWiki\Content\Content::isCountable ( $hasLinks = null)

Whether this content may count towards a "real" wiki page.

The only factor not taken into account here is the content location (i.e. whether the content belongs to a current revision, of an existant page, in a "content" namespace).

Since
1.21
See also
WikiPage::isCountable
Parameters
bool | null$hasLinksIf it is known whether this content contains links, provide this information here, to avoid redundant parsing to find out.
Returns
bool

Implemented in MediaWiki\Content\FallbackContent, and MediaWiki\Content\TextContent.

◆ isEmpty()

MediaWiki\Content\Content::isEmpty ( )

Whether this Content object is considered empty.

Since
1.21
Returns
bool

Implemented in MediaWiki\Content\AbstractContent.

◆ isRedirect()

MediaWiki\Content\Content::isRedirect ( )

Whether this Content represents a redirect.

Shorthand for getRedirectTarget() !== null.

Since
1.21
See also
SlotRoleHandler::supportsRedirects
Returns
bool

Implemented in MediaWiki\Content\AbstractContent.

◆ isSupportedFormat()

MediaWiki\Content\Content::isSupportedFormat ( $format)

Whether a given format is a supported serialization format for this Content.

Note that this should always return true if $format is null, because null stands for the default serialization.

Shorthand for $this->getContentHandler()->isSupportedFormat( $format ).

Since
1.21
See also
ContentHandler::isSupportedFormat
Parameters
string$formatThe serialization format to check.
Returns
bool Whether the format is supported

Implemented in MediaWiki\Content\AbstractContent.

◆ isValid()

MediaWiki\Content\Content::isValid ( )

Whether the content is valid.

This is intended for local validity checks, not considering global consistency. Content needs to be valid before it can be saved.

The default AbstractContent implementation always returns true.

Since
1.21
Returns
bool

Implemented in MediaWiki\Content\AbstractContent, and MediaWiki\Content\JsonContent.

Referenced by MediaWiki\Content\JsonContentHandler\fillParserOutput(), MediaWiki\Parser\ParserObserver\notifyParse(), and MediaWiki\Content\ContentHandler\validateSave().

◆ matchMagicWord()

MediaWiki\Content\Content::matchMagicWord ( MagicWord $word)

Returns true if this Content object matches the given magic word.

Since
1.21
Parameters
MagicWord$wordThe magic word to match
Returns
bool Whether this Content object matches the given magic word.

Implemented in MediaWiki\Content\AbstractContent, and MediaWiki\Content\WikitextContent.

◆ replaceSection()

MediaWiki\Content\Content::replaceSection ( $sectionId,
Content $with,
$sectionTitle = '' )

Create a derived Content with the specified section added or replaced.

Since
1.21
Parameters
string | int | null | false$sectionIdSection identifier as a number or string (e.g. 0, 1 or 'T-1'), null/false or an empty string for the whole page or 'new' for a new section.
Content$withNew content of the section
string$sectionTitleNew section's subject, only if $section is 'new'
Returns
Content|null New content of the entire page, or null if error

Implemented in MediaWiki\Content\AbstractContent, and MediaWiki\Content\WikitextContent.

Referenced by MediaWiki\Content\WikitextContent\replaceSection().

◆ serialize()

MediaWiki\Content\Content::serialize ( $format = null)

Serialize this Content object.

Shorthand for $this->getContentHandler()->serializeContent( $this, $format )

Since
1.21
See also
ContentHandler::serializeContent
Parameters
string | null$formatThe desired serialization format, or null for the default format.
Returns
string

Implemented in MediaWiki\Content\AbstractContent, and MediaWiki\Content\FallbackContent.

Referenced by MediaWiki\Content\AbstractContent\equalsInternal(), MediaWiki\Parser\ParserObserver\notifyParse(), and WikiRevision\setContent().

◆ updateRedirect()

MediaWiki\Content\Content::updateRedirect ( Title $target)

Create a derived Content with a replaced redirect destination.

If the content is not already a redirect, this method returns the same object unchanged. If the content can contain other information besides a redirect (e.g. WikitextContent::updateRedirect) then that information is preserved. Otherwise it is effectively the same as creating a new content object via ContentHandler::makeRedirectContent.

Since
1.21
Parameters
Title$targetThe new redirect target
Returns
Content A new Content object with the updated redirect, or $this if it wasn't a redirect already

Implemented in MediaWiki\Content\AbstractContent, MediaWiki\Content\CssContent, MediaWiki\Content\JavaScriptContent, and MediaWiki\Content\WikitextContent.


The documentation for this interface was generated from the following file: