-
Notifications
You must be signed in to change notification settings - Fork 2
/
document.php
176 lines (147 loc) · 5.62 KB
/
document.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
<?php
require_once 'src/TemplateRenderer.class.php';
include_once 'php/database.php';
// Extend DOMElement and DOMDocument to prevent unterminated entity reference errors
// per http://www.php.net/manual/en/domdocument.createelement.php#73617
class XDOMElement extends DOMElement {
function __construct($name, $value = null, $namespaceURI = null) {
parent::__construct($name, null, $namespaceURI);
}
}
class XDOMDocument extends DOMDocument {
function __construct($version = null, $encoding = null) {
parent::__construct($version, $encoding);
$this->registerNodeClass('DOMElement', 'XDOMElement');
}
function createElement($name, $value = null, $namespaceURI = null) {
$element = new XDOMElement($name, $value, $namespaceURI);
$element = $this->importNode($element);
if (!empty($value)) {
$element->appendChild(new DOMText($value));
}
return $element;
}
}
// Functions
function queryDB(){
$id = $_GET['id'];
$new_id = str_replace(".xml", "", $id);
$query =
"SELECT d.id id,
d.xml xml,
d.title title,
d.summary summary,
d.creation creation,
d.vectorLength vectorLength,
op.name origin,
dp.name destination,
ap.name author,
rp.name recipient,
rp.id toPersonId,
ap.id fromPersonId
FROM Document d
LEFT OUTER JOIN NormalizedPlace op
ON d.sentFromPlace = op.id
LEFT OUTER JOIN NormalizedPlace dp
ON d.sentFromPlace = dp.id
LEFT OUTER JOIN NormalizedPerson ap
ON d.sentFromPerson = ap.id
LEFT OUTER JOIN NormalizedPerson rp
ON d.sentToPerson = rp.id
WHERE d.id = '$new_id'";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;
return mysqli_fetch_assoc($result);
}
// TODO: handle converting HI to I via xslt (later)
function getCitation($row) {
$raw_xml = $row['xml'];
$doc = new XDOMDocument();
$success = $doc->loadXml( $raw_xml );
# we're looking for contents like this:
# <div1 type="body">
$all_bibls = $doc->getElementsByTagName('bibl');
$bibl = $all_bibls->item(0);
$citeString = $doc->saveXML($bibl);
$citeString = str_replace('<bibl>', '<div class="document__citation-body">', $citeString);
$citeString = str_replace('</bibl>', '</div>', $citeString);
$citeString = str_replace('<hi rend="italic">', '<span class="document__citation-publication">', $citeString);
$citeString = str_replace('</hi>', '</span>', $citeString);
// # now process the body
// logString($body_node->textContent);
return $citeString;
}
function getDocXmlFromRow($row) {
$raw_xml = $row['xml'];
$body_node = null;
$doc = new XDOMDocument();
$success = $doc->loadXml( $raw_xml );
return $doc;
}
function getLetterBodyNode($row, $doc=null) {
if($doc == null) {
$doc = getDocXmlFromRow($row);
}
# we're looking for contents like this:
# <div1 type="body">
$all_div1s = $doc->getElementsByTagName('div1');
foreach($all_div1s as $div1) {
$typeNode = $div1->attributes->getNamedItem("type");
if($typeNode) {
$type = $typeNode->value;
if($type == 'body') {
$body_node = $div1;
}
}
}
return $body_node;
}
function getLetterBodyForDisplay($row) {
$doc = getDocXmlFromRow($row);
$body_node = getLetterBodyNode($row, $doc);
transformBodyForDisplay($doc, $body_node);
return $doc->saveXML($body_node);
}
function transformBodyForDisplay($doc, $body) {
# change people to links
transformNames($doc, $body, 'persName');
# change places to links
transformNames($doc, $body, 'placeName');
}
function transformNames($doc, $body, $tagName) {
$all_names = $body->getElementsByTagName($tagName);
while($all_names->length > 0) {
logString("{$tagName} count={$all_names->length}");
foreach($all_names as $currName) {
$reference = $currName->textContent;
# clean up the reference for text search
# remove spaces from begining
$cleaned_reference = preg_replace('/^\s*/', '', $reference);
# remove spaces from end
$cleaned_reference = preg_replace('/\s*$/', '', $cleaned_reference);
# remove newline characters
$cleaned_reference = preg_replace('/[\n\r]/', ' ', $cleaned_reference);
# logString("reference [{$reference}] cleaned to [{$cleaned_reference}]");
$search_target = urlencode($cleaned_reference);
$link = $doc->createElement('a', $reference);
$link->setAttribute('class', "document__{$tagName}");
$link->setAttribute('href', "search?query={$search_target}");
$link->setAttribute('id', preg_replace('/\W+/', '', $cleaned_reference));
# logString($link->textContent);
$result = $currName->parentNode->replaceChild($link, $currName);
}
$all_names = $body->getElementsByTagName($tagName);
# continue until they are all transformed
}
}
connectToDB();
$result = queryDB();
$letter_body_display = getLetterBodyForDisplay($result);
$citation = getCitation($result);
$template = new TemplateRenderer();
// Include any variables as an array in the second param
print $template->render('document.html.twig', array(
'document' => $result,
'letter_body_display' => $letter_body_display,
'citation' => $citation,
'body_id' => 'document'
));