-
Notifications
You must be signed in to change notification settings - Fork 902
/
DepositPackage.inc.php
397 lines (324 loc) · 13.7 KB
/
DepositPackage.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* @file plugins/generic/pln/classes/DepositPackage.inc.php
*
* Copyright (c) 2013-2014 Simon Fraser University Library
* Copyright (c) 2003-2014 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class DepositPackage
* @ingroup plugins_generic_pln
*
* @brief Handle PLN requests
*/
import('classes.file.JournalFileManager');
require_once(dirname(__FILE__).'/../lib/bagit.php');
class DepositPackage {
/**
* @var $deposit Deposit
*/
var $_deposit;
/**
* Constructor
* @param $deposit Deposit
* @return DepositPackage
*/
function DepositPackage($deposit) {
$this->_deposit = $deposit;
}
/**
* Get the directory used to store deposit data.
* @return string
*/
function getDepositDir() {
$journalDao =& DAORegistry::getDAO('JournalDAO');
$fileManager =& new JournalFileManager($journalDao->getById($this->_deposit->getJournalId()));
return $fileManager->filesDir . PLN_PLUGIN_ARCHIVE_FOLDER . DIRECTORY_SEPARATOR . $this->_deposit->getUUID();
}
/**
* Get the filename used to store the deposit's atom document.
* @return string
*/
function getAtomDocumentPath() {
return $this->getDepositDir() . DIRECTORY_SEPARATOR . $this->_deposit->getUUID() . ".xml";
}
/**
* Get the filename used to store the deposit's bag.
* @return string
*/
function getPackageFilePath() {
return $this->getDepositDir() . DIRECTORY_SEPARATOR . $this->_deposit->getUUID() . ".zip";
}
/**
* Create an atom document for this deposit.
* @return string
*/
function generateAtomDocument() {
$plnPlugin =& PluginRegistry::getPlugin('generic',PLN_PLUGIN_NAME);
$journalDao =& DAORegistry::getDAO('JournalDAO');
$journal =& $journalDao->getById($this->_deposit->getJournalId());
$fileManager = new JournalFileManager($journal);
// set up folder and file locations
$atomFile = $this->getAtomDocumentPath();
$packageFile = $this->getPackageFilePath();
// make sure our bag is present
if (!$fileManager->fileExists($packageFile)) return false;
$atom = new DOMDocument('1.0', 'utf-8');
$entry = $atom->createElementNS('http://www.w3.org/2005/Atom', 'entry');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:dcterms', 'http://purl.org/dc/terms/');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:pkp', 'http://pkp.sfu.ca/SWORD');
$email = $atom->createElement('email', $journal->getSetting('contactEmail'));
$entry->appendChild($email);
$title = $atom->createElement('title', $journal->getLocalizedTitle());
$entry->appendChild($title);
$pkpJournalUrl = $atom->createElementNS('http://pkp.sfu.ca/SWORD', 'pkp:journal_url', $journal->getUrl());
$entry->appendChild($pkpJournalUrl);
$issn = '';
if ($journal->getSetting('onlineIssn')) {
$issn = $journal->getSetting('onlineIssn');
} else if ($journal->getSetting('printIssn')) {
$issn = $journal->getSetting('printIssn');
}
$pkpIssn = $atom->createElementNS('http://pkp.sfu.ca/SWORD', 'pkp:issn', $issn);
$entry->appendChild($pkpIssn);
$id = $atom->createElement('id', 'urn:uuid:'.$this->_deposit->getUUID());
$entry->appendChild($id);
$updated = $atom->createElement('updated', strftime("%FT%TZ",strtotime($this->_deposit->getDateModified())));
$entry->appendChild($updated);
$url = $journal->getUrl() . '/' . PLN_PLUGIN_ARCHIVE_FOLDER . '/deposits/' . $this->_deposit->getUUID();
$pkpDetails = $atom->createElementNS('http://pkp.sfu.ca/SWORD', 'pkp:content', $url);
$pkpDetails->setAttribute('size', ceil(filesize($packageFile)/1024));
$objectVolume = "";
$objectIssue = "";
$objectOublicationDate = 0;
switch ($this->_deposit->getObjectType()) {
case PLN_PLUGIN_DEPOSIT_OBJECT_ARTICLE:
$depositObjects = $this->_deposit->getDepositObjects();
while ($depositObject =& $depositObjects->next()) {
$publishedArticleDao =& DAORegistry::getDAO('PublishedArticleDAO');
$article = $publishedArticleDao->getPublishedArticleByArticleId($depositObject->getObjectId());
if ($article->getDatePublished() > $objectOublicationDate)
$objectOublicationDate = $article->getDatePublished();
unset($depositObject);
}
break;
case PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE:
$depositObjects = $this->_deposit->getDepositObjects();
while ($depositObject =& $depositObjects->next()) {
$issueDao =& DAORegistry::getDAO('IssueDAO');
$issue = $issueDao->getIssueById($depositObject->getObjectId());
$objectVolume = $issue->getVolume();
$objectIssue = $issue->getNumber();
if ($issue->getDatePublished() > $objectOublicationDate)
$objectOublicationDate = $issue->getDatePublished();
unset($depositObject);
}
break;
}
$pkpDetails->setAttribute('volume', $objectVolume);
$pkpDetails->setAttribute('issue', $objectIssue);
$pkpDetails->setAttribute('pubdate', strftime("%F",strtotime($objectOublicationDate)));
switch ($plnPlugin->getSetting($journal->getId(), 'checksum_type')) {
case 'SHA-1':
$pkpDetails->setAttribute('checksumType', 'SHA-1');
$pkpDetails->setAttribute('checksumValue', sha1_file($packageFile));
break;
case 'MD5':
$pkpDetails->setAttribute('checksumType', 'MD5');
$pkpDetails->setAttribute('checksumValue', md5_file($packageFile));
break;
}
$entry->appendChild($pkpDetails);
$atom->appendChild($entry);
$atom->save($atomFile);
return $atomFile;
}
/**
* Create a package containing the serialized deposit objects
* @return string The full path of the created zip archive
*/
function generatePackage() {
// get DAOs, plugins and settings
$journalDao =& DAORegistry::getDAO('JournalDAO');
$issueDao =& DAORegistry::getDAO('IssueDAO');
$sectionDao =& DAORegistry::getDAO('SectionDAO');
$publishedArticleDao =& DAORegistry::getDAO('PublishedArticleDAO');
PluginRegistry::loadCategory('importexport');
$exportPlugin =& PluginRegistry::getPlugin('importexport','NativeImportExportPlugin');
$plnPlugin =& PluginRegistry::getPlugin('generic',PLN_PLUGIN_NAME);
$fileManager = new JournalFileManager($journalDao->getById($this->_deposit->getJournalId()));
$journal =& $journalDao->getById($this->_deposit->getJournalId());
$depositObjects = $this->_deposit->getDepositObjects();
// set up folder and file locations
$bagDir = $this->getDepositDir() . DIRECTORY_SEPARATOR . 'bag';
$packageFile = $this->getPackageFilePath();
$exportFile = $bagDir . DIRECTORY_SEPARATOR . $this->_deposit->getObjectType() . '.xml';
$termsFile = $bagDir . DIRECTORY_SEPARATOR . 'terms.xml';
$bag = new BagIt($bagDir);
switch ($this->_deposit->getObjectType()) {
case PLN_PLUGIN_DEPOSIT_OBJECT_ARTICLE:
$articles = array();
// we need to add all of the relevant articles to an array to export as a batch
while ($depositObject =& $depositObjects->next()) {
$article =& $publishedArticleDao->getPublishedArticleByArticleId($this->_deposit->getObjectId(), $journal->getId());
$issue =& $issueDao->getIssueById($article->getIssueId(), $journal->getId());
$section =& $sectionDao->getSection($article->getSectionId());
// add the article to the array we'll pass for export
$articles[] = array(
'publishedArticle' => $article,
'section' => $section,
'issue' => $issue,
'journal' => $journal
);
unset($depositObject);
}
// export all of the articles together
if ($exportPlugin->exportArticles($articles, $exportFile) !== true) return false;
break;
case PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE:
// we only ever do one issue at a time, so get that issue
$depositObject =& $depositObjects->next();
$issue =& $issueDao->getIssueByBestIssueId($depositObject->getObjectId(),$journal->getId());
// export the issue
if ($exportPlugin->exportIssue($journal, $issue, $exportFile) !== true) return false;
break;
default:
}
// add the current terms to the bag
$termsXml = new DOMDocument('1.0', 'utf-8');
$entry = $termsXml->createElementNS('http://www.w3.org/2005/Atom', 'entry');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:dcterms', 'http://purl.org/dc/terms/');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:pkp', PLN_PLUGIN_NAME);
$terms = unserialize($plnPlugin->getSetting($this->_deposit->getJournalId(), 'terms_of_use'));
$pkpTermsOfUse = $termsXml->createElementNS(PLN_PLUGIN_NAME, 'pkp:terms_of_use');
foreach ($terms as $termName => $termData) {
$element = $termsXml->createElementNS(PLN_PLUGIN_NAME, $termName, $termData['term']);
$element->setAttribute('updated',$termData['updated']);
$pkpTermsOfUse->appendChild($element);
}
$entry->appendChild($pkpTermsOfUse);
$termsXml->appendChild($entry);
$termsXml->save($termsFile);
// add the exported content to the bag
$bag->addFile($exportFile, $this->_deposit->getObjectType() . $this->_deposit->getUUID() . '.xml');
// add the exported content to the bag
$bag->addFile($termsFile, 'terms' . $this->_deposit->getUUID() . '.xml');
$bag->update();
// delete files
$fileManager->deleteFile($exportFile);
$fileManager->deleteFile($termsFile);
// create the bag
$bag->package($packageFile,'zip');
// remove the temporary bag directory
$fileManager->rmtree($bagDir);
return $packageFile;
}
/**
* Transfer the atom document to the PLN.
*/
function transferDeposit() {
$depositDao =& DAORegistry::getDAO('DepositDAO');
$journalDao =& DAORegistry::getDAO('JournalDAO');
$plnPlugin =& PluginRegistry::getPlugin('generic',PLN_PLUGIN_NAME);
$plnNetworks = unserialize(PLN_PLUGIN_NETWORKS);
$fileManager = new JournalFileManager($journalDao->getById($this->_deposit->getJournalId()));
$plnDir = $fileManager->filesDir . PLN_PLUGIN_ARCHIVE_FOLDER;
// post the atom document
$url = 'http://' . $plnNetworks[$plnPlugin->getSetting($this->_deposit->getJournalID(), 'pln_network')];
if ($this->_deposit->getUpdateStatus()) {
$url .= PLN_PLUGIN_CONT_IRI . '/' . $plnPlugin->getSetting($this->_deposit->getJournalID(), 'journal_uuid');
$url .= '/' . $this->_deposit->getUUID() . '/edit';
$result = $plnPlugin->_curlPutFile(
$url,
$this->getAtomDocumentPath()
);
} else {
$url .= PLN_PLUGIN_COL_IRI . '/' . $plnPlugin->getSetting($this->_deposit->getJournalID(), 'journal_uuid');
$result = $plnPlugin->_curlPostFile(
$url,
$this->getAtomDocumentPath()
);
}
// if we get the OK, set the status as transferred
if (($result['status'] == PLN_PLUGIN_HTTP_STATUS_OK) || ($result['status'] == PLN_PLUGIN_HTTP_STATUS_CREATED)) {
$this->_deposit->setTransferredStatus();
// unset a remote error if this worked
$this->_deposit->setRemoteFailureStatus(false);
// if this was an update, unset the update flag
$this->_deposit->setUpdateStatus(false);
$this->_deposit->setLastStatusDate(time());
$depositDao->updateDeposit($this->_deposit);
} else {
// we got an error back from the staging server
$this->_deposit->setRemoteFailureStatus();
$this->_deposit->setLastStatusDate(time());
$depositDao->updateDeposit($this->_deposit);
}
}
/**
* Package a deposit for transfer to and retrieval by the PLN.
*/
function packageDeposit() {
$depositDao =& DAORegistry::getDAO('DepositDAO');
$journalDao =& DAORegistry::getDAO('JournalDAO');
$fileManager = new JournalFileManager($journalDao->getById($this->_deposit->getJournalId()));
$plnDir = $fileManager->filesDir . PLN_PLUGIN_ARCHIVE_FOLDER;
// make sure the pln work directory exists
if ($fileManager->fileExists($plnDir,'dir') !== true) { $fileManager->mkdir($plnDir); }
// make a location for our work and clear it out if it's there
$depositDir = $plnDir . DIRECTORY_SEPARATOR . $this->_deposit->getUUID();
if ($fileManager->fileExists($depositDir,'dir')) $fileManager->rmtree($depositDir);
$fileManager->mkdir($depositDir);
if (!$fileManager->fileExists($this->generatePackage())) {
$this->_deposit->setLocalFailureStatus();
$depositDao->updateDeposit($this->_deposit);
return;
}
if (!$fileManager->fileExists($this->generateAtomDocument())) {
$this->_deposit->setLocalFailureStatus();
$depositDao->updateDeposit($this->_deposit);
return;
}
// update the deposit's status
$this->_deposit->setPackagedStatus();
$depositDao->updateDeposit($this->_deposit);
}
/**
* Update the deposit's status by checking with the PLN.
*/
function updateDepositStatus() {
$depositDao =& DAORegistry::getDAO('DepositDAO');
$plnPlugin =& PluginRegistry::getPlugin('generic','plnplugin');
$plnNetworks = unserialize(PLN_PLUGIN_NETWORKS);
$url = 'http://' . $plnNetworks[$plnPlugin->getSetting($this->_deposit->getJournalID(), 'pln_network')] . PLN_PLUGIN_CONT_IRI;
$url .= '/' . $plnPlugin->getSetting($this->_deposit->getJournalID(), 'journal_uuid');
$url .= '/' . $this->_deposit->getUUID() . '/state';
// retrieve the content document
$result = $plnPlugin->_curlGet($url);
// stop here if we didn't get an OK
if ($result['status'] != PLN_PLUGIN_HTTP_STATUS_OK) {
$this->_deposit->setRemoteFailureStatus();
$depositDao->updateDeposit($this->_deposit);
}
$contentState = new DOMDocument();
$contentState->preserveWhiteSpace = false;
$contentState->loadXML($result['result']);
// get the remote deposit state
$element = $contentState->getElementsByTagName('category')->item(0);
$state = $element->getAttribute('term');
switch ($state) {
case 'agreement':
$this->_deposit->setSyncedStatus();
case 'disagreement':
$this->_deposit->setSyncingStatus();
case 'in_progress':
$this->_deposit->setReceivedStatus();
break;
case 'failed':
$this->_deposit->setRemoteFailureStatus();
break;
}
$depositDao->updateDeposit($this->_deposit);
}
}
?>