2012-02-01 22:07:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A content handler knows how do deal with a specific type of content on a wiki page.
|
|
|
|
|
* Content is stored in the database in a serialized form (using a serialization format aka mime type)
|
|
|
|
|
* and is be unserialized into it's native PHP represenation (the content model).
|
|
|
|
|
*
|
|
|
|
|
* Some content types have a flat model, that is, their native represenation is the
|
|
|
|
|
* same as their serialized form. Examples would be JavaScript and CSS code. As of now,
|
|
|
|
|
* this also applies to wikitext (mediawiki's default content type), but wikitext
|
|
|
|
|
* content may be represented by a DOM or AST structure in the future.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
abstract class ContentHandler {
|
2012-03-05 11:53:21 +00:00
|
|
|
|
2012-03-05 15:53:25 +00:00
|
|
|
public static function getContentText( Content $content ) {
|
|
|
|
|
if ( !$content ) return '';
|
|
|
|
|
|
|
|
|
|
if ( $content instanceof TextContent ) {
|
|
|
|
|
#XXX: or check by model name?
|
|
|
|
|
#XXX: or define $content->allowRawData()?
|
2012-03-09 17:23:05 +00:00
|
|
|
#XXX: or define $content->getDefaultWikiText()?
|
2012-03-05 15:53:25 +00:00
|
|
|
return $content->getRawData();
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-09 17:23:05 +00:00
|
|
|
#XXX: this must not be used for editing, otherwise we may loose data:
|
|
|
|
|
#XXX: e.g. if this returns the "main" text from a multipart page, all attachments would be lost
|
|
|
|
|
|
2012-03-05 15:53:25 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-05 17:09:41 +00:00
|
|
|
public static function makeContent( $text, Title $title, $format = null, $revId = null ) {
|
|
|
|
|
$handler = ContentHandler::getForTitle( $title );
|
|
|
|
|
|
|
|
|
|
#FIXME: pass revid?
|
|
|
|
|
return $handler->unserialize( $text, $title, $format );
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-05 11:53:21 +00:00
|
|
|
public static function getDefaultModelFor( Title $title ) {
|
|
|
|
|
global $wgNamespaceContentModels;
|
|
|
|
|
|
|
|
|
|
# NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
|
|
|
|
|
# because it is used to initialized the mContentModelName memebr.
|
|
|
|
|
|
|
|
|
|
$ns = $title->getNamespace();
|
|
|
|
|
|
|
|
|
|
$ext = false;
|
|
|
|
|
$m = null;
|
|
|
|
|
$model = null;
|
|
|
|
|
|
|
|
|
|
if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
|
|
|
|
|
$model = $wgNamespaceContentModels[ $ns ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# hook can determin default model
|
|
|
|
|
if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
|
|
|
|
|
if ( $model ) return $model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Could this page contain custom CSS or JavaScript, based on the title?
|
|
|
|
|
$isCssOrJsPage = ( NS_MEDIAWIKI == $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
|
|
|
|
|
if ( $isCssOrJsPage ) $ext = $m[1];
|
|
|
|
|
|
|
|
|
|
# hook can force js/css
|
|
|
|
|
wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
|
|
|
|
|
|
|
|
|
|
# Is this a .css subpage of a user page?
|
|
|
|
|
$isJsCssSubpage = ( NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
|
|
|
|
|
if ( $isJsCssSubpage ) $ext = $m[1];
|
|
|
|
|
|
|
|
|
|
# is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
|
|
|
|
|
$isWikitext = ( $model == CONTENT_MODEL_WIKITEXT || $model === null );
|
|
|
|
|
$isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
|
|
|
|
|
|
|
|
|
|
# hook can override $isWikitext
|
|
|
|
|
wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
|
|
|
|
|
|
|
|
|
|
if ( !$isWikitext ) {
|
|
|
|
|
|
|
|
|
|
if ( $ext == 'js' )
|
|
|
|
|
return CONTENT_MODEL_JAVASCRIPT;
|
|
|
|
|
else if ( $ext == 'css' )
|
|
|
|
|
return CONTENT_MODEL_CSS;
|
|
|
|
|
|
|
|
|
|
if ( $model )
|
|
|
|
|
return $model;
|
|
|
|
|
else
|
|
|
|
|
return CONTENT_MODEL_TEXT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# we established that is must be wikitext
|
|
|
|
|
return CONTENT_MODEL_WIKITEXT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getForTitle( Title $title ) {
|
|
|
|
|
$modelName = $title->getContentModelName();
|
2012-03-09 17:23:05 +00:00
|
|
|
return ContentHandler::getForModelName( $modelName );
|
2012-03-05 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getForContent( Content $content ) {
|
|
|
|
|
$modelName = $content->getModelName();
|
2012-03-09 17:23:05 +00:00
|
|
|
return ContentHandler::getForModelName( $modelName );
|
2012-03-05 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getForModelName( $modelName ) {
|
|
|
|
|
global $wgContentHandlers;
|
|
|
|
|
|
|
|
|
|
if ( empty( $wgContentHandlers[$modelName] ) ) {
|
|
|
|
|
#FIXME: hook here!
|
|
|
|
|
throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_string( $wgContentHandlers[$modelName] ) ) {
|
|
|
|
|
$class = $wgContentHandlers[$modelName];
|
|
|
|
|
$wgContentHandlers[$modelName] = new $class( $modelName );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $wgContentHandlers[$modelName];
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------------
|
2012-02-01 22:07:47 +00:00
|
|
|
public function __construct( $modelName, $formats ) {
|
|
|
|
|
$this->mModelName = $modelName;
|
|
|
|
|
$this->mSupportedFormats = $formats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getModelName() {
|
|
|
|
|
# for wikitext: wikitext; in the future: wikiast, wikidom?
|
|
|
|
|
# for wikidata: wikidata
|
|
|
|
|
return $this->mModelName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getSupportedFormats() {
|
|
|
|
|
# for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
|
|
|
|
|
# for wikidata: "application/json", "application/x-php", etc
|
|
|
|
|
return $this->mSupportedFormats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDefaultFormat() {
|
|
|
|
|
return $this->mSupportedFormats[0];
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
public abstract function serialize( Content $content, Title $title, $format = null );
|
2012-02-01 22:07:47 +00:00
|
|
|
|
2012-03-05 17:09:41 +00:00
|
|
|
public abstract function unserialize( $blob, Title $title, $format = null ); #FIXME: ...and revId?
|
2012-02-01 22:07:47 +00:00
|
|
|
|
2012-03-09 17:51:10 +00:00
|
|
|
public abstract function newContent( Title $title );
|
|
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
# public abstract function doPreSaveTransform( $title, $obj ); #TODO...
|
2012-02-01 22:07:47 +00:00
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
/**
|
2012-02-01 22:07:47 +00:00
|
|
|
* Return an Article object suitable for viewing the given object
|
2012-03-09 17:23:05 +00:00
|
|
|
*
|
|
|
|
|
* NOTE: does *not* do special handling for Image and Category pages!
|
|
|
|
|
* Use Article::newFromTitle() for that!
|
|
|
|
|
*
|
2012-02-01 22:07:47 +00:00
|
|
|
* @param type $title
|
2012-03-09 17:23:05 +00:00
|
|
|
* @return \Article
|
2012-02-01 22:07:47 +00:00
|
|
|
* @todo Article is being refactored into an action class, keep track of that
|
|
|
|
|
*/
|
2012-03-09 17:23:05 +00:00
|
|
|
public function createArticle( Title $title ) {
|
|
|
|
|
#XXX: assert that $title->getContentModelName() == $this->getModelname()?
|
2012-02-01 22:07:47 +00:00
|
|
|
$article = new Article($title);
|
|
|
|
|
return $article;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return an EditPage object suitable for editing the given object
|
|
|
|
|
*
|
|
|
|
|
* @param type $article
|
|
|
|
|
* @return \EditPage
|
|
|
|
|
*/
|
2012-03-09 17:23:05 +00:00
|
|
|
public function createEditPage( Article $article ) {
|
|
|
|
|
#XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
|
|
|
|
|
$editPage = new EditPage( $article );
|
2012-02-01 22:07:47 +00:00
|
|
|
return $editPage;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-09 17:23:05 +00:00
|
|
|
/**
|
|
|
|
|
* Return an ExternalEdit object suitable for editing the given object
|
|
|
|
|
*
|
|
|
|
|
* @param type $article
|
|
|
|
|
* @return \ExternalEdit
|
|
|
|
|
*/
|
|
|
|
|
public function createExternalEdit( IContextSource $context ) {
|
|
|
|
|
#XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
|
|
|
|
|
$externalEdit = new ExternalEdit( $context );
|
|
|
|
|
return $externalEdit;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-01 22:07:47 +00:00
|
|
|
/**
|
|
|
|
|
public function updatePage( $title, $obj ) {
|
|
|
|
|
}
|
|
|
|
|
**/
|
|
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
public function getDiffEngine( Article $article ) {
|
2012-02-01 22:07:47 +00:00
|
|
|
$de = new DifferenceEngine( $article->getContext() );
|
|
|
|
|
return $de;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
public function getIndexUpdateJobs( Title $title, ParserOutput $parserOutput, $recursive = true ) {
|
2012-02-01 22:07:47 +00:00
|
|
|
# for wikitext, create a LinksUpdate object
|
|
|
|
|
# for wikidata: serialize arrays to json
|
|
|
|
|
$update = new LinksUpdate( $title, $parserOutput, $recursive );
|
|
|
|
|
return $update;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
|
2012-03-09 17:23:05 +00:00
|
|
|
|
|
|
|
|
#TODO: how to handle extra message for JS/CSS previews??
|
|
|
|
|
#TODO: Article::showCssOrJsPage ---> specialized classes!
|
2012-03-09 17:51:10 +00:00
|
|
|
|
|
|
|
|
#XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
|
2012-02-01 22:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
|
|
|
|
|
abstract class TextContentHandler extends ContentHandler {
|
|
|
|
|
|
|
|
|
|
public function __construct( $modelName, $formats ) {
|
2012-03-09 16:16:40 +00:00
|
|
|
parent::__construct( $modelName, $formats );
|
2012-03-06 17:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function serialize( Content $content, Title $title, $format = null ) {
|
|
|
|
|
return $content->getRawData();
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-01 22:07:47 +00:00
|
|
|
}
|
2012-03-06 17:35:46 +00:00
|
|
|
class WikitextContentHandler extends TextContentHandler {
|
|
|
|
|
|
|
|
|
|
public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
|
2012-03-09 16:16:40 +00:00
|
|
|
parent::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
|
2012-03-06 17:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unserialize( $text, Title $title, $format = null ) {
|
|
|
|
|
return new WikitextContent($text, $title);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-09 17:51:10 +00:00
|
|
|
public function newContent( Title $title) {
|
|
|
|
|
return new WikitextContent("", $title);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class JavaScriptContentHandler extends TextContentHandler {
|
|
|
|
|
|
|
|
|
|
public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
|
2012-03-09 16:16:40 +00:00
|
|
|
parent::__construct( $modelName, array( 'text/javascript' ) );
|
2012-03-06 17:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unserialize( $text, Title $title, $format = null ) {
|
|
|
|
|
return new JavaScriptContent($text, $title);
|
|
|
|
|
}
|
2012-02-01 22:07:47 +00:00
|
|
|
|
2012-03-09 17:51:10 +00:00
|
|
|
public function newContent( Title $title) {
|
|
|
|
|
return new JavaScriptContent("", $title);
|
|
|
|
|
}
|
2012-02-01 22:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
class CssContentHandler extends TextContentHandler {
|
|
|
|
|
|
|
|
|
|
public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
|
2012-03-09 16:16:40 +00:00
|
|
|
parent::__construct( $modelName, array( 'text/css' ) );
|
2012-03-06 17:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unserialize( $text, Title $title, $format = null ) {
|
|
|
|
|
return new CssContent($text, $title);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-09 17:51:10 +00:00
|
|
|
public function newContent( Title $title) {
|
|
|
|
|
return new CssContent("", $title);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-01 22:07:47 +00:00
|
|
|
}
|