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

Change subject: Split the Collection model and add JSON serialization
......................................................................


Split the Collection model and add JSON serialization

And also add JSON serialization on the models.

On the list of collections page it will use CollectionInfo which is the base
model plus a count attribute.

On the details page of a collection it will use the full Collection, which has
the base plus the items of the collection fully expanded.

This should work but is part of a bigger patch for creating the storage for the
index file. Check previous and next patches also.

Change-Id: Ia36e9d61d771241f55aa8202f7a9dcf6f8dd3f64
---
M Gather.php
M extension.json
M includes/models/Collection.php
A includes/models/CollectionBase.php
A includes/models/CollectionInfo.php
M includes/models/CollectionItem.php
6 files changed, 229 insertions(+), 135 deletions(-)

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



diff --git a/Gather.php b/Gather.php
index 70b2c01..f8f02d6 100644
--- a/Gather.php
+++ b/Gather.php
@@ -43,6 +43,8 @@
        'Gather\Hooks' => 'Gather.hooks',
 
        'Gather\models\CollectionItem' => 'models/CollectionItem',
+       'Gather\models\CollectionBase' => 'models/CollectionBase',
+       'Gather\models\CollectionInfo' => 'models/CollectionInfo',
        'Gather\models\Collection' => 'models/Collection',
        'Gather\models\WithImage' => 'models/WithImage',
 
diff --git a/extension.json b/extension.json
index da9426b..13648fe 100644
--- a/extension.json
+++ b/extension.json
@@ -26,6 +26,8 @@
        "AutoloadClasses": {
                "Gather\\Hooks": "includes/Gather.hooks.php",
                "Gather\\models\\CollectionItem": 
"includes/models/CollectionItem.php",
+               "Gather\\models\\CollectionBase": 
"includes/models/CollectionBase.php",
+               "Gather\\models\\CollectionInfo": 
"includes/models/CollectionInfo.php",
                "Gather\\models\\Collection": "includes/models/Collection.php",
                "Gather\\models\\WithImage": "includes/models/WithImage.php",
                "Gather\\stores\\Collection": "includes/stores/Collection.php",
diff --git a/includes/models/Collection.php b/includes/models/Collection.php
index 01eddc7..e9234d6 100644
--- a/includes/models/Collection.php
+++ b/includes/models/Collection.php
@@ -6,16 +6,13 @@
 
 namespace Gather\models;
 
-use \User;
-use \Title;
 use \IteratorAggregate;
 use \ArrayIterator;
-use \SpecialPage;
 
 /**
- * A collection of items, which are represented by the CollectionItem class.
+ * A collection with a list of items, which are represented by the 
CollectionItem class.
  */
-class Collection implements IteratorAggregate, WithImage {
+class Collection extends CollectionBase implements IteratorAggregate {
 
        /**
         * The internal collection of items.
@@ -23,63 +20,6 @@
         * @var CollectionItem[]
         */
        protected $items = array();
-
-       /**
-        * The internal id of a collection
-        *
-        * @var int id
-        */
-       protected $id;
-
-       /**
-        * Owner of collection
-        * @var User
-        */
-       protected $owner;
-
-       /**
-        * @var string
-        */
-       protected $title;
-
-       /**
-        * @var string
-        */
-       protected $description;
-
-       /**
-        * Whether collection is public or private
-        * Collection by default is true
-        *
-        * @var bool
-        */
-       protected $public;
-
-       /**
-        * Image that represents the collection.
-        *
-        * @var File
-        */
-       protected $image;
-
-       /**
-        * @param int $id id of the collection. Null if not persisted yet
-        * @param User $user User that owns the collection
-        * @param string $title Title of the collection
-        * @param string $description Description of the collection
-        * @param File $image Main image of the collection
-        * @param boolean $public Whether the collection is public or private
-        */
-       public function __construct( $id = null, User $user, $title = '', 
$description = '',
-               $image = null, $public = true ) {
-
-               $this->id = $id;
-               $this->owner = $user;
-               $this->title = $title;
-               $this->description = $description;
-               $this->image = $image;
-               $this->public = $public;
-       }
 
        /**
         * Adds a item to the collection.
@@ -111,49 +51,10 @@
        }
 
        /**
-        * @return User
+        * @return array list of items
         */
-       public function getOwner() {
-               return $this->owner;
-       }
-
-       /**
-        * @return string
-        */
-       public function getTitle() {
-               return $this->title;
-       }
-
-       /**
-        * @return string
-        */
-       public function getDescription() {
-               return $this->description;
-       }
-
-       /**
-        * Returns if the list is public
-        *
-        * @return boolean
-        */
-       public function isPublic() {
-               return $this->public;
-       }
-
-       /**
-        * Set if the list is public
-        *
-        * @param boolean $public
-        */
-       public function setPublic( $public ) {
-               $this->public = $public;
-       }
-
-       /**
-        * @return int id The internal id of a collection
-        */
-       public function getId() {
-               return $this->id;
+       public function getItems() {
+               return $this->items;
        }
 
        /**
@@ -166,37 +67,14 @@
        }
 
        /**
-        * Return local url for collection
-        * Example: /wiki/Special:Gather/user/id
-        *
-        * @return string localized url for collection
+        * Serialise to JSON
         */
-       public function getUrl() {
-               return SpecialPage::getTitleFor( 'Gather' )
-                       ->getSubpage( $this->getOwner() )
-                       ->getSubpage( $this->getId() )
-                       ->getLocalURL();
+       public function toJSON() {
+               $data = parent::toJSON();
+               $data['items'] = array();
+               foreach ( $this as $item ) {
+                       $data['items'][] = $item->toJSON();
+               }
+               return $data;
        }
-
-       /**
-        * @return array list of items
-        */
-       public function getItems() {
-               return $this->items;
-       }
-
-       /**
-        * @inheritdoc
-        */
-       public function hasImage() {
-               return $this->image ? true : false;
-       }
-
-       /**
-        * @inheritdoc
-        */
-       public function getFile() {
-               return $this->image;
-       }
-
 }
diff --git a/includes/models/CollectionBase.php 
b/includes/models/CollectionBase.php
new file mode 100644
index 0000000..4d62372
--- /dev/null
+++ b/includes/models/CollectionBase.php
@@ -0,0 +1,156 @@
+<?php
+
+/**
+ * CollectionBase.php
+ */
+
+namespace Gather\models;
+
+use \User;
+use \Title;
+use \SpecialPage;
+
+/**
+ * Base model for a Collection.
+ */
+abstract class CollectionBase implements WithImage {
+
+       /**
+        * The internal id of a collection
+        *
+        * @var int id
+        */
+       protected $id;
+
+       /**
+        * Owner of collection
+        * @var User
+        */
+       protected $owner;
+
+       /**
+        * @var string
+        */
+       protected $title;
+
+       /**
+        * @var string
+        */
+       protected $description;
+
+       /**
+        * Whether collection is public or private
+        * Collection by default is true
+        *
+        * @var bool
+        */
+       protected $public;
+
+       /**
+        * @param int $id id of the collection. Null if not persisted yet
+        * @param User $user User that owns the collection
+        * @param string $title Title of the collection
+        * @param string $description Description of the collection
+        * @param boolean $public Whether the collection is public or private
+        */
+       public function __construct( $id = null, User $user, $title = '', 
$description = '', $public = true ) {
+               $this->id = $id;
+               $this->owner = $user;
+               $this->title = $title;
+               $this->description = $description;
+               $this->public = $public;
+       }
+
+       /**
+        * @return int id The internal id of a collection
+        */
+       public function getId() {
+               return $this->id;
+       }
+
+       /**
+        * @return User
+        */
+       public function getOwner() {
+               return $this->owner;
+       }
+
+       /**
+        * @return string
+        */
+       public function getTitle() {
+               return $this->title;
+       }
+
+       /**
+        * @return string
+        */
+       public function getDescription() {
+               return $this->description;
+       }
+
+       /**
+        * Returns if the list is public
+        *
+        * @return boolean
+        */
+       public function isPublic() {
+               return $this->public;
+       }
+
+       /**
+        * Image that represents the collection.
+        *
+        * @var File
+        */
+       protected $image;
+
+       /**
+        * Returns items count
+        *
+        * @return int count of items in collection
+        */
+       abstract public function getCount();
+
+       /**
+        * Return local url for collection
+        * Example: /wiki/Special:Gather/user/id
+        *
+        * @return string localized url for collection
+        */
+       public function getUrl() {
+               return SpecialPage::getTitleFor( 'Gather' )
+                       ->getSubpage( $this->getOwner() )
+                       ->getSubpage( $this->getId() )
+                       ->getLocalURL();
+       }
+
+       /**
+        * Serialise to JSON
+        */
+       public function toJSON() {
+               $data = array(
+                       'id' => $this->id,
+                       'owner' => $this->owner->getName(),
+                       'title' => $this->title,
+                       'description' => $this->description,
+                       'public' => $this->public,
+               );
+               return $data;
+       }
+
+       /**
+        * @inheritdoc
+        */
+       public function hasImage() {
+               return $this->image ? true : false;
+       }
+
+       /**
+        * @inheritdoc
+        */
+       public function getFile() {
+               return $this->image;
+       }
+
+}
diff --git a/includes/models/CollectionInfo.php 
b/includes/models/CollectionInfo.php
new file mode 100644
index 0000000..f469c8c
--- /dev/null
+++ b/includes/models/CollectionInfo.php
@@ -0,0 +1,45 @@
+<?php
+
+/**
+ * CollectionInfo.php
+ */
+
+namespace Gather\models;
+
+/**
+ * The info of collection of items.
+ *
+ * Extends the base class with a count you can set.
+ */
+class CollectionInfo extends CollectionBase {
+       /** @var int $count of items in the collection */
+       protected $count;
+
+       /**
+        * Returns items count
+        * @param int $count of items in collection
+        */
+       public function setCount( $count ) {
+               $this->count = $count;
+       }
+
+       /**
+        * Returns items count
+        *
+        * @return int count of items in collection
+        */
+       public function getCount() {
+               return $this->count;
+       }
+
+       /**
+        * Serialise to JSON
+        */
+       public function toJSON() {
+               $data = parent::toJSON();
+               $data['count'] = $this->count;
+               return $data;
+       }
+
+
+}
diff --git a/includes/models/CollectionItem.php 
b/includes/models/CollectionItem.php
index 62b05df..193005c 100644
--- a/includes/models/CollectionItem.php
+++ b/includes/models/CollectionItem.php
@@ -76,4 +76,15 @@
        public function getFile() {
                return $this->file;
        }
+
+       /**
+        * Serialise to JSON
+        */
+       public function toJSON() {
+               return array(
+                       'title' => $this->title,
+                       'extract' => $this->extract,
+               );
+       }
+
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia36e9d61d771241f55aa8202f7a9dcf6f8dd3f64
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Gather
Gerrit-Branch: master
Gerrit-Owner: Jhernandez <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Robmoen <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to