Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sketch of copy method #157

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
56 changes: 55 additions & 1 deletion controllers/ItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion include/SQS.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}