Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/154447

Change subject: Make it easier to subclass Content and ContentHandler subclasses
......................................................................

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
---
M includes/content/CssContent.php
M includes/content/CssContentHandler.php
M includes/content/JSONContentHandler.php
M includes/content/JavaScriptContent.php
M includes/content/JavaScriptContentHandler.php
M includes/content/TextContentHandler.php
M includes/content/WikitextContent.php
M includes/content/WikitextContentHandler.php
8 files changed, 40 insertions(+), 83 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/47/154447/1

diff --git a/includes/content/CssContent.php b/includes/content/CssContent.php
index 2673084..7241458 100644
--- a/includes/content/CssContent.php
+++ b/includes/content/CssContent.php
@@ -58,7 +58,7 @@
                $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 fd326f0..1ab4ee2 100644
--- a/includes/content/CssContentHandler.php
+++ b/includes/content/CssContentHandler.php
@@ -36,27 +36,8 @@
                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 6b77527..33f2036 100644
--- a/includes/content/JSONContentHandler.php
+++ b/includes/content/JSONContentHandler.php
@@ -16,6 +16,8 @@
        /**
         * The class name of objects that should be created
         *
+        * @deprecated override getContentClass instead
+        *
         * @var string
         */
        protected $contentClass = 'JSONContent';
@@ -25,25 +27,13 @@
        }
 
        /**
-        * 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;
        }
 
        /**
diff --git a/includes/content/JavaScriptContent.php 
b/includes/content/JavaScriptContent.php
index 442b705..0991f07 100644
--- a/includes/content/JavaScriptContent.php
+++ b/includes/content/JavaScriptContent.php
@@ -57,7 +57,7 @@
                $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 122003f..8d62e2a 100644
--- a/includes/content/JavaScriptContentHandler.php
+++ b/includes/content/JavaScriptContentHandler.php
@@ -36,27 +36,8 @@
                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 b728d31..ddbc476 100644
--- a/includes/content/TextContentHandler.php
+++ b/includes/content/TextContentHandler.php
@@ -93,6 +93,19 @@
        }
 
        /**
+        * 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.
         *
         * @since 1.21
@@ -105,7 +118,8 @@
        public function unserializeContent( $text, $format = null ) {
                $this->checkFormat( $format );
 
-               return new TextContent( $text );
+               $class = $this->getContentClass();
+               return new $class( $text );
        }
 
        /**
@@ -116,7 +130,8 @@
         * @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 237029b..d23f925 100644
--- a/includes/content/WikitextContent.php
+++ b/includes/content/WikitextContent.php
@@ -52,7 +52,7 @@
                if ( $sect === false ) {
                        return false;
                } else {
-                       return new WikitextContent( $sect );
+                       return new static( $sect );
                }
        }
 
@@ -104,7 +104,7 @@
                        $text = $wgParser->replaceSection( $oldtext, 
$sectionId, $text );
                }
 
-               $newContent = new WikitextContent( $text );
+               $newContent = new static( $text );
 
                wfProfileOut( __METHOD__ );
 
@@ -125,7 +125,7 @@
                $text .= "\n\n";
                $text .= $this->getNativeData();
 
-               return new WikitextContent( $text );
+               return new static( $text );
        }
 
        /**
@@ -145,7 +145,7 @@
                $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 @@
                $text = $this->getNativeData();
                $plt = $wgParser->getPreloadText( $text, $title, $popts, 
$params );
 
-               return new WikitextContent( $plt );
+               return new static( $plt );
        }
 
        /**
@@ -246,7 +246,7 @@
                        '[[' . $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 5ae3e25..c1db1de 100644
--- a/includes/content/WikitextContentHandler.php
+++ b/includes/content/WikitextContentHandler.php
@@ -34,19 +34,8 @@
                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 @@
                        $redirectText .= "\n" . $text;
                }
 
-               return new WikitextContent( $redirectText );
+               $class = $this->getContentClass();
+               return new $class( $redirectText );
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/154447
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I70f1a3291aec3460120ec20121a23f4cb68e04d1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to