From c6f82792bdf84ca39e7161469c1c951c1b88c39e Mon Sep 17 00:00:00 2001 From: Federico Poloni Date: Fri, 25 Dec 2015 18:06:03 +0100 Subject: [PATCH 1/3] Inverted order of conditionals in GetUrlLink, and renamed function. Fixes #50. Fixed test case as well. --- bibtexbrowser-test.php | 4 ++-- bibtexbrowser.php | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index 6973ce0..fa08f9c 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -204,7 +204,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')); } @@ -218,7 +218,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 diff --git a/bibtexbrowser.php b/bibtexbrowser.php index a371f17..48f777b 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1199,13 +1199,13 @@ class BibEntry { } /** 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); - } + 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')) { @@ -1816,7 +1816,7 @@ function bib2links_default(&$bibentry) { } if (BIBTEXBROWSER_PDF_LINKS) { - $link = $bibentry->getUrlLink(); + $link = $bibentry->getPdfLink(); if ($link != '') { $links[] = $link; }; } From 1a289d262b5d9a519541adca01e21dfd83930a14 Mon Sep 17 00:00:00 2001 From: Federico Poloni Date: Fri, 8 Jan 2016 14:35:16 +0100 Subject: [PATCH 2/3] changed documentation of getPdfLink As requested by @monperrus in a comment to the pull request https://github.com/monperrus/bibtexbrowser/pull/51/files#r49186982 --- bibtexbrowser.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bibtexbrowser.php b/bibtexbrowser.php index 48f777b..2a7b2ba 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1198,7 +1198,9 @@ class BibEntry { return $link; } - /** returns a "[pdf]" link if relevant. modified to exploit the new method, while keeping backward compatibility */ + /** 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); From 0a59f63524bd1f44fa7c9da1b91aa0a3cbc8741c Mon Sep 17 00:00:00 2001 From: Federico Poloni Date: Sun, 31 Jul 2016 14:36:14 +0200 Subject: [PATCH 3/3] getpdfurl fallback return + test case --- bibtexbrowser-test.php | 32 ++++++++++++++++++++++++++++++++ bibtexbrowser.php | 1 + 2 files changed, 33 insertions(+) diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index fa08f9c..0b29709 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -297,6 +297,38 @@ class BTBTest extends PHPUnit_Framework_TestCase { $dis = $db->getEntryByKey('aKey'); $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}, + } + @article{cKey, + pdf={magic2}, + url={magic3} + }"; + bibtexbrowser_configure('BIBTEXBROWSER_USE_LATEX2HTML', true); + $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()); + } } // end class ?> \ No newline at end of file diff --git a/bibtexbrowser.php b/bibtexbrowser.php index 2a7b2ba..0fcb35d 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1213,6 +1213,7 @@ class BibEntry { if ($this->hasField('file')) { return $this->getLink('file', $iconurl, $label); } + return ""; }