diff options
author | Bharat Mediratta <bharat@menalto.com> | 2010-12-16 20:37:00 -0800 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2010-12-16 20:37:00 -0800 |
commit | 7e31f97b4cbc5cf1894611de1e9de7a3efc6ad50 (patch) | |
tree | 87f8f95bf5cf8277f3ae2ba8ee153eaafe90832e /modules/gallery/models | |
parent | 53a2652fd6ba652b1b6604f8a4930403376a3ef5 (diff) |
Improve Item_Model::as_restful_array() to take an array of fields so
that we only return the fields we care about. This improves
performance when retrieving large numbers of items. Fixes #1536.
Diffstat (limited to 'modules/gallery/models')
-rw-r--r-- | modules/gallery/models/item.php | 82 |
1 files changed, 61 insertions, 21 deletions
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index ad577066..fc5c3ff9 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -987,48 +987,88 @@ class Item_Model_Core extends ORM_MPTT { /** * Same as ORM::as_array() but convert id fields into their RESTful form. + * + * @param array if specified, only return the named fields */ - public function as_restful_array() { + public function as_restful_array($fields=array()) { + if ($fields) { + $data = array(); + foreach ($fields as $field) { + if (isset($this->object[$field])) { + $data[$field] = $this->__get($field); + } + } + $fields = array_flip($fields); + } else { + $data = $this->as_array(); + } + // Convert item ids to rest URLs for consistency - $data = $this->as_array(); - if ($tmp = $this->parent()) { - $data["parent"] = rest::url("item", $tmp); + if (empty($fields) || isset($fields["parent"])) { + if ($tmp = $this->parent()) { + $data["parent"] = rest::url("item", $tmp); + } + unset($data["parent_id"]); } - unset($data["parent_id"]); - if ($tmp = $this->album_cover()) { - $data["album_cover"] = rest::url("item", $tmp); + + if (empty($fields) || isset($fields["album_cover"])) { + if ($tmp = $this->album_cover()) { + $data["album_cover"] = rest::url("item", $tmp); + } + unset($data["album_cover_item_id"]); } - unset($data["album_cover_item_id"]); - $data["web_url"] = $this->abs_url(); + if (empty($fields) || isset($fields["web_url"])) { + $data["web_url"] = $this->abs_url(); + } if (!$this->is_album()) { if (access::can("view_full", $this)) { - $data["file_url"] = rest::url("data", $this, "full"); - $data["file_size"] = filesize($this->file_path()); - } - if (access::user_can(identity::guest(), "view_full", $this)) { - $data["file_url_public"] = $this->file_url(true); + if (empty($fields) || isset($fields["file_url"])) { + $data["file_url"] = rest::url("data", $this, "full"); + } + if (empty($fields) || isset($fields["file_size"])) { + $data["file_size"] = filesize($this->file_path()); + } + if (access::user_can(identity::guest(), "view_full", $this)) { + if (empty($fields) || isset($fields["file_url_public"])) { + $data["file_url_public"] = $this->file_url(true); + } + } } } if ($this->is_photo()) { - $data["resize_url"] = rest::url("data", $this, "resize"); - $data["resize_size"] = filesize($this->resize_path()); + if (empty($fields) || isset($fields["resize_url"])) { + $data["resize_url"] = rest::url("data", $this, "resize"); + } + if (empty($fields) || isset($fields["resize_size"])) { + $data["resize_size"] = filesize($this->resize_path()); + } if (access::user_can(identity::guest(), "view", $this)) { - $data["resize_url_public"] = $this->resize_url(true); + if (empty($fields) || isset($fields["resize_url_public"])) { + $data["resize_url_public"] = $this->resize_url(true); + } } } if ($this->has_thumb()) { - $data["thumb_url"] = rest::url("data", $this, "thumb"); - $data["thumb_size"] = filesize($this->thumb_path()); + if (empty($fields) || isset($fields["thumb_url"])) { + $data["thumb_url"] = rest::url("data", $this, "thumb"); + } + if (empty($fields) || isset($fields["thumb_size"])) { + $data["thumb_size"] = filesize($this->thumb_path()); + } if (access::user_can(identity::guest(), "view", $this)) { - $data["thumb_url_public"] = $this->thumb_url(true); + if (empty($fields) || isset($fields["thumb_url_public"])) { + $data["thumb_url_public"] = $this->thumb_url(true); + } } } - $data["can_edit"] = access::can("edit", $this); + if (empty($fields) || isset($fields["can_edit"])) { + $data["can_edit"] = access::can("edit", $this); + } // Elide some internal-only data that is going to cause confusion in the client. foreach (array("relative_path_cache", "relative_url_cache", "left_ptr", "right_ptr", |