1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Colligator\Search; |
4
|
|
|
|
5
|
|
|
use Colligator\Document; |
6
|
|
|
use Colligator\XisbnResponse; |
7
|
|
|
|
8
|
|
|
class SearchableDocument |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var DocumentsIndex |
12
|
|
|
*/ |
13
|
|
|
protected $docIndex; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $sortableCallCodePattern = '/FA ([0-9]+)(\/([A-Z]))?/'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Document $doc |
22
|
|
|
* @param DocumentsIndex $docIndex |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Document $doc, DocumentsIndex $docIndex) |
25
|
|
|
{ |
26
|
|
|
$this->doc = $doc; |
27
|
|
|
$this->docIndex = $docIndex; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Generate ElasticSearch index payload. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function toArray() |
36
|
|
|
{ |
37
|
|
|
$body = $this->doc->bibliographic; // PHP makes a copy for us |
38
|
|
|
|
39
|
|
|
$body['id'] = $this->doc->id; |
40
|
|
|
$body['bibsys_id'] = $this->doc->bibsys_id; |
41
|
|
|
|
42
|
|
|
// Remove some stuff we don't need |
43
|
|
|
foreach (['agency', 'catalogingRules', 'debug', 'modified', 'extent', 'cover_image'] as $key) { |
44
|
|
|
unset($body[$key]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Add local collections |
48
|
|
|
$body['collections'] = []; |
49
|
|
|
foreach ($this->doc->collections as $collection) { |
50
|
|
|
$body['collections'][] = $collection['name']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Add cover |
54
|
|
|
$body['cover'] = $this->doc->cover ? $this->doc->cover->toArray() : null; |
55
|
|
|
|
56
|
|
|
// Add subjects |
57
|
|
|
$body['subjects'] = []; |
58
|
|
|
foreach ($this->doc->subjects as $subject) { |
59
|
|
|
$body['subjects'][$subject['vocabulary'] ?: 'keywords'][] = [ |
60
|
|
|
'id' => array_get($subject, 'id'), |
61
|
|
|
'prefLabel' => str_replace('--', ' : ', array_get($subject, 'term')), |
62
|
|
|
'type' => array_get($subject, 'type'), |
63
|
|
|
'count' => $this->docIndex->getUsageCount(array_get($subject, 'id'), 'subject'), |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Add genres |
68
|
|
|
$body['genres'] = []; |
69
|
|
|
foreach ($this->doc->genres as $genre) { |
70
|
|
|
$body['genres'][$genre['vocabulary'] ?: 'keywords'][] = [ |
71
|
|
|
'id' => array_get($genre, 'id'), |
72
|
|
|
'prefLabel' => array_get($genre, 'term'), |
73
|
|
|
'count' => $this->docIndex->getUsageCount(array_get($genre, 'id'), 'genre'), |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Add holdings |
78
|
|
|
$this->addHoldings($body, $this->doc); |
79
|
|
|
|
80
|
|
|
// Add xisbns |
81
|
|
|
$body['xisbns'] = (new XisbnResponse($this->doc->xisbn))->getSimpleRepr(); |
82
|
|
|
|
83
|
|
|
// Add description |
84
|
|
|
$body['description'] = $this->doc->description; |
85
|
|
|
|
86
|
|
|
// Add 'other form' |
87
|
|
|
$otherFormId = array_get($body, 'other_form.id'); |
88
|
|
|
if (!empty($otherFormId)) { |
89
|
|
|
|
90
|
|
|
// @TODO: https://github.com/scriptotek/colligator-backend/issues/34 |
91
|
|
|
// Not sure how to handle this in Alma yet |
92
|
|
|
unset($body['other_form']); |
93
|
|
|
// $otherFormDoc = Document::where('bibsys_id', '=', $otherFormId)->firstOrFail(); |
|
|
|
|
94
|
|
|
// $body['other_form'] = [ |
|
|
|
|
95
|
|
|
// 'id' => $otherFormDoc->id, |
|
|
|
|
96
|
|
|
// 'bibsys_id' => $otherFormDoc->bibsys_id, |
|
|
|
|
97
|
|
|
// 'electronic' => $otherFormDoc->isElectronic(), |
|
|
|
|
98
|
|
|
// ]; |
99
|
|
|
// $this->addHoldings($body['other_form'], $otherFormDoc); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Add 'cannot_find_cover' |
103
|
|
|
$body['cannot_find_cover'] = $this->doc->cannot_find_cover; |
104
|
|
|
|
105
|
|
|
return $body; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function sortableCallCode($holding) |
109
|
|
|
{ |
110
|
|
|
$m = preg_match($this->sortableCallCodePattern, array_get($holding, 'callcode'), $matches); |
111
|
|
|
if ($m) { |
112
|
|
|
return intval($matches[1]); |
113
|
|
|
// TODO: Også ta hensyn til undersortering i $matches[3], men |
114
|
|
|
// denne er en blanding av romertall og alfabetisk sortering |
115
|
|
|
// https://github.com/scriptotek/colligator-backend/issues/28 |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function addHoldings(&$body, Document $doc) |
122
|
|
|
{ |
123
|
|
|
if ($doc->isElectronic()) { |
|
|
|
|
124
|
|
|
|
125
|
|
|
// @ TODO: Virker ikke med Alma |
126
|
|
|
// https://github.com/scriptotek/colligator-backend/issues/34 |
127
|
|
|
// $body['fulltext'] = $this->fulltextFromHoldings($doc->holdings); |
|
|
|
|
128
|
|
|
|
129
|
|
|
} else { |
130
|
|
|
$body['holdings'] = []; |
131
|
|
|
foreach ($doc->holdings as $holding) { |
132
|
|
|
array_forget($holding, 'fulltext'); |
133
|
|
|
array_forget($holding, 'bibliographic_record'); |
134
|
|
|
array_forget($holding, 'nonpublic_notes'); |
135
|
|
|
$s = $this->sortableCallCode($holding); |
136
|
|
|
if (!is_null($s)) { |
137
|
|
|
$holding['callcodeSortable'] = $s; |
138
|
|
|
} |
139
|
|
|
$body['holdings'][] = $holding; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function fulltextFromHoldings($holdings) |
145
|
|
|
{ |
146
|
|
|
$fulltext = [ |
147
|
|
|
'access' => false, |
148
|
|
|
]; |
149
|
|
|
foreach ($holdings as $holding) { |
150
|
|
|
if (!count($holding['fulltext'])) { |
151
|
|
|
continue; |
152
|
|
|
} |
153
|
|
|
if ($holding['location'] == 'UBO' || stripos($holding['fulltext'][0]['comment'], 'gratis') !== false) { |
154
|
|
|
$fulltext = $holding['fulltext'][0]; |
155
|
|
|
$fulltext['access'] = true; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $fulltext; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.