forked from piernov/zotprime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFullTextController.php
160 lines (123 loc) · 4.28 KB
/
FullTextController.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
<?php
/*
***** BEGIN LICENSE BLOCK *****
This file is part of the Zotero Data Server.
Copyright © 2013 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
require('ApiController.php');
class FullTextController extends ApiController {
public function __construct($controllerName, $action, $params) {
parent::__construct($controllerName, $action, $params);
}
public function fulltext() {
$this->allowMethods(['GET', 'POST']);
// Check for general library access
if (!$this->permissions->canAccess($this->objectLibraryID)) {
$this->e403();
}
// Multi-item write
if ($this->isWriteMethod()) {
if ($this->apiVersion < 3) {
$this->e405();
}
// Check for library write access
if (!$this->permissions->canWrite($this->objectLibraryID)) {
$this->e403("Write access denied");
}
$this->requireContentType("application/json");
// Make sure library hasn't been modified
$this->checkLibraryIfUnmodifiedSinceVersion(true);
Zotero_Libraries::updateVersionAndTimestamp($this->objectLibraryID);
$this->libraryVersion = Zotero_Libraries::getUpdatedVersion($this->objectLibraryID);
$this->queryParams['format'] = 'writereport';
$obj = $this->jsonDecode($this->body);
$results = Zotero_FullText::updateMultipleFromJSON(
$obj,
$this->queryParams,
$this->objectLibraryID,
$this->userID,
$this->permissions
);
Zotero_API::multiResponse([
'action' => $this->action,
'uri' => $this->uri,
'results' => $results,
'requestParams' => $this->queryParams,
'permissions' => $this->permissions
]);
}
// Default to ?format=versions for GET
else {
$newer = Zotero_FullText::getNewerInLibrary(
$this->objectLibraryID,
!empty($this->queryParams['since']) ? $this->queryParams['since'] : 0
);
$this->libraryVersion = Zotero_Libraries::getVersion($this->objectLibraryID);
echo Zotero_Utilities::formatJSON($newer);
}
$this->end();
}
public function itemContent() {
$this->allowMethods(array('GET', 'PUT'));
// Check for general library access
if (!$this->permissions->canAccess($this->objectLibraryID)) {
$this->e403();
}
if (!$this->singleObject) {
$this->e404();
}
if ($this->isWriteMethod()) {
Zotero_DB::beginTransaction();
// Check for library write access
if (!$this->permissions->canWrite($this->objectLibraryID)) {
$this->e403("Write access denied");
}
Zotero_Libraries::updateVersionAndTimestamp($this->objectLibraryID);
}
$item = Zotero_Items::getByLibraryAndKey($this->objectLibraryID, $this->objectKey);
if (!$item) {
$this->e404();
}
if (!$item->isAttachment() || $item->attachmentLinkMode == 'linked_url') {
$this->e404();
}
if ($this->isWriteMethod()) {
$this->libraryVersion = Zotero_Libraries::getUpdatedVersion($this->objectLibraryID);
if ($this->method == 'PUT') {
$this->requireContentType("application/json");
Zotero_FullText::indexItem($item, $this->jsonDecode($this->body));
Zotero_DB::commit();
$this->e204();
}
$this->e405();
}
$data = Zotero_FullText::getItemData($item->libraryID, $item->key);
if (!$data) {
$this->e404();
}
$this->libraryVersion = $data['version'];
$json = [
"content" => $data['content']
];
foreach (Zotero_FullText::$metadata as $prop) {
if (!empty($data[$prop])) {
$json[$prop] = $data[$prop];
}
}
echo Zotero_Utilities::formatJSON($json);
$this->end();
}
}