Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Check that items have at least one revision where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed May 12, 2015
1 parent 7bcfe52 commit 220ea63
Show file tree
Hide file tree
Showing 33 changed files with 191 additions and 57 deletions.
9 changes: 9 additions & 0 deletions core/controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public function editmetadataAction()
} else {
$metadataItemRevision = $this->Item->getLastRevision($itemDao);
}
if ($metadataItemRevision === false) {
throw new Zend_Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
$metadatavalues = $this->ItemRevision->getMetadata($metadataItemRevision);
$this->view->metadata = null;

Expand Down Expand Up @@ -121,6 +124,9 @@ public function viewAction()
} else {
$metadataItemRevision = $itemRevision;
}
if ($metadataItemRevision === false) {
throw new Zend_Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
$deleteMetadata = $this->getParam('deleteMetadata');
$editMetadata = $this->getParam('editMetadata');
if (isset($deleteMetadata) && !empty($deleteMetadata) && $this->view->isModerator) { // delete metadata field
Expand Down Expand Up @@ -603,6 +609,9 @@ public function getmetadatavalueexistsAction()
} else {
$metadataItemRevision = $this->Item->getLastRevision($itemDao);
}
if ($metadataItemRevision === false) {
throw new Zend_Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
$metadataDao->setItemrevisionId($metadataItemRevision->getKey());
if ($this->Metadata->getMetadataValueExists($metadataDao)) {
$exists = 1;
Expand Down
10 changes: 8 additions & 2 deletions core/controllers/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,13 @@ public function linksAction()
case 'folder':
$dao = $this->Folder->load($id);
$name = $dao->getName().'.zip';
$hasDownload = true;
break;
case 'item':
$dao = $this->Item->load($id);
$headRev = $this->Item->getLastRevision($dao);
$name = $dao->getName();
$headRev = $this->Item->getLastRevision($dao);
$hasDownload = $headRev !== false;
if (count($headRev->getBitstreams()) > 1) {
$name .= '.zip';
}
Expand All @@ -318,6 +320,10 @@ public function linksAction()
$this->view->type = $type;
$this->view->id = $id;
$this->view->viewUrl = $baseUrl.'/'.$type.'/'.$id;
$this->view->downloadUrl = $baseUrl.'/download/'.$type.'/'.$id.'/'.urlencode($name);
if ($hasDownload === false) {
$this->view->downloadUrl = '';
} else {
$this->view->downloadUrl = $baseUrl.'/download/'.$type.'/'.$id.'/'.urlencode($name);
}
}
}
6 changes: 3 additions & 3 deletions core/controllers/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public function revisionAction()

// Check if the revision exists and if it does, we send its license ID to
// the view. If it does not exist we use our default license
if ($itemRevision) {
$this->view->selectedLicense = $itemRevision->getLicenseId();
} else {
if ($itemRevision === false) {
$this->view->selectedLicense = Zend_Registry::get('configGlobal')->defaultlicense;
} else {
$this->view->selectedLicense = $itemRevision->getLicenseId();
}

$this->view->allLicenses = $this->License->getAll();
Expand Down
6 changes: 3 additions & 3 deletions core/controllers/components/ApihelperComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function getItemRevision($item, $revisionNumber = null)
if ($revisionDao) {
return $revisionDao;
} else {
throw new Exception("The item must have at least one revision to have metadata.", MIDAS_INVALID_POLICY);
throw new Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
}

Expand All @@ -129,7 +129,7 @@ public function getItemRevision($item, $revisionNumber = null)
}
$revisions = $item->getRevisions();
if (count($revisions) === 0) {
throw new Exception("The item must have at least one revision to have metadata.", MIDAS_INVALID_POLICY);
throw new Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
// check revisions exist
foreach ($revisions as $revision) {
Expand Down Expand Up @@ -231,7 +231,7 @@ public function setMetadata($item, $type, $element, $qualifier, $value, $revisio

// If no module handles this metadata, we add it as normal metadata on the item revision
if (!$revisionDao) {
throw new Exception("The item must have at least one revision to have metadata.", MIDAS_INVALID_POLICY);
throw new Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}

/** @var MetadataModel $metadataModel */
Expand Down
2 changes: 1 addition & 1 deletion core/controllers/components/ApiitemComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function itemDeletemetadataAll($args)
) {
$revisions = $item->getRevisions();
if (count($revisions) === 0) {
throw new Exception("The item must have at least one revision to have metadata.", MIDAS_INVALID_POLICY);
throw new Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
foreach ($revisions as $revisionDao) {
$itemRevisionModel->deleteMetadata($revisionDao);
Expand Down
2 changes: 1 addition & 1 deletion core/controllers/components/ApisystemComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public function uploadGeneratetoken($args)
) {
$revision = $itemModel->getLastRevision($item);

if ($revision == false) {
if ($revision === false) {
// Create new revision if none exists yet
Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH.'/core/models/dao');
$revision = new ItemRevisionDao();
Expand Down
12 changes: 7 additions & 5 deletions core/controllers/components/ExportComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,20 @@ public function exportBitstreams($userDao, $targetDir, $itemIds, $shouldSymLink)
}
// Use the given revision_number if it is not empty
if (isset($tmpId[1])) {
$revisionNum = $itemModel->getRevision($item, $tmpId[1]);
if ($revisionNum !== false) {
$revisions[] = $revisionNum;
$revision = $itemModel->getRevision($item, $tmpId[1]);
if ($revision !== false) {
$revisions[] = $revision;
} else {
throw new Zend_Exception(
"Revision number ".$tmpId[1]." for item ".$tmpId[0]." does not exist. Please check your input."
);
}
} else {
// Otherwise use the latest revision
$revisionNum = $itemModel->getLastRevision($item);
$revisions[] = $revisionNum;
$revision = $itemModel->getLastRevision($item);
if ($revision !== false) {
$revisions[] = $revision;
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion core/models/base/MetadataModelBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public function addMetadataValue(
/** @var ItemModel $itemModel */
$itemModel = MidasLoader::loadModel('Item');
$lastrevision = $itemModel->getLastRevision($item);

if ($lastrevision === false) {
throw new Zend_Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
// refresh zend search index if latest revision has changed
if ($lastrevision->getKey() == $itemRevisionDao->getKey()) {
$itemModel->save($item, $passItemMetadataChanged);
Expand Down
2 changes: 1 addition & 1 deletion core/models/pdo/FolderModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ public function zipStream(&$zip, $path, $folder, &$userDao, &$overrideOutputFunc
$item = $this->initDao('Item', $row);
$rev = $this->Item->getLastRevision($item);

if (!$rev) {
if ($rev === false) {
$zip->addFile($path.'/'.$item->getName(), ''); // add empty item
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions core/models/pdo/ItemRevisionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public function deleteMetadata($revisiondao, $metadataId = null)
/** @var ItemModel $itemModel */
$itemModel = MidasLoader::loadModel('Item');
$lastrevision = $itemModel->getLastRevision($item);
if ($lastrevision === false) {
throw new Zend_Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}

// refresh lucene search index
if ($lastrevision->getKey() == $revisiondao->getKey()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ public function exportSingleBitstreamItemsToWorkDataDir($userDao, $taskDao, $ite
foreach ($itemsForExport as $itemName => $itemId) {
$itemDao = $itemModel->load($itemId);
$revisionDao = $itemModel->getLastRevision($itemDao);
if ($revisionDao === false) {
throw new Zend_Exception('The item has no revisions', MIDAS_INVALID_POLICY);
}
$bitstreamDaos = $revisionDao->getBitstreams();
if (empty($bitstreamDaos)) {
throw new Zend_Exception("Item ".$itemId." had no bitstreams.");
throw new Zend_Exception("Item ".$itemId." has no bitstreams.");
}
$imageBitstreamDao = $bitstreamDaos[0];
$exportedBitstreamPath = $datapath.'/'.$itemId.'/'.$imageBitstreamDao->getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function extract($args)
}

$revisionDao = $itemModel->getLastRevision($itemDao);
if ($revisionDao === false) {
throw new Exception('The item has no revisions', MIDAS_INVALID_POLICY);
}

/** @var Dicomextractor_ExtractorComponent $dicomComponent */
$dicomComponent = MidasLoader::loadComponent('Extractor', 'dicomextractor');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public function thumbnail($item)
/** @var ItemModel $itemModel */
$itemModel = MidasLoader::loadModel("Item");
$revision = $itemModel->getLastRevision($item);
if ($revision === false) {
return;

This comment has been minimized.

Copy link
@cpatrick

cpatrick May 26, 2015

Contributor

Should we log something here?

}

$bitstreams = $revision->getBitstreams();
$numBitstreams = count($bitstreams);
if ($numBitstreams < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ public function register($args)
}

$revisionDao = $itemModel->getLastRevision($itemDao);
if ($revisionDao === false) {
throw new Exception('The item has no revisions', MIDAS_INVALID_POLICY);
}

/** @var Dicomserver_ServerComponent $dicomComponent */
$dicomComponent = MidasLoader::loadComponent('Server', 'dicomserver');
Expand Down
3 changes: 3 additions & 0 deletions modules/oai/library/oai/listrecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@
$output .= ' </header>'."\n";

$revision = $itemModel->getLastRevision($element);
if ($revision === false) {
throw new Zend_Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
$metadata = $itemRevisionModel->getMetadata($revision);
include BASE_PATH.'/modules/oai/library/oai/'.$inc_record;

Expand Down
12 changes: 12 additions & 0 deletions modules/packages/controllers/components/ApiComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ public function extensionList($args)

foreach ($daos as $dao) {
$revision = $itemModel->getLastRevision($dao->getItem());
if ($revision === false) {
continue;
}
$bitstreams = $revision->getBitstreams();
if (count($bitstreams) === 0) {
continue;
}
$bitstream = $bitstreams[0];

$results[] = array(
Expand Down Expand Up @@ -231,6 +237,9 @@ public function extensionUpload($args)
/** @var ItemRevisionModel $itemRevisionModel */
$itemRevisionModel = MidasLoader::loadModel('ItemRevision');
$itemRevision = $itemModel->getLastRevision($item);
if ($itemRevision === false) {
throw new Exception('The item has no revisions', MIDAS_INVALID_POLICY);
}
$itemRevision->setChanges($args['revision']);
$itemRevisionModel->save($itemRevision);

Expand Down Expand Up @@ -317,6 +326,9 @@ public function packageList($args)
$results = array();
foreach ($daos as $dao) {
$revision = $itemModel->getLastRevision($dao->getItem());
if ($revision === false) {
continue;
}
$bitstreams = $revision->getBitstreams();
$bitstreamsArray = array();
foreach ($bitstreams as $bitstream) {
Expand Down
12 changes: 12 additions & 0 deletions modules/pvw/controllers/components/ParaviewComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,26 @@ private function _createDataDir($itemDao, $meshItems, $instanceDao)
/** @var ItemRevisionModel $revisionModel */
$revisionModel = MidasLoader::loadModel('ItemRevision');
$rev = $itemModel->getLastRevision($itemDao);
if ($rev === false) {
throw new Zend_Exception('The item has no revisions', MIDAS_INVALID_POLICY);
}
$bitstreams = $rev->getBitstreams();
if (count($bitstreams) === 0) {
throw new Zend_Exception('The item has no bitstreams', MIDAS_INVALID_POLICY);
}
$src = $bitstreams[0]->getFullpath();
symlink($src, $path.'/main/'.$bitstreams[0]->getName());

// Symlink all the surfaces into the surfaces subdir
foreach ($meshItems as $meshItem) {
$rev = $itemModel->getLastRevision($meshItem);
if ($rev === false) {
continue;
}
$bitstreams = $rev->getBitstreams();
if (count($bitstreams) === 0) {
continue;
}
$src = $bitstreams[0]->getFullpath();
$linkPath = $path.'/surfaces/'.$meshItem->getKey().'_'.$bitstreams[0]->getName();
symlink($src, $linkPath);
Expand Down
4 changes: 2 additions & 2 deletions modules/pvw/controllers/components/ValidationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ private function _testItem($itemDao, $extensions)
/** @var ItemModel $itemModel */
$itemModel = MidasLoader::loadModel('Item');
$revision = $itemModel->getLastRevision($itemDao);
if (empty($revision)) {
if ($revision === false) {
return false;
}
$bitstreams = $revision->getBitstreams();
if (count($bitstreams) == 0) {
if (count($bitstreams) === 0) {
return false;
}

Expand Down
6 changes: 6 additions & 0 deletions modules/readmes/controllers/components/GetReadmeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ public function fromFolder($folder)
return array('text' => '');
}
$revisionDao = $itemModel->getLastRevision($readmeItem);
if ($revisionDao === false) {
return array('text' => '');
}
$bitstreams = $revisionDao->getBitstreams();
if (count($bitstreams) === 0) {
return array('text' => '');
}
$bitstream = $bitstreams[0];
$path = $bitstream->getAssetstore()->getPath().'/'.$bitstream->getPath();
$contents = file_get_contents($path);
Expand Down
3 changes: 3 additions & 0 deletions modules/remoteprocessing/controllers/ExecutableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public function defineAction()
file_put_contents($pathFile, $xmlContent);

$revision = $this->Item->getLastRevision($itemDao);
if ($revision === false) {
throw new Zend_Exception('The item has no revisions', MIDAS_INVALID_POLICY);
}
$bitstreams = $revision->getBitstreams();

$itemRevisionDao = new ItemRevisionDao();
Expand Down
16 changes: 11 additions & 5 deletions modules/remoteprocessing/controllers/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,11 @@ public function viewAction()
} elseif ($item->type == MIDAS_REMOTEPROCESSING_RELATION_TYPE_INPUT) {
$inputs[$item->getName()] = $item;
} elseif ($item->type == MIDAS_REMOTEPROCESSING_RELATION_TYPE_OUPUT) {
$reviesion = $this->Item->getLastRevision($item);
$metadata = $this->ItemRevision->getMetadata($reviesion);
$revision = $this->Item->getLastRevision($item);
if ($revision === false) {
throw new Zend_Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
$metadata = $this->ItemRevision->getMetadata($revision);
$item->metadata = $metadata;

foreach ($metadata as $m) {
Expand All @@ -271,11 +274,14 @@ public function viewAction()

$outputs[] = $item;
} elseif ($item->type == MIDAS_REMOTEPROCESSING_RELATION_TYPE_RESULTS) {
$reviesion = $this->Item->getLastRevision($item);
$metadata = $this->ItemRevision->getMetadata($reviesion);
$revision = $this->Item->getLastRevision($item);
if ($revision === false) {
throw new Zend_Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
}
$metadata = $this->ItemRevision->getMetadata($revision);
$item->metadata = $metadata;

$bitstreams = $reviesion->getBitstreams();
$bitstreams = $revision->getBitstreams();
if (count($bitstreams) == 1) {
$log = file_get_contents($bitstreams[0]->getFullPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public function getMetaIoFile($itemDao)
/** @var ItemModel $itemModel */
$itemModel = MidasLoader::loadModel('Item');
$revision = $itemModel->getLastRevision($itemDao);
if ($revision === false) {
return false;
}
$bitstreams = $revision->getBitstreams();
$metaFile = false;
foreach ($bitstreams as $b) {
Expand All @@ -52,6 +55,9 @@ public function getExecutable($itemDao)
/** @var ItemModel $itemModel */
$itemModel = MidasLoader::loadModel('Item');
$revision = $itemModel->getLastRevision($itemDao);
if ($revision === false) {
return false;
}
$bitstreams = $revision->getBitstreams();
foreach ($bitstreams as $b) {
if (is_executable($b->getFullPath())) {
Expand Down
2 changes: 1 addition & 1 deletion modules/solr/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function indexItem($args)

$revision = $this->Item->getLastRevision($item);

if ($revision != false) {
if ($revision !== false) {
$metadata = $this->ItemRevision->getMetadata($revision);
$metadataString = '';

Expand Down
Loading

1 comment on commit 220ea63

@cpatrick
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This certainly improves the behavior when we don't have any revisions on an item. Unfortunately, having to check for the presence of a revision is rather tedious.

Please sign in to comment.