Open Journal Systems  3.3.0
packager_mets_swap.php
1 <?php
2 
4 
5  // The location of the files (without final directory)
6  public $sac_root_in;
7 
8  // The directory to zip up in the $sac_root_in directory
9  public $sac_dir_in;
10 
11  // The location to write the package out to
12  public $sac_root_out;
13 
14  // The filename to save the package as
15  public $sac_file_out;
16 
17  // The name of the metadata file
18  public $sac_metadata_filename = "mets.xml";
19 
20  // The type (e.g. ScholarlyWork)
21  public $sac_type;
22 
23  // The title of the item
24  public $sac_title;
25 
26  // The abstract of the item
27  public $sac_abstract;
28 
29  // Creators
30  public $sac_creators;
31 
32  // Subjects
33  public $sac_subjects;
34 
35  // Identifier
37 
38  // Date made available
40 
41  // Status
43 
44  // Copyright holder
46 
47  // Custodian
49 
50  // Bibliographic citation
51  public $sac_citation;
52 
53  // Language
54  public $sac_language;
55 
56  // File name
57  public $sac_files;
58 
59  // MIME type
61 
62  // Provenances
64 
65  // Rights
66  public $sac_rights;
67 
68  // Publisher
70 
71  // Number of files added
73 
74 
75  function __construct($sac_rootin, $sac_dirin, $sac_rootout, $sac_fileout) {
76  // Store the values
77  $this->sac_root_in = $sac_rootin;
78  $this->sac_dir_in = $sac_dirin;
79  $this->sac_root_out = $sac_rootout;
80  $this->sac_file_out = $sac_fileout;
81  $this->sac_creators = array();
82  $this->sac_subjects = array();
83  $this->sac_files = array();
84  $this->sac_mimetypes = array();
85  $this->sac_provenances = array();
86  $this->sac_rights = array();
87  $this->sac_filecount = 0;
88  }
89 
90  function setType($sac_thetype) {
91  $this->sac_type = $sac_thetype;
92  }
93 
94  function setTitle($sac_thetitle) {
95  $this->sac_title = $this->clean($sac_thetitle);
96  }
97 
98  function setAbstract($sac_thetitle) {
99  $this->sac_abstract = $this->clean($sac_thetitle);
100  }
101 
102  function addCreator($sac_creator) {
103  array_push($this->sac_creators, $this->clean($sac_creator));
104  }
105 
106  function addSubject($sac_subject) {
107  array_push($this->sac_subjects, $this->clean($sac_subject));
108  }
109 
110  function addProvenance($sac_provenance) {
111  array_push($this->sac_provenances, $this->clean($sac_provenance));
112  }
113 
114  function addRights($sac_right) {
115  array_push($this->sac_rights, $this->clean($sac_right));
116  }
117 
118  function setIdentifier($sac_theidentifier) {
119  $this->sac_identifier = $sac_theidentifier;
120  }
121 
122  function setStatusStatement($sac_thestatus) {
123  $this->sac_statusstatement = $sac_thestatus;
124  }
125 
126  function setCopyrightHolder($sac_thecopyrightholder) {
127  $this->sac_copyrightholder = $this->clean($sac_thecopyrightholder);
128  }
129 
130  function setCustodian($sac_thecustodian) {
131  $this->sac_custodian = $this->clean($sac_thecustodian);
132  }
133 
134  function setCitation($sac_thecitation) {
135  $this->sac_citation = $this->clean($sac_thecitation);
136  }
137 
138  function setLanguage($sac_thelanguage) {
139  $this->sac_language = $this->clean($sac_thelanguage);
140  }
141 
142  function setDateAvailable($sac_thedta) {
143  $this->sac_dateavailable = $sac_thedta;
144  }
145 
146  function setPublisher($sac_thepublisher) {
147  $this->sac_publisher = $sac_thepublisher;
148  }
149 
150  function addFile($sac_thefile, $sac_themimetype) {
151  array_push($this->sac_files, $sac_thefile);
152  array_push($this->sac_mimetypes, $sac_themimetype);
153  $this->sac_filecount++;
154  }
155 
156  function addMetadata($sac_theelement, $sac_thevalue) {
157  switch ($sac_theelement) {
158  case "abstract":
159  $this->setAbstract($sac_thevalue);
160  break;
161  case "available":
162  $this->setDateAvailable($sac_thevalue);
163  break;
164  case "bibliographicCitation":
165  $this->setCitation($sac_thevalue);
166  break;
167  case "creator":
168  $this->addCreator($sac_thevalue);
169  break;
170  case "identifier":
171  $this->setIdentifier($sac_thevalue);
172  break;
173  case "publisher":
174  $this->setPublisher($sac_thevalue);
175  break;
176  case "title":
177  $this->setTitle($sac_thevalue);
178  break;
179  }
180  }
181 
182  function create() {
183  // Write the metadata (mets) file
184  $fh = @fopen($this->sac_root_in . '/' . $this->sac_dir_in . '/' . $this->sac_metadata_filename, 'w');
185  if (!$fh) {
186  throw new Exception("Error writing metadata file (" .
187  $this->sac_root_in . '/' . $this->sac_dir_in . '/' . $this->sac_metadata_filename . ")");
188  }
189  $this->writeHeader($fh);
190  $this->writeDmdSec($fh);
191  $this->writeFileGrp($fh);
192  $this->writeStructMap($fh);
193  $this->writeFooter($fh);
194  fclose($fh);
195 
196  // Create the zipped package (force an overwrite if it already exists)
197  $zip = new ZipArchive();
198  $zip->open($this->sac_root_out . '/' . $this->sac_file_out, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
199  $zip->addFile($this->sac_root_in . '/' . $this->sac_dir_in . '/mets.xml',
200  'mets.xml');
201 
202  for ($i = 0; $i < $this->sac_filecount; $i++) {
203 
204  $zip->addFile($this->sac_root_in . '/' . $this->sac_dir_in . '/' . $this->sac_files[$i],
205  $this->sac_files[$i]);
206  }
207  $zip->close();
208  }
209 
210  function writeheader($fh) {
211  fwrite($fh, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\" ?" . ">\n");
212  fwrite($fh, "<mets ID=\"sort-mets_mets\" OBJID=\"sword-mets\" LABEL=\"DSpace SWORD Item\" PROFILE=\"DSpace METS SIP Profile 1.0\" xmlns=\"http://www.loc.gov/METS/\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/mets.xsd\">\n");
213  fwrite($fh, "\t<metsHdr CREATEDATE=\"2008-09-04T00:00:00\">\n");
214  fwrite($fh, "\t\t<agent ROLE=\"CUSTODIAN\" TYPE=\"ORGANIZATION\">\n");
215  if (isset($this->sac_custodian)) { fwrite($fh, "\t\t\t<name>$this->sac_custodian</name>\n"); }
216  else { fwrite($fh, "\t\t\t<name>Unknown</name>\n"); }
217  fwrite($fh, "\t\t</agent>\n");
218  fwrite($fh, "\t</metsHdr>\n");
219  }
220 
221  function writeDmdSec($fh) {
222  fwrite($fh, "<dmdSec ID=\"sword-mets-dmd-1\" GROUPID=\"sword-mets-dmd-1_group-1\">\n");
223  fwrite($fh, "<mdWrap LABEL=\"SWAP Metadata\" MDTYPE=\"OTHER\" OTHERMDTYPE=\"EPDCX\" MIMETYPE=\"text/xml\">\n");
224  fwrite($fh, "<xmlData>\n");
225  fwrite($fh, "<epdcx:descriptionSet xmlns:epdcx=\"http://purl.org/eprint/epdcx/2006-11-16/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://purl.org/eprint/epdcx/2006-11-16/ http://purl.org/eprint/epdcx/xsd/2006-11-16/epdcx.xsd\">\n");
226  fwrite($fh, "<epdcx:description epdcx:resourceId=\"sword-mets-epdcx-1\">\n");
227 
228  if (isset($this->sac_type)) {
229  $this->statementVesURIValueURI($fh,
230  "http://purl.org/dc/elements/1.1/type",
231  "http://purl.org/eprint/terms/Type",
232  $this->sac_type);
233  }
234 
235  if (isset($this->sac_title)) {
236  $this->statement($fh,
237  "http://purl.org/dc/elements/1.1/title",
238  $this->valueString($this->sac_title));
239  }
240 
241  if (isset($this->sac_abstract)) {
242  $this->statement($fh,
243  "http://purl.org/dc/terms/abstract",
244  $this->valueString($this->sac_abstract));
245  }
246 
247  foreach ($this->sac_creators as $sac_creator) {
248  $this->statement($fh,
249  "http://purl.org/dc/elements/1.1/creator",
250  $this->valueString($sac_creator));
251  }
252 
253  foreach ($this->sac_subjects as $sac_subject) {
254  $this->statement($fh,
255  "http://purl.org/dc/elements/1.1/subject",
256  $this->valueString($sac_subject));
257  }
258 
259  foreach ($this->sac_provenances as $sac_provenance) {
260  $this->statement($fh,
261  "http://purl.org/dc/terms/provenance",
262  $this->valueString($sac_provenance));
263  }
264 
265  foreach ($this->sac_rights as $sac_right) {
266  $this->statement($fh,
267  "http://purl.org/dc/terms/rights",
268  $this->valueString($sac_right));
269  }
270 
271  if (isset($this->sac_identifier)) {
272  $this->statement($fh,
273  "http://purl.org/dc/elements/1.1/identifier",
274  $this->valueString($this->sac_identifier));
275  }
276 
277  if (isset($this->sac_publisher)) {
278  $this->statement($fh,
279  "http://purl.org/dc/elements/1.1/publisher",
280  $this->valueString($this->sac_publisher));
281  }
282 
283  fwrite($fh, "<epdcx:statement epdcx:propertyURI=\"http://purl.org/eprint/terms/isExpressedAs\" " .
284  "epdcx:valueRef=\"sword-mets-expr-1\" />\n");
285 
286  fwrite($fh, "</epdcx:description>\n");
287 
288  fwrite($fh, "<epdcx:description epdcx:resourceId=\"sword-mets-expr-1\">\n");
289 
290  $this->statementValueURI($fh,
291  "http://purl.org/dc/elements/1.1/type",
292  "http://purl.org/eprint/entityType/Expression");
293 
294  if (isset($this->sac_language)) {
295  $this->statementVesURI($fh,
296  "http://purl.org/dc/elements/1.1/language",
297  "http://purl.org/dc/terms/RFC3066",
298  $this->valueString($this->sac_language));
299  }
300 
301  $this->statementVesURIValueURI($fh,
302  "http://purl.org/dc/elements/1.1/type",
303  "http://purl.org/eprint/terms/Type",
304  "http://purl.org/eprint/entityType/Expression");
305 
306  if (isset($this->sac_dateavailable)) {
307  $this->statement($fh,
308  "http://purl.org/dc/terms/available",
309  $this->valueStringSesURI("http://purl.org/dc/terms/W3CDTF",
310  $this->sac_dateavailable));
311  }
312 
313  if (isset($this->sac_statusstatement)) {
314  $this->statementVesURIValueURI($fh,
315  "http://purl.org/eprint/terms/Status",
316  "http://purl.org/eprint/terms/Status",
317  $this->sac_statusstatement);
318  }
319 
320  if (isset($this->sac_copyrightholder)) {
321  $this->statement($fh,
322  "http://purl.org/eprint/terms/copyrightHolder",
323  $this->valueString($this->sac_copyrightholder));
324  }
325 
326  if (isset($this->sac_citation)) {
327  $this->statement($fh,
328  "http://purl.org/eprint/terms/bibliographicCitation",
329  $this->valueString($this->sac_citation));
330  }
331 
332  fwrite($fh, "</epdcx:description>\n");
333 
334  fwrite($fh, "</epdcx:descriptionSet>\n");
335  fwrite($fh, "</xmlData>\n");
336  fwrite($fh, "</mdWrap>\n");
337  fwrite($fh, "</dmdSec>\n");
338  }
339 
340  function writeFileGrp($fh) {
341  fwrite($fh, "\t<fileSec>\n");
342  fwrite($fh, "\t\t<fileGrp ID=\"sword-mets-fgrp-1\" USE=\"CONTENT\">\n");
343  for ($i = 0; $i < $this->sac_filecount; $i++) {
344  fwrite($fh, "\t\t\t<file GROUPID=\"sword-mets-fgid-0\" ID=\"sword-mets-file-" . $i ."\" " .
345  "MIMETYPE=\"" . $this->sac_mimetypes[$i] . "\">\n");
346  fwrite($fh, "\t\t\t\t<FLocat LOCTYPE=\"URL\" xlink:href=\"" . $this->clean($this->sac_files[$i]) . "\" />\n");
347  fwrite($fh, "\t\t\t</file>\n");
348  }
349  fwrite($fh, "\t\t</fileGrp>\n");
350  fwrite($fh, "\t</fileSec>\n");
351  }
352 
353  function writeStructMap($fh) {
354  fwrite($fh, "\t<structMap ID=\"sword-mets-struct-1\" LABEL=\"structure\" TYPE=\"LOGICAL\">\n");
355  fwrite($fh, "\t\t<div ID=\"sword-mets-div-1\" DMDID=\"sword-mets-dmd-1\" TYPE=\"SWORD Object\">\n");
356  fwrite($fh, "\t\t\t<div ID=\"sword-mets-div-2\" TYPE=\"File\">\n");
357  for ($i = 0; $i < $this->sac_filecount; $i++) {
358  fwrite($fh, "\t\t\t\t<fptr FILEID=\"sword-mets-file-" . $i . "\" />\n");
359  }
360  fwrite($fh, "\t\t\t</div>\n");
361  fwrite($fh, "\t\t</div>\n");
362  fwrite($fh, "\t</structMap>\n");
363  }
364 
365  function writeFooter($fh) {
366  fwrite($fh, "</mets>\n");
367  }
368 
369  function valueString($value) {
370  return "<epdcx:valueString>" .
371  $value .
372  "</epdcx:valueString>\n";
373  }
374 
375  function valueStringSesURI($sesURI, $value) {
376  return "<epdcx:valueString epdcx:sesURI=\"" . $sesURI . "\">" .
377  $value .
378  "</epdcx:valueString>\n";
379  }
380 
381  function statement($fh, $propertyURI, $value) {
382  fwrite($fh, "<epdcx:statement epdcx:propertyURI=\"" . $propertyURI . "\">\n" .
383  $value .
384  "</epdcx:statement>\n");
385  }
386 
387  function statementValueURI($fh, $propertyURI, $value) {
388  fwrite($fh, "<epdcx:statement epdcx:propertyURI=\"" . $propertyURI . "\" " .
389  "epdcx:valueURI=\"" . $value . "\" />\n");
390  }
391 
392  function statementVesURI($fh, $propertyURI, $vesURI, $value) {
393  fwrite($fh, "<epdcx:statement epdcx:propertyURI=\"" . $propertyURI . "\" " .
394  "epdcx:vesURI=\"" . $vesURI . "\">\n" .
395  $value .
396  "</epdcx:statement>\n");
397  }
398 
399  function statementVesURIValueURI($fh, $propertyURI, $vesURI, $value) {
400  fwrite($fh, "<epdcx:statement epdcx:propertyURI=\"" . $propertyURI . "\" " .
401  "epdcx:vesURI=\"" . $vesURI . "\" " .
402  "epdcx:valueURI=\"" . $value . "\" />\n");
403  }
404 
405  function clean($data) {
406  return str_replace('&#039;', '&apos;', htmlspecialchars($data, ENT_QUOTES));
407  }
408 }
409 ?>
PackagerMetsSwap\setTitle
setTitle($sac_thetitle)
Definition: packager_mets_swap.php:94
PackagerMetsSwap\setCopyrightHolder
setCopyrightHolder($sac_thecopyrightholder)
Definition: packager_mets_swap.php:126
PackagerMetsSwap\addFile
addFile($sac_thefile, $sac_themimetype)
Definition: packager_mets_swap.php:150
PackagerMetsSwap\$sac_creators
$sac_creators
Definition: packager_mets_swap.php:30
PackagerMetsSwap\setCitation
setCitation($sac_thecitation)
Definition: packager_mets_swap.php:134
PackagerMetsSwap\setDateAvailable
setDateAvailable($sac_thedta)
Definition: packager_mets_swap.php:142
PackagerMetsSwap\$sac_dateavailable
$sac_dateavailable
Definition: packager_mets_swap.php:39
PackagerMetsSwap\$sac_provenances
$sac_provenances
Definition: packager_mets_swap.php:63
PackagerMetsSwap\writeStructMap
writeStructMap($fh)
Definition: packager_mets_swap.php:353
PackagerMetsSwap\$sac_filecount
$sac_filecount
Definition: packager_mets_swap.php:72
PackagerMetsSwap\$sac_citation
$sac_citation
Definition: packager_mets_swap.php:51
PackagerMetsSwap\$sac_title
$sac_title
Definition: packager_mets_swap.php:24
PackagerMetsSwap\writeFooter
writeFooter($fh)
Definition: packager_mets_swap.php:365
PackagerMetsSwap\setCustodian
setCustodian($sac_thecustodian)
Definition: packager_mets_swap.php:130
PackagerMetsSwap
Definition: packager_mets_swap.php:3
PackagerMetsSwap\$sac_identifier
$sac_identifier
Definition: packager_mets_swap.php:36
PackagerMetsSwap\$sac_metadata_filename
$sac_metadata_filename
Definition: packager_mets_swap.php:18
PackagerMetsSwap\addProvenance
addProvenance($sac_provenance)
Definition: packager_mets_swap.php:110
PackagerMetsSwap\valueStringSesURI
valueStringSesURI($sesURI, $value)
Definition: packager_mets_swap.php:375
PackagerMetsSwap\setStatusStatement
setStatusStatement($sac_thestatus)
Definition: packager_mets_swap.php:122
PackagerMetsSwap\setAbstract
setAbstract($sac_thetitle)
Definition: packager_mets_swap.php:98
PackagerMetsSwap\$sac_type
$sac_type
Definition: packager_mets_swap.php:21
PackagerMetsSwap\$sac_file_out
$sac_file_out
Definition: packager_mets_swap.php:15
PackagerMetsSwap\setIdentifier
setIdentifier($sac_theidentifier)
Definition: packager_mets_swap.php:118
PackagerMetsSwap\addRights
addRights($sac_right)
Definition: packager_mets_swap.php:114
PackagerMetsSwap\writeDmdSec
writeDmdSec($fh)
Definition: packager_mets_swap.php:221
PackagerMetsSwap\$sac_root_out
$sac_root_out
Definition: packager_mets_swap.php:12
PackagerMetsSwap\$sac_mimetypes
$sac_mimetypes
Definition: packager_mets_swap.php:60
PackagerMetsSwap\clean
clean($data)
Definition: packager_mets_swap.php:405
PackagerMetsSwap\valueString
valueString($value)
Definition: packager_mets_swap.php:369
PackagerMetsSwap\writeFileGrp
writeFileGrp($fh)
Definition: packager_mets_swap.php:340
PackagerMetsSwap\$sac_language
$sac_language
Definition: packager_mets_swap.php:54
PackagerMetsSwap\$sac_root_in
$sac_root_in
Definition: packager_mets_swap.php:6
PackagerMetsSwap\writeheader
writeheader($fh)
Definition: packager_mets_swap.php:210
PackagerMetsSwap\create
create()
Definition: packager_mets_swap.php:182
PackagerMetsSwap\$sac_subjects
$sac_subjects
Definition: packager_mets_swap.php:33
PackagerMetsSwap\setLanguage
setLanguage($sac_thelanguage)
Definition: packager_mets_swap.php:138
PackagerMetsSwap\setPublisher
setPublisher($sac_thepublisher)
Definition: packager_mets_swap.php:146
PackagerMetsSwap\statementVesURIValueURI
statementVesURIValueURI($fh, $propertyURI, $vesURI, $value)
Definition: packager_mets_swap.php:399
PackagerMetsSwap\$sac_dir_in
$sac_dir_in
Definition: packager_mets_swap.php:9
PackagerMetsSwap\$sac_publisher
$sac_publisher
Definition: packager_mets_swap.php:69
PackagerMetsSwap\addSubject
addSubject($sac_subject)
Definition: packager_mets_swap.php:106
PackagerMetsSwap\$sac_statusstatement
$sac_statusstatement
Definition: packager_mets_swap.php:42
PackagerMetsSwap\addCreator
addCreator($sac_creator)
Definition: packager_mets_swap.php:102
PackagerMetsSwap\$sac_abstract
$sac_abstract
Definition: packager_mets_swap.php:27
PackagerMetsSwap\statementValueURI
statementValueURI($fh, $propertyURI, $value)
Definition: packager_mets_swap.php:387
PackagerMetsSwap\$sac_copyrightholder
$sac_copyrightholder
Definition: packager_mets_swap.php:45
PackagerMetsSwap\statementVesURI
statementVesURI($fh, $propertyURI, $vesURI, $value)
Definition: packager_mets_swap.php:392
PackagerMetsSwap\addMetadata
addMetadata($sac_theelement, $sac_thevalue)
Definition: packager_mets_swap.php:156
PackagerMetsSwap\$sac_custodian
$sac_custodian
Definition: packager_mets_swap.php:48
PackagerMetsSwap\__construct
__construct($sac_rootin, $sac_dirin, $sac_rootout, $sac_fileout)
Definition: packager_mets_swap.php:75
PackagerMetsSwap\$sac_files
$sac_files
Definition: packager_mets_swap.php:57
PackagerMetsSwap\$sac_rights
$sac_rights
Definition: packager_mets_swap.php:66
PackagerMetsSwap\setType
setType($sac_thetype)
Definition: packager_mets_swap.php:90
PackagerMetsSwap\statement
statement($fh, $propertyURI, $value)
Definition: packager_mets_swap.php:381