From a0cae4a4dd94659ce94693801e4a510eae71a010 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Tue, 27 Oct 2015 04:24:37 -0400 Subject: [PATCH 1/3] uploadGeneratetoken: Add support to optionally create new revision This commit updates the "uploadGeneratetoken" API introducing the new parameter "create_additional_revision" allowing to optionally change the default behavior and ensure a new revision is created when: - a bitstream associated with the file already exists on the server - and the checksum parameter is specified - and the item updated has at least one revision Since the http uploader doesn't specify the "checksum" parameter, the problem is only reproducible using the web API. For example, considering the following configuration: folder |--- item1 --- revision1 | |--- bitstream1 (filename1) | |--- item2 --- revision1 | |--- bitstream2 (filename2) By default, trying to associate bitstream1 with item2 will result in this configuration: folder |--- item1 --- revision1 | |--- bitstream1 (filename1) | |--- item2 --- revision1 | |--- bitstream2 (filename2) | |--- bitstream1 (filename1) By setting the option "create_additional_revision" to True, the following configuration will be obtained: folder |--- item1 --- revision1 | |--- bitstream1 (filename1) | |--- item2 -|--- revision1 | | |--- bitstream2 (filename2) | | | |--- revision2 | | |--- bitstream1 (filename1) --- core/controllers/components/ApisystemComponent.php | 14 +++++++++++--- .../api/controllers/components/ApiComponent.php | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/controllers/components/ApisystemComponent.php b/core/controllers/components/ApisystemComponent.php index 0299c4874..64b9c7f4d 100644 --- a/core/controllers/components/ApisystemComponent.php +++ b/core/controllers/components/ApisystemComponent.php @@ -324,6 +324,10 @@ public function linkCreate($args) * When passing the folderid param, the name of the newly created item, * if not supplied, the item will have the same name as filename. * @param checksum (Optional) The md5 checksum of the file to be uploaded. + * @param create_additional_revision (Optional) When a checksum is passed and + * the server already has the file, by default a reference to the existing + * bitstream will be added to the latest revision. By setting + * create_additional_revision to true, a new revision will be created. * @return An upload token that can be used to upload a file. * If folderid is passed instead of itemid, a new item will be created * in that folder, but the id of the newly created item will not be @@ -404,13 +408,17 @@ public function uploadGeneratetoken($args) MIDAS_POLICY_READ ) ) { + $create_additional_revision = isset($args['create_additional_revision']) ? $args['create_additional_revision'] : false; $revision = $itemModel->getLastRevision($item); - if ($revision === false) { - // Create new revision if none exists yet + if ($revision === false || $create_additional_revision) { + // Create new revision if none exists yet or if the user explicitly asked for creating a new revision when + // a bitstream with the same checksum was found. Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH.'/core/models/dao'); $revision = new ItemRevisionDao(); - $revision->setChanges('Initial revision'); + if($create_additional_revision === false) { + $revision->setChanges('Initial revision'); + } $revision->setUser_id($userDao->getKey()); $revision->setDate(date('Y-m-d H:i:s')); $revision->setLicenseId(null); diff --git a/modules/api/controllers/components/ApiComponent.php b/modules/api/controllers/components/ApiComponent.php index a937bb7a2..559a037d6 100644 --- a/modules/api/controllers/components/ApiComponent.php +++ b/modules/api/controllers/components/ApiComponent.php @@ -317,6 +317,10 @@ public function linkCreate($args) * When passing the folderid param, the name of the newly created item, * if not supplied, the item will have the same name as filename. * @param checksum (Optional) The md5 checksum of the file to be uploaded. + * @param create_additional_revision (Optional) When a checksum is passed and + * the server already has the file, by default a reference to the existing + * bitstream will be added to the latest revision. By setting + * create_additional_revision to true, a new revision will be created. * @return An upload token that can be used to upload a file. * If folderid is passed instead of itemid, a new item will be created * in that folder, but the id of the newly created item will not be From 25fbe4b4ffcff1b61c13a0b7f83de9d66ff567a7 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Tue, 27 Oct 2015 05:20:58 -0400 Subject: [PATCH 2/3] uploadGeneratetoken: Smarter "create_additional_revision" support This commit improves the support for "create_additional_revision" introduced in a previous commit. It will not create an additional revision if last revision has one bitstream and checkum matches --- core/controllers/components/ApisystemComponent.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/controllers/components/ApisystemComponent.php b/core/controllers/components/ApisystemComponent.php index 64b9c7f4d..0e05b4b42 100644 --- a/core/controllers/components/ApisystemComponent.php +++ b/core/controllers/components/ApisystemComponent.php @@ -411,6 +411,14 @@ public function uploadGeneratetoken($args) $create_additional_revision = isset($args['create_additional_revision']) ? $args['create_additional_revision'] : false; $revision = $itemModel->getLastRevision($item); + // do not create an additional revision if last revision has one bitstream and checkum matches + if ($create_additional_revision && $revision) { + $bitstreams = $revision->getBitstreams(); + if (count($bitstreams) == 1 && $args['checksum'] == $bitstreams[0]->getChecksum()) { + return array('token' => ''); + } + } + if ($revision === false || $create_additional_revision) { // Create new revision if none exists yet or if the user explicitly asked for creating a new revision when // a bitstream with the same checksum was found. From 4139b77faa0e3723802bf2152c79f1d1edc69f4b Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Tue, 27 Oct 2015 17:18:18 -0400 Subject: [PATCH 3/3] uploadGeneratetoken:: Fix style --- .../components/ApisystemComponent.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core/controllers/components/ApisystemComponent.php b/core/controllers/components/ApisystemComponent.php index 0e05b4b42..968e1e491 100644 --- a/core/controllers/components/ApisystemComponent.php +++ b/core/controllers/components/ApisystemComponent.php @@ -408,23 +408,23 @@ public function uploadGeneratetoken($args) MIDAS_POLICY_READ ) ) { - $create_additional_revision = isset($args['create_additional_revision']) ? $args['create_additional_revision'] : false; + $createAdditionalRevision = isset($args['create_additional_revision']) ? $args['create_additional_revision'] : false; $revision = $itemModel->getLastRevision($item); - // do not create an additional revision if last revision has one bitstream and checkum matches - if ($create_additional_revision && $revision) { - $bitstreams = $revision->getBitstreams(); - if (count($bitstreams) == 1 && $args['checksum'] == $bitstreams[0]->getChecksum()) { - return array('token' => ''); + // Do not create an additional revision if last revision has one bitstream and checksum matches. + if ($createAdditionalRevision && $revision) { + $bitstreams = $revision->getBitstreams(); + if (count($bitstreams) === 1 && $args['checksum'] == $bitstreams[0]->getChecksum()) { + return array('token' => ''); } - } + } - if ($revision === false || $create_additional_revision) { - // Create new revision if none exists yet or if the user explicitly asked for creating a new revision when + if ($revision === false || $createAdditionalRevision) { + // Create a new revision if none exists yet or if the user explicitly asks to create a new revision when // a bitstream with the same checksum was found. Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH.'/core/models/dao'); $revision = new ItemRevisionDao(); - if($create_additional_revision === false) { + if ($createAdditionalRevision === false) { $revision->setChanges('Initial revision'); } $revision->setUser_id($userDao->getKey());