Make it easier to subclass Content and ContentHandler subclasses
authorKunal Mehta <legoktm@gmail.com>
Sun, 17 Aug 2014 06:04:08 +0000 (23:04 -0700)
committerKunal Mehta <legoktm@gmail.com>
Sun, 17 Aug 2014 06:04:08 +0000 (23:04 -0700)
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

includes/content/CssContent.php
includes/content/CssContentHandler.php
includes/content/JSONContentHandler.php
includes/content/JavaScriptContent.php
includes/content/JavaScriptContentHandler.php
includes/content/TextContentHandler.php
includes/content/WikitextContent.php
includes/content/WikitextContentHandler.php

index 2673084..7241458 100644 (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 );
        }
 
        /**
index fd326f0..1ab4ee2 100644 (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';
        }
 
        /**
index 6b77527..33f2036 100644 (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.
-        *
-        * @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;
        }
 
        /**
index 442b705..0991f07 100644 (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 );
        }
 
        /**
index 122003f..8d62e2a 100644 (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';
        }
 
        /**
index b728d31..ddbc476 100644 (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( '' );
        }
 
 }
index 237029b..d23f925 100644 (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 );
        }
 
        /**
index 5ae3e25..c1db1de 100644 (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 );
        }
 
        /**