diff --git a/controllers/ApiController.php b/controllers/ApiController.php index b02bb4e08..e6336e4fb 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -108,7 +108,7 @@ public function init($extra) { $this->uri = Z_CONFIG::$API_BASE_URI . substr($_SERVER["REQUEST_URI"], 1); $this->path = $_SERVER["REQUEST_URI"]; - if (!in_array($this->method, array('HEAD', 'OPTIONS', 'GET', 'PUT', 'POST', 'DELETE', 'PATCH'))) { + if (!in_array($this->method, array('HEAD', 'OPTIONS', 'GET', 'PUT', 'POST', 'DELETE', 'PATCH', 'COPY'))) { $this->e501(); } diff --git a/controllers/ItemsController.php b/controllers/ItemsController.php index d3a6e4894..de7c0da68 100644 --- a/controllers/ItemsController.php +++ b/controllers/ItemsController.php @@ -124,7 +124,7 @@ public function items() { } } else { - $this->allowMethods(array('HEAD', 'GET', 'PUT', 'PATCH', 'DELETE')); + $this->allowMethods(array('HEAD', 'GET', 'PUT', 'PATCH', 'DELETE', 'COPY')); } if (!Zotero_ID::isValidKey($this->objectKey)) { @@ -222,6 +222,60 @@ public function items() { if (!$item) { $this->e404("Item does not exist"); } + if ($this->method == 'COPY'){ + // Method called by the client. + // Collects all children of an item, groups them by level + // and sends them to SQS + + if (!isset($_SERVER['HTTP_DESTINATION'])) { + $this->e400("Destination header is required"); + } + $destinationLibrary = intval($_SERVER['HTTP_DESTINATION']); + if (!$this->permissions->canWrite($destinationLibrary)) { + $this->e403("No write access to destination library"); + } + $libraryURI = Zotero_URI::getLibraryURI($destinationLibrary); + + $level = 0; + $results = [$level => [ $item->toResponseJSON($this->queryParams, $this->permissions)['data'] ], 1 => [], 2 => []]; + $itemsWithChildren = [$level => [ $item ], 1 => [], 2 => []]; + while (sizeof($itemsWithChildren[$level]) > 0) { + $itemCandidate = array_shift($itemsWithChildren[$level]); + if ($itemCandidate->isAttachment()) { + $itemIDs = $itemCandidate->getAnnotations(); + } + else if ($item->isNote()) { + $itemIDs = $itemCandidate->getAttachments(); + } + else { + $notes = $itemCandidate->getNotes(); + $attachments = $itemCandidate->getAttachments(); + $itemIDs = array_merge($notes, $attachments); + } + $this->queryParams['itemIDs'] = $itemIDs; + if (sizeof($itemIDs) == 0) { + continue; + } + $searchResult = Zotero_Items::search( + $this->objectLibraryID, + false, + $this->queryParams, + $this->permissions + ); + $itemsWithChildren[$level+1] = array_merge($itemsWithChildren[$level+1],$searchResult['results'] ); + foreach($searchResult['results'] as $searchItem) { + $json = $searchItem->toResponseJSON($this->queryParams, $this->permissions)['data']; + array_push($results[$level+1],$json); + } + if (sizeof($itemsWithChildren[$level]) == 0) { + $level += 1; + } + } + $message = json_encode( [ 'libraryUri' => $libraryURI, 'userID' => $this->objectUserID, 'items' => $results ] ); + Z_SQS::send(Z_CONFIG::$COPY_SQS_URL, $message); + #echo $message; + $this->e200(); + } $this->libraryVersion = $item->version; diff --git a/include/SQS.inc.php b/include/SQS.inc.php index 88b870f36..9c8a93eea 100644 --- a/include/SQS.inc.php +++ b/include/SQS.inc.php @@ -102,7 +102,7 @@ public static function deleteBatch($queueURL, $batchEntries) { private static function load() { if (!self::$sqs) { - self::$sqs = Z_Core::$AWS->get('sqs'); + self::$sqs = Z_Core::$AWS->createSQS(); } } }