/** * 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; }