diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index e4b79ad..50a5639 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -237,7 +237,7 @@ class BTBTest extends PHPUnit_Framework_TestCase { $btb->update_internal("inline", $test_data); $first_entry=$btb->bibdb[array_keys($btb->bibdb)[0]]; $this->assertEquals('[pdf]',$first_entry->getLink('pdf')); - $this->assertEquals('[pdf]',$first_entry->getUrlLink()); + $this->assertEquals('[pdf]',$first_entry->getPdfLink()); $this->assertEquals('[pdf]',$first_entry->getLink('pdf','pdficon.png')); $this->assertEquals('[see]',$first_entry->getLink('pdf',NULL,'see')); } @@ -251,7 +251,7 @@ class BTBTest extends PHPUnit_Framework_TestCase { $btb = new BibDataBase(); $btb->update_internal("inline", $test_data); $first_entry=$btb->bibdb[array_keys($btb->bibdb)[0]]; - $this->assertEquals('[pdf]',$first_entry->getUrlLink()); + $this->assertEquals('[pdf]',$first_entry->getPdfLink()); } // https://github.com/monperrus/bibtexbrowser/issues/40 @@ -332,6 +332,40 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals(2,count($dis->getKeywords())); } + # https://github.com/monperrus/bibtexbrowser/pull/51 + function test_emptyGetPdfLink() { + $bibtex = " + @article{aKey, + title={\`a Book}, + author={Martin Monperrus}, + publisher={Springer}, + year=2009, + pages={42--4242}, + number=1 + } + @article{bKey, + url={magic.pdf}, + } + @article{cKey, + pdf={magic2.pdf}, + url={magic3} + }"; + $test_data = fopen('php://memory','x+'); + fwrite($test_data, $bibtex); + fseek($test_data,0); + $db = new BibDataBase(); + $db->update_internal("inline", $test_data); + + $dis = $db->getEntryByKey('aKey'); + $this->assertEquals("",$dis->getPdfLink()); + + $dis = $db->getEntryByKey('bKey'); + $this->assertEquals('[pdf]',$dis->getPdfLink()); + + $dis = $db->getEntryByKey('cKey'); + $this->assertEquals('[pdf]',$dis->getPdfLink()); + } + function test_formatting() { $bibtex = "@article{aKey61,title={An article Book},author = {Meyer, Heribert and {Advanced Air and Ground Research Team} and Foo Bar}}\n"; @@ -426,7 +460,6 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals("Foo Bar", $authors[2]); } - } // end class ?> diff --git a/bibtexbrowser.php b/bibtexbrowser.php index 2e2fe6f..aeb0744 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1273,19 +1273,27 @@ class BibEntry { return $link; } - /** returns a "[pdf]" link if relevant. modified to exploit the new method, while keeping backward compatibility */ - function getUrlLink($iconurl = NULL, $label = 'pdf') { - if ($this->hasField('url')) { - return $this->getLink('url', $iconurl, $label); - } + /** same as `getPdfLink`, kept for backward compatibility */ + function getUrlLink($iconurl, $label) { + return $this->getPdfLink($iconurl, $label); + } + + /** returns a "[pdf]" link for the entry, if possible. + Tries to get the target URL from the 'pdf' field first, then from 'url' or 'file'. + */ + function getPdfLink($iconurl = NULL, $label = 'pdf') { if ($this->hasField('pdf')) { return $this->getLink('pdf', $iconurl, $label); } + if ($this->hasField('url')) { + return $this->getLink('url', $iconurl, $label); + } // Adding link to PDF file exported by Zotero // ref: https://github.com/monperrus/bibtexbrowser/pull/14 if ($this->hasField('file')) { return $this->getLink('file', $iconurl, $label); } + return ""; } @@ -1913,7 +1921,7 @@ function bib2links_default(&$bibentry) { } if (BIBTEXBROWSER_PDF_LINKS) { - $link = $bibentry->getUrlLink(); + $link = $bibentry->getPdfLink(); if ($link != '') { $links[] = $link; }; }