From a7280e63689f0a7b2c5360126cf55b75bf8c46f5 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Sat, 31 Jul 2010 16:42:38 -0400 Subject: [PATCH] Use Content_Tagger::getSimilarObjects for the related images widget. --- ansel/lib/Tagger.php | 39 +++++++++++++++++++++++++++- ansel/lib/Widget/SimilarPhotos.php | 52 ++++++++++++++------------------------ kronolith/lib/Tagger.php | 2 +- 3 files changed, 58 insertions(+), 35 deletions(-) diff --git a/ansel/lib/Tagger.php b/ansel/lib/Tagger.php index e679e84dd..f9592d923 100644 --- a/ansel/lib/Tagger.php +++ b/ansel/lib/Tagger.php @@ -275,7 +275,7 @@ class Ansel_Tagger * * @param array $tags An array of either tag names or ids. * @param integer $limit Limit results to this many. - * + * * @return array An array of hashes, tag_id, tag_name, and count. * @throws Ansel_Exception */ @@ -346,4 +346,41 @@ class Ansel_Tagger return $results; } + /** + * List image ids of images related (via similar tags) to the specified + * image + * + * @param Ansel_Image $image The image to get related images for. + * @param bolean $ownerOnly Only return images owned by the specified + * image's owner. + * + * @return array An array of 'image' and 'rank' keys.. + */ + public function listRelatedImages(Ansel_Image $image, $ownerOnly = true) + { + $args = array('typeId' => 'image', 'limit' => 1); + if ($ownerOnly) { + $gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope()->getGallery($image->gallery); + $args['userId'] = $gallery->get('owner'); + } + + try { + $ids = $GLOBALS['injector']->getInstance('Content_Tagger')->getSimilarObjects(array('object' => (string)$image->id, 'type' => 'image'), $args); + } catch (Content_Exception $e) { + throw new Ansel_Exception($e); + } + + try { + $images = $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope()->getImages(array('ids' => array_keys($ids))); + } catch (Horde_Exception_NotFound $e) { + $images = array(); + } + $results = array(); + foreach ($images as $key => $image) { + $results[] = array('image' => $image, 'rank' => $ids[$key]); + } + + return $results; + } + } diff --git a/ansel/lib/Widget/SimilarPhotos.php b/ansel/lib/Widget/SimilarPhotos.php index 8cdad5d64..6349e00c4 100755 --- a/ansel/lib/Widget/SimilarPhotos.php +++ b/ansel/lib/Widget/SimilarPhotos.php @@ -44,9 +44,6 @@ class Ansel_Widget_SimilarPhotos extends Ansel_Widget_Base /** * Helper function for generating a widget of images related to this one. * - * @TODO Rethink the way we determine if an image is related. This one is - * not ideal, as it just pops tags off the tag list until all the tags - * match. This could miss many related images. Maybe make this random? * * @return string The HTML */ @@ -55,40 +52,29 @@ class Ansel_Widget_SimilarPhotos extends Ansel_Widget_Base $ansel_storage = $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope(); $html = ''; - $tags = array_values($this->_view->resource->getTags()); - $imgs = $GLOBALS['injector']->getInstance('Ansel_Tagger')->search($tags); - while (count($imgs['images']) <= 5 && count($tags)) { - array_pop($tags); - $newImgs =$GLOBALS['injector']->getInstance('Ansel_Tagger')->search($tags); - $imgs['images'] = array_merge($imgs['images'], $newImgs['images']); - } - if (count($imgs['images'])) { - $i = 0; - foreach ($imgs['images'] as $imgId) { - if ($i >= min(count($imgs['images']), 5)) { - break; - } - if ($imgId != $this->_view->resource->id) { - try { - $rImg = $ansel_storage->getImage($imgId); - $rGal = $ansel_storage->getGallery($rImg->gallery); - } catch (Ansel_Exception $e) { - continue; - } + $args = array('typeId' => 'image', + 'userId' => $this->_view->gallery->get('owner')); - $html .= Ansel::getUrlFor( - 'view', - array('image' => $imgId, - 'view' => 'Image', - 'gallery' => $rImg->gallery, - 'slug' => $rGal->get('slug')), - true)->link(array('title' => sprintf(_("%s from %s"), $rImg->filename, $rGal->get('name')))) - . '' . htmlspecialchars($rImg->filename) . ''; - $i++; - } + $results = $GLOBALS['injector']->getInstance('Ansel_Tagger')->listRelatedImages($this->_view->resource); + if (count($results)) { + $i = 0; + foreach ($results as $result) { + $img = $result['image']; + $rGal = $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope()->getGallery($img->gallery); + if ($rGal->hasPermission()) + $html .= Ansel::getUrlFor( + 'view', + array('image' => $img->id, + 'view' => 'Image', + 'gallery' => $img->gallery, + 'slug' => $rGal->get('slug')), + true)->link(array('title' => sprintf(_("%s from %s"), $img->filename, $rGal->get('name')))) + . '' . htmlspecialchars($img->filename) . ''; + $i++; } } return $html; } + } diff --git a/kronolith/lib/Tagger.php b/kronolith/lib/Tagger.php index dc92edf68..ebe1074f2 100644 --- a/kronolith/lib/Tagger.php +++ b/kronolith/lib/Tagger.php @@ -212,7 +212,7 @@ class Kronolith_Tagger $args['typeId'] = $this->_type_ids['event']; $event_results = $GLOBALS['injector']->getInstance('Content_Tagger')->getObjects($args); } - + $results = array('calendars' => array_values($cal_results), 'events' => (!empty($args['calendarId']) && count($event_results)) ? Kronolith::getDriver()->filterEventsByCalendar(array_values($event_results), $args['calendarId']) -- 2.11.0