use exceptions, some misc. PHP5 cleanup
authorMichael J. Rubinsky <mrubinsk@horde.org>
Wed, 19 May 2010 19:23:09 +0000 (15:23 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Wed, 19 May 2010 19:23:09 +0000 (15:23 -0400)
ansel/group.php
ansel/lib/Faces.php
ansel/lib/GalleryMode/Date.php
ansel/lib/GalleryMode/Normal.php
ansel/lib/Storage.php
ansel/perms.php
ansel/rss.php

index 20e9c2c..2ba047f 100644 (file)
@@ -43,19 +43,21 @@ case 'category':
     break;
 
 case 'owner':
-    $num_groups = $ansel_storage->shares->countOwners(Horde_Perms::SHOW, null,
-                                                      false);
-    if (is_a($num_groups, 'PEAR_Error')) {
-        $notification->push($num_groups);
+    try {
+        if ($num_groups = $ansel_storage->shares->countOwners(Horde_Perms::SHOW, null, false)) {
+
+            $groups = $ansel_storage->shares->listOwners(Horde_Perms::SHOW,
+                                                         null,
+                                                         false,
+                                                         $gbpage * $groups_perpage,
+                                                         $groups_perpage);
+        } else {
+            $groups = array();
+        }
+    } catch (Horde_Share_Exception $e) {
+        $notification->push($e->getMessage());
         $num_groups = 0;
         $groups = array();
-    } elseif ($num_groups) {
-        $groups = $ansel_storage->shares->listOwners(Horde_Perms::SHOW, null,
-                                                     false,
-                                                     $gbpage * $groups_perpage,
-                                                     $groups_perpage);
-    } else {
-        $groups = array();
     }
     break;
 
index 786805e..5e7a8dc 100644 (file)
@@ -33,6 +33,8 @@ class Ansel_Faces
     /**
      * Delete faces from VFS and DB storage.
      *
+     * @TODO: Move SQL queries to Ansel_Storage::
+     * 
      * @param Ansel_Image $image Image object to delete faces for
      * @param integer $face  Face id
      * @static
index ac86800..4254107 100644 (file)
@@ -453,7 +453,7 @@ class Ansel_GalleryMode_Date {
         }
 
         /* Bulk update the images to their new gallery_id */
-        $result = $this->_gallery->_shareOb->_write_db->exec('UPDATE ansel_images SET gallery_id = ' . $gallery->id . ' WHERE image_id IN (' . implode(',', $ids) . ')');
+        $result = $this->_gallery->_shareOb->getWriteDb()->exec('UPDATE ansel_images SET gallery_id = ' . $gallery->id . ' WHERE image_id IN (' . implode(',', $ids) . ')');
         if (is_a($result, 'PEAR_Error')) {
             return $result;
         }
@@ -526,10 +526,10 @@ class Ansel_GalleryMode_Date {
         } catch (VFS_Exception $e) {}
 
         /* Delete from SQL. */
-        $this->_gallery->_shareOb->_write_db->exec('DELETE FROM ansel_images WHERE image_id = ' . (int)$image->id);
+        $this->_gallery->_shareOb->getWriteDb()->exec('DELETE FROM ansel_images WHERE image_id = ' . (int)$image->id);
 
         /* Remove any attributes */
-        $this->_gallery->_shareOb->_write_db->exec('DELETE FROM ansel_image_attributes WHERE image_id = ' . (int)$image->id);
+        $this->_gallery->_shareOb->getWriteDb()->exec('DELETE FROM ansel_image_attributes WHERE image_id = ' . (int)$image->id);
 
         if (!$isStack) {
             $this->_gallery->updateImageCount(1, false, $image_gallery);
index b3f5aa2..3af6b6d 100644 (file)
@@ -191,7 +191,7 @@ class Ansel_GalleryMode_Normal {
             }
         }
 
-        $result = $this->_gallery->_shareOb->_write_db->exec('UPDATE ansel_images SET gallery_id = ' . $gallery->id . ' WHERE image_id IN (' . implode(',', $ids) . ')');
+        $result = $this->_gallery->_shareOb->getWriteDb()->exec('UPDATE ansel_images SET gallery_id = ' . $gallery->id . ' WHERE image_id IN (' . implode(',', $ids) . ')');
         if ($result instanceof PEAR_Error) {
             throw new Ansel_Exception($result->getMessage());
         }
@@ -244,10 +244,10 @@ class Ansel_GalleryMode_Normal {
         } catch (VFS_Exception $e) {}
 
         /* Delete from SQL. */
-        $this->_gallery->_shareOb->_write_db->exec('DELETE FROM ansel_images WHERE image_id = ' . (int)$image->id);
+        $this->_gallery->_shareOb->getWriteDb()->exec('DELETE FROM ansel_images WHERE image_id = ' . (int)$image->id);
 
         /* Remove any attributes */
-        $this->_gallery->_shareOb->_write_db->exec('DELETE FROM ansel_image_attributes WHERE image_id = ' . (int)$image->id);
+        $this->_gallery->_shareOb->getWriteDb()->exec('DELETE FROM ansel_image_attributes WHERE image_id = ' . (int)$image->id);
 
         if (!$isStack) {
             $this->_gallery->updateImageCount(1, false);
index 76a7bda..df1c4df 100644 (file)
@@ -138,10 +138,11 @@ class Ansel_Storage
         }
 
         /* Create the gallery */
-        $gallery = $this->_shares->newShare('');
-        if ($gallery instanceof PEAR_Error) {
-            Horde::logMessage($gallery, 'ERR');
-            throw new Horde_Exception($gallery->getMessage());
+        try {
+            $gallery = $this->_shares->newShare('');
+        } catch (Horde_Share_Exception $e) {
+            Horde::logMessage($e->getMessage, 'ERR');
+            throw new Ansel_Exception($e);
         }
         Horde::logMessage('New Ansel_Gallery object instantiated', 'DEBUG');
 
@@ -164,10 +165,11 @@ class Ansel_Storage
         }
 
         /* Save it to storage */
-        $result = $this->_shares->addShare($gallery);
-        if ($result instanceof PEAR_Error) {
+        try {
+            $result = $this->_shares->addShare($gallery);
+        } catch (Horde_Share_Exception $e) {
             $error = sprintf(_("The gallery \"%s\" could not be created: %s"),
-                             $attributes['name'], $result->getMessage());
+                             $attributes['name'], $e->getMessage());
             Horde::logMessage($error, 'ERR');
             throw new Ansel_Exception($error);
         }
@@ -326,11 +328,12 @@ class Ansel_Storage
                return $this->_galleries[$gallery_id];
        }
 
-       $result = &$this->_shares->getShareById($gallery_id);
-       if ($result instanceof PEAR_Error) {
-           throw new Ansel_Exception($result);
+       try {
+           $result = $this->_shares->getShareById($gallery_id);
+       } catch (Horde_Share_Exception $e) {
+           throw new Ansel_Exception($e);
        }
-       $this->_galleries[$gallery_id] = &$result;
+       $this->_galleries[$gallery_id] = $result;
 
        // Don't cache if we have overridden anything
        if (!count($overrides)) {
@@ -439,9 +442,10 @@ class Ansel_Storage
         $id = $gallery->id;
 
         /* Delete the gallery from storage */
-        $result = $this->_shares->removeShare($gallery);
-        if ($result instanceof PEAR_Error) {
-            throw new Ansel_Exception($result);
+        try {
+            $this->_shares->removeShare($gallery);
+        } catch (Horde_Share_Exception $e) {
+            throw new Ansel_Exception($e);
         }
 
         /* Expire the cache */
@@ -876,11 +880,11 @@ class Ansel_Storage
             return $counts[$key];
         }
 
-        $count = $this->_shares->countShares($userid, $perm, $attributes,
-                                            $parent, $allLevels);
-
-        if ($count instanceof PEAR_Error) {
-            throw new Ansel_Exception($count);
+        try {
+            $count = $this->_shares->countShares($userid, $perm, $attributes,
+                                                 $parent, $allLevels);
+        } catch (Horde_Share_Exception $e) {
+            throw new Ansel_Exception($e);
         }
 
         $counts[$key] = $count;
@@ -920,12 +924,13 @@ class Ansel_Storage
                            $sort_by = null,
                            $direction = 0)
     {
-        $shares = $this->_shares->listShares(Horde_Auth::getAuth(), $perm, $attributes,
-                                             $from, $count, $sort_by, $direction,
-                                             $parent, $allLevels);
+        try {
+            $shares = $this->_shares->listShares(Horde_Auth::getAuth(), $perm, $attributes,
+                                                 $from, $count, $sort_by, $direction,
+                                                 $parent, $allLevels);
 
-        if ($shares instanceof PEAR_Error) {
-            throw new Ansel_Exception($shares);
+        } catch (Horde_Share_Exception $e) {
+            throw new Ansel_Exception($e);
         }
 
         return $shares;
index d8a8093..774d1c7 100644 (file)
@@ -28,37 +28,42 @@ $reload = false;
 $actionID = Horde_Util::getFormData('actionID', 'edit');
 switch ($actionID) {
 case 'edit':
-    $share = &$ansel_storage->getGallery(Horde_Util::getFormData('cid'));
-    if (!is_a($share, 'PEAR_Error')) {
+    try {
+        $share = $ansel_storage->getGallery(Horde_Util::getFormData('cid'));
         $form = 'edit.inc';
         $perm = &$share->getPermission();
-    } elseif (($share_name = Horde_Util::getFormData('share')) !== null) {
-        $share = &$ansel_storage->shares->getShare($share_name);
-        if (!is_a($share, 'PEAR_Error')) {
-            $form = 'edit.inc';
-            $perm = &$share->getPermission();
+    } catch (Horde_Share_Exception $e) {
+        if (($share_name = Horde_Util::getFormData('share')) !== null) {
+            try {
+                $share = $ansel_storage->shares->getShare($share_name);
+                $form = 'edit.inc';
+                $perm = $share->getPermission();
+            } catch (Horde_Share_Exception $e) {
+                $notification->push($e->getMessage(), 'horde.error');
+            }
         }
     }
 
-    if (is_a($share, 'PEAR_Error')) {
-        $notification->push($share, 'horde.error');
-    } elseif (!Horde_Auth::getAuth() ||
-              (isset($share) && Horde_Auth::getAuth() != $share->get('owner'))) {
+    if (!Horde_Auth::getAuth() ||
+        (isset($share) && Horde_Auth::getAuth() != $share->get('owner'))) {
         exit('permission denied');
     }
     break;
 
 case 'editform':
 case 'editforminherit':
-    $share = &$ansel_storage->getGallery(Horde_Util::getFormData('cid'));
-    if (is_a($share, 'PEAR_Error')) {
+    try {
+        $share = &$ansel_storage->getGallery(Horde_Util::getFormData('cid'));
+    } catch (Horde_Share_Exception $e) {
         $notification->push(_("Attempt to edit a non-existent share."), 'horde.error');
-    } else {
+    }
+
+    if (!empty($share)) {
         if (!Horde_Auth::getAuth() ||
             Horde_Auth::getAuth() != $share->get('owner')) {
             exit('permission denied');
         }
-        $perm = &$share->getPermission();
+        $perm = $share->getPermission();
 
         // Process owner and owner permissions.
         $old_owner = $share->get('owner');
@@ -244,11 +249,10 @@ case 'editforminherit':
     break;
 }
 
-if (is_a($share, 'PEAR_Error')) {
+if (empty($share)) {
     $title = _("Edit Permissions");
 } else {
-    $children = $GLOBALS['ansel_storage']->listGalleries(Horde_Perms::READ, false,
-                                                         $share);
+    $children = $GLOBALS['ansel_storage']->listGalleries(Horde_Perms::READ, false, $share);
     $title = sprintf(_("Edit Permissions for %s"), $share->get('name'));
 }
 
@@ -261,7 +265,7 @@ if ($auth->hasCapability('list')) {
 
 $groupList = $groups->listGroups();
 asort($groupList);
-if (is_a($groupList, 'PEAR_Error')) {
+if ($groupList instanceof PEAR_Error) {
     Horde::logMessage($groupList, 'NOTICE');
     $groupList = array();
 }
index 9f0bf2c..8ddff72 100644 (file)
@@ -40,31 +40,33 @@ if (empty($rss)) {
     // Determine what we are requesting
     switch ($stream_type) {
     case 'all':
-        $images = $ansel_storage->getRecentImages();
-        if (is_a($images, 'PEAR_Error') || !count($images)) {
+        try {
+            $images = $ansel_storage->getRecentImages();
+        } catch (Ansel_Exception $e) {
             $images = array();
-        } else {
-            // Eventually would like the link to link to a search
-            // results page containing the same images present in this
-            // feed. For now, just link to the List view until some of
-            // the search code can be refactored.
-            $params = array('last_modified' => $images[0]->uploaded,
-                            'name' => sprintf(_("Recently added photos on %s"),
-                                              $conf['server']['name']),
-                            'link' => Ansel::getUrlFor('view',
-                                                       array('view' => 'List'),
-                                                       true),
-                            'desc' => sprintf(_("Recently added photos on %s"),
-                                              $conf['server']['name']),
-                            'image_url' => Ansel::getImageUrl($images[0]->id,
-                                                              'thumb', true),
-                            'image_alt' => $images[0]->caption,
-                            'image_link' => Ansel::getUrlFor(
-                                'view', array('image' => $images[0]->id,
-                                              'view' => 'Image',
-                                              'gallery' => $images[0]->gallery),
-                                true));
         }
+
+        // Eventually would like the link to link to a search
+        // results page containing the same images present in this
+        // feed. For now, just link to the List view until some of
+        // the search code can be refactored.
+        $params = array('last_modified' => $images[0]->uploaded,
+                        'name' => sprintf(_("Recently added photos on %s"),
+                                          $conf['server']['name']),
+                        'link' => Ansel::getUrlFor('view',
+                                                   array('view' => 'List'),
+                                                   true),
+                        'desc' => sprintf(_("Recently added photos on %s"),
+                                          $conf['server']['name']),
+                        'image_url' => Ansel::getImageUrl($images[0]->id,
+                                                          'thumb', true),
+                        'image_alt' => $images[0]->caption,
+                        'image_link' => Ansel::getUrlFor(
+                            'view', array('image' => $images[0]->id,
+                                          'view' => 'Image',
+                                          'gallery' => $images[0]->gallery),
+                            true));
+
         break;
 
     case 'gallery':
@@ -75,9 +77,9 @@ if (empty($rss)) {
         } elseif (is_numeric($id)) {
             $gallery = $ansel_storage->getGallery($id);
         }
-        if (!is_a($gallery, 'PEAR_Error') &&
-            $gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::SHOW) &&
+        if ($gallery->hasPermission(Horde_Auth::getAuth(), Horde_Perms::SHOW) &&
             !$gallery->hasPasswd() && $gallery->isOldEnough()) {
+
             if (!$gallery->countImages() && $gallery->hasSubGalleries()) {
                 $subgalleries = $ansel_storage->listGalleries(Horde_Perms::SHOW,
                                                               null,
@@ -94,7 +96,7 @@ if (empty($rss)) {
             }
         }
 
-        if (!isset($images) || is_a($images, 'PEAR_Error') || !count($images)) {
+        if (!count($images)) {
             $images = array();
         } else {
             $style = $gallery->getStyle();
@@ -103,9 +105,6 @@ if (empty($rss)) {
                                               'gallery' => $id),
                                         true);
             $img = &$ansel_storage->getImage($gallery->getDefaultImage('ansel_default'));
-            if (is_a($img, 'PEAR_Error')) {
-                break;
-            }
             $params = array('last_modified' => $gallery->get('last_modified'),
                             'name' => sprintf(_("%s on %s"),
                                               $gallery->get('name'),
@@ -124,19 +123,25 @@ if (empty($rss)) {
         break;
 
     case 'user':
-        $shares = $ansel_storage->listGalleries(Horde_Perms::SHOW, $id);
-        if (!is_a($shares, 'PEAR_Error')) {
-            $galleries = array();
+        $galleries = array();
+        try {
+            $shares = $ansel_storage->listGalleries(Horde_Perms::SHOW, $id);
             foreach ($shares as $gallery) {
                 if ($gallery->isOldEnough() && !$gallery->hasPasswd()) {
                     $galleries[] = $gallery->id;
                 }
             }
+        } catch (Horde_Share_Exception $e) {
+            Horde::logMessage($e->getMessage(), 'ERR');
         }
         $images = array();
         if (isset($galleries) && count($galleries)) {
-            $images = $ansel_storage->getRecentImages($galleries);
-            if (!is_a($images, 'PEAR_Error') && count($images)) {
+            try {
+                $images = $ansel_storage->getRecentImages($galleries);
+            } catch (Ansel_Exception $e) {
+                 Horde::logMessage($e->getMessage(), 'ERR');
+            }
+            if (count($images)) {
                 $owner = $injector->getInstance('Horde_Prefs_Identity')->getIdentity($id);
                 $name = $owner->getValue('fullname');
                 $author = $owner->getValue('from_addr');
@@ -170,8 +175,13 @@ if (empty($rss)) {
         $tag_id = array_values(Ansel_Tags::getTagIds(array($id)));
         $images = Ansel_Tags::searchTagsById($tag_id, 10, 0, 'images');
         $tag_id = array_pop($tag_id);
-        $images = $ansel_storage->getImages(array('ids' => $images['images']));
-        if (!is_a($images, 'PEAR_Error') && count($images)) {
+        try {
+            $images = $ansel_storage->getImages(array('ids' => $images['images']));
+        } catch (Ansel_Exception $e) {
+             Horde::logMessage($e->getMessage(), 'ERR');
+             $images = array();
+        }
+        if (count($images)) {
             $tag_id = $tag_id[0];
             $images = array_values($images);
             $params = array('last_modified' => $images[0]->uploaded,
@@ -202,10 +212,9 @@ if (empty($rss)) {
         for ($i = 0; $i < $cnt; ++$i) {
             $gallery_id = $images[$i]->gallery;
             if (empty($galleries[$gallery_id])) {
-                $galleries[$gallery_id]['gallery'] = $GLOBALS['ansel_storage']->getGallery($gallery_id);
-                if (is_a($galleries[$gallery_id]['gallery'], 'PEAR_Error')) {
-                    continue;
-                }
+                try {
+                    $galleries[$gallery_id]['gallery'] = $GLOBALS['ansel_storage']->getGallery($gallery_id);
+                } catch (Ansel_Exception $e) {}
             }
             if (!isset($galleries[$gallery_id]['perm'])) {
                 $galleries[$gallery_id]['perm'] =