Tommy 碎碎念

Tommy Wu's blog

« 上一篇 | 下一篇 »

修改 Gallery 3 的 RSS, 新增用路徑取代 ID 的選擇方式與資料亂數選擇的方式
post by tommy @ 15 十二月, 2011 14:17

當我們利用 minislideshow 之類的小東西在顯示某個相簿的照片時, 是直接利用 RSS 的方式來取得要顯示的照片. 不過, 在 Gallery 3 裡頭, 對於 RSS 中指定要某個相簿的時候, 是使用相簿的 ID 來選取, 利如, 我有個相簿放在 /var/albums/tommy/tommy_photo 這個目錄下, 我必需要知道這個相簿在 Gallery 資料庫中的 ID (例如是 6), 才能用 /rss/feed/gallery/album/6 的方式來取得資料. 這個對於一般的使用者來說, 要知道那個 ID 是多少, 是比較有難度的, 並不如 tommy/tommy_photo 來的直覺. 所以... 改一下程式允許用路徑來選擇, 會比較方便些.

這個修正, 讓我們改用參數的方式來傳送路徑取代項目, 如 /rss/feed/gallery/album/6 改成 /rss/feed/gallery/album/path?path=tommy/tommy_photo 來使用:

--- gallery3.orig/modules/gallery/helpers/gallery_rss.php  2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/gallery/helpers/gallery_rss.php 2011-12-15 14:14:47.838235321 +0800
@@ -45,6 +45,17 @@
return $feed;
 
case "album":
+ // twu2 begin
+ // use path instead if id
+ if ($id == 'path') {
+ $my_item = item::find_by_path(Input::instance()->get("path"));
+ $id = $my_item->id;
+ if ($id < 1)
+ $id = 1;
+ }
+ else if (!is_numeric($id) || $id < 1)
+ $id = 1;
+ // twu2 end
$item = ORM::factory("item", $id);
access::required("view", $item);

另外, 由於每次 minislideshow 只會取得最多 20 張圖片的資料, 雖然 minislideshow 自己有 shuffle 的功能, 不過, 每次 RSS 取得的都是同樣的那 20 張圖片, 再怎麼亂數播放, 都覺得是固定的那 20 張圖.

所以, 我們會希望這個亂數選擇的動作在 RSS 那邊就直接處理, 這樣子至少每次去 RSS 抓的資料會有所不同.

--- gallery3.orig/modules/gallery/helpers/gallery_rss.php  2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/gallery/helpers/gallery_rss.php 2011-12-12 14:27:17.507144041 +0800
@@ -46,13 +46,39 @@
 
case "album":
$item = ORM::factory("item", $id);
+ if (!$item->is_album())
+ $item = $item->parent;
access::required("view", $item);
 
- $feed->items = $item
- ->viewable()
- ->descendants($limit, $offset, array(array("type", "=", "photo")));
- $feed->max_pages = ceil(
- $item->viewable()->descendants_count(array(array("type", "=", "photo"))) / $limit);
+ if (Input::instance()->get("random") == "1") {
+ $where = array(
+ array("rand_key", "<", random::percent()),
+ array("type", "<>", "album")
+ );
+ $order["rand_key"] = "DESC";
+ $feed->items = $item
+ ->viewable()
+ ->descendants($limit, 0, $where, $order);
+ $feed->max_pages = 1;
+ $cnt = $item->viewable()->descendants_count($where);
+ if ($cnt < $limit) {
+ $item = ORM::factory("item", $id);
+ if (!$item->is_album())
+ $item = $item->parent;
+ $feed->items = $item
+ ->viewable()
+ ->descendants($limit, 0, array(array("type", "<>", "album")), $order);
+ $feed->max_pages = ceil(
+ $item->viewable()->descendants_count(array(array("type", "<>", "album"))) / $limit);
+ }
+ }
+ else {
+ $feed->items = $item
+ ->viewable()
+ ->descendants($limit, $offset, array(array("type", "=", "photo")));
+ $feed->max_pages = ceil(
+ $item->viewable()->descendants_count(array(array("type", "=", "photo"))) / $limit);
+ }
if ($item->id == item::root()->id) {
$feed->title = html::purify($item->title);
} else {

使用時, 只要加上 random=1 這個變數就可以. 例如: /rss/feed/gallery/album/path?path=tommy/tommy_photo&random=1

這樣子取到的圖片會變比較多樣化一些.

Del.icio.us Furl HEMiDEMi Technorati MyShare
迴響
暱稱:
標題:
個人網頁:
電子郵件:
authimage

迴響

  

Bad Behavior 已經阻擋了 97 個過去 7 天試圖闖關的垃圾迴響與引用。
Power by LifeType. Template design by JamesHuang. Valid XHTML and CSS