The ContentHandler facility, introduced in MediaWiki 1.21, allows wiki pages to store and render arbitrary content types instead of treating all pages as wikitext. Each content type is identified by a content model — a unique string ID. MediaWiki ships with built-in models and allows extensions to register their own.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/wikimedia/mediawiki/llms.txt
Use this file to discover all available pages before exploring further.
Content models
Every page in MediaWiki has a content model that determines how its content is stored, rendered, edited, compared, and serialized.Built-in content models
| Constant | ID string | Description |
|---|---|---|
CONTENT_MODEL_WIKITEXT | wikitext | Standard wiki markup |
CONTENT_MODEL_JAVASCRIPT | javascript | User-provided JavaScript |
CONTENT_MODEL_CSS | css | User-provided CSS |
CONTENT_MODEL_JSON | json | JSON data, used by extensions |
CONTENT_MODEL_TEXT | text | Plain text |
CONTENT_MODEL_XXX constants rather than raw strings.
How a page’s model is determined
The model for a page is resolved in this order:$wgNamespaceContentModels— specifies a default model for an entire namespace.- The
ContentHandlerDefaultModelForhook — allows overriding the model for specific pages. - Pages in
NS_MEDIAWIKIandNS_USERdefault tocssorjavascriptif they end in.cssor.js. - All other pages default to
wikitext.
Revisions of a page are not guaranteed to share the same content model. Use
SlotRecord::getModel() to get the model for a specific revision’s slot.Architecture
The ContentHandler system uses two class hierarchies:Content interface
Content (and AbstractContent base class) — represents the actual content of a specific page revision. Provides methods like getParserOutput(), diff(), isValid().ContentHandler class
ContentHandler — a singleton that provides functionality for a content model without acting on specific content. Acts as a factory for Content objects.ContentHandler for a given model can be retrieved via:
ContentHandler objects are singletons. Use ContentHandler::makeEmptyContent() and ContentHandler::unserializeContent() to create Content objects. However, the preferred approach is:
Content serialization
Each content model supports one or more serialization formats identified by MIME-type strings. Use theCONTENT_FORMAT_XXX constants in PHP.
Built-in serialization formats
| Constant | MIME type | Usage |
|---|---|---|
CONTENT_FORMAT_WIKITEXT | text/x-wiki | Wikitext |
CONTENT_FORMAT_JAVASCRIPT | text/javascript | JavaScript pages |
CONTENT_FORMAT_CSS | text/css | CSS pages |
CONTENT_FORMAT_TEXT | text/plain | Plain text |
CONTENT_FORMAT_HTML | text/html | Future use |
CONTENT_FORMAT_JSON | application/json | JSON content |
CONTENT_FORMAT_XML | application/xml | XML content |
CONTENT_FORMAT_SERIALIZED | application/vnd.php.serialized | PHP serialized |
Implementing a custom ContentHandler
To add support for a new content type, subclassContentHandler and implement the required methods:
Content class:
Registering a content handler in extension.json
Register your handler using theContentHandlers key in extension.json:
$wgNamespaceContentModels in LocalSettings.php:
ContentHandlerDefaultModelFor hook to override the model for individual pages:
Custom action handlers
ContentHandler::getActionOverrides() allows a content handler to replace the default edit, view, or history actions with custom implementations. This is similar to WikiPage::getActionOverrides():
Database storage
Content is stored using the same mechanism as wikitext. Non-text content is serialized first. Each revision records itscontent_model and content_format in the revision table — but only if they differ from the page’s default (the defaults are stored as NULL to save space).
JavaScript and CSS pages are no longer parsed as wikitext. Links and category tags inside
.js and .css pages do not create actual page links or category memberships.