Skip to content

Commit

Permalink
Merge pull request #468 from GlazerMann/patch-8
Browse files Browse the repository at this point in the history
Remove ASIN, if we have ISBN
  • Loading branch information
Martin R. Smith committed Aug 1, 2018
2 parents 6eecefb + b7228a9 commit e527c28
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
28 changes: 25 additions & 3 deletions Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,25 @@ public function add_if_new($param_name, $value) {
return $this->add($param_name, $value);
}
return FALSE;

case 'asin':
if ($this->blank($param_name)) {
if($this->has('isbn')) { // Already have ISBN
quiet_echo("\n . Not adding ASIN: redundant to existing ISBN.");
return FALSE;
} elseif (preg_match("~^\d~", $value) && substr($value, 0, 3) !== '630') { // 630 ones are not ISBNs
$possible_isbn = sanitize_string($value);
$possible_isbn13 = $this->isbn10Toisbn13($possible_isbn);
if ($possible_isbn === $possible_isbn13) {
return $this->add('asin', $possible_isbn); // Something went wrong, add as ASIN
} else {
return $this->add('isbn', $possible_isbn13);
}
} else { // NOT ISBN
return $this->add($param_name, sanitize_string($value));
}
}
return FALSE;

default:
if ($this->blank($param_name)) {
Expand Down Expand Up @@ -2267,8 +2286,11 @@ protected function tidy() {
$this->rename('origyear', 'year');
}

if ($this->has('isbn')) $this->set('isbn',$this->isbn10Toisbn13($this->get('isbn'))); // Upgrade ISBN

if ($this->has('isbn')) {
$this->set('isbn', $this->isbn10Toisbn13($this->get('isbn'))); // Upgrade ISBN
$this->forget('asin');
}

$authors = $this->get('authors');
if (!$authors) {
$authors = $this->get('author'); # Order _should_ be irrelevant as only one will be set... but prefer 'authors' if not.
Expand Down Expand Up @@ -2447,7 +2469,7 @@ protected function verify_doi () {
if ($this->query_crossref() === FALSE) {
// Replace old "doi_inactivedate" and/or other broken/inactive-date parameters,
// if present, with new "doi-broken-date"
$url_test = "https://dx.doi.org/".$doi ;
$url_test = "https://dx.doi.org/" . $doi;
$headers_test = @get_headers($url_test, 1);
if ($headers_test === FALSE) {
echo "\n ! DOI status unkown. dx.doi.org failed to respond at all to: " . htmlspecialchars($doi);
Expand Down
21 changes: 19 additions & 2 deletions tests/phpunit/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,27 @@ public function testArxivExpansion() {
}

public function testAmazonExpansion() {
$text = "{{Cite web | http://www.amazon.com/On-Origin-Phyla-James-Valentine/dp/0226845494 | accessdate=2012-04-20}}";
$text = "{{Cite web | url=http://www.amazon.com/On-Origin-Phyla-James-Valentine/dp/0226845494 | accessdate=2012-04-20 |isbn=}}";
$expanded = $this->process_citation($text);
$this->assertEquals('cite book', $expanded->wikiname());
$this->assertEquals('0226845494', $expanded->get('asin'));
$this->assertEquals('978-0226845494', $expanded->get('isbn'));
$this->assertNull($expanded->get('asin'));

$text = "{{Cite web | url=https://www.amazon.com/Gold-Toe-Metropolitan-Dress-Three/dp/B0002TV0K8 | accessdate=2012-04-20}}";
$expanded = $this->process_citation($text);
$this->assertEquals($text, $expanded->parsed_text()); // We do not touch this kind of URL
}

public function testRemoveASIN() {
$text = "{{Cite book | asin=B0002TV0K8 |isbn=}}";
$expanded = $this->process_citation($text);
$this->assertEquals('B0002TV0K8', $expanded->get('asin'));
$this->assertEquals('', $expanded->get('isbn'));

$text = "{{Cite book | asin=0226845494 |isbn=0226845494}}";
$expanded = $this->process_citation($text);
$this->assertEquals('978-0226845494', $expanded->get('isbn'));
$this->assertNull($expanded->get('asin'));
}

public function testDoiExpansion() {
Expand Down

0 comments on commit e527c28

Please sign in to comment.