Esempio n. 1
0
 /**
  * Generate self/first/prev/next/last/alternate links
  */
 public static function buildLinks($action, $path, $totalResults, $queryParams, $nonDefaultParams, $excludeParams = [])
 {
     $apiVersion = $queryParams['v'];
     $baseURI = Zotero_API::getBaseURI() . substr($path, 1);
     $alternateBaseURI = Zotero_URI::getBaseWWWURI() . substr($path, 1);
     $links = [];
     //
     // Generate URIs for 'self', 'first', 'next' and 'last' links
     //
     // 'self'
     $links['self'] = $baseURI;
     if ($nonDefaultParams) {
         $links['self'] .= Zotero_API::buildQueryString($apiVersion, $action, $nonDefaultParams, $excludeParams);
     }
     // 'first'
     $links['first'] = $baseURI;
     if ($nonDefaultParams) {
         $p = $nonDefaultParams;
         unset($p['start']);
         $links['first'] .= Zotero_API::buildQueryString($apiVersion, $action, $p, $excludeParams);
     }
     // 'prev'
     if ($queryParams['start']) {
         $p = $nonDefaultParams;
         $prevStart = $queryParams['start'] - $queryParams['limit'];
         if ($prevStart <= 0) {
             unset($p['start']);
         } else {
             $p['start'] = $prevStart;
         }
         $links['prev'] = $baseURI . Zotero_API::buildQueryString($apiVersion, $action, $p, $excludeParams);
     }
     // 'last'
     if (!$queryParams['start'] && $queryParams['limit'] >= $totalResults) {
         $links['last'] = $links['self'];
     } else {
         if ($queryParams['limit'] != 0) {
             // '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']);
             }
             $links['last'] = $baseURI . Zotero_API::buildQueryString($apiVersion, $action, $p, $excludeParams);
             // 'next'
             $nextStart = $queryParams['start'] + $queryParams['limit'];
             if ($nextStart < $totalResults) {
                 $p = $nonDefaultParams;
                 $p['start'] = $nextStart;
                 $links['next'] = $baseURI . Zotero_API::buildQueryString($apiVersion, $action, $p, $excludeParams);
             }
         }
     }
     $links['alternate'] = $alternateBaseURI;
     return $links;
 }
Esempio n. 2
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;
 }