forked from revealit/ting_fulltext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathting_fulltext.parse.inc
104 lines (90 loc) · 2.69 KB
/
ting_fulltext.parse.inc
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
<?php
/**
* @file
* Rudimentary parser for a simple subset of docbook.
*/
/**
* Parse given xml. Return an array holding selected values.
*/
function ting_fulltext_parse($xml) {
static $xpath;
$ret = array();
if (!isset($xpath)) {
$xpath = ting_fulltext_get_xpath($xml);
}
// Main title.
$query = '//docbook:article/docbook:title';
$nodelist = $xpath->query($query);
if ($nodelist->length > 0) {
(isset($nodelist->item(0)->nodeValue) ? $ret['title'] = $nodelist->item(0)->nodeValue : $ret['title'] = '');
}
// Author first name.
$query = '//docbook:firstname';
$nodelist = $xpath->query($query);
if ($nodelist->length > 0) {
(isset($nodelist->item(0)->nodeValue) ? $ret['firstname'] = $nodelist->item(0)->nodeValue : $ret['firstname'] = '');
}
// Author surname.
$query = '//docbook:surname';
$nodelist = $xpath->query($query);
if ($nodelist->length > 0) {
(isset($nodelist->item(0)->nodeValue) ? $ret['surname'] = $nodelist->item(0)->nodeValue : $ret['surname'] = '');
}
// Section (there can be more than one section) and furthermore there can
// be one or more sections in a section.
$query = '//docbook:section';
$nodelist = $xpath->query($query);
if ($nodelist->length > 0) {
foreach ($nodelist as $node) {
$sections[] = ting_fulltext_get_section($node);
}
$ret['section'] = $sections;
}
// <docbook:subjectset><docbook:subject><docbook:subjectitem>.
$query = '//docbook:subjectitem';
$nodelist = $xpath->query($query);
if ($nodelist->length > 0) {
foreach ($nodelist as $node) {
$ret['subject'][] = $node->nodeValue;
}
}
return $ret;
}
/**
* Get a docbook section?.
*
* Not sure whether this is a complete parse of section - there might be more
* cases. TODO make this parse complete? what? Document me.
*/
function ting_fulltext_get_section($node) {
foreach ($node->childNodes as $child) {
switch ($child->nodeName) {
case 'docbook:title':
$ret['title'] = $child->nodeValue;
break;
// There might be more than one para in each section.
case 'docbook:para':
$ret['para'][] = $child->nodeValue;
break;
case 'docbook:info':
$ret['title'] = $child->getElementsByTagName('title')->item(0)->nodeValue;
break;
case 'docbook:section':
// Recursive.
ting_fulltext_get_section($child);
break;
}
}
return $ret;
}
/**
* Prepare XML string for xpath processing.
*/
function ting_fulltext_get_xpath($xml) {
// TODO errorhandling.
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');
return $xpath;
}