public static function get($groupID, $skipExistsCheck = false) { if (!$groupID) { throw new Exception('$groupID not set'); } if (isset(self::$groups[$groupID])) { $group = self::$groups[$groupID]; if ($group->erased) { unset(self::$groups[$groupID]); return false; } return $group; } $group = new Zotero_Group(); $group->id = $groupID; if (!$skipExistsCheck && !$group->exists()) { return false; } self::$groups[$groupID] = $group; return self::$groups[$groupID]; }
private static function processDownloadInternal($userID, $lastsync, DOMDocument $doc, $syncDownloadQueueID = null, $syncDownloadProcessID = null, $params = []) { $apiVersion = (int) $doc->documentElement->getAttribute('version'); if ($lastsync == 1) { StatsD::increment("sync.process.download.full"); } // TEMP $cacheKeyExtra = (!empty($params['ft']) ? json_encode($params['ft']) : "") . (!empty($params['ftkeys']) ? json_encode($params['ftkeys']) : ""); try { $cached = Zotero_Sync::getCachedDownload($userID, $lastsync, $apiVersion, $cacheKeyExtra); if ($cached) { $doc->loadXML($cached); StatsD::increment("sync.process.download.cache.hit"); return; } } catch (Exception $e) { $msg = $e->getMessage(); if (strpos($msg, "Too many connections") !== false) { $msg = "'Too many connections' from MySQL"; } else { $msg = "'{$msg}'"; } Z_Core::logError("Warning: {$msg} getting cached download"); StatsD::increment("sync.process.download.cache.error"); } set_time_limit(1800); $profile = false; if ($profile) { $shardID = Zotero_Shards::getByUserID($userID); Zotero_DB::profileStart(0); } if ($syncDownloadQueueID) { self::addDownloadProcess($syncDownloadQueueID, $syncDownloadProcessID); } $updatedNode = $doc->createElement('updated'); $doc->documentElement->appendChild($updatedNode); $userLibraryID = Zotero_Users::getLibraryIDFromUserID($userID); $updatedCreators = array(); try { Zotero_DB::beginTransaction(); // Blocks until any upload processes are done $updateTimes = Zotero_Libraries::getUserLibraryUpdateTimes($userID); $timestamp = Zotero_DB::getTransactionTimestampUnix(); $doc->documentElement->setAttribute('timestamp', $timestamp); $doc->documentElement->setAttribute('userID', $userID); $doc->documentElement->setAttribute('defaultLibraryID', $userLibraryID); $updateKey = Zotero_Users::getUpdateKey($userID); $doc->documentElement->setAttribute('updateKey', $updateKey); // Get libraries with update times >= $timestamp $updatedLibraryIDs = array(); foreach ($updateTimes as $libraryID => $timestamp) { if ($timestamp >= $lastsync) { $updatedLibraryIDs[] = $libraryID; } } // Add new and updated groups $joinedGroups = Zotero_Groups::getJoined($userID, (int) $lastsync); $updatedIDs = array_unique(array_merge($joinedGroups, Zotero_Groups::getUpdated($userID, (int) $lastsync))); if ($updatedIDs) { $node = $doc->createElement('groups'); $showGroups = false; foreach ($updatedIDs as $id) { $group = new Zotero_Group(); $group->id = $id; $xmlElement = $group->toXML($userID); $newNode = dom_import_simplexml($xmlElement); $newNode = $doc->importNode($newNode, true); $node->appendChild($newNode); $showGroups = true; } if ($showGroups) { $updatedNode->appendChild($node); } } // If there's updated data in any library or // there are any new groups (in which case we need all their data) $hasData = $updatedLibraryIDs || $joinedGroups; if ($hasData) { foreach (Zotero_DataObjects::$classicObjectTypes as $syncObject) { $Name = $syncObject['singular']; // 'Item' $Names = $syncObject['plural']; // 'Items' $name = strtolower($Name); // 'item' $names = strtolower($Names); // 'items' $className = 'Zotero_' . $Names; $updatedIDsByLibraryID = call_user_func(array($className, 'getUpdated'), $userID, $lastsync, $updatedLibraryIDs); if ($updatedIDsByLibraryID) { $node = $doc->createElement($names); foreach ($updatedIDsByLibraryID as $libraryID => $ids) { if ($name == 'creator') { $updatedCreators[$libraryID] = $ids; } foreach ($ids as $id) { if ($name == 'item') { $obj = call_user_func(array($className, 'get'), $libraryID, $id); $data = array('updatedCreators' => isset($updatedCreators[$libraryID]) ? $updatedCreators[$libraryID] : array()); $xmlElement = Zotero_Items::convertItemToXML($obj, $data, $apiVersion); } else { $instanceClass = 'Zotero_' . $Name; $obj = new $instanceClass(); if (method_exists($instanceClass, '__construct')) { $obj->__construct(); } $obj->libraryID = $libraryID; if ($name == 'setting') { $obj->name = $id; } else { $obj->id = $id; } if ($name == 'tag') { $xmlElement = call_user_func(array($className, "convert{$Name}ToXML"), $obj, true); } else { if ($name == 'creator') { $xmlElement = call_user_func(array($className, "convert{$Name}ToXML"), $obj, $doc); if ($xmlElement->getAttribute('libraryID') == $userLibraryID) { $xmlElement->removeAttribute('libraryID'); } $node->appendChild($xmlElement); } else { if ($name == 'relation') { // Skip new-style related items if ($obj->predicate == 'dc:relation') { continue; } $xmlElement = call_user_func(array($className, "convert{$Name}ToXML"), $obj); if ($apiVersion <= 8) { unset($xmlElement['libraryID']); } } else { if ($name == 'setting') { $xmlElement = call_user_func(array($className, "convert{$Name}ToXML"), $obj, $doc); $node->appendChild($xmlElement); } else { $xmlElement = call_user_func(array($className, "convert{$Name}ToXML"), $obj); } } } } } if ($xmlElement instanceof SimpleXMLElement) { if ($xmlElement['libraryID'] == $userLibraryID) { unset($xmlElement['libraryID']); } $newNode = dom_import_simplexml($xmlElement); $newNode = $doc->importNode($newNode, true); $node->appendChild($newNode); } } } if ($node->hasChildNodes()) { $updatedNode->appendChild($node); } } } } // Add full-text content if the client supports it if (isset($params['ft'])) { $libraries = Zotero_Libraries::getUserLibraries($userID); $fulltextNode = false; foreach ($libraries as $libraryID) { if (!empty($params['ftkeys']) && $params['ftkeys'] === 'all') { $ftlastsync = 1; } else { $ftlastsync = $lastsync; } if (!empty($params['ftkeys'][$libraryID])) { $keys = $params['ftkeys'][$libraryID]; } else { $keys = []; } $data = Zotero_FullText::getNewerInLibraryByTime($libraryID, $ftlastsync, $keys); if ($data) { if (!$fulltextNode) { $fulltextNode = $doc->createElement('fulltexts'); } foreach ($data as $itemData) { if ($params['ft']) { $empty = $itemData['empty']; } else { $empty = true; } $first = false; $node = Zotero_FullText::itemDataToXML($itemData, $doc, $empty); $fulltextNode->appendChild($node); } } } if ($fulltextNode) { $updatedNode->appendChild($fulltextNode); } } // Get earliest timestamp $earliestModTime = Zotero_Users::getEarliestDataTimestamp($userID); $doc->documentElement->setAttribute('earliest', $earliestModTime ? $earliestModTime : 0); // Deleted objects $deletedKeys = $hasData ? self::getDeletedObjectKeys($userID, $lastsync, true) : false; $deletedIDs = self::getDeletedObjectIDs($userID, $lastsync, true); if ($deletedKeys || $deletedIDs) { $deletedNode = $doc->createElement('deleted'); // Add deleted data objects if ($deletedKeys) { foreach (Zotero_DataObjects::$classicObjectTypes as $syncObject) { $Name = $syncObject['singular']; // 'Item' $Names = $syncObject['plural']; // 'Items' $name = strtolower($Name); // 'item' $names = strtolower($Names); // 'items' if (empty($deletedKeys[$names])) { continue; } $typeNode = $doc->createElement($names); foreach ($deletedKeys[$names] as $row) { $node = $doc->createElement($name); if ($row['libraryID'] != $userLibraryID || $name == 'setting') { $node->setAttribute('libraryID', $row['libraryID']); } $node->setAttribute('key', $row['key']); $typeNode->appendChild($node); } $deletedNode->appendChild($typeNode); } } // Add deleted groups if ($deletedIDs) { $name = "group"; $names = "groups"; $typeNode = $doc->createElement($names); $ids = $doc->createTextNode(implode(' ', $deletedIDs[$names])); $typeNode->appendChild($ids); $deletedNode->appendChild($typeNode); } $updatedNode->appendChild($deletedNode); } Zotero_DB::commit(); } catch (Exception $e) { Zotero_DB::rollback(true); if ($syncDownloadQueueID) { self::removeDownloadProcess($syncDownloadProcessID); } throw $e; } function relaxNGErrorHandler($errno, $errstr) { Zotero_Sync::$validationError = $errstr; } set_error_handler('relaxNGErrorHandler'); $valid = $doc->relaxNGValidate(Z_ENV_MODEL_PATH . 'relax-ng/updated.rng'); restore_error_handler(); if (!$valid) { if ($syncDownloadQueueID) { self::removeDownloadProcess($syncDownloadProcessID); } throw new Exception(self::$validationError . "\n\nXML:\n\n" . $doc->saveXML()); } // Cache response if response isn't empty try { if ($doc->documentElement->firstChild->hasChildNodes()) { self::cacheDownload($userID, $updateKey, $lastsync, $apiVersion, $doc->saveXML(), $cacheKeyExtra); } } catch (Exception $e) { Z_Core::logError("WARNING: " . $e); } if ($syncDownloadQueueID) { self::removeDownloadProcess($syncDownloadProcessID); } if ($profile) { $shardID = Zotero_Shards::getByUserID($userID); Zotero_DB::profileEnd(0); } }
public function groups() { if (($this->method == 'POST' || $this->method == 'PUT') && !$this->body) { $this->e400("{$this->method} data not provided"); } $groupID = $this->groupID; // // Add a group // if ($this->method == 'POST') { if (!$this->permissions->isSuper()) { $this->e403(); } if ($groupID) { $this->e400("POST requests cannot end with a groupID (did you mean PUT?)"); } try { $group = @new SimpleXMLElement($this->body); } catch (Exception $e) { $this->e400("{$this->method} data is not valid XML"); } if ((int) $group['id']) { $this->e400("POST requests cannot contain a groupID in '" . $this->body . "'"); } $fields = $this->getFieldsFromGroupXML($group); Zotero_DB::beginTransaction(); try { $group = new Zotero_Group(); foreach ($fields as $field => $val) { $group->{$field} = $val; } $group->save(); } catch (Exception $e) { if (strpos($e->getMessage(), "Invalid") === 0) { $this->e400($e->getMessage() . " in " . $this->body . "'"); } switch ($e->getCode()) { case Z_ERROR_GROUP_NAME_UNAVAILABLE: $this->e400($e->getMessage()); default: $this->e500($e->getMessage()); } } $this->responseXML = $group->toAtom(array('full'), $this->queryParams, $this->apiVersion); Zotero_DB::commit(); $url = Zotero_Atom::getGroupURI($group); header("Location: " . $url, false, 201); $this->end(); } // // Update a group // if ($this->method == 'PUT') { if (!$this->permissions->isSuper()) { $this->e403(); } if (!$groupID) { $this->e400("PUT requests must end with a groupID (did you mean POST?)"); } try { $group = @new SimpleXMLElement($this->body); } catch (Exception $e) { $this->e400("{$this->method} data is not valid XML"); } $fields = $this->getFieldsFromGroupXML($group); // Group id is optional, but, if it's there, make sure it matches $id = (string) $group['id']; if ($id && $id != $groupID) { $this->e400("Group ID {$id} does not match group ID {$groupID} from URI"); } Zotero_DB::beginTransaction(); try { $group = Zotero_Groups::get($groupID); if (!$group) { $this->e404("Group {$groupID} does not exist"); } foreach ($fields as $field => $val) { $group->{$field} = $val; } if ($this->ifUnmodifiedSince && strtotime($group->dateModified) > $this->ifUnmodifiedSince) { $this->e412(); } $group->save(); } catch (Exception $e) { if (strpos($e->getMessage(), "Invalid") === 0) { $this->e400($e->getMessage() . " in " . $this->body . "'"); } else { if ($e->getCode() == Z_ERROR_GROUP_DESCRIPTION_TOO_LONG) { $this->e400($e->getMessage()); } } $this->e500($e->getMessage()); } $this->responseXML = $group->toAtom(array('full'), $this->queryParams, $this->apiVersion); Zotero_DB::commit(); $this->end(); } // // Delete a group // if ($this->method == 'DELETE') { if (!$this->permissions->isSuper()) { $this->e403(); } if (!$groupID) { $this->e400("DELETE requests must end with a groupID"); } Zotero_DB::beginTransaction(); $group = Zotero_Groups::get($groupID); if (!$group) { $this->e404("Group {$groupID} does not exist"); } $group->erase(); Zotero_DB::commit(); header("HTTP/1.1 204 No Content"); exit; } // // View one or more groups // // Single group if ($groupID) { $group = Zotero_Groups::get($groupID); if (!$this->permissions->canAccess($this->objectLibraryID)) { $this->e403(); } if (!$group) { $this->e404("Group not found"); } $this->responseXML = $group->toAtom($this->queryParams['content'], $this->queryParams, $this->apiVersion); } else { if ($this->objectUserID) { // Users (or their keys) can see only their own groups if (!$this->permissions->isSuper() && $this->userID != $this->objectUserID) { $this->e403(); } $title = Zotero_Users::getUsername($this->objectUserID) . "’s Groups"; } else { // For now, only root can do unrestricted group searches if (!$this->permissions->isSuper()) { $this->e403(); } $title = "Groups"; } try { $results = Zotero_Groups::getAllAdvanced($this->objectUserID, $this->queryParams, $this->permissions); } catch (Exception $e) { switch ($e->getCode()) { case Z_ERROR_INVALID_GROUP_TYPE: $this->e400($e->getMessage()); } throw $e; } $groups = $results['groups']; $totalResults = $results['totalResults']; $this->responseXML = Zotero_Atom::createAtomFeed($title, $this->uri, $groups, $totalResults, $this->queryParams, $this->apiVersion, $this->permissions); } $this->end(); }
public function groups() { $groupID = $this->objectGroupID; // // Add a group // if ($this->method == 'POST') { if (!$this->permissions->isSuper()) { $this->e403(); } if ($groupID) { $this->e400("POST requests cannot end with a groupID (did you mean PUT?)"); } try { $group = @new SimpleXMLElement($this->body); } catch (Exception $e) { $this->e400("{$this->method} data is not valid XML"); } if ((int) $group['id']) { $this->e400("POST requests cannot contain a groupID in '" . $this->body . "'"); } $fields = $this->getFieldsFromGroupXML($group); Zotero_DB::beginTransaction(); try { $group = new Zotero_Group(); foreach ($fields as $field => $val) { $group->{$field} = $val; } $group->save(); } catch (Exception $e) { if (strpos($e->getMessage(), "Invalid") === 0) { $this->e400($e->getMessage() . " in " . $this->body . "'"); } switch ($e->getCode()) { case Z_ERROR_GROUP_NAME_UNAVAILABLE: $this->e400($e->getMessage()); default: $this->handleException($e); } } $this->queryParams['content'] = array('full'); $this->responseXML = $group->toAtom($this->queryParams); Zotero_DB::commit(); $url = Zotero_API::getGroupURI($group); $this->responseCode = 201; header("Location: " . $url, false, 201); $this->end(); } // // Update a group // if ($this->method == 'PUT') { if (!$this->permissions->isSuper()) { $this->e403(); } if (!$groupID) { $this->e400("PUT requests must end with a groupID (did you mean POST?)"); } try { $group = @new SimpleXMLElement($this->body); } catch (Exception $e) { $this->e400("{$this->method} data is not valid XML"); } $fields = $this->getFieldsFromGroupXML($group); // Group id is optional, but, if it's there, make sure it matches $id = (string) $group['id']; if ($id && $id != $groupID) { $this->e400("Group ID {$id} does not match group ID {$groupID} from URI"); } Zotero_DB::beginTransaction(); try { $group = Zotero_Groups::get($groupID); if (!$group) { $this->e404("Group {$groupID} does not exist"); } foreach ($fields as $field => $val) { $group->{$field} = $val; } if ($this->ifUnmodifiedSince && strtotime($group->dateModified) > $this->ifUnmodifiedSince) { $this->e412(); } $group->save(); } catch (Exception $e) { if (strpos($e->getMessage(), "Invalid") === 0) { $this->e400($e->getMessage() . " in " . $this->body . "'"); } else { if ($e->getCode() == Z_ERROR_GROUP_DESCRIPTION_TOO_LONG) { $this->e400($e->getMessage()); } } $this->handleException($e); } $this->queryParams['content'] = array('full'); $this->responseXML = $group->toAtom($this->queryParams); Zotero_DB::commit(); $this->end(); } // // Delete a group // if ($this->method == 'DELETE') { if (!$this->permissions->isSuper()) { $this->e403(); } if (!$groupID) { $this->e400("DELETE requests must end with a groupID"); } Zotero_DB::beginTransaction(); $group = Zotero_Groups::get($groupID); if (!$group) { $this->e404("Group {$groupID} does not exist"); } $group->erase(); Zotero_DB::commit(); header("HTTP/1.1 204 No Content"); exit; } // // View one or more groups // // Single group if ($groupID) { $group = Zotero_Groups::get($groupID); if (!$this->permissions->canAccess($this->objectLibraryID)) { $this->e403(); } if (!$group) { $this->e404("Group not found"); } if ($this->apiVersion >= 3) { $this->libraryVersion = $group->version; } else { header("ETag: " . $group->etag); } if ($this->method == 'HEAD') { $this->end(); } switch ($this->queryParams['format']) { case 'atom': $this->responseXML = $group->toAtom($this->queryParams); break; case 'json': $json = $group->toResponseJSON($this->queryParams); echo Zotero_Utilities::formatJSON($json); break; default: throw new Exception("Unexpected format '" . $this->queryParams['format'] . "'"); } } else { if ($this->objectUserID) { $title = Zotero_Users::getUsername($this->objectUserID) . "’s Groups"; } else { // For now, only root can do unrestricted group searches if (!$this->permissions->isSuper()) { $this->e403(); } $title = "Groups"; } try { $results = Zotero_Groups::getAllAdvanced($this->objectUserID, $this->queryParams, $this->permissions); } catch (Exception $e) { switch ($e->getCode()) { case Z_ERROR_INVALID_GROUP_TYPE: $this->e400($e->getMessage()); } throw $e; } $options = ['action' => $this->action, 'uri' => $this->uri, 'results' => $results, 'requestParams' => $this->queryParams, 'permissions' => $this->permissions, 'head' => $this->method == 'HEAD']; switch ($this->queryParams['format']) { case 'atom': $this->responseXML = Zotero_API::multiResponse(array_merge($options, ['title' => $title])); break; case 'json': Zotero_API::multiResponse($options); break; case 'etags': case 'versions': $prop = substr($this->queryParams['format'], 0, -1); // remove 's' $newResults = []; foreach ($results['results'] as $group) { $newResults[$group->id] = $group->{$prop}; } $options['results']['results'] = $newResults; Zotero_API::multiResponse($options, 'versions'); break; default: throw new Exception("Unexpected format '" . $this->queryParams['format'] . "'"); } } $this->end(); }