1 The ContentHandler facility adds support for arbitrary content types on wiki pages, instead of relying on wikitext
2 for everything. It was introduced in MediaWiki 1.21.
4 Each kind of content ("content model") supported by MediaWiki is identified by unique name. The content model determines
5 how a page's content is rendered, compared, stored, edited, and so on.
7 Built-in content types are:
9 * wikitext - wikitext, as usual
10 * javascript - user provided javascript code
11 * json - simple implementation for use by extensions, etc.
12 * css - user provided css code
15 In PHP, use the corresponding CONTENT_MODEL_XXX constant.
17 A page's content model is available using the Title::getContentModel() method. A page's default model is determined by
18 ContentHandler::getDefaultModelFor($title) as follows:
20 * The global setting $wgNamespaceContentModels specifies a content model for the given namespace.
21 * The hook ContentHandlerDefaultModelFor may be used to override the page's default model.
22 * Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript model if they end in .css or .js, respectively.
23 Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
24 * The hook TitleIsCssOrJsPage may be used to force a page to use the CSS or JavaScript model.
25 This is a compatibility feature. The ContentHandlerDefaultModelFor hook should be used instead if possible.
26 * The hook TitleIsWikitextPage may be used to force a page to use the wikitext model.
27 This is a compatibility feature. The ContentHandlerDefaultModelFor hook should be used instead if possible.
28 * Otherwise, the wikitext model is used.
30 Note that is currently no mechanism to convert a page from one content model to another, and there is no guarantee that
31 revisions of a page will all have the same content model. Use Revision::getContentModel() to find it.
36 Two class hierarchies are used to provide the functionality associated with the different content models:
38 * Content interface (and AbstractContent base class) define functionality that acts on the concrete content of a page, and
39 * ContentHandler base class provides functionality specific to a content model, but not acting on concrete content.
41 The most important function of ContentHandler is to act as a factory for the appropriate implementation of Content. These
42 Content objects are to be used by MediaWiki everywhere, instead of passing page content around as text. All manipulation
43 and analysis of page content must be done via the appropriate methods of the Content object.
45 For each content model, a subclass of ContentHandler has to be registered with $wgContentHandlers. The ContentHandler
46 object for a given content model can be obtained using ContentHandler::getForModelID( $id ). Also Title, WikiPage and
47 Revision now have getContentHandler() methods for convenience.
49 ContentHandler objects are singletons that provide functionality specific to the content type, but not directly acting
50 on the content of some page. ContentHandler::makeEmptyContent() and ContentHandler::unserializeContent() can be used to
51 create a Content object of the appropriate type. However, it is recommended to instead use WikiPage::getContent() resp.
52 Revision::getContent() to get a page's content as a Content object. These two methods should be the ONLY way in which
53 page content is accessed.
55 Another important function of ContentHandler objects is to define custom action handlers for a content model, see
56 ContentHandler::getActionOverrides(). This is similar to what WikiPage::getActionOverrides() was already doing.
61 With the ContentHandler facility, page content no longer has to be text based. Objects implementing the Content interface
62 are used to represent and handle the content internally. For storage and data exchange, each content model supports
63 at least one serialization format via ContentHandler::serializeContent( $content ). The list of supported formats for
64 a given content model can be accessed using ContentHandler::getSupportedFormats().
66 Content serialization formats are identified using MIME type like strings. The following formats are built in:
68 * text/x-wiki - wikitext
69 * text/javascript - for js pages
70 * text/css - for css pages
71 * text/plain - for future use, e.g. with plain text messages.
72 * text/html - for future use, e.g. with plain html messages.
73 * application/vnd.php.serialized - for future use with the api and for extensions
74 * application/json - for future use with the api, and for use by extensions
75 * application/xml - for future use with the api, and for use by extensions
77 In PHP, use the corresponding CONTENT_FORMAT_XXX constant.
79 Note that when using the API to access page content, especially action=edit, action=parse and action=query&prop=revisions,
80 the model and format of the content should always be handled explicitly. Without that information, interpretation of
81 the provided content is not reliable. The same applies to XML dumps generated via maintenance/dumpBackup.php or
84 Also note that the API will provide encapsulated, serialized content - so if the API was called with format=json, and
85 contentformat is also json (or rather, application/json), the page content is represented as a string containing an
86 escaped json structure. Extensions that use JSON to serialize some types of page content may provide specialized API
87 modules that allow access to that content in a more natural form.
92 The ContentHandler facility is introduced in a way that should allow all existing code to keep functioning at least
93 for pages that contain wikitext or other text based content. However, a number of functions and hooks have been
94 deprecated in favor of new versions that are aware of the page's content model, and will now generate warnings when
97 Most importantly, the following functions have been deprecated:
99 * Revisions::getText() is deprecated in favor Revisions::getContent()
100 * WikiPage::getText() is deprecated in favor WikiPage::getContent()
102 Also, the old Article::getContent() (which returns text) is superceded by Article::getContentObject(). However, both
103 methods should be avoided since they do not provide clean access to the page's actual content. For instance, they may
104 return a system message for non-existing pages. Use WikiPage::getContent() instead.
106 Code that relies on a textual representation of the page content should eventually be rewritten. However,
107 ContentHandler::getContentText() provides a stop-gap that can be used to get text for a page. Its behavior is controlled
108 by $wgContentHandlerTextFallback; per default it will return the text for text based content, and null for any other
111 For rendering page content, Content::getParserOutput() should be used instead of accessing the parser directly.
112 ContentHandler::makeParserOptions() can be used to construct appropriate options.
115 Besides some functions, some hooks have also been replaced by new versions (see hooks.txt for details).
116 These hooks will now trigger a warning when used:
118 * ArticleAfterFetchContent was replaced by ArticleAfterFetchContentObject
119 * ArticleInsertComplete was replaced by PageContentInsertComplete
120 * ArticleSave was replaced by PageContentSave
121 * ArticleSaveComplete was replaced by PageContentSaveComplete
122 * ArticleViewCustom was replaced by ArticleContentViewCustom (also consider a custom implementation of the view action)
123 * EditFilterMerged was replaced by EditFilterMergedContent
124 * EditPageGetDiffText was replaced by EditPageGetDiffContent
125 * EditPageGetPreviewText was replaced by EditPageGetPreviewContent
126 * ShowRawCssJs was deprecated in favor of custom rendering implemented in the respective ContentHandler object.
129 == Database Storage ==
131 Page content is stored in the database using the same mechanism as before. Non-text content is serialized first. The
132 appropriate serialization and deserialization is handled by the Revision class.
134 Each revision's content model and serialization format is stored in the revision table (resp. in the archive table, if
135 the revision was deleted). The page's (current) content model (that is, the content model of the latest revision) is also
136 stored in the page table.
138 Note however that the content model and format is only stored if it differs from the page's default, as determined by
139 ContentHandler::getDefaultModelFor( $title ). The default values are represented as NULL in the database, to preserve
142 Storage of content model and format can be disabled altogether by setting $wgContentHandlerUseDB = false. In that case,
143 the page's default model (and the model's default format) will be used everywhere. Attempts to store a revision of a page
144 using a model or format different from the default will result in an error.
149 There are some new globals that can be used to control the behavior of the ContentHandler facility:
151 * $wgContentHandlers associates content model IDs with the names of the appropriate ContentHandler subclasses
152 or callbacks that create an instance of the appropriate ContentHandler subclass.
154 * $wgNamespaceContentModels maps namespace IDs to a content model that should be the default for that namespace.
156 * $wgContentHandlerUseDB determines whether each revision's content model should be stored in the database.
159 * $wgContentHandlerTextFallback determines how the compatibility method ContentHandler::getContentText() will behave for
161 'ignore' causes null to be returned for non-text content (default).
162 'serialize' causes the serialized form of any non-text content to be returned (scary).
163 'fail' causes an exception to be thrown for non-text content (strict).
168 There are some changes in behavior that might be surprising to users:
170 * Javascript and CSS pages are no longer parsed as wikitext (though pre-save transform is still applied). Most
171 importantly, this means that links, including categorization links, contained in the code will not work.
173 * With $wgContentHandlerUseDB = false, pages can not be moved in a way that would change the
174 default model. E.g. [[MediaWiki:foo.js]] can not be moved to [[MediaWiki:foo bar]], but can still be moved to
175 [[User:John/foo.js]]. Also, in this mode, changing the default content model for a page (e.g. by changing
176 $wgNamespaceContentModels) may cause it to become inaccessible.
178 * action=edit will fail for pages with non-text content, unless the respective ContentHandler implementation has
179 provided a specialized handler for the edit action. This is true for the API as well.
181 * action=raw will fail for all non-text content. This seems better than serving content in other formats to an
182 unsuspecting recipient. This will also cause client-side diffs to fail.
184 * File pages provide their own action overrides that do not combine gracefully with any custom handlers defined by a
185 ContentHandler. If for example a File page used a content model with a custom revert action, this would be overridden by
186 WikiFilePage's handler for the revert action.