Пример #1
0
 public static function createAtomFeed($action, $title, $url, $entries, $totalResults = null, $queryParams = [], Zotero_Permissions $permissions = null, $fixedValues = array())
 {
     if ($queryParams) {
         $nonDefaultParams = Zotero_API::getNonDefaultParams($action, $queryParams);
     } else {
         $nonDefaultParams = [];
     }
     $feed = '<?xml version="1.0" encoding="UTF-8"?>' . '<feed xmlns="' . Zotero_Atom::$nsAtom . '" ' . 'xmlns:zapi="' . Zotero_Atom::$nsZoteroAPI . '"/>';
     $xml = new SimpleXMLElement($feed);
     $xml->title = $title;
     $path = parse_url($url, PHP_URL_PATH);
     // Generate canonical URI
     $zoteroURI = Zotero_URI::getBaseURI() . substr($path, 1) . Zotero_API::buildQueryString($queryParams['v'], $action, $nonDefaultParams, ['v']);
     $baseURI = Zotero_API::getBaseURI() . substr($path, 1);
     // API version isn't included in URLs (as with the API key)
     //
     // It could alternatively be made a private parameter so that it didn't appear
     // in the Link header either, but for now it's still there.
     $excludeParams = ['v'];
     $links = Zotero_API::buildLinks($action, $path, $totalResults, $queryParams, $nonDefaultParams, $excludeParams);
     $xml->id = $zoteroURI;
     $link = $xml->addChild("link");
     $link['rel'] = "self";
     $link['type'] = "application/atom+xml";
     $link['href'] = $links['self'];
     $link = $xml->addChild("link");
     $link['rel'] = "first";
     $link['type'] = "application/atom+xml";
     $link['href'] = $links['first'];
     if (isset($links['next'])) {
         $link = $xml->addChild("link");
         $link['rel'] = "next";
         $link['type'] = "application/atom+xml";
         $link['href'] = $links['next'];
     }
     $link = $xml->addChild("link");
     $link['rel'] = "last";
     $link['type'] = "application/atom+xml";
     $link['href'] = $links['last'];
     // Generate alternate URI
     $link = $xml->addChild("link");
     $link['rel'] = "alternate";
     $link['type'] = "text/html";
     $link['href'] = $links['alternate'];
     if ($queryParams['v'] < 3) {
         $xml->addChild("zapi:totalResults", is_numeric($totalResults) ? $totalResults : sizeOf($entries), self::$nsZoteroAPI);
     }
     if ($queryParams['v'] < 2) {
         $xml->addChild("zapi:apiVersion", 1, self::$nsZoteroAPI);
     }
     $latestUpdated = '';
     // Check memcached for bib data
     $sharedData = array();
     if ($entries && $entries[0] instanceof Zotero_Item) {
         if (in_array('citation', $queryParams['content'])) {
             $sharedData["citation"] = Zotero_Cite::multiGetFromMemcached("citation", $entries, $queryParams);
         }
         if (in_array('bib', $queryParams['content'])) {
             $sharedData["bib"] = Zotero_Cite::multiGetFromMemcached("bib", $entries, $queryParams);
         }
     }
     $xmlEntries = array();
     foreach ($entries as $entry) {
         if ($entry->dateModified > $latestUpdated) {
             $latestUpdated = $entry->dateModified;
         }
         if ($entry instanceof SimpleXMLElement) {
             $xmlEntries[] = $entry;
         } else {
             if ($entry instanceof Zotero_Collection) {
                 $entry = Zotero_Collections::convertCollectionToAtom($entry, $queryParams);
                 $xmlEntries[] = $entry;
             } else {
                 if ($entry instanceof Zotero_Item) {
                     $entry = Zotero_Items::convertItemToAtom($entry, $queryParams, $permissions, $sharedData);
                     $xmlEntries[] = $entry;
                 } else {
                     if ($entry instanceof Zotero_Search) {
                         $entry = $entry->toAtom($queryParams);
                         $xmlEntries[] = $entry;
                     } else {
                         if ($entry instanceof Zotero_Tag) {
                             $xmlEntries[] = $entry->toAtom($queryParams, isset($fixedValues[$entry->id]) ? $fixedValues[$entry->id] : null);
                         } else {
                             if ($entry instanceof Zotero_Group) {
                                 $entry = $entry->toAtom($queryParams);
                                 $xmlEntries[] = $entry;
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($latestUpdated) {
         $xml->updated = Zotero_Date::sqlToISO8601($latestUpdated);
     } else {
         $xml->updated = str_replace("+00:00", "Z", date('c'));
     }
     // Import object XML nodes into document
     $doc = dom_import_simplexml($xml);
     foreach ($xmlEntries as $xmlEntry) {
         $subNode = dom_import_simplexml($xmlEntry);
         $importedNode = $doc->ownerDocument->importNode($subNode, true);
         $doc->appendChild($importedNode);
     }
     return $xml;
 }