jenkins-bot has submitted this change and it was merged.

Change subject: Store compressed JSON since size is limited
......................................................................


Store compressed JSON since size is limited

We only have 65535 bytes, let's use them wisely.

Prior to Ib2db241a in core (1.22wmf12) even binary page properties
were sometimes shown as plaintext (e.g. on Special:PagesWithProp).
Thus this introduces a "soft dependency" on that change.

No backwards-incompatible changes were made in public methods.
Added two new ones:

* Add TemplateDataBlob#getJSONForDatabase which returns compressed
  JSON, use it where applicable.
* Add TemplateDataBlob::newFromDatabase which accept compressed and
  uncompressed JSON, use it where applicable.

Use a long pseudorandom string in the test.

Bug: 51740
Change-Id: Ie66b0dd6b6dab6f8648e78595c41e52d9c704d57
---
M TemplateData.hooks.php
M TemplateDataBlob.php
M api/ApiTemplateData.php
M tests/TemplateDataBlobTest.php
4 files changed, 52 insertions(+), 6 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  Jforrester: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/TemplateData.hooks.php b/TemplateData.hooks.php
index c21b5e5..ccfc87b 100644
--- a/TemplateData.hooks.php
+++ b/TemplateData.hooks.php
@@ -85,7 +85,7 @@
                        return '<div class="error">' . $status->getHtml() . 
'</div>';
                }
 
-               $parser->getOutput()->setProperty( 'templatedata', 
$ti->getJSON() );
+               $parser->getOutput()->setProperty( 'templatedata', 
$ti->getJSONForDatabase() );
 
                $parser->getOutput()->addModules( 'ext.templateData' );
 
diff --git a/TemplateDataBlob.php b/TemplateDataBlob.php
index cec94ef..9270c32 100644
--- a/TemplateDataBlob.php
+++ b/TemplateDataBlob.php
@@ -26,11 +26,16 @@
        private $status;
 
        /**
-        *  @param string $json
+        * Parse and validate passed JSON and create a TemplateDataBlob object.
+        * Accepts and handles user-provided data.
+        *
+        * @param string $json
+        * @throws MWException
         * @return TemplateInfo
         */
        public static function newFromJSON( $json ) {
                $tdb = new self( json_decode( $json ) );
+
                $status = $tdb->parse();
 
                if ( !$status->isOK() ) {
@@ -39,6 +44,20 @@
                }
                $tdb->status = $status;
                return $tdb;
+       }
+
+       /**
+        * Parse and validate passed JSON (possibly gzip-compressed) and create 
a TemplateDataBlob object.
+        *
+        * @param string $json
+        * @return TemplateInfo
+        */
+       public static function newFromDatabase( $json ) {
+               // Handle GZIP compression. \037\213 is the header for GZIP 
files.
+               if ( substr( $json, 0, 2 ) === "\037\213" ) {
+                       $json = gzdecode( $json );
+               }
+               return self::newFromJSON( $json );
        }
 
        /**
@@ -308,7 +327,7 @@
                        }
                }
 
-               $length = strlen( $this->getJSON() );
+               $length = strlen( $this->getJSONForDatabase() );
                if ( $length > self::MAX_LENGTH ) {
                        return Status::newFatal( 'templatedata-invalid-length', 
$length, self::MAX_LENGTH );
                }
@@ -346,6 +365,13 @@
                return json_encode( $this->data );
        }
 
+       /**
+        * @return string JSON, gzip-compressed
+        */
+       public function getJSONForDatabase() {
+               return gzencode( $this->getJSON() );
+       }
+
        public function getHtml( Language $lang ) {
                global $wgContLang;
                $langCode = $wgContLang->getCode();
diff --git a/api/ApiTemplateData.php b/api/ApiTemplateData.php
index bf6b138..dc3b5a1 100644
--- a/api/ApiTemplateData.php
+++ b/api/ApiTemplateData.php
@@ -80,7 +80,7 @@
 
                foreach ( $res as $row ) {
                        $rawData = $row->pp_value;
-                       $tdb = TemplateDataBlob::newFromJSON( $rawData );
+                       $tdb = TemplateDataBlob::newFromDatabase( $rawData );
                        $status = $tdb->getStatus();
                        if ( !$status->isOK() ) {
                                $this->dieUsage(
diff --git a/tests/TemplateDataBlobTest.php b/tests/TemplateDataBlobTest.php
index 10a2498..712d84a 100644
--- a/tests/TemplateDataBlobTest.php
+++ b/tests/TemplateDataBlobTest.php
@@ -10,6 +10,25 @@
                ) );
        }
 
+       /**
+        * Helper method to generate a string that gzip can't compress.
+        *
+        * Output is consistent when given the same seed.
+        */
+       private static function generatePseudorandomString( $length, $seed ) {
+               mt_srand( $seed );
+
+               $characters = 
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+               $characters_max = strlen( $characters ) - 1;
+
+               $string = '';
+
+               for ( $i = 0; $i < $length; $i++ ) {
+                       $string .= $characters[mt_rand( 0, $characters_max )];
+               }
+
+               return $string;
+       }
        public static function provideParse() {
                $cases = array(
                        array(
@@ -305,11 +324,12 @@
                                'status' => true
                        ),
                        array(
+                               // Should be long enough to trigger this 
condition after gzipping.
                                'input' => '{
-                                       "description": "' . str_repeat( 'X', 
65535 ) . '",
+                                       "description": "' . 
self::generatePseudorandomString( 100000, 42 ) . '",
                                        "params": {}
                                }',
-                               'status' => 'Data too large to save (65,582 
bytes, limit is 65,535)'
+                               'status' => 'Data too large to save (75,195 
bytes, limit is 65,535)'
                        ),
                );
                $calls = array();

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie66b0dd6b6dab6f8648e78595c41e52d9c704d57
Gerrit-PatchSet: 16
Gerrit-Project: mediawiki/extensions/TemplateData
Gerrit-Branch: master
Gerrit-Owner: Matmarex <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Matmarex <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to