Manual:Page content models

The ContentHandler introduced in MediaWiki 1.21 allows you to add new content models other than wikitext. It makes it possible for wiki pages to be composed of data other than wikitext, and represented in any way — for example: Markdown, reStructuredText, icalendar, or a custom XML format. The display and editing of these content models can be handled in custom ways (e.g. different syntax highlighting, or whole new data-entry forms).

This page steps through how to create a new content model in an extension. It assumes some familiarity with general extension development practices. For a brief summary of the requirements, see the #Summary section at the bottom of this page.

A meaningless "Goat" content model will be used for the examples.

Registration
First of all, add the content model's name and handler class to your :

The lefthand value here is the name of the content type, it should be all-lowercase, and it lives alongside the five built-in content types 'wikitext', 'javascript', 'css', 'text', and 'json'. The righthand value is the fully-qualified name of a class that extends.

The GoatContent and GoatContentHandler classes must also be added to AutoloadClasses.

Setting page's content models
If you want an entire wiki namespace to have a default content model, you can define it as such in extension.json:

Or if you want to determine the content type by the addition of a quasi-file-type suffix on the wiki page name:

ContentHandler
The next thing to define is the GoatContentHandler class, which is where we also specify what format this content type will be stored as (in this case, text). ContentHandlers don't know anything about any particular page content, but determine the general structure and storage of the content.

The content handler will also implement the serializeContent and unserializeContent methods, but we'll get to those later.

Content
The GoatContent class is the representation of the content's data, and does not know anything about pages, revisions, or how it is stored in the database.

Edit form
Now we've got the skeleton set up, we'll want to try editing a Goat. To do this, we create  and specify what actions we want to map to what classes. To start with, we'll just deal with 'edit' (which corresponds to '?action=edit' in the URL).

And we'll create our new EditAction class, basically the same as the core EditAction but using our own EditPage:

Our new EditPage class is where the action happens (excuse the pun):

You should now be able to edit a page and see your form. But when you put data into it, and hit 'preview', you'll see that things are not yet working fully and that you get no output, nor is your submitted text shown again in the form.

So we must override the 'submit' action as well, with a new SubmitAction class and the addition of  to our   method. Our SubmitAction class should be the same as that of core, but inheriting from our EditAction.

Display
A content model is responsible for producing any required output for display. This usually involves working with its data and producing HTML in some way, to add to the parser output.

Summary
To implement a new content model with a custom editing form, create the following: