| Content Models and Handlers |
|---|
| MediaWiki allows for page content types other than wikitext. It offers built-in support for JavaScript, CSS, JSON and plain text. Extensions can use MediaWiki's ContentHandler mechanism to add new content models for accepting different formats and controlling how they are rendered, stored and edited. |
| About the ContentHandler Content handlers and their implementation |
| Changing the content model of a page Changing the content model of a namespace Extensions using the ContentHandler Adding content models with an extension Examples: |
| $wgContentHandlers $wgNamespaceContentModels $wgContentHandlerTextFallback $wgContentHandlerUseDB |
| Support and development |
| v · d · e |
TheContentHandler facility is a mechanism for supporting arbitrary content types on wiki pages, instead of relying on wikitext for everything.It was developed as part of theWikidata project and is part of the MediaWiki core since version 1.21.
For the canonical overview of the ContentHandler's architecture, seeContentHandler in the MediaWiki code documentation.
For a list of available content handlers, seeContent handlers.
The rationale behind this rather radical change is that being forced to rely on wikitext for all content makes a lot of things quite cumbersome in MediaWiki.The new pluggable architecture for arbitrary types of page content will allow us to hopefully:
The idea is to store other kinds of data in exactly the same way as wikitext is stored currently, but make MediaWiki aware of the type of content it is dealing with for every page.This way, any kind of data can be used as the content of a wiki page, and it would be stored and versioned exactly as before.To achieve this, the following was implemented in the MediaWiki core:
Title,RevisionRecord, andWikiPage. Thecontent model defines thenative form of the content, be it a string containing text, a nested structure of arrays, or a PHP object. All operations on the content are performed on itsnative form.RevisionRecord. Note that the serialization format is only relevant when loading and storing the revision, no operations are performed on the serialized form of the content.This means that all code that needs to perform any operation on the content must be aware of the content's native form.This knowledge is encapsulated using a pluggable framework of handlers, based on two classes:
Content class represents the content as such, and provides an interface for all standard operations to be performed on the content's native form. It does not have any knowledge of the page or revision the content belongs to. Content objects are generally, but not necessarily, immutable.ContentHandler class, representing the knowledge about the specifics of a content model without access to concrete Content. Most importantly, instances of ContentHandler act as a factory for Content objects and provide serialization/deserialization. ContentHandler objects are stateless singletons, one for each content model.The ContentHandler is also used to generate suitable instances of subclasses ofArticle,EditPage,SlotDiffRenderer, etc.This way, a specialized UI for each content type can easily be plugged in through the ContentHandler interface.
All code that accesses the revision text in any way should be changed to use the methods provided by the Content object instead.Core classes that provide access the revision text (most importantly,RevisionRecord andWikiPage) have been adapted to provide access to the appropriate Content object instead of the text.
The assumption that pages contain wikitext is widespread through the MediaWiki code base. To remain compatible with parts of the code that still assume this, especially with extensions, is thus quite important.The right way to provide good compatibility is of course not to change public interfaces.Thus, all methods providing access to the revision content (likeRevision::getText(), etc.) remained in place, and are complemented with an alternative method that allows access to the content object instead (e.g.Revision::getContent()).The text-based methods are now deprecated, but shall function exactly as before for all pages/revisions that contain wikitext.This is also true for theaction API.
A convenience method,ContentHandler::getContentText(), is provided to make it easy to retrieve a page's text.For flat text-based content models such as wikitext (but also JS and CSS),getContentText() will just return the text, so the old text-based method will return the same as it did before for such revisions.However, in case a text-based backwards-compatible method is called on a page/revision that does not contain wikitext (or another flat text content model, such a CSS), the behavior depends on the setting of$wgContentHandlerTextFallback:ignore makes it return null,fail causes it to raise an exception, andserialize causes it to return the default serialization of the content.The default isignore, which is probably the most conservative option in most scenarios.
For editing however, non-text content is not supported per default.EditPage and the respective handlers in the action API will fail for non-textual content.
Content andContentHandler classes