Open Journal Systems  3.3.0
XMLPSWrapper.inc.php
1 <?php
2 
16 class XMLPSWrapper {
17 
19  const JOB_STATUS_PENDING = 0;
22  const JOB_STATUS_FAILED = 3;
23 
24  const DEFAULT_DEMO_HOST = 'https://pkp-xml-demo.lib.sfu.ca';
25 
27  protected $_username = null;
28 
30  protected $_token = null;
31 
33  protected $_host = null;
34 
43  public function __construct($host, $username = null, $token = null) {
44 
45  if (is_null($host) || empty($host)) {
46  $this->_host = self::DEFAULT_DEMO_HOST;
47  }
48 
49  $this->_host = rtrim($host, '/');
50  $this->_username = $username;
51  $this->_token = $token;
52  }
53 
62  protected function _buildRequestUrl($endpoint, $params = array()) {
63 
64  $endpoint = trim($endpoint, '/');
65  $params = http_build_query($params);
66  $url = "{$this->_host}/{$endpoint}";
67 
68  return empty($params) ? $url : "{$url}?{$params}";
69  }
70 
77  protected function _withLoginCredentials($params) {
78  $credentials = array(
79  'email' => $this->_username,
80  'access_token' => $this->_token,
81  );
82 
83  return array_merge($params, $credentials);
84  }
85 
99  protected function _makeApiRequest($endpoint, $params, $authRequired = false, $isPost = false) {
100 
101  if ($authRequired) {
102 
103  if (empty($this->_username) || empty($this->_token)) {
104  throw new Exception('Login credentials (username & token) are required for server authentication.');
105  }
106 
107  $params = $this->_withLoginCredentials($params);
108  }
109 
110  $apiUrl = null;
111  $ch = curl_init();
112  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
113 
114  if ($isPost) {
115  curl_setopt($ch, CURLOPT_POST, 1);
116  curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
117 
118  $apiUrl = $this->_buildRequestUrl($endpoint);
119  }
120  else {
121  $apiUrl = $this->_buildRequestUrl($endpoint, $params);
122  }
123 
124 
125  curl_setopt($ch, CURLOPT_URL, $apiUrl);
126  $response = curl_exec($ch);
127  $response = json_decode($response, true);
128 
129  if (!$response) {
130  $error = curl_error($ch);
131 
132  if (empty($error)) {
133  $error = 'HTTP status: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE);
134  }
135 
136  throw new Exception($error);
137  }
138 
139  curl_close($ch);
140 
141  return $response;
142  }
143 
156  public function submitJob($fileName, $fileContent, $citationStyleHash, $metadata = null) {
157 
158  $params = array(
159  'fileName' => $fileName,
160  'fileContent' => $fileContent,
161  'citationStyleHash' => $citationStyleHash,
162  );
163 
164  if (!is_null($metadata)) {
165  $params['fileMetadata'] = json_encode($metadata);
166  }
167 
168  $response = $this->_makeApiRequest('api/job/submit', $params, true, true);
169 
170  if ($response['status'] != "success") {
171  throw new Exception('Job submission failed.');
172  }
173 
174  return $response['id'];
175  }
176 
186  public function getJobStatus($jobId) {
187 
188  $params = array(
189  'id' => $jobId
190  );
191 
192  $response = $this->_makeApiRequest('api/job/status', $params, true);
193 
194  if ($response['status'] != "success") {
195  throw new Exception('Unable to fetch job status.');
196  }
197 
198  return $response['jobStatus'];
199  }
200 
208  protected function _getFileUrl($jobId) {
209 
210  $params = array(
211  'id' => $jobId,
212  'conversionStage' => self::JOB_CONVERSION_STAGE_ZIP,
213  );
214 
215  $params = $this->_withLoginCredentials($params);
216 
217  return $this->_buildRequestUrl('api/job/retrieve', $params);
218  }
219 
229  public function downloadFile($jobId, $filename = 'documents.zip', $destinationDir = null)
230  {
231  if (is_null($destinationDir)) {
232  $destinationDir = sys_get_temp_dir();
233  }
234 
235  $fileUrl = $this->_getFileUrl($jobId);
236  $filePath = rtrim($destinationDir, '/') . '/' . $filename;
237 
238  if (file_exists($filePath)) {
239  @unlink($filePath);
240  }
241 
242  if (!@copy($fileUrl, $filePath)) {
243  throw new Exception("Unable to copy from {$fileUrl} to {$filePath}");
244  }
245 
246  return $filePath;
247  }
248 
256  public function getCitationList() {
257 
258  $response = $this->_makeApiRequest('/api/job/citationStyleList', array());
259 
260  if ($response['status'] != "success") {
261  throw new Exception('Job submission failed.');
262  }
263 
264  return $response['citationStyles'];
265  }
266 
272  public function statusCodeToLabel($code) {
273  switch ($code) {
275  return 'Pending';
277  return 'Processing';
279  return 'Completed';
281  return 'Failed';
282  }
283  }
284 
285 }
XMLPSWrapper\$_token
$_token
Definition: XMLPSWrapper.inc.php:36
XMLPSWrapper\downloadFile
downloadFile($jobId, $filename='documents.zip', $destinationDir=null)
Definition: XMLPSWrapper.inc.php:238
XMLPSWrapper\submitJob
submitJob($fileName, $fileContent, $citationStyleHash, $metadata=null)
Definition: XMLPSWrapper.inc.php:165
XMLPSWrapper\__construct
__construct($host, $username=null, $token=null)
Definition: XMLPSWrapper.inc.php:52
XMLPSWrapper\_getFileUrl
_getFileUrl($jobId)
Definition: XMLPSWrapper.inc.php:217
XMLPSWrapper\getJobStatus
getJobStatus($jobId)
Definition: XMLPSWrapper.inc.php:195
XMLPSWrapper\JOB_STATUS_PROCESSING
const JOB_STATUS_PROCESSING
Definition: XMLPSWrapper.inc.php:20
XMLPSWrapper
Wrapper class for XML Parsing Service.
Definition: XMLPSWrapper.inc.php:16
GuzzleHttp\json_decode
json_decode($json, $assoc=false, $depth=512, $options=0)
Definition: guzzlehttp/guzzle/src/functions.php:301
XMLPSWrapper\_withLoginCredentials
_withLoginCredentials($params)
Definition: XMLPSWrapper.inc.php:86
XMLPSWrapper\DEFAULT_DEMO_HOST
const DEFAULT_DEMO_HOST
Definition: XMLPSWrapper.inc.php:24
XMLPSWrapper\getCitationList
getCitationList()
Definition: XMLPSWrapper.inc.php:265
XMLPSWrapper\JOB_STATUS_PENDING
const JOB_STATUS_PENDING
Definition: XMLPSWrapper.inc.php:19
XMLPSWrapper\$_host
$_host
Definition: XMLPSWrapper.inc.php:42
XMLPSWrapper\JOB_STATUS_FAILED
const JOB_STATUS_FAILED
Definition: XMLPSWrapper.inc.php:22
XMLPSWrapper\$_username
$_username
Definition: XMLPSWrapper.inc.php:30
XMLPSWrapper\_buildRequestUrl
_buildRequestUrl($endpoint, $params=array())
Definition: XMLPSWrapper.inc.php:71
XMLPSWrapper\statusCodeToLabel
statusCodeToLabel($code)
Definition: XMLPSWrapper.inc.php:281
XMLPSWrapper\JOB_STATUS_COMPLETED
const JOB_STATUS_COMPLETED
Definition: XMLPSWrapper.inc.php:21
XMLPSWrapper\JOB_CONVERSION_STAGE_ZIP
const JOB_CONVERSION_STAGE_ZIP
Definition: XMLPSWrapper.inc.php:18
XMLPSWrapper\_makeApiRequest
_makeApiRequest($endpoint, $params, $authRequired=false, $isPost=false)
Definition: XMLPSWrapper.inc.php:108