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