Beispiel #1
0
 public static function createAtomFeed($title, $url, $entries, $totalResults = null, $queryParams = null, $apiVersion = null, $permissions = null, $fixedValues = array())
 {
     if ($queryParams) {
         $nonDefaultParams = Zotero_API::getNonDefaultQueryParams($queryParams);
         // Convert 'content' array to sorted comma-separated string
         if (isset($nonDefaultParams['content'])) {
             $nonDefaultParams['content'] = implode(',', $nonDefaultParams['content']);
         }
     } else {
         $nonDefaultParams = array();
     }
     $feed = '<feed xmlns="' . Zotero_Atom::$nsAtom . '" ' . 'xmlns:zapi="' . Zotero_Atom::$nsZoteroAPI . '"';
     if ($queryParams && $queryParams['content'][0] == 'full') {
         $feed .= ' xmlns:zxfer="' . Zotero_Atom::$nsZoteroTransfer . '"';
     }
     $feed .= '/>';
     $xml = new SimpleXMLElement($feed);
     $xml->title = $title;
     $path = parse_url($url, PHP_URL_PATH);
     // Generate canonical URI
     $zoteroURI = Zotero_URI::getBaseURI() . substr($path, 1);
     if ($nonDefaultParams) {
         $zoteroURI .= "?" . http_build_query($nonDefaultParams);
     }
     $atomURI = Zotero_Atom::getBaseURI() . substr($path, 1);
     //
     // Generate URIs for 'self', 'first', 'next' and 'last' links
     //
     // 'self'
     $atomSelfURI = $atomURI;
     if ($nonDefaultParams) {
         $atomSelfURI .= "?" . http_build_query($nonDefaultParams);
     }
     // 'first'
     $atomFirstURI = $atomURI;
     if ($nonDefaultParams) {
         $p = $nonDefaultParams;
         unset($p['start']);
         if ($first = http_build_query($p)) {
             $atomFirstURI .= "?" . $first;
         }
     }
     // 'last'
     if (!$queryParams['start'] && $queryParams['limit'] >= $totalResults) {
         $atomLastURI = $atomSelfURI;
     } else {
         // 'start' past results
         if ($queryParams['start'] >= $totalResults) {
             $lastStart = $totalResults - $queryParams['limit'];
         } else {
             $lastStart = $totalResults - $totalResults % $queryParams['limit'];
             if ($lastStart == $totalResults) {
                 $lastStart = $totalResults - $queryParams['limit'];
             }
         }
         $p = $nonDefaultParams;
         if ($lastStart > 0) {
             $p['start'] = $lastStart;
         } else {
             unset($p['start']);
         }
         $atomLastURI = $atomURI;
         if ($last = http_build_query($p)) {
             $atomLastURI .= "?" . $last;
         }
         // 'next'
         $nextStart = $queryParams['start'] + $queryParams['limit'];
         if ($nextStart < $totalResults) {
             $p = $nonDefaultParams;
             $p['start'] = $nextStart;
             $atomNextURI = $atomURI . "?" . http_build_query($p);
         }
     }
     $xml->id = $zoteroURI;
     $link = $xml->addChild("link");
     $link['rel'] = "self";
     $link['type'] = "application/atom+xml";
     $link['href'] = $atomSelfURI;
     $link = $xml->addChild("link");
     $link['rel'] = "first";
     $link['type'] = "application/atom+xml";
     $link['href'] = $atomFirstURI;
     if (isset($atomNextURI)) {
         $link = $xml->addChild("link");
         $link['rel'] = "next";
         $link['type'] = "application/atom+xml";
         $link['href'] = $atomNextURI;
     }
     $link = $xml->addChild("link");
     $link['rel'] = "last";
     $link['type'] = "application/atom+xml";
     $link['href'] = $atomLastURI;
     // Generate alternate URI
     $alternateURI = Zotero_URI::getBaseURI() . substr($path, 1);
     if ($nonDefaultParams) {
         $p = $nonDefaultParams;
         if (isset($p['content'])) {
             unset($p['content']);
         }
         if ($p) {
             $alternateURI .= "?" . http_build_query($p);
         }
     }
     $link = $xml->addChild("link");
     $link['rel'] = "alternate";
     $link['type'] = "text/html";
     $link['href'] = $alternateURI;
     $xml->addChild("zapi:totalResults", is_numeric($totalResults) ? $totalResults : sizeOf($entries), self::$nsZoteroAPI);
     $xml->addChild("zapi:apiVersion", $apiVersion, self::$nsZoteroAPI);
     $latestUpdated = '';
     // Get bib data using parallel requests
     $sharedData = array();
     if ($entries && $entries[0] instanceof Zotero_Item) {
         if (in_array('citation', $queryParams['content'])) {
             $sharedData["citation"] = Zotero_Cite::multiGetFromCiteServer("citation", $entries, $queryParams['style']);
         }
         if (in_array('bib', $queryParams['content'])) {
             $sharedData["bib"] = Zotero_Cite::multiGetFromCiteServer("bib", $entries, $queryParams['style']);
         }
     }
     $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['content'], $apiVersion);
                 $xmlEntries[] = $entry;
             } else {
                 if ($entry instanceof Zotero_Creator) {
                     $entry = Zotero_Creators::convertCreatorToAtom($entry, $queryParams['content'], $apiVersion);
                     $xmlEntries[] = $entry;
                 } else {
                     if ($entry instanceof Zotero_Item) {
                         $entry = Zotero_Items::convertItemToAtom($entry, $queryParams, $apiVersion, $permissions, $sharedData);
                         $xmlEntries[] = $entry;
                     } else {
                         if ($entry instanceof Zotero_Search) {
                             $entry = Zotero_Searches::convertSearchToAtom($entry, $queryParams['content'], $apiVersion);
                             $xmlEntries[] = $entry;
                         } else {
                             if ($entry instanceof Zotero_Tag) {
                                 $xmlEntries[] = $entry->toAtom($queryParams['content'], $apiVersion, isset($fixedValues[$entry->id]) ? $fixedValues[$entry->id] : null);
                             } else {
                                 if ($entry instanceof Zotero_Group) {
                                     $entry = $entry->toAtom($queryParams['content'], $apiVersion);
                                     $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;
 }