private static function processUploadInternal($userID, SimpleXMLElement $xml, $syncQueueID = null, $syncProcessID = null) { $userLibraryID = Zotero_Users::getLibraryIDFromUserID($userID); $affectedLibraries = self::parseAffectedLibraries($xml->asXML()); // Relations-only uploads don't have affected libraries if (!$affectedLibraries) { $affectedLibraries = array(Zotero_Users::getLibraryIDFromUserID($userID)); } $processID = self::addUploadProcess($userID, $affectedLibraries, $syncQueueID, $syncProcessID); set_time_limit(5400); $profile = false; if ($profile) { $shardID = Zotero_Shards::getByUserID($userID); Zotero_DB::profileStart($shardID); } try { Zotero_DB::beginTransaction(); // Mark libraries as updated foreach ($affectedLibraries as $libraryID) { Zotero_Libraries::updateVersion($libraryID); } $timestamp = Zotero_Libraries::updateTimestamps($affectedLibraries); Zotero_DB::registerTransactionTimestamp($timestamp); // Make sure no other upload sessions use this same timestamp // for any of these libraries, since we return >= 1 as the next // last sync time if (!Zotero_Libraries::setTimestampLock($affectedLibraries, $timestamp)) { throw new Exception("Library timestamp already used", Z_ERROR_LIBRARY_TIMESTAMP_ALREADY_USED); } $modifiedItems = array(); // Add/update creators if ($xml->creators) { // DOM $keys = array(); $xmlElements = dom_import_simplexml($xml->creators); $xmlElements = $xmlElements->getElementsByTagName('creator'); Zotero_DB::query("SET foreign_key_checks = 0"); try { $addedLibraryIDs = array(); $addedCreatorDataHashes = array(); foreach ($xmlElements as $xmlElement) { $key = $xmlElement->getAttribute('key'); if (isset($keys[$key])) { throw new Exception("Creator {$key} already processed"); } $keys[$key] = true; $creatorObj = Zotero_Creators::convertXMLToCreator($xmlElement); if (Zotero_Utilities::unicodeTrim($creatorObj->firstName) === '' && Zotero_Utilities::unicodeTrim($creatorObj->lastName) === '') { continue; } $addedLibraryIDs[] = $creatorObj->libraryID; $changed = $creatorObj->save($userID); // If the creator changed, we need to update all linked items if ($changed) { $modifiedItems = array_merge($modifiedItems, $creatorObj->getLinkedItems()); } } } catch (Exception $e) { Zotero_DB::query("SET foreign_key_checks = 1"); throw $e; } Zotero_DB::query("SET foreign_key_checks = 1"); unset($keys); unset($xml->creators); // // Manual foreign key checks // // libraryID foreach (array_unique($addedLibraryIDs) as $addedLibraryID) { $shardID = Zotero_Shards::getByLibraryID($addedLibraryID); $sql = "SELECT COUNT(*) FROM shardLibraries WHERE libraryID=?"; if (!Zotero_DB::valueQuery($sql, $addedLibraryID, $shardID)) { throw new Exception("libraryID inserted into `creators` not found in `shardLibraries` ({$addedLibraryID}, {$shardID})"); } } } // Add/update items $savedItems = array(); if ($xml->items) { $childItems = array(); // DOM $xmlElements = dom_import_simplexml($xml->items); $xmlElements = $xmlElements->getElementsByTagName('item'); foreach ($xmlElements as $xmlElement) { $libraryID = (int) $xmlElement->getAttribute('libraryID'); $key = $xmlElement->getAttribute('key'); if (isset($savedItems[$libraryID . "/" . $key])) { throw new Exception("Item {$libraryID}/{$key} already processed"); } $itemObj = Zotero_Items::convertXMLToItem($xmlElement); if (!$itemObj->getSourceKey()) { try { $modified = $itemObj->save($userID); if ($modified) { $savedItems[$libraryID . "/" . $key] = true; } } catch (Exception $e) { if (strpos($e->getMessage(), 'libraryIDs_do_not_match') !== false) { throw new Exception($e->getMessage() . " ({$key})"); } throw $e; } } else { $childItems[] = $itemObj; } } unset($xml->items); while ($childItem = array_shift($childItems)) { $libraryID = $childItem->libraryID; $key = $childItem->key; if (isset($savedItems[$libraryID . "/" . $key])) { throw new Exception("Item {$libraryID}/{$key} already processed"); } $modified = $childItem->save($userID); if ($modified) { $savedItems[$libraryID . "/" . $key] = true; } } } // Add/update collections if ($xml->collections) { $collections = array(); $collectionSets = array(); // DOM // Build an array of unsaved collection objects and the keys of child items $keys = array(); $xmlElements = dom_import_simplexml($xml->collections); $xmlElements = $xmlElements->getElementsByTagName('collection'); foreach ($xmlElements as $xmlElement) { $key = $xmlElement->getAttribute('key'); if (isset($keys[$key])) { throw new Exception("Collection {$key} already processed"); } $keys[$key] = true; $collectionObj = Zotero_Collections::convertXMLToCollection($xmlElement); $xmlItems = $xmlElement->getElementsByTagName('items')->item(0); // Fix an error if there's leading or trailing whitespace, // which was possible in 2.0.3 if ($xmlItems) { $xmlItems = trim($xmlItems->nodeValue); } $arr = array('obj' => $collectionObj, 'items' => $xmlItems ? explode(' ', $xmlItems) : array()); $collections[] = $collectionObj; $collectionSets[] = $arr; } unset($keys); unset($xml->collections); self::saveCollections($collections, $userID); unset($collections); // Set child items foreach ($collectionSets as $collection) { // Child items if (isset($collection['items'])) { $ids = array(); foreach ($collection['items'] as $key) { $item = Zotero_Items::getByLibraryAndKey($collection['obj']->libraryID, $key); if (!$item) { throw new Exception("Child item '{$key}' of collection {$collection['obj']->id} not found", Z_ERROR_ITEM_NOT_FOUND); } $ids[] = $item->id; } $collection['obj']->setItems($ids); } } unset($collectionSets); } // Add/update saved searches if ($xml->searches) { $searches = array(); $keys = array(); foreach ($xml->searches->search as $xmlElement) { $key = (string) $xmlElement['key']; if (isset($keys[$key])) { throw new Exception("Search {$key} already processed"); } $keys[$key] = true; $searchObj = Zotero_Searches::convertXMLToSearch($xmlElement); $searchObj->save($userID); } unset($xml->searches); } // Add/update tags if ($xml->tags) { $keys = array(); // DOM $xmlElements = dom_import_simplexml($xml->tags); $xmlElements = $xmlElements->getElementsByTagName('tag'); foreach ($xmlElements as $xmlElement) { // TEMP $tagItems = $xmlElement->getElementsByTagName('items'); if ($tagItems->length && $tagItems->item(0)->nodeValue == "") { error_log("Skipping tag with no linked items"); continue; } $libraryID = (int) $xmlElement->getAttribute('libraryID'); $key = $xmlElement->getAttribute('key'); $lk = $libraryID . "/" . $key; if (isset($keys[$lk])) { throw new Exception("Tag {$lk} already processed"); } $keys[$lk] = true; $itemKeysToUpdate = array(); $tagObj = Zotero_Tags::convertXMLToTag($xmlElement, $itemKeysToUpdate); // We need to update removed items, added items, and, // if the tag itself has changed, existing items $modifiedItems = array_merge($modifiedItems, array_map(function ($key) use($libraryID) { return $libraryID . "/" . $key; }, $itemKeysToUpdate)); $tagObj->save($userID, true); } unset($keys); unset($xml->tags); } // Add/update relations if ($xml->relations) { // DOM $xmlElements = dom_import_simplexml($xml->relations); $xmlElements = $xmlElements->getElementsByTagName('relation'); foreach ($xmlElements as $xmlElement) { $relationObj = Zotero_Relations::convertXMLToRelation($xmlElement, $userLibraryID); if ($relationObj->exists()) { continue; } $relationObj->save($userID); } unset($keys); unset($xml->relations); } // Add/update settings if ($xml->settings) { // DOM $xmlElements = dom_import_simplexml($xml->settings); $xmlElements = $xmlElements->getElementsByTagName('setting'); foreach ($xmlElements as $xmlElement) { $settingObj = Zotero_Settings::convertXMLToSetting($xmlElement); $settingObj->save($userID); } unset($xml->settings); } if ($xml->fulltexts) { // DOM $xmlElements = dom_import_simplexml($xml->fulltexts); $xmlElements = $xmlElements->getElementsByTagName('fulltext'); foreach ($xmlElements as $xmlElement) { Zotero_FullText::indexFromXML($xmlElement, $userID); } unset($xml->fulltexts); } // TODO: loop if ($xml->deleted) { // Delete collections if ($xml->deleted->collections) { Zotero_Collections::deleteFromXML($xml->deleted->collections, $userID); } // Delete items if ($xml->deleted->items) { Zotero_Items::deleteFromXML($xml->deleted->items, $userID); } // Delete creators if ($xml->deleted->creators) { Zotero_Creators::deleteFromXML($xml->deleted->creators, $userID); } // Delete saved searches if ($xml->deleted->searches) { Zotero_Searches::deleteFromXML($xml->deleted->searches, $userID); } // Delete tags if ($xml->deleted->tags) { $xmlElements = dom_import_simplexml($xml->deleted->tags); $xmlElements = $xmlElements->getElementsByTagName('tag'); foreach ($xmlElements as $xmlElement) { $libraryID = (int) $xmlElement->getAttribute('libraryID'); $key = $xmlElement->getAttribute('key'); $tagObj = Zotero_Tags::getByLibraryAndKey($libraryID, $key); if (!$tagObj) { continue; } // We need to update all items on the deleted tag $modifiedItems = array_merge($modifiedItems, array_map(function ($key) use($libraryID) { return $libraryID . "/" . $key; }, $tagObj->getLinkedItems(true))); } Zotero_Tags::deleteFromXML($xml->deleted->tags, $userID); } // Delete relations if ($xml->deleted->relations) { Zotero_Relations::deleteFromXML($xml->deleted->relations, $userID); } // Delete relations if ($xml->deleted->settings) { Zotero_Settings::deleteFromXML($xml->deleted->settings, $userID); } } $toUpdate = array(); foreach ($modifiedItems as $item) { // libraryID/key string if (is_string($item)) { if (isset($savedItems[$item])) { continue; } $savedItems[$item] = true; list($libraryID, $key) = explode("/", $item); $item = Zotero_Items::getByLibraryAndKey($libraryID, $key); if (!$item) { // Item was deleted continue; } } else { $lk = $item->libraryID . "/" . $item->key; if (isset($savedItems[$lk])) { continue; } $savedItems[$lk] = true; } $toUpdate[] = $item; } Zotero_Items::updateVersions($toUpdate, $userID); unset($savedItems); unset($modifiedItems); try { self::removeUploadProcess($processID); } catch (Exception $e) { if (strpos($e->getMessage(), 'MySQL server has gone away') !== false) { // Reconnect error_log("Reconnecting to MySQL master"); Zotero_DB::close(); self::removeUploadProcess($processID); } else { throw $e; } } // Send notifications for changed libraries foreach ($affectedLibraries as $libraryID) { Zotero_Notifier::trigger('modify', 'library', $libraryID); } Zotero_DB::commit(); if ($profile) { $shardID = Zotero_Shards::getByUserID($userID); Zotero_DB::profileEnd($shardID); } // Return timestamp + 1, to keep the next /updated call // (using >= timestamp) from returning this data return $timestamp + 1; } catch (Exception $e) { Zotero_DB::rollback(true); self::removeUploadProcess($processID); throw $e; } }
public function items() { if (($this->method == 'POST' || $this->method == 'PUT') && !$this->body) { $this->e400("{$this->method} data not provided"); } $itemIDs = array(); $responseItems = array(); $responseKeys = array(); $totalResults = null; // // Single item // if (($this->objectID || $this->objectKey) && !$this->subset) { if ($this->fileMode) { if ($this->fileView) { $this->allowMethods(array('GET', 'HEAD', 'POST')); } else { $this->allowMethods(array('GET', 'PUT', 'POST', 'HEAD', 'PATCH')); } } else { $this->allowMethods(array('GET', 'PUT', 'DELETE')); } // Check for general library access if (!$this->permissions->canAccess($this->objectLibraryID)) { //var_dump($this->objectLibraryID); //var_dump($this->permissions); $this->e403(); } if ($this->objectKey) { $item = Zotero_Items::getByLibraryAndKey($this->objectLibraryID, $this->objectKey); } else { try { $item = Zotero_Items::get($this->objectLibraryID, $this->objectID); } catch (Exception $e) { if ($e->getCode() == Z_ERROR_OBJECT_LIBRARY_MISMATCH) { $item = false; } else { throw $e; } } } if (!$item) { // Possibly temporary workaround to block unnecessary full syncs if ($this->fileMode && $this->method == 'POST') { // If > 2 requests for missing file, trigger a full sync via 404 $cacheKey = "apiMissingFile_" . $this->objectLibraryID . "_" . ($this->objectKey ? $this->objectKey : $this->objectID); $set = Z_Core::$MC->get($cacheKey); if (!$set) { Z_Core::$MC->set($cacheKey, 1, 86400); } else { if ($set < 2) { Z_Core::$MC->increment($cacheKey); } else { Z_Core::$MC->delete($cacheKey); $this->e404("A file sync error occurred. Please sync again."); } } $this->e500("A file sync error occurred. Please sync again."); } // If we have an id, make sure this isn't really an all-numeric key if ($this->objectID && strlen($this->objectID) == 8 && preg_match('/[0-9]{8}/', $this->objectID)) { $item = Zotero_Items::getByLibraryAndKey($this->objectLibraryID, $this->objectID); if ($item) { $this->objectKey = $this->objectID; unset($this->objectID); } } if (!$item) { $this->e404("Item does not exist"); } } if ($item->isNote() && !$this->permissions->canAccess($this->objectLibraryID, 'notes')) { $this->e403(); } // Make sure URL libraryID matches item libraryID if ($this->objectLibraryID != $item->libraryID) { $this->e404("Item does not exist"); } // File access mode if ($this->fileMode) { $this->_handleFileRequest($item); } // If id, redirect to key URL if ($this->objectID) { $this->allowMethods(array('GET')); $qs = !empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : ''; header("Location: " . Zotero_API::getItemURI($item) . $qs); exit; } if ($this->scopeObject) { switch ($this->scopeObject) { // Remove item from collection case 'collections': $this->allowMethods(array('DELETE')); if (!$this->permissions->canWrite($this->objectLibraryID)) { $this->e403("Write access denied"); } $collection = Zotero_Collections::getByLibraryAndKey($this->objectLibraryID, $this->scopeObjectKey); if (!$collection) { $this->e404("Collection not found"); } if (!$collection->hasItem($item->id)) { $this->e404("Item not found in collection"); } Zotero_DB::beginTransaction(); $timestamp = Zotero_Libraries::updateTimestamps($this->objectLibraryID); Zotero_DB::registerTransactionTimestamp($timestamp); $collection->removeItem($item->id); Zotero_DB::commit(); $this->e204(); default: $this->e400(); } } if ($this->method == 'PUT' || $this->method == 'DELETE') { if (!$this->permissions->canWrite($this->objectLibraryID)) { $this->e403("Write access denied"); } if (!Z_CONFIG::$TESTING_SITE || empty($_GET['skipetag'])) { if (empty($_SERVER['HTTP_IF_MATCH'])) { $this->e400("If-Match header not provided"); } if (!preg_match('/^"?([a-f0-9]{32})"?$/', $_SERVER['HTTP_IF_MATCH'], $matches)) { $this->e400("Invalid ETag in If-Match header"); } if ($item->etag != $matches[1]) { $this->e412("ETag does not match current version of item"); } } // Update existing item if ($this->method == 'PUT') { $obj = $this->jsonDecode($this->body); Zotero_Items::updateFromJSON($item, $obj, false, null, $this->userID); $this->queryParams['format'] = 'atom'; $this->queryParams['content'] = array('json'); if ($cacheKey = $this->getWriteTokenCacheKey()) { Z_Core::$MC->set($cacheKey, true, $this->writeTokenCacheTime); } } else { Zotero_Items::delete($this->objectLibraryID, $this->objectKey, true); try { Zotero_Processors::notifyProcessors('index'); } catch (Exception $e) { Z_Core::logError($e); } $this->e204(); } } // Display item switch ($this->queryParams['format']) { case 'atom': $this->responseXML = Zotero_Items::convertItemToAtom($item, $this->queryParams, $this->apiVersion, $this->permissions); break; case 'bib': echo Zotero_Cite::getBibliographyFromCitationServer(array($item), $this->queryParams['style'], $this->queryParams['css']); exit; case 'csljson': $json = Zotero_Cite::getJSONFromItems(array($item), true); if ($this->queryParams['pprint']) { header("Content-Type: text/plain"); $json = Zotero_Utilities::json_encode_pretty($json); } else { header("Content-Type: application/vnd.citationstyles.csl+json"); $json = json_encode($json); } echo $json; exit; default: $export = Zotero_Translate::doExport(array($item), $this->queryParams['format']); if ($this->queryParams['pprint']) { header("Content-Type: text/plain"); } else { header("Content-Type: " . $export['mimeType']); } echo $export['body']; exit; } } else { $this->allowMethods(array('GET', 'POST')); if (!$this->permissions->canAccess($this->objectLibraryID)) { $this->e403(); } $includeTrashed = false; $formatAsKeys = $this->queryParams['format'] == 'keys'; if ($this->scopeObject) { $this->allowMethods(array('GET', 'POST')); // If id, redirect to key URL if ($this->scopeObjectID) { $this->allowMethods(array('GET')); if (!in_array($this->scopeObject, array("collections", "tags"))) { $this->e400(); } $className = 'Zotero_' . ucwords($this->scopeObject); $obj = call_user_func(array($className, 'get'), $this->objectLibraryID, $this->scopeObjectID); if (!$obj) { $this->e404("Scope " . substr($this->scopeObject, 0, -1) . " not found"); } $base = call_user_func(array('Zotero_API', 'get' . substr(ucwords($this->scopeObject), 0, -1) . 'URI'), $obj); $qs = !empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : ''; header("Location: " . $base . "/items" . $qs); exit; } switch ($this->scopeObject) { case 'collections': $collection = Zotero_Collections::getByLibraryAndKey($this->objectLibraryID, $this->scopeObjectKey); if (!$collection) { $this->e404("Collection not found"); } // Add items to collection if ($this->method == 'POST') { if (!$this->permissions->canWrite($this->objectLibraryID)) { $this->e403("Write access denied"); } Zotero_DB::beginTransaction(); $timestamp = Zotero_Libraries::updateTimestamps($this->objectLibraryID); Zotero_DB::registerTransactionTimestamp($timestamp); $itemKeys = explode(' ', $this->body); $itemIDs = array(); foreach ($itemKeys as $key) { try { $item = Zotero_Items::getByLibraryAndKey($this->objectLibraryID, $key); } catch (Exception $e) { if ($e->getCode() == Z_ERROR_OBJECT_LIBRARY_MISMATCH) { $item = false; } else { throw $e; } } if (!$item) { throw new Exception("Item '{$key}' not found in library", Z_ERROR_INVALID_INPUT); } if ($item->getSource()) { throw new Exception("Child items cannot be added to collections directly", Z_ERROR_INVALID_INPUT); } $itemIDs[] = $item->id; } $collection->addItems($itemIDs); Zotero_DB::commit(); $this->e204(); } $title = "Items in Collection ‘" . $collection->name . "’"; $itemIDs = $collection->getChildItems(); break; case 'tags': $this->allowMethods(array('GET')); $tagIDs = Zotero_Tags::getIDs($this->objectLibraryID, $this->scopeObjectName); if (!$tagIDs) { $this->e404("Tag not found"); } $itemIDs = array(); $title = ''; foreach ($tagIDs as $tagID) { $tag = new Zotero_Tag(); $tag->libraryID = $this->objectLibraryID; $tag->id = $tagID; // Use a real tag name, in case case differs if (!$title) { $title = "Items of Tag ‘" . $tag->name . "’"; } $itemIDs = array_merge($itemIDs, $tag->getLinkedItems(true)); } $itemIDs = array_unique($itemIDs); break; default: throw new Exception("Invalid items scope object '{$this->scopeObject}'"); } } else { // Top-level items if ($this->subset == 'top') { $this->allowMethods(array('GET')); $title = "Top-Level Items"; $results = Zotero_Items::search($this->objectLibraryID, true, $this->queryParams, false, $formatAsKeys); } else { if ($this->subset == 'trash') { $this->allowMethods(array('GET')); $title = "Deleted Items"; $itemIDs = Zotero_Items::getDeleted($this->objectLibraryID, true); $includeTrashed = true; } else { if ($this->subset == 'children') { // If we have an id, make sure this isn't really an all-numeric key if ($this->objectID && strlen($this->objectID) == 8 && preg_match('/[0-9]{8}/', $this->objectID)) { $item = Zotero_Items::getByLibraryAndKey($this->objectLibraryID, $this->objectID); if ($item) { $this->objectKey = $this->objectID; unset($this->objectID); } } // If id, redirect to key URL if ($this->objectID) { $this->allowMethods(array('GET')); $item = Zotero_Items::get($this->objectLibraryID, $this->objectID); if (!$item) { $this->e404("Item not found"); } $qs = !empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : ''; header("Location: " . Zotero_API::getItemURI($item) . '/children' . $qs); exit; } $item = Zotero_Items::getByLibraryAndKey($this->objectLibraryID, $this->objectKey); if (!$item) { $this->e404("Item not found"); } // Create new child items if ($this->method == 'POST') { if (!$this->permissions->canWrite($this->objectLibraryID)) { $this->e403("Write access denied"); } $obj = $this->jsonDecode($this->body); $keys = Zotero_Items::addFromJSON($obj, $this->objectLibraryID, $item, $this->userID); if ($cacheKey = $this->getWriteTokenCacheKey()) { Z_Core::$MC->set($cacheKey, true, $this->writeTokenCacheTime); } $uri = Zotero_API::getItemURI($item) . "/children"; $queryString = "itemKey=" . urlencode(implode(",", $keys)) . "&content=json"; if ($this->apiKey) { $queryString .= "&key=" . $this->apiKey; } $uri .= "?" . $queryString; $this->responseCode = 201; $this->queryParams = Zotero_API::parseQueryParams($queryString, $this->action, false); } // Display items $title = "Child Items of ‘" . $item->getDisplayTitle() . "’"; $notes = $item->getNotes(); $attachments = $item->getAttachments(); $itemIDs = array_merge($notes, $attachments); } else { // Create new items if ($this->method == 'POST') { if (!$this->permissions->canWrite($this->objectLibraryID)) { $this->e403("Write access denied"); } $obj = $this->jsonDecode($this->body); if (isset($obj->url)) { $response = Zotero_Items::addFromURL($obj, $this->objectLibraryID, $this->userID, $this->getTranslationToken()); if ($response instanceof stdClass) { header("Content-Type: application/json"); echo json_encode($response->select); $this->e300(); } else { if (is_int($response)) { switch ($response) { case 501: $this->e501("No translators found for URL"); break; default: $this->e500("Error translating URL"); } } else { $keys = $response; } } } else { $keys = Zotero_Items::addFromJSON($obj, $this->objectLibraryID, null, $this->userID); } if (!$keys) { throw new Exception("No items added"); } if ($cacheKey = $this->getWriteTokenCacheKey()) { Z_Core::$MC->set($cacheKey, true, $this->writeTokenCacheTime); } $uri = Zotero_API::getItemsURI($this->objectLibraryID); $queryString = "itemKey=" . urlencode(implode(",", $keys)) . "&content=json"; if ($this->apiKey) { $queryString .= "&key=" . $this->apiKey; } $uri .= "?" . $queryString; $this->responseCode = 201; $this->queryParams = Zotero_API::parseQueryParams($queryString, $this->action, false); } $title = "Items"; $results = Zotero_Items::search($this->objectLibraryID, false, $this->queryParams, false, $formatAsKeys); } } } if (!empty($results)) { if ($formatAsKeys) { $responseKeys = $results['keys']; } else { $responseItems = $results['items']; } $totalResults = $results['total']; } } if ($this->queryParams['format'] == 'bib') { if (($itemIDs ? sizeOf($itemIDs) : $results['total']) > Zotero_API::$maxBibliographyItems) { $this->e413("Cannot generate bibliography with more than " . Zotero_API::$maxBibliographyItems . " items"); } } if ($itemIDs) { $this->queryParams['itemIDs'] = $itemIDs; $results = Zotero_Items::search($this->objectLibraryID, false, $this->queryParams, $includeTrashed, $formatAsKeys); if ($formatAsKeys) { $responseKeys = $results['keys']; } else { $responseItems = $results['items']; } $totalResults = $results['total']; } else { if (!isset($results)) { if ($formatAsKeys) { $responseKeys = array(); } else { $responseItems = array(); } $totalResults = 0; } } // Remove notes if not user and not public for ($i = 0; $i < sizeOf($responseItems); $i++) { if ($responseItems[$i]->isNote() && !$this->permissions->canAccess($responseItems[$i]->libraryID, 'notes')) { array_splice($responseItems, $i, 1); $totalResults--; $i--; } } switch ($this->queryParams['format']) { case 'atom': $this->responseXML = Zotero_Atom::createAtomFeed($this->getFeedNamePrefix($this->objectLibraryID) . $title, $this->uri, $responseItems, $totalResults, $this->queryParams, $this->apiVersion, $this->permissions); break; case 'bib': echo Zotero_Cite::getBibliographyFromCitationServer($responseItems, $this->queryParams['style'], $this->queryParams['css']); exit; case 'csljson': $json = Zotero_Cite::getJSONFromItems($responseItems, true); if ($this->queryParams['pprint']) { header("Content-Type: text/plain"); $json = Zotero_Utilities::json_encode_pretty($json); } else { header("Content-Type: application/vnd.citationstyles.csl+json"); $json = json_encode($json); } echo $json; exit; case 'keys': if (!$formatAsKeys) { $responseKeys = array(); foreach ($responseItems as $item) { $responseKeys[] = $item->key; } } header("Content-Type: text/plain"); echo implode("\n", $responseKeys) . "\n"; exit; default: $export = Zotero_Translate::doExport($responseItems, $this->queryParams['format']); if ($this->queryParams['pprint']) { header("Content-Type: text/plain"); } else { header("Content-Type: " . $export['mimeType']); } echo $export['body']; exit; } } $this->end(); }
public static function updateVersionAndTimestamp($libraryID) { self::updateVersion($libraryID); $timestamp = self::updateTimestamps($libraryID); Zotero_DB::registerTransactionTimestamp($timestamp); }
public static function delete($libraryID, $key, $updateLibrary = false) { $table = static::field('table'); $id = static::field('id'); $type = static::field('object'); $types = static::field('objects'); if (!$key) { throw new Exception("Invalid key {$key}"); } // Get object (and trigger caching) $obj = static::getByLibraryAndKey($libraryID, $key); if (!$obj) { return; } static::editCheck($obj); Z_Core::debug("Deleting {$type} {$libraryID}/{$key}", 4); $shardID = Zotero_Shards::getByLibraryID($libraryID); Zotero_DB::beginTransaction(); // Needed for API deletes to get propagated via sync if ($updateLibrary) { $timestamp = Zotero_Libraries::updateTimestamps($obj->libraryID); Zotero_DB::registerTransactionTimestamp($timestamp); } // Delete child items if ($type == 'item') { if ($obj->isRegularItem()) { $children = array_merge($obj->getNotes(), $obj->getAttachments()); if ($children) { $children = Zotero_Items::get($libraryID, $children); foreach ($children as $child) { static::delete($child->libraryID, $child->key); } } } } if ($type == 'relation') { // TODO: add key column to relations to speed this up $sql = "DELETE FROM {$table} WHERE libraryID=? AND MD5(CONCAT(subject, '_', predicate, '_', object))=?"; $deleted = Zotero_DB::query($sql, array($libraryID, $key), $shardID); } else { $sql = "DELETE FROM {$table} WHERE libraryID=? AND `key`=?"; $deleted = Zotero_DB::query($sql, array($libraryID, $key), $shardID); } unset(self::$idCache[$type][$libraryID][$key]); static::uncachePrimaryData($libraryID, $key); if ($deleted) { $sql = "INSERT INTO syncDeleteLogKeys (libraryID, objectType, `key`, timestamp)\n\t\t\t\t\t\tVALUES (?, '{$type}', ?, ?) ON DUPLICATE KEY UPDATE timestamp=?"; $timestamp = Zotero_DB::getTransactionTimestamp(); $params = array($libraryID, $key, $timestamp, $timestamp); Zotero_DB::query($sql, $params, $shardID); } Zotero_DB::commit(); }
private static function processUploadInternal($userID, SimpleXMLElement $xml, $syncQueueID = null, $syncProcessID = null) { $userLibraryID = Zotero_Users::getLibraryIDFromUserID($userID); $affectedLibraries = self::parseAffectedLibraries($xml->asXML()); // Relations-only uploads don't have affected libraries if (!$affectedLibraries) { $affectedLibraries = array(Zotero_Users::getLibraryIDFromUserID($userID)); } $processID = self::addUploadProcess($userID, $affectedLibraries, $syncQueueID, $syncProcessID); set_time_limit(5400); $profile = false; if ($profile) { $shardID = Zotero_Shards::getByUserID($userID); Zotero_DB::profileStart($shardID); } try { Z_Core::$MC->begin(); Zotero_DB::beginTransaction(); // Mark libraries as updated $timestamp = Zotero_Libraries::updateTimestamps($affectedLibraries); Zotero_DB::registerTransactionTimestamp($timestamp); // Make sure no other upload sessions use this same timestamp // for any of these libraries, since we return >= 1 as the next // last sync time if (!Zotero_Libraries::setTimestampLock($affectedLibraries, $timestamp)) { throw new Exception("Library timestamp already used", Z_ERROR_LIBRARY_TIMESTAMP_ALREADY_USED); } // Add/update creators if ($xml->creators) { // DOM $keys = array(); $xmlElements = dom_import_simplexml($xml->creators); $xmlElements = $xmlElements->getElementsByTagName('creator'); Zotero_DB::query("SET foreign_key_checks = 0"); try { $addedLibraryIDs = array(); $addedCreatorDataHashes = array(); foreach ($xmlElements as $xmlElement) { $key = $xmlElement->getAttribute('key'); if (isset($keys[$key])) { throw new Exception("Creator {$key} already processed"); } $keys[$key] = true; $creatorObj = Zotero_Creators::convertXMLToCreator($xmlElement); $addedLibraryIDs[] = $creatorObj->libraryID; $creatorObj->save(); } } catch (Exception $e) { Zotero_DB::query("SET foreign_key_checks = 1"); throw $e; } Zotero_DB::query("SET foreign_key_checks = 1"); unset($keys); unset($xml->creators); // // Manual foreign key checks // // libraryID foreach ($addedLibraryIDs as $addedLibraryID) { $shardID = Zotero_Shards::getByLibraryID($addedLibraryID); $sql = "SELECT COUNT(*) FROM shardLibraries WHERE libraryID=?"; if (!Zotero_DB::valueQuery($sql, $addedLibraryID, $shardID)) { throw new Exception("libraryID inserted into `creators` not found in `shardLibraries` ({$addedLibraryID}, {$shardID})"); } } } // Add/update items if ($xml->items) { $childItems = array(); $relatedItemsStore = array(); // DOM $keys = array(); $xmlElements = dom_import_simplexml($xml->items); $xmlElements = $xmlElements->getElementsByTagName('item'); foreach ($xmlElements as $xmlElement) { $key = $xmlElement->getAttribute('key'); if (isset($keys[$key])) { throw new Exception("Item {$key} already processed"); } $keys[$key] = true; $missing = Zotero_Items::removeMissingRelatedItems($xmlElement); $itemObj = Zotero_Items::convertXMLToItem($xmlElement); if ($missing) { $relatedItemsStore[$itemObj->libraryID . '_' . $itemObj->key] = $missing; } if (!$itemObj->getSourceKey()) { try { $itemObj->save($userID); } catch (Exception $e) { if (strpos($e->getMessage(), 'libraryIDs_do_not_match') !== false) { throw new Exception($e->getMessage() . " (" . $itemObj->key . ")"); } throw $e; } } else { $childItems[] = $itemObj; } } unset($keys); unset($xml->items); while ($childItem = array_shift($childItems)) { $childItem->save($userID); } // Add back related items (which now exist) foreach ($relatedItemsStore as $itemLibraryKey => $relset) { $lk = explode('_', $itemLibraryKey); $libraryID = $lk[0]; $key = $lk[1]; $item = Zotero_Items::getByLibraryAndKey($libraryID, $key); foreach ($relset as $relKey) { $relItem = Zotero_Items::getByLibraryAndKey($libraryID, $relKey); $item->addRelatedItem($relItem->id); } $item->save(); } unset($relatedItemsStore); } // Add/update collections if ($xml->collections) { $collections = array(); $collectionSets = array(); // DOM // Build an array of unsaved collection objects and the keys of child items $keys = array(); $xmlElements = dom_import_simplexml($xml->collections); $xmlElements = $xmlElements->getElementsByTagName('collection'); foreach ($xmlElements as $xmlElement) { $key = $xmlElement->getAttribute('key'); if (isset($keys[$key])) { throw new Exception("Collection {$key} already processed"); } $keys[$key] = true; $collectionObj = Zotero_Collections::convertXMLToCollection($xmlElement); $xmlItems = $xmlElement->getElementsByTagName('items')->item(0); // Fix an error if there's leading or trailing whitespace, // which was possible in 2.0.3 if ($xmlItems) { $xmlItems = trim($xmlItems->nodeValue); } $arr = array('obj' => $collectionObj, 'items' => $xmlItems ? explode(' ', $xmlItems) : array()); $collections[] = $collectionObj; $collectionSets[] = $arr; } unset($keys); unset($xml->collections); self::saveCollections($collections); unset($collections); // Set child items foreach ($collectionSets as $collection) { // Child items if (isset($collection['items'])) { $ids = array(); foreach ($collection['items'] as $key) { $item = Zotero_Items::getByLibraryAndKey($collection['obj']->libraryID, $key); if (!$item) { throw new Exception("Child item '{$key}' of collection {$collection['obj']->id} not found", Z_ERROR_ITEM_NOT_FOUND); } $ids[] = $item->id; } $collection['obj']->setChildItems($ids); } } unset($collectionSets); } // Add/update saved searches if ($xml->searches) { $searches = array(); $keys = array(); foreach ($xml->searches->search as $xmlElement) { $key = (string) $xmlElement['key']; if (isset($keys[$key])) { throw new Exception("Search {$key} already processed"); } $keys[$key] = true; $searchObj = Zotero_Searches::convertXMLToSearch($xmlElement); $searchObj->save(); } unset($xml->searches); } // Add/update tags if ($xml->tags) { $keys = array(); // DOM $xmlElements = dom_import_simplexml($xml->tags); $xmlElements = $xmlElements->getElementsByTagName('tag'); foreach ($xmlElements as $xmlElement) { $key = $xmlElement->getAttribute('key'); if (isset($keys[$key])) { throw new Exception("Tag {$key} already processed"); } $keys[$key] = true; $tagObj = Zotero_Tags::convertXMLToTag($xmlElement); $tagObj->save(true); } unset($keys); unset($xml->tags); } // Add/update relations if ($xml->relations) { // DOM $xmlElements = dom_import_simplexml($xml->relations); $xmlElements = $xmlElements->getElementsByTagName('relation'); foreach ($xmlElements as $xmlElement) { $relationObj = Zotero_Relations::convertXMLToRelation($xmlElement, $userLibraryID); if ($relationObj->exists()) { continue; } $relationObj->save(); } unset($keys); unset($xml->relations); } // TODO: loop if ($xml->deleted) { // Delete collections if ($xml->deleted->collections) { Zotero_Collections::deleteFromXML($xml->deleted->collections); } // Delete items if ($xml->deleted->items) { Zotero_Items::deleteFromXML($xml->deleted->items); } // Delete creators if ($xml->deleted->creators) { Zotero_Creators::deleteFromXML($xml->deleted->creators); } // Delete saved searches if ($xml->deleted->searches) { Zotero_Searches::deleteFromXML($xml->deleted->searches); } // Delete tags if ($xml->deleted->tags) { Zotero_Tags::deleteFromXML($xml->deleted->tags); } // Delete tags if ($xml->deleted->relations) { Zotero_Relations::deleteFromXML($xml->deleted->relations); } } self::removeUploadProcess($processID); Zotero_DB::commit(); Z_Core::$MC->commit(); if ($profile) { $shardID = Zotero_Shards::getByUserID($userID); Zotero_DB::profileEnd($shardID); } // Return timestamp + 1, to keep the next /updated call // (using >= timestamp) from returning this data return $timestamp + 1; } catch (Exception $e) { Z_Core::$MC->rollback(); Zotero_DB::rollback(true); self::removeUploadProcess($processID); throw $e; } }
public static function updateFromJSON(Zotero_Item $item, $json, $isNew = false, Zotero_Item $parentItem = null, $userID = null) { self::validateJSONItem($json, $item->libraryID, $isNew ? null : $item, !is_null($parentItem)); Zotero_DB::beginTransaction(); // Mark library as updated if (!$isNew) { $timestamp = Zotero_Libraries::updateTimestamps($item->libraryID); Zotero_DB::registerTransactionTimestamp($timestamp); } $forceChange = false; $twoStage = false; // Set itemType first $item->setField("itemTypeID", Zotero_ItemTypes::getID($json->itemType)); foreach ($json as $key => $val) { switch ($key) { case 'itemType': continue; case 'deleted': continue; case 'creators': if (!$val && !$item->numCreators()) { continue 2; } $orderIndex = -1; foreach ($val as $orderIndex => $newCreatorData) { if ((!isset($newCreatorData->name) || trim($newCreatorData->name) == "") && (!isset($newCreatorData->firstName) || trim($newCreatorData->firstName) == "") && (!isset($newCreatorData->lastName) || trim($newCreatorData->lastName) == "")) { // This should never happen, because of check in validateJSONItem() if (!$isNew) { throw new Exception("Nameless creator in update request"); } // On item creation, ignore creators with empty names, // because that's in the item template that the API returns break; } // JSON uses 'name' and 'firstName'/'lastName', // so switch to just 'firstName'/'lastName' if (isset($newCreatorData->name)) { $newCreatorData->firstName = ''; $newCreatorData->lastName = $newCreatorData->name; unset($newCreatorData->name); $newCreatorData->fieldMode = 1; } else { $newCreatorData->fieldMode = 0; } $newCreatorTypeID = Zotero_CreatorTypes::getID($newCreatorData->creatorType); // Same creator in this position $existingCreator = $item->getCreator($orderIndex); if ($existingCreator && $existingCreator['ref']->equals($newCreatorData)) { // Just change the creatorTypeID if ($existingCreator['creatorTypeID'] != $newCreatorTypeID) { $item->setCreator($orderIndex, $existingCreator['ref'], $newCreatorTypeID); } continue; } // Same creator in a different position, so use that $existingCreators = $item->getCreators(); for ($i = 0, $len = sizeOf($existingCreators); $i < $len; $i++) { if ($existingCreators[$i]['ref']->equals($newCreatorData)) { $item->setCreator($orderIndex, $existingCreators[$i]['ref'], $newCreatorTypeID); continue; } } // Make a fake creator to use for the data lookup $newCreator = new Zotero_Creator(); $newCreator->libraryID = $item->libraryID; foreach ($newCreatorData as $key => $val) { if ($key == 'creatorType') { continue; } $newCreator->{$key} = $val; } // Look for an equivalent creator in this library $candidates = Zotero_Creators::getCreatorsWithData($item->libraryID, $newCreator, true); if ($candidates) { $c = Zotero_Creators::get($item->libraryID, $candidates[0]); $item->setCreator($orderIndex, $c, $newCreatorTypeID); continue; } // None found, so make a new one $creatorID = $newCreator->save(); $newCreator = Zotero_Creators::get($item->libraryID, $creatorID); $item->setCreator($orderIndex, $newCreator, $newCreatorTypeID); } // Remove all existing creators above the current index if (!$isNew && ($indexes = array_keys($item->getCreators()))) { $i = max($indexes); while ($i > $orderIndex) { $item->removeCreator($i); $i--; } } break; case 'tags': // If item isn't yet saved, add tags below if (!$item->id) { $twoStage = true; break; } if ($item->setTags($val)) { $forceChange = true; } break; case 'attachments': case 'notes': if (!$val) { continue; } $twoStage = true; break; case 'note': $item->setNote($val); break; // Attachment properties // Attachment properties case 'linkMode': $item->attachmentLinkMode = Zotero_Attachments::linkModeNameToNumber($val, true); break; case 'contentType': case 'charset': case 'filename': $k = "attachment" . ucwords($key); $item->{$k} = $val; break; case 'md5': $item->attachmentStorageHash = $val; break; case 'mtime': $item->attachmentStorageModTime = $val; break; default: $item->setField($key, $val); break; } } if ($parentItem) { $item->setSource($parentItem->id); } $item->deleted = !empty($json->deleted); // For changes that don't register as changes internally, force a dateModified update if ($forceChange) { $item->setField('dateModified', Zotero_DB::getTransactionTimestamp()); } $item->save($userID); // Additional steps that have to be performed on a saved object if ($twoStage) { foreach ($json as $key => $val) { switch ($key) { case 'attachments': if (!$val) { continue; } foreach ($val as $attachment) { $childItem = new Zotero_Item(); $childItem->libraryID = $item->libraryID; self::updateFromJSON($childItem, $attachment, true, $item, $userID); } break; case 'notes': if (!$val) { continue; } $noteItemTypeID = Zotero_ItemTypes::getID("note"); foreach ($val as $note) { $childItem = new Zotero_Item(); $childItem->libraryID = $item->libraryID; $childItem->itemTypeID = $noteItemTypeID; $childItem->setSource($item->id); $childItem->setNote($note->note); $childItem->save(); } break; case 'tags': if ($item->setTags($val)) { $forceChange = true; } break; } } // For changes that don't register as changes internally, force a dateModified update if ($forceChange) { $item->setField('dateModified', Zotero_DB::getTransactionTimestamp()); } $item->save($userID); } Zotero_DB::commit(); }
public static function updateFromJSON(Zotero_Collection $collection, $json, $isNew = false) { self::validateJSONCollection($json); Zotero_DB::beginTransaction(); $timestamp = Zotero_Libraries::updateTimestamps($collection->libraryID); Zotero_DB::registerTransactionTimestamp($timestamp); $collection->name = $json->name; $parentKey = $json->parent; if ($parentKey) { $collection->parentKey = $parentKey; } else { $collection->parent = false; } $collection->save(); Zotero_DB::commit(); }