4 * A content handler knows how do deal with a specific type of content on a wiki page.
5 * Content is stored in the database in a serialized form (using a serialization format aka mime type)
6 * and is be unserialized into it's native PHP represenation (the content model).
8 * Some content types have a flat model, that is, their native represenation is the
9 * same as their serialized form. Examples would be JavaScript and CSS code. As of now,
10 * this also applies to wikitext (mediawiki's default content type), but wikitext
11 * content may be represented by a DOM or AST structure in the future.
14 abstract class ContentHandler
{
16 public static function getDefaultModelFor( Title
$title ) {
17 global $wgNamespaceContentModels;
19 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
20 # because it is used to initialized the mContentModelName memebr.
22 $ns = $title->getNamespace();
28 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
29 $model = $wgNamespaceContentModels[ $ns ];
32 # hook can determin default model
33 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
34 if ( $model ) return $model;
37 # Could this page contain custom CSS or JavaScript, based on the title?
38 $isCssOrJsPage = ( NS_MEDIAWIKI
== $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
39 if ( $isCssOrJsPage ) $ext = $m[1];
41 # hook can force js/css
42 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
44 # Is this a .css subpage of a user page?
45 $isJsCssSubpage = ( NS_USER
== $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
46 if ( $isJsCssSubpage ) $ext = $m[1];
48 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
49 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT ||
$model === null );
50 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
52 # hook can override $isWikitext
53 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
58 return CONTENT_MODEL_JAVASCRIPT
;
59 else if ( $ext == 'css' )
60 return CONTENT_MODEL_CSS
;
65 return CONTENT_MODEL_TEXT
;
68 # we established that is must be wikitext
69 return CONTENT_MODEL_WIKITEXT
;
72 public static function getForTitle( Title
$title ) {
73 $modelName = $title->getContentModelName();
74 return ContenteHandler
::getForModelName( $modelName );
77 public static function getForContent( Content
$content ) {
78 $modelName = $content->getModelName();
79 return ContenteHandler
::getForModelName( $modelName );
82 public static function getForModelName( $modelName ) {
83 global $wgContentHandlers;
85 if ( empty( $wgContentHandlers[$modelName] ) ) {
87 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
90 if ( is_string( $wgContentHandlers[$modelName] ) ) {
91 $class = $wgContentHandlers[$modelName];
92 $wgContentHandlers[$modelName] = new $class( $modelName );
95 return $wgContentHandlers[$modelName];
99 public function __construct( $modelName, $formats ) {
100 $this->mModelName
= $modelName;
101 $this->mSupportedFormats
= $formats;
104 public function getModelName() {
105 # for wikitext: wikitext; in the future: wikiast, wikidom?
106 # for wikidata: wikidata
107 return $this->mModelName
;
111 public function getSupportedFormats() {
112 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
113 # for wikidata: "application/json", "application/x-php", etc
114 return $this->mSupportedFormats
;
117 public function getDefaultFormat() {
118 return $this->mSupportedFormats
[0];
121 public abstract function serialize( $obj, $format = null );
122 # for wikitext, do nothing (in the future: serialise ast/dom)
123 # for wikidata: serialize arrays to json
125 public abstract function unserialize( $blob, $format = null );
126 # for wikitext, do nothing (in the future: parse into ast/dom)
127 # for wikidata: serialize arrays to json
130 public function getSearchText( $obj ) {
131 # for wikitext, return wikitext
132 # for wikidata, return pseudo-wikitext composed of property values (or some such)
133 $text = $this->serialize( $obj );
134 return $text; # return the default serialization.
137 public function getWikitextForTransclusion( $obj ) {
138 # for wikitext, return text
139 # for wikidata, return false, or some generated wikitext
140 $text = $this->serialize( $obj );
141 return '<pre>' . $text . '</pre>'; # return a pre-formatted block containing the default serialization.
144 public abstract function render( $obj, Title
$title, ParserOptions
$options, $revid = null );
145 # returns a ParserOutput instance!
146 # are parser options, generic?!
148 public abstract function doPreSaveTransform( $title, $obj );
150 # TODO: getPreloadText()
154 * Return an Article object suitable for viewing the given object
159 * @todo Article is being refactored into an action class, keep track of that
161 public function createArticle( $title, $obj ) {
162 $article = new Article($title);
167 * Return an EditPage object suitable for editing the given object
171 * @param type $article
174 public function createEditPage( $title, $obj, $article ) {
175 $editPage = new EditPage($article);
180 public function updatePage( $title, $obj ) {
184 public function getDiffEngine( $article ) {
185 $de = new DifferenceEngine( $article->getContext() );
189 public function getIndexUpdateJobs( $title, $parserOutput, $recursive = true ) {
190 # for wikitext, create a LinksUpdate object
191 # for wikidata: serialize arrays to json
192 $update = new LinksUpdate( $title, $parserOutput, $recursive );
196 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
199 abstract class WikitextContentHandler
extends ContentHandler
{
202 abstract class JavaScriptContentHandler
extends WikitextHandler
{
205 abstract class CssContentHandler
extends WikitextHandler
{