From e33023d6b114edcb58ef750be627516c8b69ea98 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 20 May 2015 17:06:04 -0400 Subject: [PATCH] Add support for passing remote style URLs in 'style' param Closes #24 --- model/Cite.inc.php | 14 +++--- tests/remote/tests/API/3/BibTest.php | 67 ++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/model/Cite.inc.php b/model/Cite.inc.php index ffb92c5d..efecba44 100644 --- a/model/Cite.inc.php +++ b/model/Cite.inc.php @@ -402,7 +402,7 @@ private static function buildURLPath(array $queryParams, $mode) { foreach ($queryParams as $param => $value) { switch ($param) { case 'style': - if (!is_string($value) || !preg_match('/^[a-zA-Z0-9\-]+$/', $value)) { + if (!is_string($value) || !preg_match('/^(https?|[a-zA-Z0-9\-]+$)/', $value)) { throw new Exception("Invalid style", Z_ERROR_CITESERVER_INVALID_STYLE); } $url .= "&" . $param . "=" . urlencode($value); @@ -447,13 +447,17 @@ private static function makeRequest(array $queryParams, $mode, $json) { $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - if ($code != 200) { - error_log($code . " from citation server -- trying another " - . "[URL: '$url'] [INPUT: '$json'] [RESPONSE: '$response']"); + if ($code == 400) { + throw new Exception("Invalid style", Z_ERROR_CITESERVER_INVALID_STYLE); } if ($code == 404) { - throw new Exception("Invalid style", Z_ERROR_CITESERVER_INVALID_STYLE); + throw new Exception("Style not found", Z_ERROR_CITESERVER_INVALID_STYLE); + } + + if ($code != 200) { + error_log($code . " from citation server -- trying another " + . "[URL: '$url'] [INPUT: '$json'] [RESPONSE: '$response']"); } // If no response, try another server diff --git a/tests/remote/tests/API/3/BibTest.php b/tests/remote/tests/API/3/BibTest.php index 335c3811..09203850 100644 --- a/tests/remote/tests/API/3/BibTest.php +++ b/tests/remote/tests/API/3/BibTest.php @@ -32,7 +32,12 @@ class BibTests extends APITests { private static $items; private static $multiResponses = []; - private static $styles = array("default", "apa"); + private static $styles = [ + "default", + "apa", + "https://www.zotero.org/styles/apa", + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" + ]; public static function setUpBeforeClass() { parent::setUpBeforeClass(); @@ -54,21 +59,29 @@ public static function setUpBeforeClass() { 'json' => [ "citation" => array( "default" => 'Last, Title.', - "apa" => '(Last, 2014)' + "apa" => '(Last, 2014)', + "https://www.zotero.org/styles/apa" => '(Last, 2014)', + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" => '[1]' ), "bib" => array( "default" => '
Last, First. Title, 2014.
', - "apa" => '
Last, F. (2014). Title.
' + "apa" => '
Last, F. (2014). Title.
', + "https://www.zotero.org/styles/apa" => '
Last, F. (2014). Title.
', + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" => '
[1]
F. Last, Title. 2014.
' ) ], 'atom' => [ "citation" => array( "default" => 'Last, Title.', - "apa" => '(Last, 2014)' + "apa" => '(Last, 2014)', + "https://www.zotero.org/styles/apa" => '(Last, 2014)', + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" => '[1]' ), "bib" => array( "default" => '
Last, First. Title, 2014.
', - "apa" => '
Last, F. (2014). Title.
' + "apa" => '
Last, F. (2014). Title.
', + "https://www.zotero.org/styles/apa" => '
Last, F. (2014). Title.
', + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" => '
[1]
F. Last, Title. 2014.
' ) ] ]; @@ -93,28 +106,38 @@ public static function setUpBeforeClass() { 'json' => [ "citation" => array( "default" => 'Last, Title 2.', - "apa" => '(Last, 2014)' + "apa" => '(Last, 2014)', + "https://www.zotero.org/styles/apa" => '(Last, 2014)', + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" => '[1]' ), "bib" => array( "default" => '
Last, First. Title 2. Edited by Ed McEditor, 2014.
', - "apa" => '
Last, F. (2014). Title 2. (E. McEditor, Ed.).
' + "apa" => '
Last, F. (2014). Title 2. (E. McEditor, Ed.).
', + "https://www.zotero.org/styles/apa" => '
Last, F. (2014). Title 2. (E. McEditor, Ed.).
', + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" => '
[1]
F. Last, Title 2. 2014.
' ) ], 'atom' => [ "citation" => array( "default" => 'Last, Title 2.', - "apa" => '(Last, 2014)' + "apa" => '(Last, 2014)', + "https://www.zotero.org/styles/apa" => '(Last, 2014)', + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" => '[1]' ), "bib" => array( "default" => '
Last, First. Title 2. Edited by Ed McEditor, 2014.
', - "apa" => '
Last, F. (2014). Title 2. (E. McEditor, Ed.).
' + "apa" => '
Last, F. (2014). Title 2. (E. McEditor, Ed.).
', + "https://www.zotero.org/styles/apa" => '
Last, F. (2014). Title 2. (E. McEditor, Ed.).
', + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" => '
[1]
F. Last, Title 2. 2014.
' ) ] ]; self::$multiResponses = [ "default" => '
Last, First. Title, 2014.
———. Title 2. Edited by Ed McEditor, 2014.
', - "apa" => '
Last, F. (2014a). Title.
Last, F. (2014b). Title 2. (E. McEditor, Ed.).
' + "apa" => '
Last, F. (2014a). Title.
Last, F. (2014b). Title 2. (E. McEditor, Ed.).
', + "https://www.zotero.org/styles/apa" => '
Last, F. (2014a). Title.
Last, F. (2014b). Title 2. (E. McEditor, Ed.).
', + "https://raw.githubusercontent.com/citation-style-language/styles/master/ieee.csl" => '
[1]
F. Last, Title 2. 2014.
[2]
F. Last, Title. 2014.
' ]; } @@ -130,7 +153,8 @@ public function testIncludeCitationSingle() { foreach (self::$items as $key => $expected) { $response = API::userGet( self::$config['userID'], - "items/$key?include=citation" . ($style == "default" ? "" : "&style=$style") + "items/$key?include=citation" + . ($style == "default" ? "" : "&style=" . urlencode($style)) ); $this->assert200($response); $json = API::getJSONFromResponse($response); @@ -146,7 +170,8 @@ public function testContentCitationSingle() { foreach (self::$items as $key => $expected) { $response = API::userGet( self::$config['userID'], - "items/$key?content=citation" . ($style == "default" ? "" : "&style=$style") + "items/$key?content=citation" + . ($style == "default" ? "" : "&style=" . urlencode($style)) ); $this->assert200($response); $content = API::getContentFromResponse($response); @@ -167,7 +192,7 @@ public function testIncludeCitationMulti() { $response = API::userGet( self::$config['userID'], "items?itemKey=$keyStr&include=citation" - . ($style == "default" ? "" : "&style=$style") + . ($style == "default" ? "" : "&style=" . urlencode($style)) ); $this->assert200($response); $this->assertTotalResults(sizeOf($keys), $response); @@ -192,7 +217,7 @@ public function testContentCitationMulti() { $response = API::userGet( self::$config['userID'], "items?itemKey=$keyStr&content=citation" - . ($style == "default" ? "" : "&style=$style") + . ($style == "default" ? "" : "&style=" . urlencode($style)) ); $this->assert200($response); $this->assertTotalResults(sizeOf($keys), $response); @@ -217,7 +242,8 @@ public function testIncludeBibSingle() { foreach (self::$items as $key => $expected) { $response = API::userGet( self::$config['userID'], - "items/$key?include=bib" . ($style == "default" ? "" : "&style=$style") + "items/$key?include=bib" + . ($style == "default" ? "" : "&style=" . urlencode($style)) ); $this->assert200($response); $json = API::getJSONFromResponse($response); @@ -233,7 +259,8 @@ public function testContentBibSingle() { foreach (self::$items as $key => $expected) { $response = API::userGet( self::$config['userID'], - "items/$key?content=bib" . ($style == "default" ? "" : "&style=$style") + "items/$key?content=bib" + . ($style == "default" ? "" : "&style=" . urlencode($style)) ); $this->assert200($response); $content = API::getContentFromResponse($response); @@ -253,7 +280,8 @@ public function testIncludeBibMulti() { foreach (self::$styles as $style) { $response = API::userGet( self::$config['userID'], - "items?itemKey=$keyStr&include=bib" . ($style == "default" ? "" : "&style=$style") + "items?itemKey=$keyStr&include=bib" + . ($style == "default" ? "" : "&style=" . urlencode($style)) ); $this->assert200($response); $this->assertTotalResults(sizeOf($keys), $response); @@ -275,7 +303,8 @@ public function testContentBibMulti() { foreach (self::$styles as $style) { $response = API::userGet( self::$config['userID'], - "items?itemKey=$keyStr&content=bib" . ($style == "default" ? "" : "&style=$style") + "items?itemKey=$keyStr&content=bib" + . ($style == "default" ? "" : "&style=" . urlencode($style)) ); $this->assert200($response); $xml = API::getXMLFromResponse($response); @@ -298,7 +327,7 @@ public function testFormatBibMultiple() { foreach (self::$styles as $style) { $response = API::userGet( self::$config['userID'], - "items?format=bib" . ($style == "default" ? "" : "&style=$style") + "items?format=bib" . ($style == "default" ? "" : "&style=" . urlencode($style)) ); $this->assert200($response); $this->assertXmlStringEqualsXmlString(self::$multiResponses[$style], $response->getBody());