contenthandler.txt 10 KB

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