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() );
205 * attempts to merge differences between three versions.
206 * Returns a new Content object for a clean merge and false for failure or a conflict.
208 * This default implementation always returns false.
210 * @param $oldContent String
211 * @param $myContent String
212 * @param $yourContent String
213 * @return Content|Bool
215 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
219 #TODO: cover patch/undo just like merge3.
221 #TODO: how to handle extra message for JS/CSS previews??
222 #TODO: Article::showCssOrJsPage ---> specialized classes!
224 #XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
228 abstract class TextContentHandler
extends ContentHandler
{
230 public function __construct( $modelName, $formats ) {
231 parent
::__construct( $modelName, $formats );
234 public function serialize( Content
$content, $format = null ) {
235 #FIXME: assert format
236 return $content->getRawData();
240 * attempts to merge differences between three versions.
241 * Returns a new Content object for a clean merge and false for failure or a conflict.
243 * This text-based implementation uses wfMerge().
245 * @param $oldContent String
246 * @param $myContent String
247 * @param $yourContent String
248 * @return Content|Bool
250 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
251 $format = $this->getDefaultFormat();
253 $old = $this->serialize( $oldContent, $format );
254 $mine = $this->serialize( $myContent, $format );
255 $yours = $this->serialize( $yourContent, $format );
257 $ok = wfMerge( $old, $mine, $yours, $result );
259 if ( !$ok ) return false;
260 if ( !$result ) return $this->emptyContent();
262 $mergedContent = $this->unserialize( $result, $format );
263 return $mergedContent;
268 class WikitextContentHandler
extends TextContentHandler
{
270 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
271 parent
::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
274 public function unserialize( $text, $format = null ) {
275 #FIXME: assert format
276 return new WikitextContent($text);
279 public function emptyContent() {
280 return new WikitextContent("");
285 class JavaScriptContentHandler
extends TextContentHandler
{
287 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
288 parent
::__construct( $modelName, array( 'text/javascript' ) );
291 public function unserialize( $text, $format = null ) {
292 return new JavaScriptContent($text);
295 public function emptyContent() {
296 return new JavaScriptContent("");
300 class CssContentHandler
extends TextContentHandler
{
302 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
303 parent
::__construct( $modelName, array( 'text/css' ) );
306 public function unserialize( $text, $format = null ) {
307 return new CssContent($text);
310 public function emptyContent() {
311 return new CssContent("");