/**
  * Load user metadata given a path and username
  *
  * @param string $path Path to album directory
  * @param string $username Username to import
  * @return array GalleryStatus a status code,
  *               array User metadata
  */
 function getUserFieldsByUsername($path, $username)
 {
     list($ret, $uids) = Gallery1DataParser::getUserUids($path);
     if ($ret) {
         return array($ret, null);
     }
     $usernames = array_flip($uids);
     $uid = $usernames[$username];
     list($ret, $fields) = Gallery1DataParser::getUserFieldsByUid($path, $uid);
     if ($ret) {
         return array($ret, null);
     }
     return array(null, $fields);
 }
Example #2
0
 /**
  * Import a single photo or movie.
  */
 static function import_item(&$queue)
 {
     $messages = array();
     if (count($queue) == 0) {
         $messages[] = t('Empty item queue');
         return $messages;
     }
     $item_id = array_shift($queue);
     g1_import::debug(t('Now importing item %item', array('item' => $item_id)));
     // Item names come in as FolderX/ItemX
     $pos = strrpos($item_id, '/');
     if ($pos === false) {
         return $messages;
     }
     // Get ItemX into g1_item
     $g1_item = substr($item_id, $pos + 1);
     // Get FolderX into g1_item
     $g1_album = substr($item_id, 0, $pos);
     if (self::map($g1_album, $g1_item, 'item')) {
         return $messages;
     }
     $album_id = self::map($g1_album, '', 'album');
     if (!$album_id) {
         $messages[] = t('Album %name not found', array('name' => $g1_album));
         return $messages;
     }
     $album_item = null;
     $albumDir = self::$album_dir;
     if (substr($albumDir, -1) != DIRECTORY_SEPARATOR) {
         $albumDir .= DIRECTORY_SEPARATOR;
     }
     require_once 'Gallery1DataParser.php';
     list($result, $items) = Gallery1DataParser::getPhotos($albumDir . $g1_album . DIRECTORY_SEPARATOR);
     if ($result == null) {
         foreach ($items as $object) {
             if (isset($object->image) && is_a($object->image, 'G1Img') && isset($object->image->name) && $object->image->name == $g1_item) {
                 $album_item = $object;
             }
         }
     }
     if ($album_item == null) {
         $messages[] = t('Failed to import Gallery 1 item: %item', array('item' => $item_id));
         return $messages;
     }
     $corrupt = 0;
     self::$current_g1_item = array($item_id => $album_item);
     $g1_path = $albumDir . $g1_album . DIRECTORY_SEPARATOR . $album_item->image->name . '.' . $album_item->image->type;
     $parent = ORM::factory('item', $album_id);
     switch ($album_item->image->type) {
         case 'jpg':
         case 'jpeg':
         case 'gif':
         case 'png':
             $g1_type = 'GalleryPhotoItem';
             break;
         case 'wmv':
         case '3gp':
         case 'avi':
         case 'mp4':
         case 'mov':
         case 'flv':
             $g1_type = 'GalleryMovieItem';
             break;
         default:
             $g1_type = 'GalleryPhotoItem';
             break;
     }
     if (!file_exists($g1_path)) {
         // If the Gallery 1 source image isn't available, this operation is going to fail.  That can
         // happen in cases where there's corruption in the source Gallery 1.  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 1 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.
         g1_import::log(t('%path missing in import; replacing it with a placeholder', array('path' => $g1_path)));
         $g1_path = MODPATH . 'g1_import/data/broken-image.gif';
         $g1_type = 'GalleryPhotoItem';
         $corrupt = 1;
     }
     $item = null;
     switch ($g1_type) {
         case 'GalleryPhotoItem':
             if (function_exists('mime_content_type')) {
                 $type = mime_content_type($g1_path);
             } else {
                 $type = self::get_mime_type($g1_path);
             }
             if ($type != '' && !in_array($type, array('image/jpeg', 'image/gif', 'image/png'))) {
                 Kohana_Log::add('alert', "{$g1_path} is an unsupported image type {$type}; using a placeholder gif");
                 $messages[] = t("'%path' is an unsupported image type '%type', using a placeholder", array('path' => $g1_path, 'type' => $type));
                 $g1_path = MODPATH . 'g1_import/data/broken-image.gif';
                 $corrupt = 1;
             }
             try {
                 $item = ORM::factory('item');
                 $item->type = 'photo';
                 $item->parent_id = $album_id;
                 $item->set_data_file($g1_path);
                 $item->name = $g1_item . '.' . $album_item->image->type;
                 $item->slug = item::convert_filename_to_slug($g1_item);
                 $item->mime_type = $type;
                 $item->title = utf8_encode(self::_decode_html_special_chars(trim($album_item->caption)));
                 $item->title or $item->title = ' ';
                 //don't use $item->name as this clutters up the UI
                 if (isset($album_item->description) && $album_item->description != '') {
                     $item->description = utf8_encode(self::_decode_html_special_chars(trim($album_item->description)));
                 }
                 //$item->owner_id = self::map($g1_item->getOwnerId());
                 try {
                     $item->view_count = (int) $album_item->clicks;
                 } catch (Exception $e) {
                     $item->view_count = 1;
                 }
                 if (strlen($item->title) > 255) {
                     if (strlen($item->description) == 0) {
                         $item->description = $item->title;
                     }
                     $item->title = substr($item->title, 0, 252) . '...';
                 }
             } catch (Exception $e) {
                 $exception_info = (string) new G1_Import_Exception(t("Corrupt image '%path'", array('path' => $g1_path)), $e, $messages);
                 Kohana_Log::add('alert', "Corrupt image {$g1_path}\n" . $exception_info);
                 $messages[] = $exception_info;
                 $corrupt = 1;
                 $item = null;
                 return $messages;
             }
             try {
                 $item->validate();
             } catch (ORM_Validation_Exception $e) {
                 $exception_info = (string) new G1_Import_Exception(t('Failed to validate Gallery 1 item %item.', array('item' => $item_id)), $e, $messages);
                 Kohana_Log::add('alert', "Failed to validate Gallery 1 item {$item_id}.\n" . $exception_info);
                 $messages[] = $exception_info;
                 $corrupt = 1;
                 $item = null;
                 return $messages;
             }
             try {
                 $item->save();
             } catch (Exception $e) {
                 $exception_info = (string) new G1_Import_Exception(t('Failed to import Gallery 1 item %item.', array('item' => $item_id)), $e, $messages);
                 Kohana_Log::add('alert', "Failed to import Gallery 1 item {$item_id}.\n" . $exception_info);
                 $messages[] = $exception_info;
                 $corrupt = 1;
                 $item = null;
             }
             break;
         case 'GalleryMovieItem':
             // @todo we should transcode other types into FLV
             if (function_exists('mime_content_type')) {
                 $type = mime_content_type($g1_path);
             } else {
                 $type = self::get_mime_type($g1_path);
             }
             if ($type == '' || in_array($type, array('video/mp4', 'video/x-flv'))) {
                 try {
                     $item = ORM::factory('item');
                     $item->type = 'movie';
                     $item->parent_id = $album_id;
                     $item->set_data_file($g1_path);
                     $item->name = $g1_item . '.' . $album_item->image->type;
                     $item->slug = item::convert_filename_to_slug($g1_item);
                     $item->mime_type = $type;
                     $item->title = utf8_encode(self::_decode_html_special_chars(trim($album_item->caption)));
                     $item->title or $item->title = ' ';
                     //$item->name;
                     if (isset($album_item->description) && $album_item->description != '') {
                         $item->description = utf8_encode(self::_decode_html_special_chars(trim($album_item->description)));
                     }
                     //$item->owner_id = self::map($g1_item->getOwnerId());
                     try {
                         $item->view_count = (int) $album_item->clicks;
                     } catch (Exception $e) {
                         $item->view_count = 1;
                     }
                 } catch (Exception $e) {
                     $exception_info = (string) new G1_Import_Exception(t("Corrupt movie '%path'", array("path" => $g1_path)), $e, $messages);
                     Kohana_Log::add('alert', "Corrupt movie {$g1_path}\n" . $exception_info);
                     $messages[] = $exception_info;
                     $corrupt = 1;
                     $item = null;
                     return $messages;
                 }
                 try {
                     $item->validate();
                 } catch (ORM_Validation_Exception $e) {
                     $exception_info = (string) new G1_Import_Exception(t('Failed to validate Gallery 1 item %item.', array('item' => $item_id)), $e, $messages);
                     Kohana_Log::add('alert', "Failed to validate Gallery 1 item {$item_id}.\n" . $exception_info);
                     $messages[] = $exception_info;
                     $corrupt = 1;
                     $item = null;
                     return $messages;
                 }
                 try {
                     $item->save();
                 } catch (Exception $e) {
                     $exception_info = (string) new G1_Import_Exception(t('Failed to import Gallery 1 item %item.', array('item' => $item_id)), $e, $messages);
                     Kohana_Log::add('alert', "Failed to import Gallery 1 item {$item_id}.\n" . $exception_info);
                     $messages[] = $exception_info;
                     $corrupt = 1;
                     $item = null;
                 }
             } else {
                 Kohana_Log::add('alert', "{$g1_path} is an unsupported movie type {$type}");
                 $messages[] = t("'%path' is an unsupported movie type '%type'", array('path' => $g1_path, 'type' => $type));
                 $corrupt = 1;
             }
             break;
         default:
             // Ignore
             break;
     }
     if (isset($item)) {
         self::set_map($item->id, $g1_album, $g1_item, 'item');
         if (isset($album_item->keywords) && $album_item->keywords != '') {
             $keywords = utf8_encode(self::_decode_html_special_chars(trim($album_item->keywords)));
             if ($keywords != '') {
                 self::import_keywords_as_tags($keywords, $item);
             }
         }
     }
     if ($corrupt) {
         $title = utf8_encode(self::_decode_html_special_chars(trim($album_item->caption)));
         $title or $title = $g1_item;
         if (!empty($item)) {
             $messages[] = t('<a href="%g1_url">%title</a> from Gallery 1 could not be processed; (imported as <a href="%g3_url">%title</a>)', array('g1_url' => $gallery_url . '/' . $item_id, 'g3_url' => $item->url(), 'title' => $title));
         } else {
             $messages[] = t('<a href="%g1_url">%title</a> from Gallery 1 could not be processed', array('g1_url' => $gallery_url . '/' . $item_id, 'title' => $title));
         }
     }
     self::$current_g1_item = null;
     return $messages;
 }