contenthandler.txt 11 KB

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