Merge "Handle missing namespace prefix in XML dumps more gracefully"
[lhc/web/wiklou.git] / docs / contenthandler.txt
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.
3
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.
6
7 Built-in content types are:
8
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
13 * text - plain text
14
15 In PHP, use the corresponding CONTENT_MODEL_XXX constant.
16
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:
19
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 * Otherwise, the wikitext model is used.
25
26 Note that is currently no mechanism to convert a page from one content model to another, and there is no guarantee that
27 revisions of a page will all have the same content model. Use Revision::getContentModel() to find it.
28
29
30 == Architecture ==
31
32 Two class hierarchies are used to provide the functionality associated with the different content models:
33
34 * Content interface (and AbstractContent base class) define functionality that acts on the concrete content of a page, and
35 * ContentHandler base class provides functionality specific to a content model, but not acting on concrete content.
36
37 The most important function of ContentHandler is to act as a factory for the appropriate implementation of Content. These
38 Content objects are to be used by MediaWiki everywhere, instead of passing page content around as text. All manipulation
39 and analysis of page content must be done via the appropriate methods of the Content object.
40
41 For each content model, a subclass of ContentHandler has to be registered with $wgContentHandlers. The ContentHandler
42 object for a given content model can be obtained using ContentHandler::getForModelID( $id ). Also Title, WikiPage and
43 Revision now have getContentHandler() methods for convenience.
44
45 ContentHandler objects are singletons that provide functionality specific to the content type, but not directly acting
46 on the content of some page. ContentHandler::makeEmptyContent() and ContentHandler::unserializeContent() can be used to
47 create a Content object of the appropriate type. However, it is recommended to instead use WikiPage::getContent() resp.
48 Revision::getContent() to get a page's content as a Content object. These two methods should be the ONLY way in which
49 page content is accessed.
50
51 Another important function of ContentHandler objects is to define custom action handlers for a content model, see
52 ContentHandler::getActionOverrides(). This is similar to what WikiPage::getActionOverrides() was already doing.
53
54
55 == Serialization ==
56
57 With the ContentHandler facility, page content no longer has to be text based. Objects implementing the Content interface
58 are used to represent and handle the content internally. For storage and data exchange, each content model supports
59 at least one serialization format via ContentHandler::serializeContent( $content ). The list of supported formats for
60 a given content model can be accessed using ContentHandler::getSupportedFormats().
61
62 Content serialization formats are identified using MIME type like strings. The following formats are built in:
63
64 * text/x-wiki - wikitext
65 * text/javascript - for js pages
66 * text/css - for css pages
67 * text/plain - for future use, e.g. with plain text messages.
68 * text/html - for future use, e.g. with plain html messages.
69 * application/vnd.php.serialized - for future use with the api and for extensions
70 * application/json - for future use with the api, and for use by extensions
71 * application/xml - for future use with the api, and for use by extensions
72
73 In PHP, use the corresponding CONTENT_FORMAT_XXX constant.
74
75 Note that when using the API to access page content, especially action=edit, action=parse and action=query&prop=revisions,
76 the model and format of the content should always be handled explicitly. Without that information, interpretation of
77 the provided content is not reliable. The same applies to XML dumps generated via maintenance/dumpBackup.php or
78 Special:Export.
79
80 Also note that the API will provide encapsulated, serialized content - so if the API was called with format=json, and
81 contentformat is also json (or rather, application/json), the page content is represented as a string containing an
82 escaped json structure. Extensions that use JSON to serialize some types of page content may provide specialized API
83 modules that allow access to that content in a more natural form.
84
85
86 == Compatibility ==
87
88 The ContentHandler facility is introduced in a way that should allow all existing code to keep functioning at least
89 for pages that contain wikitext or other text based content. However, a number of functions and hooks have been
90 deprecated in favor of new versions that are aware of the page's content model, and will now generate warnings when
91 used.
92
93 Most importantly, the following functions have been deprecated:
94
95 * Revisions::getText() is deprecated in favor Revisions::getContent()
96 * WikiPage::getText() is deprecated in favor WikiPage::getContent()
97
98 Also, the old Article::getContent() (which returns text) is superceded by Article::getContentObject(). However, both
99 methods should be avoided since they do not provide clean access to the page's actual content. For instance, they may
100 return a system message for non-existing pages. Use WikiPage::getContent() instead.
101
102 Code that relies on a textual representation of the page content should eventually be rewritten. However,
103 ContentHandler::getContentText() provides a stop-gap that can be used to get text for a page. Its behavior is controlled
104 by $wgContentHandlerTextFallback; per default it will return the text for text based content, and null for any other
105 content.
106
107 For rendering page content, Content::getParserOutput() should be used instead of accessing the parser directly.
108 ContentHandler::makeParserOptions() can be used to construct appropriate options.
109
110
111 Besides some functions, some hooks have also been replaced by new versions (see hooks.txt for details).
112 These hooks will now trigger a warning when used:
113
114 * ArticleAfterFetchContent was replaced by ArticleAfterFetchContentObject
115 * ArticleInsertComplete was replaced by PageContentInsertComplete
116 * ArticleSave was replaced by PageContentSave
117 * ArticleSaveComplete was replaced by PageContentSaveComplete
118 * ArticleViewCustom was replaced by ArticleContentViewCustom (also consider a custom implementation of the view action)
119 * EditFilterMerged was replaced by EditFilterMergedContent
120 * EditPageGetDiffText was replaced by EditPageGetDiffContent
121 * EditPageGetPreviewText was replaced by EditPageGetPreviewContent
122 * ShowRawCssJs was deprecated in favor of custom rendering implemented in the respective ContentHandler object.
123
124
125 == Database Storage ==
126
127 Page content is stored in the database using the same mechanism as before. Non-text content is serialized first. The
128 appropriate serialization and deserialization is handled by the Revision class.
129
130 Each revision's content model and serialization format is stored in the revision table (resp. in the archive table, if
131 the revision was deleted). The page's (current) content model (that is, the content model of the latest revision) is also
132 stored in the page table.
133
134 Note however that the content model and format is only stored if it differs from the page's default, as determined by
135 ContentHandler::getDefaultModelFor( $title ). The default values are represented as NULL in the database, to preserve
136 space.
137
138 Storage of content model and format can be disabled altogether by setting $wgContentHandlerUseDB = false. In that case,
139 the page's default model (and the model's default format) will be used everywhere. Attempts to store a revision of a page
140 using a model or format different from the default will result in an error.
141
142
143 == Globals ==
144
145 There are some new globals that can be used to control the behavior of the ContentHandler facility:
146
147 * $wgContentHandlers associates content model IDs with the names of the appropriate ContentHandler subclasses
148 or callbacks that create an instance of the appropriate ContentHandler subclass.
149
150 * $wgNamespaceContentModels maps namespace IDs to a content model that should be the default for that namespace.
151
152 * $wgContentHandlerUseDB determines whether each revision's content model should be stored in the database.
153 Defaults is true.
154
155 * $wgContentHandlerTextFallback determines how the compatibility method ContentHandler::getContentText() will behave for
156 non-text content:
157 'ignore' causes null to be returned for non-text content (default).
158 'serialize' causes the serialized form of any non-text content to be returned (scary).
159 'fail' causes an exception to be thrown for non-text content (strict).
160
161
162 == Caveats ==
163
164 There are some changes in behavior that might be surprising to users:
165
166 * Javascript and CSS pages are no longer parsed as wikitext (though pre-save transform is still applied). Most
167 importantly, this means that links, including categorization links, contained in the code will not work.
168
169 * With $wgContentHandlerUseDB = false, pages can not be moved in a way that would change the
170 default model. E.g. [[MediaWiki:foo.js]] can not be moved to [[MediaWiki:foo bar]], but can still be moved to
171 [[User:John/foo.js]]. Also, in this mode, changing the default content model for a page (e.g. by changing
172 $wgNamespaceContentModels) may cause it to become inaccessible.
173
174 * action=edit will fail for pages with non-text content, unless the respective ContentHandler implementation has
175 provided a specialized handler for the edit action. This is true for the API as well.
176
177 * action=raw will fail for all non-text content. This seems better than serving content in other formats to an
178 unsuspecting recipient. This will also cause client-side diffs to fail.
179
180 * File pages provide their own action overrides that do not combine gracefully with any custom handlers defined by a
181 ContentHandler. If for example a File page used a content model with a custom revert action, this would be overridden by
182 WikiFilePage's handler for the revert action.