Exemple #1
0
 /**
  * Fill in the properties of this bitstream given its path. The file must
  * accessible by the web server.
  *
  * @deprecated
  * @throws Zend_Exception
  */
 public function fillPropertiesFromPath()
 {
     // Check if the path exists
     if (!isset($this->path) || empty($this->path)) {
         throw new Zend_Exception('BitstreamDao path is not set in fillPropertiesFromPath()');
     }
     // TODO: Compute the full path from the asset store. For now using the path.
     $this->setMimetype($this->Component->MimeType->getType($this->path));
     // clear the stat cache, as the underlying file might have changed
     // since the last time filesize was called on the same filepath
     clearstatcache();
     $this->setSizebytes(UtilityComponent::fileSize($this->path));
     if (!isset($this->checksum) || empty($this->checksum)) {
         $this->setChecksum(UtilityComponent::md5file($this->path));
     }
 }
 /** Import a directory recursively */
 private function _recursiveParseDirectory($path, $currentdir)
 {
     $it = new DirectoryIterator($path);
     foreach ($it as $fileInfo) {
         if ($fileInfo->isDot()) {
             continue;
         }
         // If the file/dir is not readable (permission issue)
         if (!$fileInfo->isReadable()) {
             $this->getLogger()->crit($fileInfo->getPathName() . ' cannot be imported. Not readable.');
             continue;
         }
         // If this is too slow we'll figure something out
         if ($this->_checkStopImport()) {
             return false;
         }
         if ($fileInfo->isDir()) {
             // we have a directory
             // If the the directory actually doesn't exist at this point,
             // skip it.
             if (!file_exists($fileInfo->getPathName())) {
                 continue;
             }
             // Get the files in the directory and skip the folder if it does not
             // contain any files and we aren't set to import empty directories. The
             // count($files) <= 2 is there to account for our our friends . and ..
             $files = scandir($fileInfo->getPathName());
             if (!$this->importemptydirectories && $files && count($files) <= 2) {
                 continue;
             }
             // Find if the child exists
             $child = $this->Folder->getFolderByName($currentdir, $fileInfo->getFilename());
             // If the folder does not exist, create one.
             if (!$child) {
                 $child = new FolderDao();
                 $child->setName($fileInfo->getFilename());
                 $child->setParentId($currentdir->getFolderId());
                 $child->setDateCreation(date('Y-m-d H:i:s'));
                 $child->setDescription('');
                 $this->Folder->save($child);
                 $this->Folderpolicyuser->createPolicy($this->userSession->Dao, $child, MIDAS_POLICY_ADMIN);
             }
             // Keep descending
             $this->_recursiveParseDirectory($fileInfo->getPathName(), $child);
         } else {
             // We have a file
             $this->_incrementFileProcessed();
             $newrevision = true;
             $item = $this->Folder->getItemByName($currentdir, $fileInfo->getFilename());
             if (!$item) {
                 // Create an item
                 $item = new ItemDao();
                 $item->setName($fileInfo->getFilename());
                 $item->setDescription('');
                 $item->setPrivacyStatus(MIDAS_PRIVACY_PRIVATE);
                 // Must set this flag private initially
                 $this->Item->save($item, true);
                 // Set the policy of the item
                 $this->Itempolicyuser->createPolicy($this->userSession->Dao, $item, MIDAS_POLICY_ADMIN);
                 // Add the item to the current directory
                 $this->Folder->addItem($currentdir, $item);
             }
             // Check if the bistream has been updated based on the local date
             $revision = $this->ItemRevision->getLatestRevision($item);
             if ($revision) {
                 $newrevision = false;
                 $bitstream = $this->ItemRevision->getBitstreamByName($revision, $fileInfo->getFilename());
                 $curMD5 = UtilityComponent::md5file($fileInfo->getPathName());
                 $diskFileIsNewer = strtotime($bitstream->getDate()) < filemtime($fileInfo->getPathName());
                 $md5IsDifferent = $bitstream->getChecksum() != $curMD5;
                 if (!$bitstream || $diskFileIsNewer && $md5IsDifferent) {
                     $newrevision = true;
                 }
             }
             if ($newrevision) {
                 // Create a revision for the item
                 $itemRevisionDao = new ItemRevisionDao();
                 $itemRevisionDao->setChanges('Initial revision');
                 $itemRevisionDao->setUser_id($this->userSession->Dao->getUserId());
                 $this->Item->addRevision($item, $itemRevisionDao);
                 // Add bitstreams to the revision
                 $this->getLogger()->debug('create New Bitstream');
                 $bitstreamDao = new BitstreamDao();
                 $bitstreamDao->setName($fileInfo->getFilename());
                 $bitstreamDao->setPath($fileInfo->getPathName());
                 $bitstreamDao->fillPropertiesFromPath();
                 // Set the Assetstore
                 $bitstreamDao->setAssetstoreId($this->assetstoreid);
                 // Upload the bitstream
                 $assetstoreDao = $this->Assetstore->load($this->assetstoreid);
                 $this->Component->Upload->uploadBitstream($bitstreamDao, $assetstoreDao, true);
                 $this->ItemRevision->addBitstream($itemRevisionDao, $bitstreamDao);
             }
         }
     }
     unset($it);
     return true;
 }
 /**
  * Save new revision in the database.
  *
  * @param UserDao $userDao The user who is creating the revision
  * @param string $name The name of the file being used to create the revision
  * @param string $path
  * @param string $changes The changes comment by the user
  * @param int $itemId The item to create the new revision in
  * @param null|int $itemRevisionNumber [optional][default=null] Revision number for the item
  * @param null|int $license [optional][default=null] License text for the revision
  * @param string $fileChecksum [optional][default=''] If passed, will use it instead of calculating it ourselves
  * @param bool $copy [optional][default=false] If true, will copy the file. Otherwise it will just move it into the assetstore.
  * @param null|int $fileSize If passed, will use it instead of calculating it ourselves
  * @param null|string $mimeType If passed, will use it instead of calculating it ourselves
  * @return ItemDao
  * @throws Zend_Exception
  */
 public function createNewRevision($userDao, $name, $path, $changes, $itemId, $itemRevisionNumber = null, $license = null, $fileChecksum = '', $copy = false, $fileSize = null, $mimeType = null)
 {
     if ($userDao === null) {
         throw new Zend_Exception('Please log in');
     }
     /** @var ItemModel $itemModel */
     $itemModel = MidasLoader::loadModel('Item');
     $itemDao = $itemModel->load($itemId);
     if ($itemDao === false) {
         throw new Zend_Exception('Unable to find item');
     }
     /** @var ItemRevisionModel $itemRevisionModel */
     $itemRevisionModel = MidasLoader::loadModel('ItemRevision');
     /** @var null|ItemRevisionDao $itemRevisionDao */
     $itemRevisionDao = null;
     if (isset($itemRevisionNumber)) {
         $revisions = $itemDao->getRevisions();
         foreach ($revisions as $revision) {
             if ($itemRevisionNumber == $revision->getRevision()) {
                 $itemRevisionDao = $revision;
                 break;
             }
         }
     }
     if (!$itemModel->policyCheck($itemDao, $userDao, MIDAS_POLICY_WRITE)) {
         throw new Zend_Exception('Parent permissions errors');
     }
     if ($itemRevisionDao === null) {
         /** @var ItemRevisionDao $itemRevisionDao */
         $itemRevisionDao = MidasLoader::newDao('ItemRevisionDao');
         $itemRevisionDao->setChanges($changes);
         $itemRevisionDao->setUser_id($userDao->getKey());
         $itemRevisionDao->setDate(date('Y-m-d H:i:s'));
         $itemRevisionDao->setLicenseId($license);
         $itemModel->addRevision($itemDao, $itemRevisionDao);
     } else {
         $itemRevisionDao->setChanges($changes);
         if ($license !== null) {
             $itemRevisionDao->setLicenseId($license);
         }
         $itemRevisionModel->save($itemRevisionDao);
     }
     /** @var BitstreamModel $bitstreamModel */
     $bitstreamModel = MidasLoader::loadModel('Bitstream');
     // Add bitstreams to the revision
     /** @var BitstreamDao $bitstreamDao */
     $bitstreamDao = MidasLoader::newDao('BitstreamDao');
     $bitstreamDao->setName($name);
     $bitstreamDao->setPath($path);
     if (empty($fileChecksum)) {
         $fileChecksum = UtilityComponent::md5file($path);
     }
     $bitstreamDao->setChecksum($fileChecksum);
     if (is_null($fileSize)) {
         $fileSize = UtilityComponent::fileSize($path);
     }
     $bitstreamDao->setSizebytes($fileSize);
     if (is_null($mimeType)) {
         /** @var MimeTypeComponent $mimeTypeComponent */
         $mimeTypeComponent = MidasLoader::loadComponent('MimeType');
         $mimeType = $mimeTypeComponent->getType($path, $name);
     }
     $bitstreamDao->setMimetype($mimeType);
     /** @var AssetstoreModel $assetStoreModel */
     $assetStoreModel = MidasLoader::loadModel('Assetstore');
     $assetStoreDao = $assetStoreModel->getDefault();
     if ($assetStoreDao === false) {
         throw new Zend_Exception('Unable to load default asset store');
     }
     $bitstreamDao->setAssetstoreId($assetStoreDao->getKey());
     // Upload the bitstream if necessary (based on the asset store type)
     $this->uploadBitstream($bitstreamDao, $assetStoreDao, $copy);
     $checksum = $bitstreamDao->getChecksum();
     $tmpBitstreamDao = $bitstreamModel->getByChecksum($checksum);
     if ($tmpBitstreamDao != false) {
         $bitstreamDao->setPath($tmpBitstreamDao->getPath());
         $bitstreamDao->setAssetstoreId($tmpBitstreamDao->getAssetstoreId());
     }
     $itemRevisionModel->addBitstream($itemRevisionDao, $bitstreamDao);
     // now that we have updated the itemRevision, the item may be stale
     $itemDao = $itemModel->load($itemId);
     $this->getLogger()->debug('Revision uploaded: [' . $bitstreamDao->getName() . '] into revision ' . $itemRevisionDao->getKey() . ' (item ' . $itemDao->getKey() . ')');
     Zend_Registry::get('notifier')->notifyEvent('EVENT_CORE_UPLOAD_FILE', array($itemRevisionDao->getItem()->toArray(), $itemRevisionDao->toArray()));
     Zend_Registry::get('notifier')->callback('CALLBACK_CORE_UPLOAD_FILE', array($itemRevisionDao->getItem()->toArray(), $itemRevisionDao->toArray()));
     return $itemDao;
 }