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 #XXX: or define $content->getDefaultWikiText()?
23 return $content->getRawData();
26 #XXX: this must not be used for editing, otherwise we may loose data:
27 #XXX: e.g. if this returns the "main" text from a multipart page, all attachments would be lost
32 public static function makeContent( $text, Title
$title, $modelName = null, $format = null ) {
34 $modelName = $title->getContentModelName();
37 $handler = ContentHandler
::getForModelName( $modelName );
38 return $handler->unserialize( $text, $format );
41 public static function getDefaultModelFor( Title
$title ) {
42 global $wgNamespaceContentModels;
44 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
45 # because it is used to initialized the mContentModelName memebr.
47 $ns = $title->getNamespace();
53 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
54 $model = $wgNamespaceContentModels[ $ns ];
57 # hook can determin default model
58 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
59 if ( $model ) return $model;
62 # Could this page contain custom CSS or JavaScript, based on the title?
63 $isCssOrJsPage = ( NS_MEDIAWIKI
== $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
64 if ( $isCssOrJsPage ) $ext = $m[1];
66 # hook can force js/css
67 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
69 # Is this a .css subpage of a user page?
70 $isJsCssSubpage = ( NS_USER
== $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
71 if ( $isJsCssSubpage ) $ext = $m[1];
73 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
74 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT ||
$model === null );
75 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
77 # hook can override $isWikitext
78 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
83 return CONTENT_MODEL_JAVASCRIPT
;
84 else if ( $ext == 'css' )
85 return CONTENT_MODEL_CSS
;
90 return CONTENT_MODEL_TEXT
;
93 # we established that is must be wikitext
94 return CONTENT_MODEL_WIKITEXT
;
97 public static function getForTitle( Title
$title ) {
98 $modelName = $title->getContentModelName();
99 return ContentHandler
::getForModelName( $modelName );
102 public static function getForContent( Content
$content ) {
103 $modelName = $content->getModelName();
104 return ContentHandler
::getForModelName( $modelName );
107 public static function getForModelName( $modelName ) {
108 global $wgContentHandlers;
110 if ( empty( $wgContentHandlers[$modelName] ) ) {
112 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
115 if ( is_string( $wgContentHandlers[$modelName] ) ) {
116 $class = $wgContentHandlers[$modelName];
117 $wgContentHandlers[$modelName] = new $class( $modelName );
120 return $wgContentHandlers[$modelName];
123 # ----------------------------------------------------------------------------------------------------------
124 public function __construct( $modelName, $formats ) {
125 $this->mModelName
= $modelName;
126 $this->mSupportedFormats
= $formats;
129 public function getModelName() {
130 # for wikitext: wikitext; in the future: wikiast, wikidom?
131 # for wikidata: wikidata
132 return $this->mModelName
;
136 public function getSupportedFormats() {
137 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
138 # for wikidata: "application/json", "application/x-php", etc
139 return $this->mSupportedFormats
;
142 public function getDefaultFormat() {
143 return $this->mSupportedFormats
[0];
146 public abstract function serialize( Content
$content, $format = null );
148 public abstract function unserialize( $blob, $format = null );
150 public abstract function emptyContent();
152 # public abstract function doPreSaveTransform( $title, $obj ); #TODO...
155 * Return an Article object suitable for viewing the given object
157 * NOTE: does *not* do special handling for Image and Category pages!
158 * Use Article::newFromTitle() for that!
162 * @todo Article is being refactored into an action class, keep track of that
164 public function createArticle( Title
$title ) {
165 #XXX: assert that $title->getContentModelName() == $this->getModelname()?
166 $article = new Article($title);
171 * Return an EditPage object suitable for editing the given object
173 * @param type $article
176 public function createEditPage( Article
$article ) {
177 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
178 $editPage = new EditPage( $article );
183 * Return an ExternalEdit object suitable for editing the given object
185 * @param type $article
186 * @return \ExternalEdit
188 public function createExternalEdit( IContextSource
$context ) {
189 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
190 $externalEdit = new ExternalEdit( $context );
191 return $externalEdit;
195 public function updatePage( $title, $obj ) {
199 public function getDiffEngine( Article
$article ) {
200 $de = new DifferenceEngine( $article->getContext() );
204 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
206 #TODO: how to handle extra message for JS/CSS previews??
207 #TODO: Article::showCssOrJsPage ---> specialized classes!
209 #XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
213 abstract class TextContentHandler
extends ContentHandler
{
215 public function __construct( $modelName, $formats ) {
216 parent
::__construct( $modelName, $formats );
219 public function serialize( Content
$content, $format = null ) {
220 #FIXME: assert format
221 return $content->getRawData();
225 class WikitextContentHandler
extends TextContentHandler
{
227 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
228 parent
::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
231 public function unserialize( $text, $format = null ) {
232 #FIXME: assert format
233 return new WikitextContent($text);
236 public function emptyContent() {
237 return new WikitextContent("");
242 class JavaScriptContentHandler
extends TextContentHandler
{
244 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
245 parent
::__construct( $modelName, array( 'text/javascript' ) );
248 public function unserialize( $text, $format = null ) {
249 return new JavaScriptContent($text);
252 public function emptyContent() {
253 return new JavaScriptContent("");
257 class CssContentHandler
extends TextContentHandler
{
259 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
260 parent
::__construct( $modelName, array( 'text/css' ) );
263 public function unserialize( $text, $format = null ) {
264 return new CssContent($text);
267 public function emptyContent() {
268 return new CssContent("");