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 getContentText( Content
$content ) {
17 if ( !$content ) return '';
19 if ( $content instanceof TextContent
) {
20 #XXX: or check by model name?
21 #XXX: or define $content->allowRawData()?
22 return $content->getRawData();
28 public static function makeContent( $text, Title
$title, $format = null, $revId = null ) {
29 $handler = ContentHandler
::getForTitle( $title );
32 return $handler->unserialize( $text, $title, $format );
35 public static function getDefaultModelFor( Title
$title ) {
36 global $wgNamespaceContentModels;
38 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
39 # because it is used to initialized the mContentModelName memebr.
41 $ns = $title->getNamespace();
47 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
48 $model = $wgNamespaceContentModels[ $ns ];
51 # hook can determin default model
52 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
53 if ( $model ) return $model;
56 # Could this page contain custom CSS or JavaScript, based on the title?
57 $isCssOrJsPage = ( NS_MEDIAWIKI
== $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
58 if ( $isCssOrJsPage ) $ext = $m[1];
60 # hook can force js/css
61 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
63 # Is this a .css subpage of a user page?
64 $isJsCssSubpage = ( NS_USER
== $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
65 if ( $isJsCssSubpage ) $ext = $m[1];
67 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
68 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT ||
$model === null );
69 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
71 # hook can override $isWikitext
72 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
77 return CONTENT_MODEL_JAVASCRIPT
;
78 else if ( $ext == 'css' )
79 return CONTENT_MODEL_CSS
;
84 return CONTENT_MODEL_TEXT
;
87 # we established that is must be wikitext
88 return CONTENT_MODEL_WIKITEXT
;
91 public static function getForTitle( Title
$title ) {
92 $modelName = $title->getContentModelName();
93 return ContenteHandler
::getForModelName( $modelName );
96 public static function getForContent( Content
$content ) {
97 $modelName = $content->getModelName();
98 return ContenteHandler
::getForModelName( $modelName );
101 public static function getForModelName( $modelName ) {
102 global $wgContentHandlers;
104 if ( empty( $wgContentHandlers[$modelName] ) ) {
106 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
109 if ( is_string( $wgContentHandlers[$modelName] ) ) {
110 $class = $wgContentHandlers[$modelName];
111 $wgContentHandlers[$modelName] = new $class( $modelName );
114 return $wgContentHandlers[$modelName];
117 # ----------------------------------------------------------------------------------------------------------
118 public function __construct( $modelName, $formats ) {
119 $this->mModelName
= $modelName;
120 $this->mSupportedFormats
= $formats;
123 public function getModelName() {
124 # for wikitext: wikitext; in the future: wikiast, wikidom?
125 # for wikidata: wikidata
126 return $this->mModelName
;
130 public function getSupportedFormats() {
131 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
132 # for wikidata: "application/json", "application/x-php", etc
133 return $this->mSupportedFormats
;
136 public function getDefaultFormat() {
137 return $this->mSupportedFormats
[0];
140 public abstract function serialize( Content
$content, Title
$title, $format = null );
142 public abstract function unserialize( $blob, Title
$title, $format = null ); #FIXME: ...and revId?
144 # public abstract function doPreSaveTransform( $title, $obj ); #TODO...
147 * Return an Article object suitable for viewing the given object
152 * @todo Article is being refactored into an action class, keep track of that
154 public function createArticle( Title
$title, $obj ) { #TODO: use this!
155 $article = new Article($title);
160 * Return an EditPage object suitable for editing the given object
164 * @param type $article
167 public function createEditPage( Title
$title, $obj, Article
$article ) { #TODO: use this!
168 $editPage = new EditPage($article);
173 public function updatePage( $title, $obj ) {
177 public function getDiffEngine( Article
$article ) {
178 $de = new DifferenceEngine( $article->getContext() );
182 public function getIndexUpdateJobs( Title
$title, ParserOutput
$parserOutput, $recursive = true ) {
183 # for wikitext, create a LinksUpdate object
184 # for wikidata: serialize arrays to json
185 $update = new LinksUpdate( $title, $parserOutput, $recursive );
189 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
193 abstract class TextContentHandler
extends ContentHandler
{
195 public function __construct( $modelName, $formats ) {
196 parent
::__construct( $modelName, $formats );
199 public function serialize( Content
$content, Title
$title, $format = null ) {
200 return $content->getRawData();
204 class WikitextContentHandler
extends TextContentHandler
{
206 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
207 parent
::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
210 public function unserialize( $text, Title
$title, $format = null ) {
211 return new WikitextContent($text, $title);
216 class JavaScriptContentHandler
extends TextContentHandler
{
218 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
219 parent
::__construct( $modelName, array( 'text/javascript' ) );
222 public function unserialize( $text, Title
$title, $format = null ) {
223 return new JavaScriptContent($text, $title);
228 class CssContentHandler
extends TextContentHandler
{
230 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
231 parent
::__construct( $modelName, array( 'text/css' ) );
234 public function unserialize( $text, Title
$title, $format = null ) {
235 return new CssContent($text, $title);