/** * Import an item by URL using the translation server * * Initial request: * * { * "url": "http://..." * } * * Item selection for multi-item results: * * { * "url": "http://...", * "token": "<token>" * "items": { * "0": "Item 1 Title", * "3": "Item 2 Title" * } * } * * Returns an array of keys of added items (like updateMultipleFromJSON) or an object * with a 'select' property containing an array of titles for multi-item results */ public static function addFromURL($json, $requestParams, $libraryID, $userID, Zotero_Permissions $permissions, $translationToken) { if (!$translationToken) { throw new Exception("Translation token not provided"); } self::validateJSONURL($json, $requestParams); $cacheKey = 'addFromURLKeyMappings_' . md5($json->url . $translationToken); // Replace numeric keys with URLs for selected items if (isset($json->items) && $requestParams['v'] >= 2) { $keyMappings = Z_Core::$MC->get($cacheKey); $newItems = []; foreach ($json->items as $number => $title) { if (!isset($keyMappings[$number])) { throw new Exception("Index '{$number}' not found for URL and token", Z_ERROR_INVALID_INPUT); } $url = $keyMappings[$number]; $newItems[$url] = $title; } $json->items = $newItems; } $response = Zotero_Translate::doWeb($json->url, $translationToken, isset($json->items) ? $json->items : null); if (!$response || is_int($response)) { return $response; } if (isset($response->items)) { if ($requestParams['v'] >= 2) { $response = $response->items; for ($i = 0, $len = sizeOf($response); $i < $len; $i++) { // Assign key here so that we can add notes if necessary do { $itemKey = Zotero_ID::getKey(); } while (Zotero_Items::existsByLibraryAndKey($libraryID, $itemKey)); $response[$i]->key = $itemKey; // Pull out notes and stick in separate items if (isset($response[$i]->notes)) { foreach ($response[$i]->notes as $note) { $newNote = (object) ["itemType" => "note", "note" => $note->note, "parentItem" => $itemKey]; $response[] = $newNote; } unset($response[$i]->notes); } // TODO: link attachments, or not possible from translation-server? } } try { self::validateMultiObjectJSON($response, $requestParams); } catch (Exception $e) { error_log($e); error_log(json_encode($response)); throw new Exception("Invalid JSON from doWeb()"); } } else { if (isset($response->select)) { // Replace URLs with numeric keys for found items if ($requestParams['v'] >= 2) { $keyMappings = []; $newItems = new stdClass(); $number = 0; foreach ($response->select as $url => $title) { $keyMappings[$number] = $url; $newItems->{$number} = $title; $number++; } Z_Core::$MC->set($cacheKey, $keyMappings, 600); $response->select = $newItems; } return $response; } else { throw new Exception("Invalid return value from doWeb()"); } } return self::updateMultipleFromJSON($response, $requestParams, $libraryID, $userID, $permissions, false, null); }
/** * Import an item by URL using the translation server * * Initial request: * * { * "url": "http://..." * } * * Item selection for multi-item results * * { * "url": "http://...", * "items": { * "0": "Item 1 Title", * "3": "Item 2 Title" * } * } * * Returns an array of keys of added items (like addFromJSON) or an object * with a 'select' property containing an array of titles for multi-item results */ public static function addFromURL($json, $libraryID, $userID, $translationToken) { self::validateJSONURL($json); $response = Zotero_Translate::doWeb($json->url, $translationToken, isset($json->items) ? $json->items : null); if (!$response || is_int($response)) { return $response; } if (isset($response->items)) { try { self::validateJSONItems($response); } catch (Exception $e) { error_log($e); error_log($response); throw new Exception("Invalid JSON from doWeb()"); } } else { if (isset($response->select)) { return $response; } else { throw new Exception("Invalid return value from doWeb()"); } } return self::addFromJSON($response, $libraryID, null, $userID); }