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, $format = null, $revId = null ) {
33 $handler = ContentHandler
::getForTitle( $title );
36 return $handler->unserialize( $text, $title, $format );
39 public static function getDefaultModelFor( Title
$title ) {
40 global $wgNamespaceContentModels;
42 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
43 # because it is used to initialized the mContentModelName memebr.
45 $ns = $title->getNamespace();
51 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
52 $model = $wgNamespaceContentModels[ $ns ];
55 # hook can determin default model
56 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
57 if ( $model ) return $model;
60 # Could this page contain custom CSS or JavaScript, based on the title?
61 $isCssOrJsPage = ( NS_MEDIAWIKI
== $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
62 if ( $isCssOrJsPage ) $ext = $m[1];
64 # hook can force js/css
65 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
67 # Is this a .css subpage of a user page?
68 $isJsCssSubpage = ( NS_USER
== $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
69 if ( $isJsCssSubpage ) $ext = $m[1];
71 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
72 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT ||
$model === null );
73 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
75 # hook can override $isWikitext
76 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
81 return CONTENT_MODEL_JAVASCRIPT
;
82 else if ( $ext == 'css' )
83 return CONTENT_MODEL_CSS
;
88 return CONTENT_MODEL_TEXT
;
91 # we established that is must be wikitext
92 return CONTENT_MODEL_WIKITEXT
;
95 public static function getForTitle( Title
$title ) {
96 $modelName = $title->getContentModelName();
97 return ContentHandler
::getForModelName( $modelName );
100 public static function getForContent( Content
$content ) {
101 $modelName = $content->getModelName();
102 return ContentHandler
::getForModelName( $modelName );
105 public static function getForModelName( $modelName ) {
106 global $wgContentHandlers;
108 if ( empty( $wgContentHandlers[$modelName] ) ) {
110 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
113 if ( is_string( $wgContentHandlers[$modelName] ) ) {
114 $class = $wgContentHandlers[$modelName];
115 $wgContentHandlers[$modelName] = new $class( $modelName );
118 return $wgContentHandlers[$modelName];
121 # ----------------------------------------------------------------------------------------------------------
122 public function __construct( $modelName, $formats ) {
123 $this->mModelName
= $modelName;
124 $this->mSupportedFormats
= $formats;
127 public function getModelName() {
128 # for wikitext: wikitext; in the future: wikiast, wikidom?
129 # for wikidata: wikidata
130 return $this->mModelName
;
134 public function getSupportedFormats() {
135 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
136 # for wikidata: "application/json", "application/x-php", etc
137 return $this->mSupportedFormats
;
140 public function getDefaultFormat() {
141 return $this->mSupportedFormats
[0];
144 public abstract function serialize( Content
$content, Title
$title, $format = null );
146 public abstract function unserialize( $blob, Title
$title, $format = null ); #FIXME: ...and revId?
148 public abstract function newContent( Title
$title );
150 # public abstract function doPreSaveTransform( $title, $obj ); #TODO...
153 * Return an Article object suitable for viewing the given object
155 * NOTE: does *not* do special handling for Image and Category pages!
156 * Use Article::newFromTitle() for that!
160 * @todo Article is being refactored into an action class, keep track of that
162 public function createArticle( Title
$title ) {
163 #XXX: assert that $title->getContentModelName() == $this->getModelname()?
164 $article = new Article($title);
169 * Return an EditPage object suitable for editing the given object
171 * @param type $article
174 public function createEditPage( Article
$article ) {
175 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
176 $editPage = new EditPage( $article );
181 * Return an ExternalEdit object suitable for editing the given object
183 * @param type $article
184 * @return \ExternalEdit
186 public function createExternalEdit( IContextSource
$context ) {
187 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
188 $externalEdit = new ExternalEdit( $context );
189 return $externalEdit;
193 public function updatePage( $title, $obj ) {
197 public function getDiffEngine( Article
$article ) {
198 $de = new DifferenceEngine( $article->getContext() );
202 public function getIndexUpdateJobs( Title
$title, ParserOutput
$parserOutput, $recursive = true ) {
203 # for wikitext, create a LinksUpdate object
204 # for wikidata: serialize arrays to json
205 $update = new LinksUpdate( $title, $parserOutput, $recursive );
209 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
211 #TODO: how to handle extra message for JS/CSS previews??
212 #TODO: Article::showCssOrJsPage ---> specialized classes!
214 #XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
218 abstract class TextContentHandler
extends ContentHandler
{
220 public function __construct( $modelName, $formats ) {
221 parent
::__construct( $modelName, $formats );
224 public function serialize( Content
$content, Title
$title, $format = null ) {
225 return $content->getRawData();
229 class WikitextContentHandler
extends TextContentHandler
{
231 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
232 parent
::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
235 public function unserialize( $text, Title
$title, $format = null ) {
236 return new WikitextContent($text, $title);
239 public function newContent( Title
$title) {
240 return new WikitextContent("", $title);
245 class JavaScriptContentHandler
extends TextContentHandler
{
247 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
248 parent
::__construct( $modelName, array( 'text/javascript' ) );
251 public function unserialize( $text, Title
$title, $format = null ) {
252 return new JavaScriptContent($text, $title);
255 public function newContent( Title
$title) {
256 return new JavaScriptContent("", $title);
260 class CssContentHandler
extends TextContentHandler
{
262 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
263 parent
::__construct( $modelName, array( 'text/css' ) );
266 public function unserialize( $text, Title
$title, $format = null ) {
267 return new CssContent($text, $title);
270 public function newContent( Title
$title) {
271 return new CssContent("", $title);