public function go()
 {
     $dir_iterator = new RecursiveDirectoryIterator($this->dir);
     $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
     $count = 0;
     foreach ($iterator as $sfile) {
         if ($sfile->isFile()) {
             $file = $sfile->getPathname();
             if (preg_match('/(^|\\/)\\./', $file)) {
                 # Silently ignore all dot folders to avoid trouble with svn and friends
                 $this->giveFeedback("info", "Ignoring dotfile", $file);
                 continue;
             }
             $pathinfo = pathinfo($file);
             if ($pathinfo['filename'] === 'Thumbs.db') {
                 continue;
             }
             $info = aImageConverter::getInfo($file);
             if ($info === false) {
                 $this->giveFeedback("warning", "Not supported or corrupt", $file);
                 continue;
             }
             $item = new aMediaItem();
             if ($info['format'] === 'pdf') {
                 $item->type = 'pdf';
             } else {
                 $item->type = 'image';
             }
             // Split it up to make tags out of the portion of the path that isn't dir (i.e. the folder structure they used)
             $dir = $this->dir;
             $dir = preg_replace('/\\/$/', '', $dir) . '/';
             $relevant = preg_replace('/^' . preg_quote($dir, '/') . '/', '', $file);
             // TODO: not Microsoft-friendly, might matter in some setting
             $components = preg_split('/\\//', $relevant);
             $tags = array_slice($components, 0, count($components) - 1);
             foreach ($tags as &$tag) {
                 // We don't strictly need to be this harsh, but it's safe and definitely
                 // takes care of some things we definitely can't allow, like periods
                 // (which cause mod_rewrite problems with pretty Symfony URLs).
                 // TODO: clean it up in a nicer way without being UTF8-clueless
                 // (aTools::slugify is UTF8-safe)
                 $tag = aTools::slugify($tag);
             }
             $item->title = aTools::slugify($pathinfo['filename']);
             $item->setTags($tags);
             if (!strlen($item->title)) {
                 $this->giveFeedback("error", "Files must have a basename", $file);
                 continue;
             }
             // The preSaveImage / save / saveImage dance is necessary because
             // the sluggable behavior doesn't kick in until save and the image file
             // needs a slug based filename.
             if (!$item->preSaveImage($file)) {
                 $this->giveFeedback("error", "Save failed", $file);
                 continue;
             }
             $item->save();
             if (!$item->saveImage($file)) {
                 $this->giveFeedback("error", "Save failed", $file);
                 $item->delete();
                 continue;
             }
             unlink($file);
             $count++;
             $this->giveFeedback("completed", $count, $file);
         }
     }
     $this->giveFeedback("total", $count);
 }
Ejemplo n.º 2
0
 /**
  * @param $areaInfos array
  * Iterates through areas, area versions and area version slots to locate the actual
  * current list of slots for each area name and arrange them in a convenient associative
  * array by culture, area name and permid. Used to implement getArea() and other methods
  * efficiently. Can also accept an array of areaInfo arrays obtained via array hydration,
  * in which case we take care of hydrating the slot objects here and skip hydrating
  * the intervening objects
  *
  * YOU MUST fetch the page with an appropriate query method such as retrieveBySlug(),
  * retrieveById, queryWithSlot or queryWithSlots, otherwise you will NOT get the 
  * right slots in the right order in the right culture for the right version. Especially
  * if your client is in the office for a demo
  */
 public function populateSlotCache($areaInfos = null)
 {
     // If we cheated on the hydration process then we'll be passed an
     // array of areaInfos explicitly and we must hydrate the slots. Otherwise
     // we have a perfectly normal Doctrine object on our hands
     if (is_null($areaInfos)) {
         $areaInfos = $this->Areas;
     }
     if ($this->slotCache === false) {
         $this->slotCache = array();
         // We have $this->Areas courtesy of whatever query
         // fetched the page in the first place
         foreach ($areaInfos as $areaInfo) {
             $areaVersionInfo = $areaInfo['AreaVersions'][0];
             foreach ($areaVersionInfo['AreaVersionSlots'] as $areaVersionSlotInfo) {
                 if (isset($areaVersionSlotInfo['Slot'])) {
                     $slotInfo = $areaVersionSlotInfo['Slot'];
                     if (is_object($slotInfo)) {
                         // Make sure there actually is a slot already for this version, don't let a surprise in the db crash us
                         if ($slotInfo->isNew()) {
                             continue;
                         }
                         $slot = $slotInfo;
                     } else {
                         $slotClass = $slotInfo['type'] . 'Slot';
                         $slot = new $slotClass();
                         $mediaItemInfos = $slotInfo['MediaItems'];
                         unset($slotInfo['MediaItems']);
                         $slot->hydrate($slotInfo);
                         $slot->setClean();
                         foreach ($mediaItemInfos as $mediaItemInfo) {
                             $mediaItem = new aMediaItem();
                             $mediaItem->hydrate($mediaItemInfo);
                             $slot->MediaItems[] = $mediaItem;
                         }
                     }
                     $this->slotCache[$areaInfo['culture']][$areaInfo['name']][$areaVersionSlotInfo['permid']] = $slot;
                 }
                 // foreach ($slot->MediaItems as $mediaItem)
                 // {
                 //   echo($mediaItem->id . ',');
                 // }
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * DOCUMENT ME
  */
 public function go()
 {
     $dir_iterator = new RecursiveDirectoryIterator($this->dir);
     $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
     $count = 0;
     $mimeTypes = aMediaTools::getOption('mime_types');
     // It comes back as a mapping of extensions to types, get the types
     $extensions = array_keys($mimeTypes);
     $mimeTypes = array_values($mimeTypes);
     foreach ($iterator as $sfile) {
         if ($sfile->isFile()) {
             $file = $sfile->getPathname();
             if (preg_match('/(^|\\/)\\./', $file)) {
                 # Silently ignore all dot folders to avoid trouble with svn and friends
                 $this->giveFeedback("info", "Ignoring dotfile", $file);
                 continue;
             }
             $pathinfo = pathinfo($file);
             // basename and filename seem backwards to me, but that's how it is in the PHP docs and
             // sure enough that's how it behaves
             if ($pathinfo['basename'] === 'Thumbs.db') {
                 continue;
             }
             $vfp = new aValidatorFilePersistent(array('mime_types' => $mimeTypes, 'validated_file_class' => 'aValidatedFile', 'required' => false), array('mime_types' => 'The following file types are accepted: ' . implode(', ', $extensions)));
             $guid = aGuid::generate();
             try {
                 $vf = $vfp->clean(array('newfile' => array('tmp_name' => $file, 'name' => $pathinfo['basename']), 'persistid' => $guid));
             } catch (Exception $e) {
                 $this->giveFeedback("warning", "Not supported or corrupt", $file);
                 continue;
             }
             $item = new aMediaItem();
             // Split it up to make tags out of the portion of the path that isn't dir (i.e. the folder structure they used)
             $dir = $this->dir;
             $dir = preg_replace('/\\/$/', '', $dir) . '/';
             $relevant = preg_replace('/^' . preg_quote($dir, '/') . '/', '', $file);
             // TODO: not Microsoft-friendly, might matter in some setting
             $components = preg_split('/\\//', $relevant);
             $tags = array_slice($components, 0, count($components) - 1);
             foreach ($tags as &$tag) {
                 // We don't strictly need to be this harsh, but it's safe and definitely
                 // takes care of some things we definitely can't allow, like periods
                 // (which cause mod_rewrite problems with pretty Symfony URLs).
                 // TODO: clean it up in a nicer way without being UTF8-clueless
                 // (aTools::slugify is UTF8-safe)
                 $tag = aTools::slugify($tag);
             }
             $item->title = aMediaTools::filenameToTitle($pathinfo['basename']);
             $item->setTags($tags);
             if (!strlen($item->title)) {
                 $this->giveFeedback("error", "Files must have a basename", $file);
                 continue;
             }
             // The preSaveImage / save / saveImage dance is necessary because
             // the sluggable behavior doesn't kick in until save and the image file
             // needs a slug based filename.
             if (!$item->preSaveFile($vf)) {
                 $this->giveFeedback("error", "Save failed", $file);
                 continue;
             }
             $item->save();
             if (!$item->saveFile($vf)) {
                 $this->giveFeedback("error", "Save failed", $file);
                 $item->delete();
                 continue;
             }
             unlink($file);
             $count++;
             $this->giveFeedback("completed", $count, $file);
         }
     }
     $this->giveFeedback("total", $count);
 }
Ejemplo n.º 4
0
 /**
  * DOCUMENT ME
  * @param mixed $arguments
  * @param mixed $options
  */
 protected function execute($arguments = array(), $options = array())
 {
     // We need a basic context so we can call helpers to format text
     $context = sfContext::createInstance($this->configuration);
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
     // PDO connection not so useful, get the doctrine one
     $conn = Doctrine_Manager::connection();
     $accounts = Doctrine::getTable('aEmbedMediaAccount')->findAll();
     foreach ($accounts as $a) {
         $perPage = 50;
         $service = aMediaTools::getEmbedService($a->service);
         if (!$service) {
             // An account for a service that has been deconfigured
             continue;
         }
         $total = null;
         $page = 1;
         $serviceUrls = array();
         while (true) {
             $results = $service->browseUser($a->username, $page, $perPage);
             if ($results === false) {
                 // We hit the rate limit, the account is bad, etc. Just
                 // be tolerant and retry later. Would be nice to distinguish
                 // these cases but it's not that hard to figure out an
                 // account is gone
                 break;
             }
             foreach ($results['results'] as $result) {
                 $serviceUrls[] = $result['url'];
             }
             // We hit the end of the results for this user
             if (!count($results['results'])) {
                 break;
             }
             $page++;
         }
         if (count($serviceUrls)) {
             $existingServiceUrls = Doctrine::getTable('aMediaItem')->createQuery('m')->select('m.service_url')->andWhereIn('m.service_url', $serviceUrls)->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR);
         } else {
             $existingServiceUrls = array();
         }
         $existingServiceUrls = array_flip($existingServiceUrls);
         foreach ($serviceUrls as $serviceUrl) {
             if (!isset($existingServiceUrls[$serviceUrl])) {
                 // If Doctrine becomes a performance problem I could use PDO
                 // and set lucene_dirty to let that clean itself up later
                 $id = $service->getIdFromUrl($serviceUrl);
                 $info = $service->getInfo($id);
                 if (!$info) {
                     // We are not actually allowed meaningful access to this video. Password protected for example
                     continue;
                 }
                 $item = new aMediaItem();
                 $item->setTitle($info['title']);
                 // We want tags to be lower case, and slashes break routes in most server configs.
                 $info['tags'] = str_replace('/', '-', aString::strtolower($info['tags']));
                 $item->setTags($info['tags']);
                 $item->setDescription(aHtml::textToHtml($info['description']));
                 $item->setCredit($info['credit']);
                 $item->setServiceUrl($info['url']);
                 $item->setType($service->getType());
                 // The dance is this: get the thumbnail if there is one;
                 // call preSaveFile to learn the width, height and format
                 // before saving; save; and then saveFile to copy it to a
                 // filename based on the slug, which is unknown until after save
                 $thumbnail = $service->getThumbnail($id);
                 if ($thumbnail) {
                     // Grab a local copy of the thumbnail, and get the pain
                     // over with all at once in a predictable way if
                     // the service provider fails to give it to us.
                     $thumbnailCopy = aFiles::getTemporaryFilename();
                     if (copy($thumbnail, $thumbnailCopy)) {
                         $item->preSaveFile($thumbnailCopy);
                     }
                 }
                 $item->save();
                 if ($thumbnail) {
                     $item->saveFile($thumbnailCopy);
                 }
                 $item->free();
             }
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * Finds or adds a video without the overhead of a proper Doctrine save.
  * @param array $info
  * @return mixed
  */
 protected function findOrAddVideo($info)
 {
     $mediaId = null;
     $slug = null;
     if (!isset($info['title'])) {
         $info['title'] = 'Imported video';
     }
     $slug = aTools::slugify(!empty($info['title']) ? $info['title'] : (!empty($info['service_url']) ? $info['service_url'] : md5($info['embed'])));
     $result = $this->sql->query('SELECT id FROM a_media_item WHERE slug = :slug', array('slug' => $slug));
     if (isset($result[0]['id'])) {
         $mediaId = $result[0]['id'];
     } else {
         $mediaItem = new aMediaItem();
         foreach ($info as $key => $value) {
             if ($key !== 'tags') {
                 $mediaItem[$key] = $value;
             }
         }
         if (empty($mediaItem['title'])) {
             $mediaItem->setTitle($slug);
         } else {
             $mediaItem->setTitle($info['title']);
         }
         $mediaItem->setSlug($slug);
         $mediaItem->setType('video');
         if ($mediaItem->service_url) {
             $service = aMediaTools::getEmbedService($mediaItem->service_url);
             $id = $service->getIdFromUrl($mediaItem->service_url);
             if ($service->supports('thumbnail')) {
                 $filename = $service->getThumbnail($id);
                 if ($filename) {
                     // saveFile can't handle a nonlocal file directly, so
                     // copy to a temporary file first
                     $bad = isset($this->failedMedia[$filename]);
                     if (!$bad) {
                         $tmpFile = aFiles::getTemporaryFilename();
                         try {
                             if (!copy($filename, $tmpFile)) {
                                 throw new sfException(sprintf('Could not copy file: %s', $src));
                             }
                             if (!$mediaItem->saveFile($tmpFile)) {
                                 throw new sfException(sprintf('Could not save file: %s', $src));
                             }
                         } catch (Exception $e) {
                             $this->failedMedia[$filename] = true;
                         }
                         unlink($tmpFile);
                     }
                 }
             }
         }
         $this->sql->fastSaveMediaItem($mediaItem);
         if (count($info['tags'])) {
             $this->sql->fastSaveTags('aMediaItem', $mediaItem->id, $info['tags']);
         }
         $mediaId = $mediaItem->id;
         $mediaItem->free(true);
     }
     return $mediaId;
 }