/** * Converts a Zotero_Collection object to a SimpleXMLElement Atom object * * @param Zotero_Collection $collection Zotero_Collection object * @param array $requestParams * @return SimpleXMLElement Collection data as SimpleXML element */ public static function convertCollectionToAtom(Zotero_Collection $collection, $requestParams) { // TEMP: multi-format support if (!empty($requestParams['content'])) { $content = $requestParams['content']; } else { $content = array('none'); } $content = $content[0]; $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>' . '<entry xmlns="' . Zotero_Atom::$nsAtom . '" xmlns:zapi="' . Zotero_Atom::$nsZoteroAPI . '"/>'); $title = $collection->name ? $collection->name : '[Untitled]'; $xml->title = $title; $author = $xml->addChild('author'); // TODO: group item creator $author->name = Zotero_Libraries::getName($collection->libraryID); $author->uri = Zotero_URI::getLibraryURI($collection->libraryID, true); $xml->id = Zotero_URI::getCollectionURI($collection); $xml->published = Zotero_Date::sqlToISO8601($collection->dateAdded); $xml->updated = Zotero_Date::sqlToISO8601($collection->dateModified); $link = $xml->addChild("link"); $link['rel'] = "self"; $link['type'] = "application/atom+xml"; $link['href'] = Zotero_API::getCollectionURI($collection); $parent = $collection->parent; if ($parent) { $parentCol = self::get($collection->libraryID, $parent); $link = $xml->addChild("link"); $link['rel'] = "up"; $link['type'] = "application/atom+xml"; $link['href'] = Zotero_API::getCollectionURI($parentCol); } $link = $xml->addChild('link'); $link['rel'] = 'alternate'; $link['type'] = 'text/html'; $link['href'] = Zotero_URI::getCollectionURI($collection, true); $xml->addChild('zapi:key', $collection->key, Zotero_Atom::$nsZoteroAPI); $xml->addChild('zapi:version', $collection->version, Zotero_Atom::$nsZoteroAPI); $collections = $collection->getChildCollections(); $xml->addChild('zapi:numCollections', sizeOf($collections), Zotero_Atom::$nsZoteroAPI); $xml->addChild('zapi:numItems', $collection->numItems(), Zotero_Atom::$nsZoteroAPI); if ($content == 'json') { $xml->content['type'] = 'application/json'; // Deprecated if ($requestParams['v'] < 2) { $xml->content->addAttribute('zapi:etag', $collection->etag, Zotero_Atom::$nsZoteroAPI); $xml->content['etag'] = $collection->etag; } $xml->content = Zotero_Utilities::formatJSON($collection->toJSON($requestParams)); } return $xml; }
public function toResponseJSON($requestParams = []) { $t = microtime(true); // Child collections and items can't be cached (easily) $numCollections = $this->numCollections(); $numItems = $this->numItems(); if (!$requestParams['uncached']) { $cacheKey = $this->getCacheKey($requestParams); $cached = Z_Core::$MC->get($cacheKey); if ($cached) { Z_Core::debug("Using cached JSON for {$this->libraryKey}"); $cached['meta']->numCollections = $numCollections; $cached['meta']->numItems = $numItems; StatsD::timing("api.collections.toResponseJSON.cached", (microtime(true) - $t) * 1000); StatsD::increment("memcached.collections.toResponseJSON.hit"); return $cached; } } $json = ['key' => $this->key, 'version' => $this->version, 'library' => Zotero_Libraries::toJSON($this->libraryID)]; // 'links' $json['links'] = ['self' => ['href' => Zotero_API::getCollectionURI($this), 'type' => 'application/json'], 'alternate' => ['href' => Zotero_URI::getCollectionURI($this, true), 'type' => 'text/html']]; $parentID = $this->getParentID(); if ($parentID) { $parentCol = Zotero_Collections::get($this->libraryID, $parentID); $json['links']['up'] = ['href' => Zotero_API::getCollectionURI($parentCol), 'type' => "application/atom+xml"]; } // 'meta' $json['meta'] = new stdClass(); $json['meta']->numCollections = $numCollections; $json['meta']->numItems = $numItems; // 'include' $include = $requestParams['include']; foreach ($include as $type) { if ($type == 'data') { $json[$type] = $this->toJSON($requestParams); } } if (!$requestParams['uncached']) { Z_Core::$MC->set($cacheKey, $json); StatsD::timing("api.collections.toResponseJSON.uncached", (microtime(true) - $t) * 1000); StatsD::increment("memcached.collections.toResponseJSON.miss"); } return $json; }
/** * Converts a Zotero_Collection object to a SimpleXMLElement Atom object * * @param object $item Zotero_Collection object * @param string $content * @return SimpleXMLElement Collection data as SimpleXML element */ public static function convertCollectionToAtom(Zotero_Collection $collection, $content = array('none')) { // TEMP: multi-format support $content = $content[0]; $xml = new SimpleXMLElement('<entry xmlns="' . Zotero_Atom::$nsAtom . '" xmlns:zapi="' . Zotero_Atom::$nsZoteroAPI . '"/>'); $title = $collection->name ? $collection->name : '[Untitled]'; $xml->title = $title; $author = $xml->addChild('author'); // TODO: group item creator $author->name = Zotero_Libraries::getName($collection->libraryID); $author->uri = Zotero_URI::getLibraryURI($collection->libraryID); $xml->id = Zotero_URI::getCollectionURI($collection); $xml->published = Zotero_Date::sqlToISO8601($collection->dateAdded); $xml->updated = Zotero_Date::sqlToISO8601($collection->dateModified); $link = $xml->addChild("link"); $link['rel'] = "self"; $link['type'] = "application/atom+xml"; $link['href'] = Zotero_Atom::getCollectionURI($collection); $parent = $collection->parent; if ($parent) { $parentCol = self::get($collection->libraryID, $parent); $link = $xml->addChild("link"); $link['rel'] = "up"; $link['type'] = "application/atom+xml"; $link['href'] = Zotero_Atom::getCollectionURI($parentCol); } $link = $xml->addChild('link'); $link['rel'] = 'alternate'; $link['type'] = 'text/html'; $link['href'] = Zotero_URI::getCollectionURI($collection); $xml->addChild('zapi:key', $collection->key, Zotero_Atom::$nsZoteroAPI); $collections = $collection->getChildCollections(); $xml->addChild('zapi:numCollections', sizeOf($collections), Zotero_Atom::$nsZoteroAPI); $xml->addChild('zapi:numItems', $collection->numItems(), Zotero_Atom::$nsZoteroAPI); if ($content == 'json') { $xml->content['type'] = 'application/json'; $xml->content->addAttribute('zapi:etag', $collection->etag, Zotero_Atom::$nsZoteroAPI); // TODO: remove non-namespaced attribute $xml->content['etag'] = $collection->etag; $xml->content = $collection->toJSON(); } else { if ($content == 'full') { $xml->content['type'] = 'application/xml'; $fullXML = Zotero_Collections::convertCollectionToXML($collection); $fullXML->addAttribute("xmlns", Zotero_Atom::$nsZoteroTransfer); $fNode = dom_import_simplexml($xml->content); $subNode = dom_import_simplexml($fullXML); $importedNode = $fNode->ownerDocument->importNode($subNode, true); $fNode->appendChild($importedNode); } } return $xml; }