BlueSpice MediaWiki REL1_27
 All Classes Namespaces Files Functions Variables Groups Pages
ContentHandler Class Reference
Inheritance diagram for ContentHandler:

Public Member Functions

 __construct ($modelId, $formats)
 
 serializeContent (Content $content, $format=null)
 
 exportTransform ($blob, $format=null)
 
 unserializeContent ($blob, $format=null)
 
 importTransform ($blob, $format=null)
 
 makeEmptyContent ()
 
 makeRedirectContent (Title $destination, $text= '')
 
 getModelID ()
 
 getSupportedFormats ()
 
 getDefaultFormat ()
 
 isSupportedFormat ($format)
 
 getActionOverrides ()
 
 createDifferenceEngine (IContextSource $context, $old=0, $new=0, $rcid=0, $refreshCache=false, $unhide=false)
 
 getPageLanguage (Title $title, Content $content=null)
 
 getPageViewLanguage (Title $title, Content $content=null)
 
 canBeUsedOn (Title $title)
 
 merge3 (Content $oldContent, Content $myContent, Content $yourContent)
 
 getAutoDeleteReason (Title $title, &$hasHistory)
 
 getUndoContent (Revision $current, Revision $undo, Revision $undoafter)
 
 makeParserOptions ($context)
 
 isParserCacheSupported ()
 
 supportsSections ()
 
 supportsCategories ()
 
 supportsRedirects ()
 
 supportsDirectEditing ()
 
 supportsDirectApiEditing ()
 

Static Public Member Functions

static getContentText (Content $content=null)
 
static makeContent ($text, Title $title=null, $modelId=null, $format=null)
 
static getDefaultModelFor (Title $title)
 
static getForTitle (Title $title)
 
static getForContent (Content $content)
 
static getForModelID ($modelId)
 
static getLocalizedName ($name, Language $lang=null)
 
static getContentModels ()
 
static getAllContentFormats ()
 
static deprecated ($func, $version, $component=false)
 
static runLegacyHooks ($event, $args=[], $warn=null)
 

Protected Member Functions

 checkModelID ($model_id)
 
 checkFormat ($format)
 
 getDiffEngineClass ()
 

Protected Attributes

 $mModelID
 
 $mSupportedFormats
 

Static Protected Attributes

static $enableDeprecationWarnings = false
 
static $handlers
 

Detailed Description

A content handler knows how do deal with a specific type of content on a wiki page. Content is stored in the database in a serialized form (using a serialization format a.k.a. MIME type) and is unserialized into its native PHP representation (the content model), which is wrapped in an instance of the appropriate subclass of Content.

ContentHandler instances are stateless singletons that serve, among other things, as a factory for Content objects. Generally, there is one subclass of ContentHandler and one subclass of Content for every type of content model.

Some content types have a flat model, that is, their native representation is the same as their serialized form. Examples would be JavaScript and CSS code. As of now, this also applies to wikitext (MediaWiki's default content type), but wikitext content may be represented by a DOM or AST structure in the future.

Constructor & Destructor Documentation

ContentHandler::__construct (   $modelId,
  $formats 
)

Constructor, initializing the ContentHandler instance with its model ID and a list of supported formats. Values for the parameters are typically provided as literals by subclass's constructors.

Parameters
string$modelId(use CONTENT_MODEL_XXX constants).
string[]$formats List for supported serialization formats (typically as MIME types)

Member Function Documentation

ContentHandler::canBeUsedOn ( Title  $title)

Determines whether the content type handled by this ContentHandler can be used on the given page.

This default implementation always returns true. Subclasses may override this to restrict the use of this content model to specific locations, typically based on the namespace or some other aspect of the title, such as a special suffix (e.g. ".svg" for SVG content).

Note
this calls the ContentHandlerCanBeUsedOn hook which may be used to override which content model can be used where.
Parameters
Title$titleThe page's title.
Returns
bool True if content of this kind can be used on the given page, false otherwise.
ContentHandler::checkFormat (   $format)
protected

Convenient for checking whether a format provided as a parameter is actually supported.

Parameters
string$formatThe serialization format to check
Exceptions
MWExceptionIf the format is not supported by this content handler.
ContentHandler::checkModelID (   $model_id)
protected
Since
1.21
Parameters
string$model_idThe model to check
Exceptions
MWExceptionIf the model ID is not the ID of the content model supported by this ContentHandler.
ContentHandler::createDifferenceEngine ( IContextSource  $context,
  $old = 0,
  $new = 0,
  $rcid = 0,
  $refreshCache = false,
  $unhide = false 
)

Factory for creating an appropriate DifferenceEngine for this content model.

Since
1.21
Parameters
IContextSource$contextContext to use, anything else will be ignored.
int$oldRevision ID we want to show and diff with.
int | string$newEither a revision ID or one of the strings 'cur', 'prev' or 'next'.
int$rcidFIXME: Deprecated, no longer used. Defaults to 0.
bool$refreshCacheIf set, refreshes the diff cache. Defaults to false.
bool$unhideIf set, allow viewing deleted revs. Defaults to false.
Returns
DifferenceEngine
static ContentHandler::deprecated (   $func,
  $version,
  $component = false 
)
static

Logs a deprecation warning, visible if $wgDevelopmentWarnings, but only if self::$enableDeprecationWarnings is set to true.

Parameters
string$funcThe name of the deprecated function
string$versionThe version since the method is deprecated. Usually 1.21 for ContentHandler related stuff.
string | bool$component: Component to which the function belongs. If false, it is assumed the function is in MediaWiki core.
See Also
ContentHandler::$enableDeprecationWarnings
wfDeprecated
ContentHandler::exportTransform (   $blob,
  $format = null 
)

Applies transformations on export (returns the blob unchanged per default). Subclasses may override this to perform transformations such as conversion of legacy formats or filtering of internal meta-data.

Parameters
string$blobThe blob to be exported
string | null$formatThe blob's serialization format
Returns
string
ContentHandler::getActionOverrides ( )

Returns overrides for action handlers. Classes listed here will be used instead of the default one when (and only when) $wgActions[$action] === true. This allows subclasses to override the default action handlers.

Since
1.21
Returns
array Always an empty array.
ContentHandler::getAutoDeleteReason ( Title  $title,
$hasHistory 
)

Auto-generates a deletion reason

Since
1.21
Parameters
Title$titleThe page's title
bool&$hasHistoryWhether the page has a history
Returns
mixed String containing deletion reason or empty string, or boolean false if no revision occurred
Todo:
&$hasHistory is extremely ugly, it's here because WikiPage::getAutoDeleteReason() and Article::generateReason() have it / want it.
static ContentHandler::getContentText ( Content  $content = null)
static

Convenience function for getting flat text from a Content object. This should only be used in the context of backwards compatibility with code that is not yet able to handle Content objects!

If $content is null, this method returns the empty string.

If $content is an instance of TextContent, this method returns the flat text as returned by $content->getNativeData().

If $content is not a TextContent object, the behavior of this method depends on the global $wgContentHandlerTextFallback:

  • If $wgContentHandlerTextFallback is 'fail' and $content is not a TextContent object, an MWException is thrown.
  • If $wgContentHandlerTextFallback is 'serialize' and $content is not a TextContent object, $content->serialize() is called to get a string form of the content.
  • If $wgContentHandlerTextFallback is 'ignore' and $content is not a TextContent object, this method returns null.
  • otherwise, the behavior is undefined.
Since
1.21
Parameters
Content$content
Exceptions
MWExceptionIf the content is not an instance of TextContent and wgContentHandlerTextFallback was set to 'fail'.
Returns
string|null Textual form of the content, if available.
ContentHandler::getDefaultFormat ( )

The format used for serialization/deserialization by default by this ContentHandler.

This default implementation will return the first element of the array of formats that was passed to the constructor.

Since
1.21
Returns
string The name of the default serialization format as a MIME type
static ContentHandler::getDefaultModelFor ( Title  $title)
static

Returns the name of the default content model to be used for the page with the given title.

Note: There should rarely be need to call this method directly. To determine the actual content model for a given page, use Title::getContentModel().

Which model is to be used by default for the page is determined based on several factors:

  • The global setting $wgNamespaceContentModels specifies a content model per namespace.
  • The hook ContentHandlerDefaultModelFor may be used to override the page's default model.
  • Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript model if they end in .js or .css, respectively.
  • Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
  • The hook TitleIsCssOrJsPage may be used to force a page to use the CSS or JavaScript model. This is a compatibility feature. The ContentHandlerDefaultModelFor hook should be used instead if possible.
  • The hook TitleIsWikitextPage may be used to force a page to use the wikitext model. This is a compatibility feature. The ContentHandlerDefaultModelFor hook should be used instead if possible.

If none of the above applies, the wikitext model is used.

Note: this is used by, and may thus not use, Title::getContentModel()

Since
1.21
Parameters
Title$title
Returns
string Default model name for the page given by $title
ContentHandler::getDiffEngineClass ( )
protected

Returns the name of the diff engine to use.

Since
1.21
Returns
string
static ContentHandler::getForContent ( Content  $content)
static

Returns the appropriate ContentHandler singleton for the given Content object.

Since
1.21
Parameters
Content$content
Returns
ContentHandler
static ContentHandler::getForModelID (   $modelId)
static

Returns the ContentHandler singleton for the given model ID. Use the CONTENT_MODEL_XXX constants to identify the desired content model.

ContentHandler singletons are taken from the global $wgContentHandlers array. Keys in that array are model names, the values are either ContentHandler singleton objects, or strings specifying the appropriate subclass of ContentHandler.

If a class name is encountered when looking up the singleton for a given model name, the class is instantiated and the class name is replaced by the resulting singleton in $wgContentHandlers.

If no ContentHandler is defined for the desired $modelId, the ContentHandler may be provided by the ContentHandlerForModelID hook. If no ContentHandler can be determined, an MWException is raised.

Since
1.21
Parameters
string$modelIdThe ID of the content model for which to get a handler. Use CONTENT_MODEL_XXX constants.
Exceptions
MWExceptionFor internal errors and problems in the configuration.
MWUnknownContentModelExceptionIf no handler is known for the model ID.
Returns
ContentHandler The ContentHandler singleton for handling the model given by the ID.
static ContentHandler::getForTitle ( Title  $title)
static

Returns the appropriate ContentHandler singleton for the given title.

Since
1.21
Parameters
Title$title
Returns
ContentHandler
static ContentHandler::getLocalizedName (   $name,
Language  $lang = null 
)
static

Returns the localized name for a given content model.

Model names are localized using system messages. Message keys have the form content-model-$name, where $name is getContentModelName( $id ).

Parameters
string$nameThe content model ID, as given by a CONTENT_MODEL_XXX constant or returned by Revision::getContentModel().
Language | null$langThe language to parse the message in (since 1.26)
Exceptions
MWExceptionIf the model ID isn't known.
Returns
string The content model's localized name.
ContentHandler::getModelID ( )

Returns the model id that identifies the content model this ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants.

Since
1.21
Returns
string The model ID
ContentHandler::getPageLanguage ( Title  $title,
Content  $content = null 
)

Get the language in which the content of the given page is written.

This default implementation just returns $wgContLang (except for pages in the MediaWiki namespace)

Note that the pages language is not cacheable, since it may in some cases depend on user settings.

Also note that the page language may or may not depend on the actual content of the page, that is, this method may load the content in order to determine the language.

Since
1.21
Parameters
Title$titleThe page to determine the language for.
Content$contentThe page's content, if you have it handy, to avoid reloading it.
Returns
Language The page's language
ContentHandler::getPageViewLanguage ( Title  $title,
Content  $content = null 
)

Get the language in which the content of this page is written when viewed by user. Defaults to $this->getPageLanguage(), but if the user specified a preferred variant, the variant will be used.

This default implementation just returns $this->getPageLanguage( $title, $content ) unless the user specified a preferred variant.

Note that the pages view language is not cacheable, since it depends on user settings.

Also note that the page language may or may not depend on the actual content of the page, that is, this method may load the content in order to determine the language.

Since
1.21
Parameters
Title$titleThe page to determine the language for.
Content$contentThe page's content, if you have it handy, to avoid reloading it.
Returns
Language The page's language for viewing
ContentHandler::getSupportedFormats ( )

Returns a list of serialization formats supported by the serializeContent() and unserializeContent() methods of this ContentHandler.

Since
1.21
Returns
string[] List of serialization formats as MIME type like strings
ContentHandler::getUndoContent ( Revision  $current,
Revision  $undo,
Revision  $undoafter 
)

Get the Content object that needs to be saved in order to undo all revisions between $undo and $undoafter. Revisions must belong to the same page, must exist and must not be deleted.

Since
1.21
Parameters
Revision$currentThe current text
Revision$undoThe revision to undo
Revision$undoafterMust be an earlier revision than $undo
Returns
mixed String on success, false on failure
ContentHandler::importTransform (   $blob,
  $format = null 
)

Apply import transformation (per default, returns $blob unchanged). This gives subclasses an opportunity to transform data blobs on import.

Since
1.24
Parameters
string$blob
string | null$format
Returns
string
ContentHandler::isParserCacheSupported ( )

Returns true for content models that support caching using the ParserCache mechanism. See WikiPage::shouldCheckParserCache().

Since
1.21
Returns
bool Always false.
ContentHandler::isSupportedFormat (   $format)

Returns true if $format is a serialization format supported by this ContentHandler, and false otherwise.

Note that if $format is null, this method always returns true, because null means "use the default format".

Since
1.21
Parameters
string$formatThe serialization format to check
Returns
bool
static ContentHandler::makeContent (   $text,
Title  $title = null,
  $modelId = null,
  $format = null 
)
static

Convenience function for creating a Content object from a given textual representation.

$text will be deserialized into a Content object of the model specified by $modelId (or, if that is not given, $title->getContentModel()) using the given format.

Since
1.21
Parameters
string$textThe textual representation, will be unserialized to create the Content object
Title$titleThe title of the page this text belongs to. Required if $modelId is not provided.
string$modelIdThe model to deserialize to. If not provided, $title->getContentModel() is used.
string$formatThe format to use for deserialization. If not given, the model's default format is used.
Exceptions
MWExceptionIf model ID or format is not supported or if the text can not be unserialized using the format.
Returns
Content A Content object representing the text.
ContentHandler::makeEmptyContent ( )
abstract

Creates an empty Content object of the type supported by this ContentHandler.

Since
1.21
Returns
Content
ContentHandler::makeParserOptions (   $context)

Get parser options suitable for rendering and caching the article

Parameters
IContextSource | User | string$contextOne of the following:
  • IContextSource: Use the User and the Language of the provided context
  • User: Use the provided User object and $wgLang for the language, so use an IContextSource object if possible.
  • 'canonical': Canonical options (anonymous user with default preferences and content language).
Exceptions
MWException
Returns
ParserOptions
ContentHandler::makeRedirectContent ( Title  $destination,
  $text = '' 
)

Creates a new Content object that acts as a redirect to the given page, or null if redirects are not supported by this content model.

This default implementation always returns null. Subclasses supporting redirects must override this method.

Note that subclasses that override this method to return a Content object should also override supportsRedirects() to return true.

Since
1.21
Parameters
Title$destinationThe page to redirect to.
string$textText to include in the redirect, if possible.
Returns
Content Always null.
ContentHandler::merge3 ( Content  $oldContent,
Content  $myContent,
Content  $yourContent 
)

Attempts to merge differences between three versions. Returns a new Content object for a clean merge and false for failure or a conflict.

This default implementation always returns false.

Since
1.21
Parameters
Content$oldContentThe page's previous content.
Content$myContentOne of the page's conflicting contents.
Content$yourContentOne of the page's conflicting contents.
Returns
Content|bool Always false.
static ContentHandler::runLegacyHooks (   $event,
  $args = [],
  $warn = null 
)
static

Call a legacy hook that uses text instead of Content objects. Will log a warning when a matching hook function is registered. If the textual representation of the content is changed by the hook function, a new Content object is constructed from the new text.

Parameters
string$eventEvent name
array$argsParameters passed to hook functions
bool$warnWhether to log a warning. Default to self::$enableDeprecationWarnings. May be set to false for testing.
Returns
bool True if no handler aborted the hook
See Also
ContentHandler::$enableDeprecationWarnings
ContentHandler::serializeContent ( Content  $content,
  $format = null 
)
abstract

Serializes a Content object of the type supported by this ContentHandler.

Since
1.21
Parameters
Content$contentThe Content object to serialize
string$formatThe desired serialization format
Returns
string Serialized form of the content
ContentHandler::supportsCategories ( )

Returns true if this content model supports categories. The default implementation returns true.

Returns
bool Always true.
ContentHandler::supportsDirectApiEditing ( )

Whether or not this content model supports direct editing via ApiEditPage

Returns
bool Default is false, and true for TextContent and derivatives.
ContentHandler::supportsDirectEditing ( )

Return true if this content model supports direct editing, such as via EditPage.

Returns
bool Default is false, and true for TextContent and it's derivatives.
ContentHandler::supportsRedirects ( )

Returns true if this content model supports redirects. This default implementation returns false.

Content models that return true here should also implement ContentHandler::makeRedirectContent to return a Content object.

Returns
bool Always false.
ContentHandler::supportsSections ( )

Returns true if this content model supports sections. This default implementation returns false.

Content models that return true here should also implement Content::getSection, Content::replaceSection, etc. to handle sections..

Returns
bool Always false.
ContentHandler::unserializeContent (   $blob,
  $format = null 
)
abstract

Unserializes a Content object of the type supported by this ContentHandler.

Since
1.21
Parameters
string$blobSerialized form of the content
string$formatThe format used for serialization
Returns
Content The Content object created by deserializing $blob

Member Data Documentation

ContentHandler::$enableDeprecationWarnings = false
staticprotected

Switch for enabling deprecation warnings. Used by ContentHandler::deprecated() and ContentHandler::runLegacyHooks().

Once the ContentHandler code has settled in a bit, this should be set to true to make extensions etc. show warnings when using deprecated functions and hooks.


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