From: Kunal Mehta Date: Sun, 17 Aug 2014 06:04:08 +0000 (-0700) Subject: Make it easier to subclass Content and ContentHandler subclasses X-Git-Tag: 1.31.0-rc.0~14228^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/?a=commitdiff_plain;h=ccee80b8a663b9882818f4a149cf38117e4b1919;p=lhc%2Fweb%2Fwiklou.git 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 --- diff --git a/includes/content/CssContent.php b/includes/content/CssContent.php index 2673084ebe..72414585d0 100644 --- a/includes/content/CssContent.php +++ b/includes/content/CssContent.php @@ -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 ); } /** diff --git a/includes/content/CssContentHandler.php b/includes/content/CssContentHandler.php index fd326f01b1..1ab4ee2a8f 100644 --- a/includes/content/CssContentHandler.php +++ b/includes/content/CssContentHandler.php @@ -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'; } /** diff --git a/includes/content/JSONContentHandler.php b/includes/content/JSONContentHandler.php index 6b7752781e..33f203661c 100644 --- a/includes/content/JSONContentHandler.php +++ b/includes/content/JSONContentHandler.php @@ -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. - * - * @param string $text Serialized form of the content - * @param null|string $format The format used for serialization - * - * @return JSONContent - */ - public function unserializeContent( $text, $format = null ) { - $this->checkFormat( $format ); - return new $this->contentClass( $text ); - } - - /** - * Creates an empty JSONContent object. + * Temporary back-compat until extensions + * are updated to override this * - * @return JSONContent + * @return string */ - public function makeEmptyContent() { - return new $this->contentClass( '' ); + protected function getContentClass() { + return $this->contentClass; } /** diff --git a/includes/content/JavaScriptContent.php b/includes/content/JavaScriptContent.php index 442b705282..0991f07683 100644 --- a/includes/content/JavaScriptContent.php +++ b/includes/content/JavaScriptContent.php @@ -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 ); } /** diff --git a/includes/content/JavaScriptContentHandler.php b/includes/content/JavaScriptContentHandler.php index 122003fe53..8d62e2a3ad 100644 --- a/includes/content/JavaScriptContentHandler.php +++ b/includes/content/JavaScriptContentHandler.php @@ -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'; } /** diff --git a/includes/content/TextContentHandler.php b/includes/content/TextContentHandler.php index b728d312ca..ddbc476006 100644 --- a/includes/content/TextContentHandler.php +++ b/includes/content/TextContentHandler.php @@ -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( '' ); } } diff --git a/includes/content/WikitextContent.php b/includes/content/WikitextContent.php index 237029b0ba..d23f925d80 100644 --- a/includes/content/WikitextContent.php +++ b/includes/content/WikitextContent.php @@ -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 ); } /** diff --git a/includes/content/WikitextContentHandler.php b/includes/content/WikitextContentHandler.php index 5ae3e25d6e..c1db1de63a 100644 --- a/includes/content/WikitextContentHandler.php +++ b/includes/content/WikitextContentHandler.php @@ -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 ); } /**