1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Colligator\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Colligator\Cover; |
7
|
|
|
use Colligator\Document; |
8
|
|
|
use Colligator\Http\Requests\SearchDocumentsRequest; |
9
|
|
|
use Colligator\Search\DocumentsIndex; |
10
|
|
|
use Colligator\Timing; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
|
13
|
|
|
class DocumentsController extends Controller |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Display a listing of the resource. |
17
|
|
|
* |
18
|
|
|
* @return Response |
19
|
|
|
*/ |
20
|
|
|
public function index(SearchDocumentsRequest $request, DocumentsIndex $se) |
21
|
|
|
{ |
22
|
|
|
$t0 = microtime(true); |
23
|
|
|
|
24
|
|
|
// Query ElasticSearch |
25
|
|
|
$response = $se->search($request); |
26
|
|
|
|
27
|
|
|
$t1 = microtime(true); |
28
|
|
|
|
29
|
|
|
// Build response, include pagination data |
30
|
|
|
$out = [ |
31
|
|
|
'warnings' => $request->warnings, |
32
|
|
|
'offset' => $response['offset'], |
33
|
|
|
'total' => intval($response['hits']['total']), |
34
|
|
|
'timing' => [ |
35
|
|
|
'elasticsearch' => $t1 - $t0, |
36
|
|
|
] |
37
|
|
|
]; |
38
|
|
|
Timing::create([ |
39
|
|
|
'event' => 'elasticsearch_request', |
40
|
|
|
'event_time' => Carbon::now(), |
41
|
|
|
'msecs' => round(($t1 - $t0) * 1000), |
42
|
|
|
'data' => 'results:' . count($response['hits']['hits']), |
43
|
|
|
]); |
44
|
|
|
$hits = count($response['hits']['hits']); |
45
|
|
|
if ($response['offset'] + $hits < $out['total']) { |
46
|
|
|
$out['continue'] = $response['offset'] + $hits; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$out['documents'] = []; |
50
|
|
|
foreach ($response['hits']['hits'] as $hit) { |
51
|
|
|
$out['documents'][] = $hit['_source']; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return response()->json($out); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Show the form for creating a new resource. |
59
|
|
|
* |
60
|
|
|
* @return Response |
61
|
|
|
*/ |
62
|
|
|
public function create() |
63
|
|
|
{ |
64
|
|
|
// |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Store a newly created resource in storage. |
69
|
|
|
* |
70
|
|
|
* @return Response |
71
|
|
|
*/ |
72
|
|
|
public function store() |
73
|
|
|
{ |
74
|
|
|
// |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Display the specified resource. |
79
|
|
|
* |
80
|
|
|
* @param DocumentsIndex $se |
81
|
|
|
* @param int $id |
82
|
|
|
* |
83
|
|
|
* @return Response |
84
|
|
|
*/ |
85
|
|
|
public function show(Request $request, DocumentsIndex $se, $id) |
86
|
|
|
{ |
87
|
|
|
if ($request->raw) { |
88
|
|
|
$doc = Document::find($id); |
89
|
|
|
} else { |
90
|
|
|
$doc = $se->get($id); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (is_null($doc)) { |
94
|
|
|
return response()->json([ |
95
|
|
|
'error' => 'Document not found.', |
96
|
|
|
]); |
97
|
|
|
} else { |
98
|
|
|
return response()->json([ |
99
|
|
|
'document' => $doc, |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Show the form for editing the specified resource. |
106
|
|
|
* |
107
|
|
|
* @param int $id |
108
|
|
|
* |
109
|
|
|
* @return Response |
110
|
|
|
*/ |
111
|
|
|
public function edit($id) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
// |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Update the specified resource in storage. |
118
|
|
|
* |
119
|
|
|
* @param int $id |
120
|
|
|
* |
121
|
|
|
* @return Response |
122
|
|
|
*/ |
123
|
|
|
public function update($id) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
// |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Remove the specified resource from storage. |
130
|
|
|
* |
131
|
|
|
* @param int $id |
132
|
|
|
* |
133
|
|
|
* @return Response |
134
|
|
|
*/ |
135
|
|
|
public function destroy($id) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
// |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function getDocumentFromSomeId($document_id) |
141
|
|
|
{ |
142
|
|
|
if (strlen($document_id) > 16) { |
143
|
|
|
return Document::with('cover') |
|
|
|
|
144
|
|
|
->where('bibsys_id', '=', $document_id) |
145
|
|
|
->firstOrFail(); |
146
|
|
|
} |
147
|
|
|
return Document::with('cover') |
148
|
|
|
->where('id', '=', $document_id) |
149
|
|
|
->orWhere('id', '=', $document_id) |
150
|
|
|
->firstOrFail(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Show cover. |
155
|
|
|
* |
156
|
|
|
* @return Response |
157
|
|
|
*/ |
158
|
|
|
public function cover($document_id) |
159
|
|
|
{ |
160
|
|
|
$doc = $this->getDocumentFromSomeId($document_id); |
161
|
|
|
|
162
|
|
|
return response()->json([ |
163
|
|
|
'cover' => $doc->cover, |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Store cover. |
169
|
|
|
* |
170
|
|
|
* @return Response |
171
|
|
|
*/ |
172
|
|
|
public function storeCover($document_id, Request $request, DocumentsIndex $se) |
173
|
|
|
{ |
174
|
|
|
$doc = $this->getDocumentFromSomeId($document_id); |
175
|
|
|
|
176
|
|
|
$inp = $request->input(); |
177
|
|
|
|
178
|
|
|
try { |
179
|
|
|
if (array_key_exists('url', $inp)) { |
180
|
|
|
if (is_null($inp['url'])) { |
181
|
|
|
if ($doc->cover) { |
182
|
|
|
\Log::debug("[DocumentsController] Removing cover from document {$doc->id}"); |
183
|
|
|
$doc->cover->delete(); |
184
|
|
|
} else { |
185
|
|
|
return response()->json([ |
186
|
|
|
'result' => 'error', |
187
|
|
|
'error' => 'There were no cover to remove', |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
$cover = null; |
191
|
|
|
} else { |
192
|
|
|
$this->validate($request, [ |
193
|
|
|
'url' => 'required|url', |
194
|
|
|
]); |
195
|
|
|
$cover = $doc->storeCover($request->url); |
196
|
|
|
$cover = $cover->toArray(); |
197
|
|
|
} |
198
|
|
|
} else { |
199
|
|
|
$data = $request->getContent(); |
200
|
|
|
if (empty($data)) { |
201
|
|
|
$this->validate($request, [ |
202
|
|
|
'url' => 'required|url', |
203
|
|
|
]); |
204
|
|
|
} |
205
|
|
|
$cover = $doc->storeCoverFromBlob($data); |
206
|
|
|
$cover = $cover->toArray(); |
207
|
|
|
} |
208
|
|
|
} catch (\ErrorException $e) { |
|
|
|
|
209
|
|
|
\Log::error('Failed to cache cover, got error: ' . $e->getMessage()); |
210
|
|
|
|
211
|
|
|
return response()->json([ |
212
|
|
|
'result' => 'error', |
213
|
|
|
'error' => 'Failed to store the cover. Please check that the URL points to a valid image file. Details: ' . $e->getMessage(), |
214
|
|
|
]); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
$se->indexById($doc->id); |
219
|
|
|
|
220
|
|
|
return response()->json([ |
221
|
|
|
'result' => 'ok', |
222
|
|
|
'cover' => $cover, |
223
|
|
|
]); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Store description. |
228
|
|
|
* |
229
|
|
|
* @return Response |
230
|
|
|
*/ |
231
|
|
|
public function storeDescription($document_id, Request $request, DocumentsIndex $se) |
232
|
|
|
{ |
233
|
|
|
$this->validate($request, [ |
234
|
|
|
// 'text' => 'required', |
|
|
|
|
235
|
|
|
// 'source' => 'required', |
|
|
|
|
236
|
|
|
// 'source_url' => 'url|required', |
|
|
|
|
237
|
|
|
]); |
238
|
|
|
|
239
|
|
|
$doc = $this->getDocumentFromSomeId($document_id); |
240
|
|
|
|
241
|
|
|
$doc->description = [ |
242
|
|
|
'text' => $request->text, |
243
|
|
|
'source' => $request->source, |
244
|
|
|
'source_url' => $request->source_url, |
245
|
|
|
]; |
246
|
|
|
|
247
|
|
|
if (is_null($request->text)) { |
248
|
|
|
\Log::info('Cleared description for ' . $doc->id); |
249
|
|
|
$doc->description = null; |
250
|
|
|
} else { |
251
|
|
|
\Log::info('Stored new description for ' . $doc->id); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$doc->save(); |
255
|
|
|
|
256
|
|
|
$se->indexById($doc->id); |
257
|
|
|
|
258
|
|
|
return response()->json([ |
259
|
|
|
'result' => 'ok', |
260
|
|
|
'description' => $doc->description, |
261
|
|
|
]); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Store "Cannot find cover" |
266
|
|
|
* |
267
|
|
|
* @return Response |
268
|
|
|
*/ |
269
|
|
|
public function cannotFindCover($document_id, Request $request, DocumentsIndex $se) |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
$doc = $this->getDocumentFromSomeId($document_id); |
272
|
|
|
|
273
|
|
|
try { |
274
|
|
|
\Log::debug("[DocumentsController] Adding 'cannotFindCover' status to {$doc->id}"); |
275
|
|
|
$doc->setCannotFindCover(); |
276
|
|
|
$doc->save(); |
277
|
|
|
|
278
|
|
|
} catch (\ErrorException $e) { |
|
|
|
|
279
|
|
|
\Log::error('Failed to store status, got error: ' . $e->getMessage()); |
280
|
|
|
|
281
|
|
|
return response()->json([ |
282
|
|
|
'result' => 'error', |
283
|
|
|
'error' => 'Failed to store status. Details: ' . $e->getMessage(), |
284
|
|
|
]); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$se->indexById($doc->id); |
288
|
|
|
|
289
|
|
|
return response()->json([ |
290
|
|
|
'result' => 'ok', |
291
|
|
|
'cannot_find_cover' => $doc->cannot_find_cover, |
292
|
|
|
]); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.