Make it easier to subclass Content and ContentHandler subclasses

Currently the names of associated Content classes are hardcoded,
meaning that any extension that wishes to subclass these implementations
must also re-implement that function, usually copying it exactly
with just the class name changed. Using "static" avoids that issue.

For ContentHandlers, I added a TextContentHandler::getContentClass,
which should be used when creating new Content objects.

Change-Id: I70f1a3291aec3460120ec20121a23f4cb68e04d1
This commit is contained in:
Kunal Mehta 2014-08-16 23:04:08 -07:00
parent 28fdbd9ea2
commit ccee80b8a6
8 changed files with 40 additions and 83 deletions

View file

@ -58,7 +58,7 @@ class CssContent extends TextContent {
$text = $this->getNativeData();
$pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
return new CssContent( $pst );
return new static( $pst );
}
/**

View file

@ -36,27 +36,8 @@ class CssContentHandler extends TextContentHandler {
parent::__construct( $modelId, array( CONTENT_FORMAT_CSS ) );
}
/**
* @param string $text
* @param string $format
*
* @return CssContent
*
* @see ContentHandler::unserializeContent()
*/
public function unserializeContent( $text, $format = null ) {
$this->checkFormat( $format );
return new CssContent( $text );
}
/**
* @return CssContent A new CssContent object with empty text.
*
* @see ContentHandler::makeEmptyContent()
*/
public function makeEmptyContent() {
return new CssContent( '' );
protected function getContentClass() {
return 'CssContent';
}
/**

View file

@ -16,6 +16,8 @@ class JSONContentHandler extends TextContentHandler {
/**
* The class name of objects that should be created
*
* @deprecated override getContentClass instead
*
* @var string
*/
protected $contentClass = 'JSONContent';
@ -25,25 +27,13 @@ class JSONContentHandler extends TextContentHandler {
}
/**
* Unserializes a JSONContent object.
* Temporary back-compat until extensions
* are updated to override this
*
* @param string $text Serialized form of the content
* @param null|string $format The format used for serialization
*
* @return JSONContent
* @return string
*/
public function unserializeContent( $text, $format = null ) {
$this->checkFormat( $format );
return new $this->contentClass( $text );
}
/**
* Creates an empty JSONContent object.
*
* @return JSONContent
*/
public function makeEmptyContent() {
return new $this->contentClass( '' );
protected function getContentClass() {
return $this->contentClass;
}
/**

View file

@ -57,7 +57,7 @@ class JavaScriptContent extends TextContent {
$text = $this->getNativeData();
$pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
return new JavaScriptContent( $pst );
return new static( $pst );
}
/**

View file

@ -36,27 +36,8 @@ class JavaScriptContentHandler extends TextContentHandler {
parent::__construct( $modelId, array( CONTENT_FORMAT_JAVASCRIPT ) );
}
/**
* @param string $text
* @param string $format
*
* @return JavaScriptContent
*
* @see ContentHandler::unserializeContent()
*/
public function unserializeContent( $text, $format = null ) {
$this->checkFormat( $format );
return new JavaScriptContent( $text );
}
/**
* @return JavaScriptContent A new JavaScriptContent object with empty text.
*
* @see ContentHandler::makeEmptyContent()
*/
public function makeEmptyContent() {
return new JavaScriptContent( '' );
protected function getContentClass() {
return 'JavaScriptContent';
}
/**

View file

@ -92,6 +92,19 @@ class TextContentHandler extends ContentHandler {
return $mergedContent;
}
/**
* Returns the name of the associated Content class, to
* be used when creating new objects. Override expected
* by subclasses.
*
* @since 1.24
*
* @return string
*/
protected function getContentClass() {
return 'TextContent';
}
/**
* Unserializes a Content object of the type supported by this ContentHandler.
*
@ -105,7 +118,8 @@ class TextContentHandler extends ContentHandler {
public function unserializeContent( $text, $format = null ) {
$this->checkFormat( $format );
return new TextContent( $text );
$class = $this->getContentClass();
return new $class( $text );
}
/**
@ -116,7 +130,8 @@ class TextContentHandler extends ContentHandler {
* @return Content A new TextContent object with empty text.
*/
public function makeEmptyContent() {
return new TextContent( '' );
$class = $this->getContentClass();
return new $class( '' );
}
}

View file

@ -52,7 +52,7 @@ class WikitextContent extends TextContent {
if ( $sect === false ) {
return false;
} else {
return new WikitextContent( $sect );
return new static( $sect );
}
}
@ -104,7 +104,7 @@ class WikitextContent extends TextContent {
$text = $wgParser->replaceSection( $oldtext, $sectionId, $text );
}
$newContent = new WikitextContent( $text );
$newContent = new static( $text );
wfProfileOut( __METHOD__ );
@ -125,7 +125,7 @@ class WikitextContent extends TextContent {
$text .= "\n\n";
$text .= $this->getNativeData();
return new WikitextContent( $text );
return new static( $text );
}
/**
@ -145,7 +145,7 @@ class WikitextContent extends TextContent {
$pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
rtrim( $pst );
return ( $text === $pst ) ? $this : new WikitextContent( $pst );
return ( $text === $pst ) ? $this : new static( $pst );
}
/**
@ -164,7 +164,7 @@ class WikitextContent extends TextContent {
$text = $this->getNativeData();
$plt = $wgParser->getPreloadText( $text, $title, $popts, $params );
return new WikitextContent( $plt );
return new static( $plt );
}
/**
@ -246,7 +246,7 @@ class WikitextContent extends TextContent {
'[[' . $target->getFullText() . ']]',
$this->getNativeData(), 1 );
return new WikitextContent( $newText );
return new static( $newText );
}
/**

View file

@ -34,19 +34,8 @@ class WikitextContentHandler extends TextContentHandler {
parent::__construct( $modelId, array( CONTENT_FORMAT_WIKITEXT ) );
}
public function unserializeContent( $text, $format = null ) {
$this->checkFormat( $format );
return new WikitextContent( $text );
}
/**
* @return Content A new WikitextContent object with empty text.
*
* @see ContentHandler::makeEmptyContent
*/
public function makeEmptyContent() {
return new WikitextContent( '' );
protected function getContentClass() {
return 'WikitextContent';
}
/**
@ -79,7 +68,8 @@ class WikitextContentHandler extends TextContentHandler {
$redirectText .= "\n" . $text;
}
return new WikitextContent( $redirectText );
$class = $this->getContentClass();
return new $class( $redirectText );
}
/**