A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://doc.wikimedia.org/mediawiki-core/master/php/contenthandler.html below:

MediaWiki: ContentHandler

The ContentHandler facility adds support for arbitrary content types on wiki pages, instead of relying on wikitext for everything. It was introduced in MediaWiki 1.21.

Each kind of content ("content model") supported by MediaWiki is identified by unique name. The content model determines how a page's content is rendered, compared, stored, edited, and so on.

Built-in content types are:

In PHP, use the corresponding CONTENT_MODEL_XXX constant.

A page's content model is available using the Title::getContentModel() method. A page's default model is determined by ContentHandler::getDefaultModelFor($title) as follows:

Note that there is no guarantee that revisions of a page will all have the same content model. To find the content model of the slot of a revision, use SlotRecord::getModel() - the content model of the main slot can for now be assumed to be the content model for the overall revision.

Architecture

Two class hierarchies are used to provide the functionality associated with the different content models:

The most important function of ContentHandler is to act as a factory for the appropriate implementation of Content. These Content objects are to be used by MediaWiki everywhere, instead of passing page content around as text. All manipulation and analysis of page content must be done via the appropriate methods of the Content object.

For each content model, a subclass of ContentHandler has to be registered with $wgContentHandlers. The ContentHandler object for a given content model can be obtained using ContentHandler::getForModelID( $id ). Also Title and WikiPage now have getContentHandler() methods for convenience.

ContentHandler objects are singletons that provide functionality specific to the content type, but not directly acting on the content of some page. ContentHandler::makeEmptyContent() and ContentHandler::unserializeContent() can be used to create a Content object of the appropriate type. However, it is recommended to instead use WikiPage::getContent() resp. RevisionRecord::getContent() to get a page's content as a Content object. These two methods should be the ONLY way in which page content is accessed. For WikiPage::getContent() the content of the main slot is returned, other slots can be retrieved by using RevisionRecord::getContent() and specifying the slot.

Another important function of ContentHandler objects is to define custom action handlers for a content model, see ContentHandler::getActionOverrides(). This is similar to what WikiPage::getActionOverrides() was already doing.

Serialization

With the ContentHandler facility, page content no longer has to be text based. Objects implementing the Content interface are used to represent and handle the content internally. For storage and data exchange, each content model supports at least one serialization format via ContentHandler::serializeContent( $content ). The list of supported formats for a given content model can be accessed using ContentHandler::getSupportedFormats().

Content serialization formats are identified using MIME type like strings. The following formats are built in:

In PHP, use the corresponding CONTENT_FORMAT_XXX constant.

Note that when using the API to access page content, especially action=edit, action=parse and action=query&prop=revisions, the model and format of the content should always be handled explicitly. Without that information, interpretation of the provided content is not reliable. The same applies to XML dumps generated via maintenance/dumpBackup.php or Special:Export.

Also note that the API will provide encapsulated, serialized content - so if the API was called with format=json, and contentformat is also json (or rather, application/json), the page content is represented as a string containing an escaped json structure. Extensions that use JSON to serialize some types of page content may provide specialized API modules that allow access to that content in a more natural form.

Compatibility

The ContentHandler facility is introduced in a way that should allow all existing code to keep functioning at least for pages that contain wikitext or other text based content. However, a number of functions and hooks have been deprecated in favor of new versions that are aware of the page's content model, and will now generate warnings when used.

Most importantly, the following functions have been deprecated:

Also, the old Article::getContent() (which returns text) is superceded by Article::getContentObject(). However, both methods should be avoided since they do not provide clean access to the page's actual content. For instance, they may return a system message for non-existing pages. Use WikiPage::getContent() instead.

Code that relies on a textual representation of the page content should eventually be rewritten. However, ContentHandler::getContentText() provides a stop-gap that can be used to get text for a page. It will return the text for text based content, and null for any other content.

For rendering page content, Content::getParserOutput() should be used instead of accessing the parser directly. WikiPage::makeParserOptions() can be used to construct appropriate options.

Besides some functions, some hooks have also been replaced by new versions (see hooks.txt for details). These hooks will now trigger a warning when used:

Database Storage

Page content is stored in the database using the same mechanism as before. Non-text content is serialized first.

Each revision's content model and serialization format is stored in the revision table (resp. in the archive table, if the revision was deleted). The page's (current) content model (that is, the content model of the latest revision) is also stored in the page table.

Note however that the content model and format is only stored if it differs from the page's default, as determined by ContentHandler::getDefaultModelFor( $title ). The default values are represented as NULL in the database, to preserve space.

Globals

There are some new globals that can be used to control the behavior of the ContentHandler facility:

Caveats

There are some changes in behavior that might be surprising to users:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4