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

Commit a322250

Browse files
author
Julien Jomier
committed
ENH: Adding page for migration
1 parent 3b6ade7 commit a322250

File tree

7 files changed

+302
-22
lines changed

7 files changed

+302
-22
lines changed

core/controllers/AdminController.php

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class AdminController extends AppController
88
public $_models = array('Errorlog', 'Assetstore');
99
public $_daos = array();
1010
public $_components = array('Upgrade', 'Utility', 'MIDAS2Migration');
11-
public $_forms = array('Admin', 'Assetstore');
11+
public $_forms = array('Admin', 'Assetstore','Migrate');
1212

1313
/** init the controller */
1414
function init()
@@ -427,9 +427,65 @@ function migratemidas2Action()
427427
throw new Zend_Exception("You should be an administrator");
428428
}
429429

430-
$this->_helper->layout->disableLayout();
431-
$this->_helper->viewRenderer->setNoRender();
432-
$this->Component->MIDAS2Migration->migrate($this->userSession->Dao->getUserId());
430+
$this->assetstores = $this->Assetstore->getAll();
431+
$this->view->migrateForm = $this->Form->Migrate->createMigrateForm($this->assetstores);
432+
$this->view->assetstoreForm = $this->Form->Assetstore->createAssetstoreForm('../assetstore/add');
433+
434+
if($this->getRequest()->isPost())
435+
{
436+
$this->_helper->layout->disableLayout();
437+
$this->_helper->viewRenderer->setNoRender();
438+
439+
if(!$this->view->migrateForm->isValid($_POST))
440+
{
441+
echo json_encode(array('error' => $this->t('The form is invalid. Missing values.')));
442+
return false;
443+
}
444+
445+
$midas2_hostname = $_POST['midas2_hostname'];
446+
$midas2_port = $_POST['midas2_port'];
447+
$midas2_user = $_POST['midas2_user'];
448+
$midas2_password = $_POST['midas2_password'];
449+
$midas2_database = $_POST['midas2_database'];
450+
$midas2_assetstore = $_POST['midas2_assetstore'];
451+
$midas3_assetstore = $_POST['assetstore'];
452+
453+
// Check that the assetstore is accessible
454+
if(!file_exists($midas2_assetstore))
455+
{
456+
echo json_encode(array('error' => $this->t('MIDAS2 assetstore is not accessible.')));
457+
return false;
458+
}
459+
460+
// Remove the last slashe if any
461+
if($midas2_assetstore[strlen($midas2_assetstore)-1] == '\\'
462+
|| $midas2_assetstore[strlen($midas2_assetstore)-1] == '/')
463+
{
464+
$midas2_assetstore = substr($midas2_assetstore,0,strlen($midas2_assetstore)-1);
465+
}
466+
467+
$this->Component->MIDAS2Migration->midas2User = $midas2_user;
468+
$this->Component->MIDAS2Migration->midas2Password = $midas2_password;
469+
$this->Component->MIDAS2Migration->midas2Host = $midas2_hostname;
470+
$this->Component->MIDAS2Migration->midas2Database = $midas2_database;
471+
$this->Component->MIDAS2Migration->midas2Port = $midas2_port;
472+
$this->Component->MIDAS2Migration->midas2Assetstore = $midas2_assetstore;
473+
$this->Component->MIDAS2Migration->assetstoreId = $midas3_assetstore;
474+
475+
try
476+
{
477+
$this->Component->MIDAS2Migration->migrate($this->userSession->Dao->getUserId());
478+
}
479+
catch(Zend_Exception $e)
480+
{
481+
echo json_encode(array('error' => $this->t($e->getMessage())));
482+
return false;
483+
}
484+
485+
echo json_encode(array('message' => $this->t('Migration sucessful.')));
486+
}
487+
488+
// Display the form
433489
}
434490

435491
} // end class

core/controllers/components/MIDAS2MigrationComponent.php

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,46 @@ private function _createFolderForItem($collectionId, $parentFolderid)
110110
$Item->addRevision($itemdao, $itemRevisionDao);
111111

112112
// Add the metadata
113+
$MetadataModel = $modelLoader->loadModel("Metadata");
114+
// Register the common metadata (this should move to the main function
115+
try
116+
{
117+
$MetadataModel->addMetadata(MIDAS_METADATA_GLOBAL,'contributor','author','Author of the data');
118+
$MetadataModel->addMetadata(MIDAS_METADATA_GLOBAL,'date','uploaded','Date when the data was uploaded to MIDAS');
119+
$MetadataModel->addMetadata(MIDAS_METADATA_GLOBAL,'date','issued','Date when the data was published');
120+
$MetadataModel->addMetadata(MIDAS_METADATA_GLOBAL,'date','created','Date when the data was created');
121+
}
122+
catch(Zend_Exception $e)
123+
{
124+
//we continue
125+
}
113126

127+
//
128+
$metadataquery = pg_query("SELECT metadata_field_id,text_value FROM metadatavalue WHERE item_id=".$item_id);
129+
while($metadata_array = pg_fetch_array($metadataquery))
130+
{
131+
$text_value = $metadata_array['text_value'];
132+
$metadata_field_id = $metadata_array['metadata_field_id'];
133+
134+
$element = "";
135+
$qualifier = "";
136+
137+
// Do not check 64 and 27 because they are stored as field and not metadata
138+
// in MIDAS3
139+
switch($metadata_field_id)
140+
{
141+
case 3: $element='contributor'; $qualifier='author'; break;
142+
case 11: $element='date'; $qualifier='uploaded'; break;
143+
case 15: $element='date'; $qualifier='issued'; break;
144+
case 14: $element='date'; $qualifier='created'; break;
145+
}
146+
147+
if($element != "")
148+
{
149+
$MetadataModel->addMetadataValue($itemRevisionDao,MIDAS_METADATA_GLOBAL,
150+
$element,$qualifier,$text_value);
151+
}
152+
}
114153

115154
// Add bitstreams to the revision
116155
$bitstreamDao = new BitstreamDao;
@@ -285,8 +324,10 @@ function migrate($userid)
285324
}
286325

287326
// Connect to the local PGSQL database
327+
ob_start(); // disable warnings
288328
$pgdb = pg_connect("host='".$this->midas2Host."' port='".$this->midas2Port."' dbname='".$this->midas2Database.
289329
"' user='".$this->midas2User."' password='".$this->midas2Password."'");
330+
ob_end_clean();
290331
if($pgdb === false)
291332
{
292333
throw new Zend_Exception("Cannot connect to the MIDAS2 database.");
@@ -300,18 +341,6 @@ function migrate($userid)
300341

301342
$modelLoader = new MIDAS_ModelLoader;
302343

303-
// Just to test the metadata
304-
$MetadataModel = $modelLoader->loadModel("Metadata");
305-
$Item = $modelLoader->loadModel("Item");
306-
307-
$itemDao = $Item->load(42);
308-
$itemRevisionDao = $Item->getLastRevision($itemDao);
309-
//$MetadataModel->addMetadata(MIDAS_METADATA_DOCUMENT,'contributor','author','Author of a text');
310-
$MetadataModel->addMetadataValue($itemRevisionDao,MIDAS_METADATA_DOCUMENT,
311-
'contributor','author','Julien!');
312-
313-
return false;
314-
315344
// STEP 1: Import the users
316345
$User = $modelLoader->loadModel("User");
317346
$query = pg_query("SELECT email,password,firstname,lastname FROM eperson");
@@ -347,6 +376,9 @@ function migrate($userid)
347376
$communityDao = false;
348377
try
349378
{
379+
// Check the policies for the community
380+
381+
350382
$communityDao = $Community->createCommunity($name, $short_description, MIDAS_COMMUNITY_PUBLIC, NULL); // no user
351383
}
352384
catch(Zend_Exception $e)

core/controllers/forms/AssetstoreForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
class AssetstoreForm extends AppForm
44
{
55
/** Create assetstore form*/
6-
public function createAssetstoreForm()
6+
public function createAssetstoreForm($action='assetstore/add')
77
{
88
$form = new Zend_Form();
9-
$form->setAction('assetstore/add');
9+
$form->setAction($action);
1010
$form->setName('assetstoreForm');
1111
$form->setMethod('post');
1212
$form->setAttrib('class', 'assetstoreForm');
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/** Migrate form */
3+
class MigrateForm extends AppForm
4+
{
5+
/** Main form*/
6+
public function createMigrateForm($assetstores)
7+
{
8+
// Setup the form
9+
$form = new Zend_Form();
10+
$form->setAction('migratemidas2');
11+
$form->setName('migrateForm');
12+
$form->setMethod('post');
13+
$form->setAttrib('class', 'migrateForm');
14+
15+
// Input directory
16+
$midas2_hostname = new Zend_Form_Element_Text('midas2_hostname', array('label' => $this->t('MIDAS2 Hostname'),'size' => 60,'value'=>'localhost'));
17+
$midas2_hostname->setRequired(true);
18+
$form->addElement($midas2_hostname);
19+
20+
$midas2_port = new Zend_Form_Element_Text('midas2_port', array('label' => $this->t('MIDAS2 Port'),'size' => 4,'value' => '5432'));
21+
$midas2_port->setRequired(true);
22+
$midas2_port->setValidators(array(new Zend_Validate_Digits()));
23+
$form->addElement($midas2_port);
24+
25+
$midas2_user = new Zend_Form_Element_Text('midas2_user', array('label' => $this->t('MIDAS2 User'),'size' => 60,'value' => 'midas'));
26+
$midas2_user->setRequired(true);
27+
$form->addElement($midas2_user);
28+
29+
$midas2_password = new Zend_Form_Element_Password('midas2_password', array('label' => $this->t('MIDAS2 Password'),'size' => 60,'value' => 'midas'));
30+
$midas2_password->setRequired(true);
31+
$form->addElement($midas2_password);
32+
33+
$midas2_database = new Zend_Form_Element_Text('midas2_database', array('label' => $this->t('MIDAS2 Database'),'size' => 60,'value' => 'midas'));
34+
$midas2_database->setRequired(true);
35+
$form->addElement($midas2_database);
36+
37+
$midas2_assetstore = new Zend_Form_Element_Text('midas2_assetstore', array('label' => $this->t('MIDAS2 Assetstore Path'),'size' => 60,'value' => 'C:/xampp/midas/assetstore'));
38+
$midas2_assetstore->setRequired(true);
39+
$form->addElement($midas2_assetstore);
40+
41+
// Assetstore
42+
$assetstoredisplay = array();
43+
$assetstoredisplay[0] = $this->t('Choose one...');
44+
45+
// Initialize with the first type (MIDAS)
46+
foreach($assetstores as $assetstore)
47+
{
48+
if($assetstore->getType() == 0)
49+
{
50+
$assetstoredisplay[$assetstore->getAssetstoreId()] = $assetstore->getName();
51+
}
52+
}
53+
54+
$assetstore = new Zend_Form_Element_Select('assetstore');
55+
$assetstore->setLabel($this->t('MIDAS3 Assetstore'));
56+
$assetstore->setMultiOptions($assetstoredisplay);
57+
$assetstore->setDescription(' <a class="load-newassetstore" href="#newassetstore-form" rel="#newassetstore-form" title="'.$this->t('Add a new assetstore').'"> '.$this->t('Add a new assetstore').'</a>')
58+
->setDecorators(array(
59+
'ViewHelper',
60+
array('Description', array('escape' => false, 'tag' => false)),
61+
array('HtmlTag', array('tag' => 'dd')),
62+
array('Label', array('tag' => 'dt')),
63+
'Errors',
64+
));
65+
$assetstore->setRequired(true);
66+
$assetstore->setValidators(array(new Zend_Validate_GreaterThan(array('min' => 0))));
67+
$assetstore->setRegisterInArrayValidator(false); // This array is dynamic so we disable the validator
68+
$form->addElement($assetstore);
69+
70+
// Submit
71+
$submit = new Zend_Form_Element_Button('migratesubmit', $this->t('Migrate'));
72+
$form->addElement($submit);
73+
74+
return $form;
75+
}
76+
} // end class
77+
?>

core/database/upgrade/3.0.13.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ class Upgrade_3_0_13 extends MIDASUpgrade
55
public function preUpgrade()
66
{
77
$this->AddTableField('metadatadocumentvalue', 'metadatavalue_id', 'bigint(20)', 'serial',false);
8-
$this->AddTablePrimaryKey('metadatadocumentvalue', 'metadatavalue_id');
98
$this->AddTableField('metadatavalue', 'metadatavalue_id', 'bigint(20)', 'serial',false);
10-
$this->AddTablePrimaryKey('metadatavalue', 'metadatavalue_id');
119
}
1210

1311
public function mysql()
1412
{
13+
$this->AddTablePrimaryKey('metadatadocumentvalue', 'metadatavalue_id');
14+
$this->AddTablePrimaryKey('metadatavalue', 'metadatavalue_id');
1515
$this->db->query("ALTER TABLE `metadatadocumentvalue` CHANGE `metadatavalue_id`
16-
`metadatavalue_id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT");
16+
`metadatavalue_id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT");
1717
$this->db->query("ALTER TABLE `metadatavalue` CHANGE `metadatavalue_id`
18-
`metadatavalue_id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT");
18+
`metadatavalue_id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT");
1919
}
2020

2121

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
$(document).ready(function()
2+
{
3+
//Bind the migrate submit to start the import
4+
$('#migratesubmit').click(function(){startMigrate()});
5+
6+
7+
// Form for the new assetstore
8+
options = {success:assetstoreAddCallback, beforeSubmit: assetstoreSubmit, dataType:'json'};
9+
$('#assetstoreForm').ajaxForm(options);
10+
11+
// Load the window for the new assetstore
12+
$('a.load-newassetstore').cluetip({cluetipClass: 'jtip',
13+
activation: 'click',
14+
local:true,
15+
cursor: 'pointer',
16+
arrows: true,
17+
clickOutClose: true
18+
});
19+
});
20+
21+
/** If the button to start the migration has been clicked */
22+
function startMigrate()
23+
{
24+
formSubmitOptions = {success:migrateCallback,
25+
dataType:'json',
26+
};
27+
$('#migrateForm').ajaxSubmit(formSubmitOptions);
28+
}
29+
30+
/** On import success */
31+
function migrateCallback(responseText, statusText, xhr, form)
32+
{
33+
if(responseText.error)
34+
{
35+
$(".viewNotice").html(responseText.error);
36+
$(".viewNotice").fadeIn(100).delay(2000).fadeOut(300);
37+
}
38+
else if(responseText.message)
39+
{
40+
$(".viewNotice").html(responseText.message);
41+
$(".viewNotice").fadeIn(100).delay(2000).fadeOut(300);
42+
}
43+
}
44+
45+
/** On assetstore add submit */
46+
function assetstoreSubmit(formData, jqForm, options)
47+
{
48+
// Add the type is the one in the main page (because it's hidden in the assetstore add page)
49+
var assetstoretype = new Object();
50+
assetstoretype.name = 'type';
51+
assetstoretype.value = $("#importassetstoretype").val();
52+
formData.push(assetstoretype);
53+
$(".assetstoreLoading").show();
54+
} // end assetstoreBeforeSubmit
55+
56+
/** On assetstore add sucess */
57+
function assetstoreAddCallback(responseText, statusText, xhr, $form)
58+
{
59+
$(".assetstoreLoading").hide();
60+
if(responseText.error)
61+
{
62+
$(".viewNotice").html(responseText.error);
63+
$(".viewNotice").fadeIn(100).delay(2000).fadeOut(100);
64+
}
65+
else if(responseText.msg)
66+
{
67+
$(document).trigger('hideCluetip');
68+
69+
// It worked, we add the assetstore to the list and we select it by default
70+
if(responseText.assetstore_id)
71+
{
72+
$("#assetstore").append($("<option></option>").
73+
attr("value",responseText.assetstore_id).
74+
text(responseText.assetstore_name)
75+
.attr("selected", "selected"));
76+
77+
// Add to JSON
78+
var newassetstore = new Object();
79+
newassetstore.assetstore_id = responseText.assetstore_id;
80+
newassetstore.name = responseText.assetstore_name;
81+
newassetstore.type = responseText.assetstore_type;
82+
assetstores.push(newassetstore);
83+
}
84+
85+
$(".viewNotice").html(responseText.msg);
86+
$(".viewNotice").fadeIn(100).delay(2000).fadeOut(100);
87+
}
88+
} // end assetstoreAddCallback
89+
90+
/** When the cancel is clicked in the new assetstore window */
91+
function newAssetstoreHide()
92+
{
93+
$(document).trigger('hideCluetip');
94+
} // end function newAssetstoreHide
95+
96+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
$this->headScript()->appendFile($this->coreWebroot.'/public/js/admin/admin.migrate.js');
3+
$this->headScript()->appendFile($this->coreWebroot.'/public/js/jquery/jquery.form.js');
4+
5+
$this->headLink()->appendStylesheet($this->coreWebroot.'/public/css/jquery/jquery.filetree.css');
6+
$this->headLink()->appendStylesheet($this->coreWebroot.'/public/css/assetstore/assetstore.add.css');
7+
?>
8+
9+
<h2>Migrating from MIDAS version 2 to MIDAS version 3</h2>
10+
<?php echo $this->migrateForm; ?>
11+
12+
13+
<div id="newassetstore-form" style="display:none">
14+
<?php echo $this->assetstoreForm; ?>
15+
</div>
16+
17+
<script language="javascript">
18+
var assetstores = eval(<?php echo json_encode($this->assetstores)?>);
19+
</script>

0 commit comments

Comments
 (0)