Legoktm has uploaded a new change for review.

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

Change subject: CachedGadgetRepo: Store Content objects internally instead of 
JSON blobs
......................................................................

CachedGadgetRepo: Store Content objects internally instead of JSON blobs

Allows for more straightforward parsing and accessing of internal data.

Change-Id: Id5e747ad8103b3ea81e889fb4d1573bcd7e0bf10
---
M backend/CachedGadgetRepo.php
M backend/ForeignAPIGadgetRepo.php
M backend/LocalGadgetRepo.php
M content/GadgetDefinitionContent.php
4 files changed, 27 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Gadgets 
refs/changes/31/160431/1

diff --git a/backend/CachedGadgetRepo.php b/backend/CachedGadgetRepo.php
index a0661b1..7657544 100644
--- a/backend/CachedGadgetRepo.php
+++ b/backend/CachedGadgetRepo.php
@@ -11,7 +11,7 @@
         * use $missCache instead. Values may be null, in which case only the 
gadget's
         * existence is cached and the data must still be retrieved from memc 
or the DB.
         *
-        * array( id => null|array( 'json' => JSON string, 'timestamp' => 
timestamp ) )
+        * array( id => null|array( 'content' => GadgetDefinitionContent 
object, 'timestamp' => timestamp ) )
         */
        protected $data = array();
 
@@ -44,14 +44,14 @@
 
        /**
         * Load the full data for all gadgets.
-        * @return array ( id => array( 'json' => JSON string, 'timestamp' => 
timestamp ) )
+        * @return array ( id => array( 'content' => GadgetDefinitionContent 
object, 'timestamp' => timestamp ) )
         */
        abstract protected function loadAllData();
 
        /**
         * Load the full data for one gadget.
         * @param string $id Gadget ID
-        * @return array( 'json' => JSON string, 'timestamp' => timestamp ) or 
empty array if the gadget doesn't exist.
+        * @return array( 'content' => GadgetDefinitionContent object, 
'timestamp' => timestamp ) or empty array if the gadget doesn't exist.
         */
        abstract protected function loadDataFor( $id );
 
@@ -76,7 +76,7 @@
         * Update the cache to account for the fact that a gadget has been
         * added, modified or deleted.
         * @param string $id Gadget ID
-        * @param array $data array( 'json' => JSON string, 'timestamp' => 
timestamp ) if added or modified, or null if deleted
+        * @param array $data array( 'content' => GadgetDefinitionContent 
object, 'timestamp' => timestamp ) if added or modified, or null if deleted
         */
        protected function updateCache( $id, $data ) {
                $toCache = $data;
@@ -119,9 +119,10 @@
                        return null;
                }
                $gadget = new Gadget( $id, $this );
-               $json = wfObjectToArray( FormatJson::decode( $data['json'] ) );
-               $gadget->setSettings( $json['settings'] );
-               $gadget->setModuleData( $json['module'] );
+               /** @var GadgetDefinitionContent $content */
+               $content = $data['content'];
+               $gadget->setSettings( $content->getSettings() );
+               $gadget->setModuleData( $content->getModuleData() );
                $gadget->setTimestamp( $data['timestamp'] );
 
                return $gadget;
@@ -173,7 +174,7 @@
        /**
         * Populate a given Gadget's data in $this->data . Tries memc first, 
then falls back to loadDataFor()
         * @param string $id Gadget ID
-        * @return array array( 'json' => JSON string, 'timestamp' => timestamp 
) or empty array if the gadget doesn't exist.
+        * @return array array( 'content' => GadgetDefinitionContent object, 
'timestamp' => timestamp ) or empty array if the gadget doesn't exist.
         */
        private function maybeLoadDataFor( $id ) {
                if ( isset( $this->data[$id] ) && is_array( $this->data[$id] ) 
) {
diff --git a/backend/ForeignAPIGadgetRepo.php b/backend/ForeignAPIGadgetRepo.php
index 1b0da2c..0a45798 100644
--- a/backend/ForeignAPIGadgetRepo.php
+++ b/backend/ForeignAPIGadgetRepo.php
@@ -101,7 +101,7 @@
        /**
         * Reformat a response from the API into the format that loadAllData()
         * is supposed to return.
-        * @param $apiResponse array
+        * @param array $apiResult
         * @return array of gadgets keyed by ID.
         */
        protected function reformatAPIResult( $apiResult ) {
@@ -116,7 +116,7 @@
                $retval = array();
                foreach ( $gadgets as $gadget ) {
                        $retval[$gadget['id']] = array(
-                               'json' => FormatJson::encode( 
$gadget['metadata'] ),
+                               'content' => 
GadgetDefinitionContent::newFromMetadata( $gadget['metadata'] ),
                                'timestamp' => $gadget['definitiontimestamp']
                        );
                }
diff --git a/backend/LocalGadgetRepo.php b/backend/LocalGadgetRepo.php
index 572a337..cb21e0e 100644
--- a/backend/LocalGadgetRepo.php
+++ b/backend/LocalGadgetRepo.php
@@ -54,7 +54,10 @@
 
                $data = array();
                foreach ( $res as $row ) {
-                       $data[$row->gd_id] = array( 'json' => $row->gd_blob, 
'timestamp' => $row->gd_timestamp );
+                       $data[$row->gd_id] = array(
+                               'json' => new GadgetDefinitionContent( 
$row->gd_blob ),
+                               'timestamp' => $row->gd_timestamp
+                       );
                }
                return $data;
        }
@@ -69,7 +72,10 @@
                        // Gadget doesn't exist
                        return array();
                } else {
-                       return array( 'json' => $row->gd_blob, 'timestamp' => 
$row->gd_timestamp );
+                       return array(
+                               'content' => new GadgetDefinitionContent( 
$row->gd_blob ),
+                               'timestamp' => $row->gd_timestamp
+                       );
                }
        }
 
diff --git a/content/GadgetDefinitionContent.php 
b/content/GadgetDefinitionContent.php
index 76a5496..3943fc9 100644
--- a/content/GadgetDefinitionContent.php
+++ b/content/GadgetDefinitionContent.php
@@ -36,6 +36,14 @@
                return $status->isOK();
        }
 
+       /**
+        * @param array $metadata Usually from a remote API request
+        * @return GadgetDefinitionContent
+        */
+       public static function newFromMetadata( $metadata ) {
+               return new GadgetDefinitionContent( FormatJson::encode( 
$metadata ) );
+       }
+
        public function getSettings() {
                $json = $this->getJsonData();
                return isset( $json['settings'] ) ? $json['settings'] : array();

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id5e747ad8103b3ea81e889fb4d1573bcd7e0bf10
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Gadgets
Gerrit-Branch: RL2
Gerrit-Owner: Legoktm <[email protected]>

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

Reply via email to