MediaWiki master
Content Interface Reference

Base interface for representing page content. More...

Inherited by AbstractContent.

Public Member Functions

 addSectionHeader ( $header)
 Returns a new WikitextContent object with the given section heading prepended, if supported.
 
 convert ( $toModel, $lossy='')
 Converts this content object into another content object with the given content model, if that is possible.
 
 copy ()
 Return a copy of this Content object.
 
 equals (Content $that=null)
 Returns true if this Content objects is conceptually equivalent to the given Content object.
 
 getContentHandler ()
 Convenience method that returns the ContentHandler singleton for handling the content model that this Content object uses.
 
 getDefaultFormat ()
 Convenience method that returns the default serialization format for the content model that this Content object uses.
 
 getModel ()
 Returns the ID of the content model used by this Content object.
 
 getNativeData ()
 Returns native representation of the data.
 
 getRedirectTarget ()
 Construct the redirect destination from this content and return a Title, or null if this content doesn't represent a redirect.
 
 getSection ( $sectionId)
 Returns the section with the given ID.
 
 getSize ()
 Returns the content's nominal size in "bogo-bytes".
 
 getSupportedFormats ()
 Convenience method that returns the list of serialization formats supported for the content model that this Content object uses.
 
 getTextForSearchIndex ()
 
 getTextForSummary ( $maxLength=250)
 Returns a textual representation of the content suitable for use in edit summaries and log messages.
 
 getWikitextForTransclusion ()
 
 isCountable ( $hasLinks=null)
 Returns true if this content is countable as a "real" wiki page, provided that it's also in a countable location (e.g.
 
 isEmpty ()
 Returns true if this Content object represents empty content.
 
 isRedirect ()
 Returns whether this Content represents a redirect.
 
 isSupportedFormat ( $format)
 Returns true if $format is a supported serialization format for this Content object, false if it isn't.
 
 isValid ()
 Returns whether the content is valid.
 
 matchMagicWord (MagicWord $word)
 Returns true if this Content object matches the given magic word.
 
 replaceSection ( $sectionId, Content $with, $sectionTitle='')
 Replaces a section of the content and returns a Content object with the section replaced.
 
 serialize ( $format=null)
 Convenience method for serializing this Content object.
 
 updateRedirect (Title $target)
 If this Content object is a redirect, this method updates the redirect target.
 

Detailed Description

Base interface for representing page content.

A content object represents page content, e.g. the text to show on a page. Content objects have no knowledge about how they relate to wiki pages.

Must not be implemented directly by extensions, extend AbstractContent instead.

Stability: stable
to type
Since
1.21
Author
Daniel Kinzler

Definition at line 37 of file Content.php.

Member Function Documentation

◆ addSectionHeader()

Content::addSectionHeader ( $header)

Returns a new WikitextContent object with the given section heading prepended, if supported.

The default implementation just returns this Content object unmodified, ignoring the section header.

Since
1.21
Parameters
string$header
Returns
Content

Implemented in AbstractContent, and WikitextContent.

Referenced by MediaWiki\EditPage\EditPage\getPreviewText(), and MediaWiki\EditPage\EditPage\internalAttemptSave().

◆ convert()

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 AbstractContent, MediaWiki\Content\FallbackContent, and MediaWiki\Content\TextContent.

◆ copy()

Content::copy ( )

Return a copy of this Content object.

The following must be true for the object returned:

if $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 immutable content objects.

Since
1.21
Returns
Content A copy of this object

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

◆ equals()

Content::equals ( Content $that = null)

Returns true if this Content objects is conceptually equivalent to the given Content object.

Contract:

  • Will return false if $that is null.
  • Will return true if $that === $this.
  • Will return false if $that->getModel() !== $this->getModel().
  • Will return false if get_class( $that ) !== get_class( $this )
  • Should return false if $that->getModel() == $this->getModel() and $that is not semantically equivalent to $this, according to the data model defined by $this->getModel().

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 )
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 AbstractContent.

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

◆ getContentHandler()

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 AbstractContent, and WikitextContent.

Referenced by DifferenceEngine\generateContentDiffBody(), and MediaWiki\EditPage\EditPage\getCurrentContent().

◆ getDefaultFormat()

Content::getDefaultFormat ( )

Convenience method that returns the default serialization format for the content model that this Content object uses.

Shorthand for $this->getContentHandler()->getDefaultFormat()

Since
1.21
Returns
string

Implemented in AbstractContent.

Referenced by WikiRevision\setContent().

◆ getModel()

◆ getNativeData()

Content::getNativeData ( )

Returns native representation of the data.

Interpretation depends on the data model used, as given by getDataModel().

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

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

◆ getRedirectTarget()

Content::getRedirectTarget ( )

Construct the redirect destination from this content and return a Title, or null if this content doesn't represent a redirect.

Since
1.21
Returns
Title|null

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

Referenced by ContentHandler\getParserOutput().

◆ getSection()

Content::getSection ( $sectionId)

Returns the section with the given ID.

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 section, or false if no such section exist, or null if sections are not supported.

Implemented in AbstractContent, and WikitextContent.

Referenced by MediaWiki\EditPage\EditPage\getContentObject().

◆ getSize()

Content::getSize ( )

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

Returns
int

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

◆ getSupportedFormats()

Content::getSupportedFormats ( )

Convenience method that returns the list of serialization formats supported for the content model that this Content object uses.

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

Since
1.21
Returns
string[] List of supported serialization formats

Implemented in AbstractContent.

◆ getTextForSearchIndex()

Content::getTextForSearchIndex ( )
Since
1.21
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.
Todo

Test that this actually works

Make sure this also works with LuceneSearch / WikiSearch

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

◆ getTextForSummary()

Content::getTextForSummary ( $maxLength = 250)

Returns 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, MessageContent, MediaWiki\Content\TextContent, and WikitextContent.

◆ getWikitextForTransclusion()

Content::getWikitextForTransclusion ( )
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.
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?!

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

◆ isCountable()

Content::isCountable ( $hasLinks = null)

Returns true if this content is countable as a "real" wiki page, provided that it's also in a countable location (e.g.

a current revision in the main namespace).

See also
SlotRoleHandler::supportsArticleCount
Since
1.21
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, MessageContent, and MediaWiki\Content\TextContent.

◆ isEmpty()

Content::isEmpty ( )

Returns true if this Content object represents empty content.

Since
1.21
Returns
bool Whether this Content object is empty

Implemented in AbstractContent.

◆ isRedirect()

Content::isRedirect ( )

Returns whether this Content represents a redirect.

Shorthand for getRedirectTarget() !== null.

See also
SlotRoleHandler::supportsRedirects
Since
1.21
Returns
bool

Implemented in AbstractContent.

Referenced by MediaWiki\EditPage\EditPage\internalAttemptSave().

◆ isSupportedFormat()

Content::isSupportedFormat ( $format)

Returns true if $format is a supported serialization format for this Content object, false if it isn't.

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
Parameters
string$formatThe serialization format to check.
Returns
bool Whether the format is supported

Implemented in AbstractContent.

◆ isValid()

Content::isValid ( )

Returns 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.

This default implementation always returns true.

Since
1.21
Returns
bool

Implemented in AbstractContent, and MediaWiki\Content\JsonContent.

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

◆ matchMagicWord()

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 AbstractContent, and WikitextContent.

◆ replaceSection()

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

Replaces a section of the content and returns a Content object with the section 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 AbstractContent, and WikitextContent.

◆ serialize()

Content::serialize ( $format = null)

Convenience method for serializing this Content object.

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

Since
1.21
Parameters
string | null$formatThe desired serialization format, or null for the default format.
Returns
string Serialized form of this Content object.

Implemented in AbstractContent, and MediaWiki\Content\FallbackContent.

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

◆ updateRedirect()

Content::updateRedirect ( Title $target)

If this Content object is a redirect, this method updates the redirect target.

Otherwise, it does nothing.

Since
1.21
Parameters
Title$targetThe new redirect target
Returns
Content A new Content object with the updated redirect (or $this if this Content object isn't a redirect)

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


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