Beispiel #1
0
 /**
  * Import a single photo or movie.
  */
 static function import_item(&$queue)
 {
     $g2_item_id = array_shift($queue);
     if (self::map($g2_item_id)) {
         return;
     }
     try {
         self::$current_g2_item = $g2_item = g2(GalleryCoreApi::loadEntitiesById($g2_item_id));
         $g2_path = g2($g2_item->fetchPath());
     } catch (Exception $e) {
         return t("Failed to import Gallery 2 item with id: %id\n%exception", array("id" => $g2_item_id, "exception" => (string) $e));
     }
     $parent = ORM::factory("item", self::map($g2_item->getParentId()));
     $g2_type = $g2_item->getEntityType();
     $corrupt = 0;
     if (!file_exists($g2_path)) {
         // If the Gallery 2 source image isn't available, this operation is going to fail.  That can
         // happen in cases where there's corruption in the source Gallery 2.  In that case, fall
         // back on using a broken image.  It's important that we import *something* otherwise
         // anything that refers to this item in Gallery 2 will have a dangling pointer in Gallery 3
         //
         // Note that this will change movies to be photos, if there's a broken movie.  Hopefully
         // this case is rare enough that we don't need to take any heroic action here.
         g2_import::log(t("%path missing in import; replacing it with a placeholder", array("path" => $g2_path)));
         $g2_path = MODPATH . "g2_import/data/broken-image.gif";
         $g2_type = "GalleryPhotoItem";
         $corrupt = 1;
     }
     $messages = array();
     switch ($g2_type) {
         case "GalleryPhotoItem":
             if (!in_array($g2_item->getMimeType(), array("image/jpeg", "image/gif", "image/png"))) {
                 Kohana_Log::add("alert", "{$g2_path} is an unsupported image type; using a placeholder gif");
                 $messages[] = t("'%path' is an unsupported image type, using a placeholder", array("path" => $g2_path));
                 $g2_path = MODPATH . "g2_import/data/broken-image.gif";
                 $corrupt = 1;
             }
             try {
                 $item = ORM::factory("item");
                 $item->type = "photo";
                 $item->parent_id = $parent->id;
                 $item->set_data_file($g2_path);
                 $item->name = $g2_item->getPathComponent();
                 $item->title = self::_decode_html_special_chars($g2_item->getTitle());
                 $item->title or $item->title = $item->name;
                 $item->description = self::_decode_html_special_chars(self::extract_description($g2_item));
                 $item->owner_id = self::map($g2_item->getOwnerId());
                 $item->save();
             } catch (Exception $e) {
                 $exception_info = (string) new G2_Import_Exception(t("Corrupt image '%path'", array("path" => $g2_path)), $e, $messages);
                 Kohana_Log::add("alert", "Corrupt image {$g2_path}\n" . $exception_info);
                 $messages[] = $exception_info;
                 $corrupt = 1;
                 $item = null;
             }
             break;
         case "GalleryMovieItem":
             // @todo we should transcode other types into FLV
             if (in_array($g2_item->getMimeType(), array("video/mp4", "video/x-flv"))) {
                 try {
                     $item = ORM::factory("item");
                     $item->parent_id = $parent->id;
                     $item->set_data_file($g2_path);
                     $item->name = $g2_item->getPathComponent();
                     $item->title = self::_decode_html_special_chars($g2_item->getTitle());
                     $item->title or $item->title = $item->name;
                     $item->description = self::_decode_html_special_chars(self::extract_description($g2_item));
                     $item->owner_id = self::map($g2_item->getOwnerId());
                     $item->save();
                 } catch (Exception $e) {
                     $exception_info = (string) new G2_Import_Exception(t("Corrupt movie '%path'", array("path" => $g2_path)), $e, $messages);
                     Kohana_Log::add("alert", "Corrupt movie {$g2_path}\n" . $exception_info);
                     $messages[] = $exception_info;
                     $corrupt = 1;
                     $item = null;
                 }
             } else {
                 Kohana_Log::add("alert", "{$g2_path} is an unsupported movie type");
                 $messages[] = t("'%path' is an unsupported movie type", array("path" => $g2_path));
                 $corrupt = 1;
             }
             break;
         default:
             // Ignore
             break;
     }
     if (!empty($item)) {
         self::import_keywords_as_tags($g2_item->getKeywords(), $item);
     }
     $g2_item_url = self::g2_url(array("view" => "core.ShowItem", "itemId" => $g2_item->getId()));
     if (isset($item)) {
         try {
             $item->view_count = (int) g2(GalleryCoreApi::fetchItemViewCount($g2_item_id));
         } catch (Exception $e) {
             $view_count = 1;
         }
         $item->save();
         self::set_map($g2_item_id, $item->id, "item", $g2_item_url);
         self::set_map($g2_item_id, $item->id, "file", self::g2_url(array("view" => "core.DownloadItem", "itemId" => $g2_item_id)));
         $derivatives = g2(GalleryCoreApi::fetchDerivativesByItemIds(array($g2_item_id)));
         if (!empty($derivatives[$g2_item_id])) {
             foreach ($derivatives[$g2_item_id] as $derivative) {
                 switch ($derivative->getDerivativeType()) {
                     case DERIVATIVE_TYPE_IMAGE_THUMBNAIL:
                         $resource_type = "thumbnail";
                         break;
                     case DERIVATIVE_TYPE_IMAGE_RESIZE:
                         $resource_type = "resize";
                         break;
                     case DERIVATIVE_TYPE_IMAGE_PREFERRED:
                         $resource_type = "full";
                         break;
                 }
                 self::set_map($derivative->getId(), $item->id, $resource_type, self::g2_url(array("view" => "core.DownloadItem", "itemId" => $derivative->getId())));
             }
         }
     }
     if ($corrupt) {
         if (!empty($item)) {
             $title = $g2_item->getTitle();
             $title or $title = $g2_item->getPathComponent();
             $messages[] = t("<a href=\"%g2_url\">%title</a> from Gallery 2 could not be processed; " . "(imported as <a href=\"%g3_url\">%title</a>)", array("g2_url" => $g2_item_url, "g3_url" => $item->url(), "title" => $title));
         } else {
             $messages[] = t("<a href=\"%g2_url\">%title</a> from Gallery 2 could not be processed", array("g2_url" => $g2_item_url, "title" => $g2_item->getTitle()));
         }
     }
     self::$current_g2_item = null;
     return $messages;
 }
Beispiel #2
0
 /**
  * Import a single photo or movie.
  */
 static function import_item(&$queue)
 {
     $g2_item_id = array_shift($queue);
     if (self::map($g2_item_id)) {
         return t("Item with id: %id already imported, skipping", array("id" => $g2_item_id));
     }
     try {
         self::$current_g2_item = $g2_item = g2(GalleryCoreApi::loadEntitiesById($g2_item_id));
         $g2_path = g2($g2_item->fetchPath());
     } catch (Exception $e) {
         return t("Failed to import Gallery 2 item with id: %id\n%exception", array("id" => $g2_item_id, "exception" => $e->__toString()));
     }
     $parent = ORM::factory("item", self::map($g2_item->getParentId()));
     $g2_type = $g2_item->getEntityType();
     $corrupt = 0;
     if (!file_exists($g2_path)) {
         // If the Gallery 2 source image isn't available, this operation is going to fail.  That can
         // happen in cases where there's corruption in the source Gallery 2.  In that case, fall
         // back on using a broken image.  It's important that we import *something* otherwise
         // anything that refers to this item in Gallery 2 will have a dangling pointer in Gallery 3
         //
         // Note that this will change movies to be photos, if there's a broken movie.  Hopefully
         // this case is rare enough that we don't need to take any heroic action here.
         g2_import::log(t("%path missing in import; replacing it with a placeholder", array("path" => $g2_path)));
         $g2_path = MODPATH . "g2_import/data/broken-image.gif";
         $g2_type = "GalleryPhotoItem";
         $corrupt = 1;
     }
     $message = array();
     switch ($g2_type) {
         case "GalleryPhotoItem":
             if (!in_array($g2_item->getMimeType(), array("image/jpeg", "image/gif", "image/png"))) {
                 Kohana::log("alert", "{$g2_path} is an unsupported image type; using a placeholder gif");
                 $message[] = t("'%path' is an unsupported image type, using a placeholder", array("path" => $g2_path));
                 $g2_path = MODPATH . "g2_import/data/broken-image.gif";
                 $corrupt = 1;
             }
             try {
                 $item = photo::create($parent, $g2_path, $g2_item->getPathComponent(), self::_decode_html_special_chars($g2_item->getTitle()), self::_decode_html_special_chars(self::extract_description($g2_item)), self::map($g2_item->getOwnerId()));
             } catch (Exception $e) {
                 Kohana::log("alert", "Corrupt image {$g2_path}\n" . $e->__toString());
                 $message[] = t("Corrupt image '%path'", array("path" => $g2_path));
                 $message[] = $e->__toString();
                 $corrupt = 1;
             }
             break;
         case "GalleryMovieItem":
             // @todo we should transcode other types into FLV
             if (in_array($g2_item->getMimeType(), array("video/mp4", "video/x-flv"))) {
                 try {
                     $item = movie::create($parent, $g2_path, $g2_item->getPathComponent(), self::_decode_html_special_chars($g2_item->getTitle()), self::_decode_html_special_chars(self::extract_description($g2_item)), self::map($g2_item->getOwnerId()));
                 } catch (Exception $e) {
                     Kohana::log("alert", "Corrupt movie {$g2_path}\n" . $e->__toString());
                     $message[] = t("Corrupt movie '%path'", array("path" => $g2_path));
                     $message[] = $e->__toString();
                     $corrupt = 1;
                 }
             } else {
                 Kohana::log("alert", "{$g2_path} is an unsupported movie type");
                 $message[] = t("'%path' is an unsupported movie type", array("path" => $g2_path));
                 $corrupt = 1;
             }
             break;
         default:
             // Ignore
             break;
     }
     if (!empty($item)) {
         self::import_keywords_as_tags($g2_item->getKeywords(), $item);
     }
     if (isset($item)) {
         self::set_map($g2_item_id, $item->id);
         $item->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_item_id));
         $item->save();
     }
     if ($corrupt) {
         $url_generator = $GLOBALS["gallery"]->getUrlGenerator();
         // @todo we need a more persistent warning
         $g2_item_url = $url_generator->generateUrl(array("itemId" => $g2_item->getId()));
         // Why oh why did I ever approve the session id placeholder idea in G2?
         $g2_item_url = str_replace('&amp;g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT', '', $g2_item_url);
         if (!empty($item)) {
             $message[] = t("<a href=\"%g2_url\">%title</a> from Gallery 2 could not be processed; " . "(imported as <a href=\"%g3_url\">%title</a>)", array("g2_url" => $g2_item_url, "g3_url" => $item->url(), "title" => $g2_item->getTitle()));
         } else {
             $message[] = t("<a href=\"%g2_url\">%title</a> from Gallery 2 could not be processed", array("g2_url" => $g2_item_url, "title" => $g2_item->getTitle()));
         }
     }
     self::$current_g2_item = null;
     return $message;
 }
Beispiel #3
0
 /**
  * Import a single album.
  */
 static function import_album(&$queue)
 {
     // The queue is a set of nested associative arrays where the key is the album id and the
     // value is an array of similar arrays.  We'll do a breadth first tree traversal using the
     // queue to keep our state.  Doing it breadth first means that the parent will be created by
     // the time we get to the child.
     // Dequeue the current album and enqueue its children
     list($g2_album_id, $children) = each($queue);
     unset($queue[$g2_album_id]);
     foreach ($children as $key => $value) {
         $queue[$key] = $value;
     }
     if (self::map($g2_album_id)) {
         return;
     }
     try {
         // Load the G2 album item, and figure out its parent in G3.
         $g2_album = g2(GalleryCoreApi::loadEntitiesById($g2_album_id));
     } catch (Exception $e) {
         g2_import::log(t("Failed to import Gallery 2 album with id: %id", array("id" => $g2_album_id)));
         return;
     }
     if ($g2_album->getParentId() == null) {
         return;
     }
     $parent_album = ORM::factory("item", self::map($g2_album->getParentId()));
     $album = album::create($parent_album, $g2_album->getPathComponent(), $g2_album->getTitle(), self::extract_description($g2_album), self::map($g2_album->getOwnerId()));
     $album->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_album_id));
     $album->created = $g2_album->getCreationTimestamp();
     $order_map = array("originationTimestamp" => "captured", "creationTimestamp" => "created", "description" => "description", "modificationTimestamp" => "updated", "orderWeight" => "weight", "pathComponent" => "name", "summary" => "description", "title" => "title", "viewCount" => "view_count");
     $direction_map = array(ORDER_ASCENDING => "asc", ORDER_DESCENDING => "desc");
     if (array_key_exists($g2_order = $g2_album->getOrderBy(), $order_map)) {
         $album->sort_column = $order_map[$g2_order];
         $album->sort_order = $direction_map[$g2_album->getOrderDirection()];
     }
     $album->save();
     self::import_keywords_as_tags($g2_album->getKeywords(), $album);
     self::set_map($g2_album_id, $album->id);
     // @todo import album highlights
 }