-
Notifications
You must be signed in to change notification settings - Fork 17
/
packager_atom_twostep.php
140 lines (110 loc) · 4.64 KB
/
packager_atom_twostep.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
<?php
require_once('utils.php');
class PackagerAtomTwoStep {
// The location of the files (without final directory)
private $sac_root_in;
// The location to write the package out to
private $sac_root_out;
// The filename to save the package as
private $sac_file_out;
// File names
private $sac_files;
// Number of files added
private $sac_filecount;
// The dcterms metadata
private $sac_entry_dctermsFields;
private $sac_entry_dctermsValues;
private $sac_entry_dctermsAttributes;
// The entry title
private $sac_entry_title;
// The entry id
private $sac_entry_id;
// The entry updated date / time stamp
private $sac_entry_updated;
// The entry author names
private $sac_entry_authors;
// The entry summary text
private $sac_entry_summary;
function __construct($sac_rootin, $sac_dirin, $sac_rootout, $sac_fileout) {
// Store the values
$this->sac_root_in = $sac_rootin;
$this->sac_dir_in = $sac_dirin;
$this->sac_root_out = $sac_rootout;
$this->sac_file_out = $sac_fileout;
$this->sac_files = array();
$this->sac_mimetypes = array();
$this->sac_filecount = 0;
$this->sac_entry_dctermsFields = array();
$this->sac_entry_dctermsValues = array();
$this->sac_entry_dctermsAttributes = array();
$this->sac_entry_authors = array();
}
function setTitle($sac_thetitle) {
$this->sac_entry_title = $this->clean($sac_thetitle);
}
function setIdentifier($sac_theID) {
$this->sac_entry_id = $this->clean($sac_theID);
}
function setUpdated($sac_theUpdated) {
$this->sac_entry_updated = $this->clean($sac_theUpdated);
}
function addEntryAuthor($sac_theauthor) {
array_push($this->sac_entry_authors, $this->clean($sac_theauthor));
}
function setSummary($sac_theSummary) {
$this->sac_entry_summary = $this->clean($sac_theSummary);
}
function addMetadata($sac_theElement, $sac_theValue, $sac_theAttributes = array()) {
array_push($this->sac_entry_dctermsFields, $this->clean($sac_theElement));
array_push($this->sac_entry_dctermsValues, $this->clean($sac_theValue));
$sac_cleanAttributes = array();
foreach ($sac_theAttributes as $attrName => $attrValue) {
$sac_cleanAttributes[$this->clean($attrName)] = $this->clean($attrValue);
}
array_push($this->sac_entry_dctermsAttributes, $sac_cleanAttributes);
}
function addFile($sac_thefile) {
array_push($this->sac_files, $sac_thefile);
$this->sac_filecount++;
}
function getFiles() {
return $this->sac_files;
}
function create() {
// Write the atom entry manifest
$sac_atom = $this->sac_root_in . '/' . $this->sac_dir_in . '/atom';
$fh = @fopen($sac_atom, 'w');
if (!$fh) {
throw new Exception("Error writing atom entry manifest (" .
$this->sac_root_in . '/' . $this->sac_dir_in . '/atom)');
}
// Write the atom entry header
fwrite($fh, "<?xml version=\"1.0\"?>\n");
fwrite($fh, "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:dcterms=\"http://purl.org/dc/terms/\">\n");
if (!empty($this->sac_entry_title)) fwrite($fh, "\t<title>" . $this->sac_entry_title . "</title>\n");
if (!empty($this->sac_entry_id)) fwrite($fh, "\t<id>" . $this->sac_entry_id . "</id>\n");
if (!empty($this->sac_entry_updated)) fwrite($fh, "\t<updated>" . $this->sac_entry_updated . "</updated>\n");
foreach ($this->sac_entry_authors as $sac_author) {
fwrite($fh, "\t<author><name>" . $sac_author . "</name></author>\n");
}
if (!empty($this->sac_entry_summary)) fwrite($fh, "\t<summary>" . $this->sac_entry_summary . "</summary>\n");
// Write the dcterms metadata
for ($i = 0; $i < count($this->sac_entry_dctermsFields); $i++) {
$dcElement = "\t<dcterms:" . $this->sac_entry_dctermsFields[$i];
if (!empty($this->sac_entry_dctermsAttributes[$i])) {
foreach ($this->sac_entry_dctermsAttributes[$i] as $attrName => $attrValue) {
$dcElement .= " $attrName=\"$attrValue\"";
}
}
$dcElement .= ">" . $this->sac_entry_dctermsValues[$i] . "</dcterms:" . $this->sac_entry_dctermsFields[$i] . ">\n";
fwrite($fh, $dcElement);
}
// Close the file
fwrite($fh, "</entry>\n");
fclose($fh);
}
function clean($data) {
return str_replace(''', ''', htmlspecialchars($data, ENT_QUOTES));
}
}
?>