diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index 6973ce0..e4b79ad 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -12,8 +12,8 @@ $ phpunit --coverage-html ./coverage btb-test.php $_GET['library']=1; -copy('bibtexbrowser.local.php','bibtexbrowser.local.php.bak'); -unlink('bibtexbrowser.local.php'); +@copy('bibtexbrowser.local.php','bibtexbrowser.local.php.bak'); +@unlink('bibtexbrowser.local.php'); require_once ('bibtexbrowser.php'); @@ -23,8 +23,9 @@ class BTBTest extends PHPUnit_Framework_TestCase { function createDB() { $test_data = fopen('php://memory','x+'); - fwrite($test_data, "@book{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009}\n". - "@book{aKey/withSlash,title={Slash Dangerous for web servers},author={Ap Ache},publisher={Springer},year=2009}\n" + fwrite($test_data, "@book{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009}\n" + ."@book{aKey/withSlash,title={Slash Dangerous for web servers},author={Ap Ache},publisher={Springer},year=2009}\n" + ."@article{aKeyA,title={An Article},author={Foo Bar and Jane Doe},volume=5,journal=\"New Results\",year=2009}\n" ); fseek($test_data,0); $btb = new BibDataBase(); @@ -33,13 +34,44 @@ class BTBTest extends PHPUnit_Framework_TestCase { } - function test_bibentry_to_html() { + function test_bibentry_to_html_book() { $btb = $this->createDB(); - $first_entry=$btb->bibdb[array_keys($btb->bibdb)[0]]; + $first_entry=$btb->getEntryByKey('aKey'); + + // default style + $this->assertEquals("A Book (Martin Monperrus), Springer, 2009. [bibtex]",strip_tags($first_entry->toHTML())); + $this->assertEquals('A Book ( ), Springer, 2009. ',$first_entry->toHTML()); + + // IEEE style + bibtexbrowser_configure('BIBLIOGRAPHYSTYLE','JanosBibliographyStyle'); + $this->assertEquals("Martin Monperrus, \"A Book\", Springer, 2009.\n [bibtex]",strip_tags($first_entry->toHTML())); + + // Vancouver style + bibtexbrowser_configure('BIBLIOGRAPHYSTYLE','VancouverBibliographyStyle'); + $this->assertEquals("Martin Monperrus. A Book. Springer; 2009.\n [bibtex]",strip_tags($first_entry->toHTML())); + + // other methods $this->assertEquals(' ',$first_entry->bib2links()); $this->assertEquals('',$first_entry->anchor()); } + function test_bibentry_to_html_article() { + $btb = $this->createDB(); + $first_entry=$btb->getEntryByKey('aKeyA'); + + // default style + $this->assertEquals("An Article (Foo Bar and Jane Doe), In New Results, volume 5, 2009. [bibtex]",strip_tags($first_entry->toHTML())); + $this->assertEquals('An Article ( ), In New Results, volume 5, 2009. ',$first_entry->toHTML()); + + // IEEE style + bibtexbrowser_configure('BIBLIOGRAPHYSTYLE','JanosBibliographyStyle'); + $this->assertEquals("Foo Bar and Jane Doe, \"An Article\", In New Results, vol. 5, 2009.\n [bibtex]",strip_tags($first_entry->toHTML())); + + // Vancouver style + bibtexbrowser_configure('BIBLIOGRAPHYSTYLE','VancouverBibliographyStyle'); + $this->assertEquals("Foo Bar and Jane Doe. An Article. New Results. 2009;5.\n [bibtex]",strip_tags($first_entry->toHTML())); + } + function testMultiSearch() { $btb = $this->createDB(); $q=array(Q_AUTHOR=>'monperrus'); @@ -67,16 +99,17 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertTrue(config_value('BIBTEXBROWSER_NO_DEFAULT')); ob_start(); default_message(); - $this->assertEquals('', ob_get_flush()); + $this->assertEquals('', ob_get_clean()); // setting to false bibtexbrowser_configure('BIBTEXBROWSER_NO_DEFAULT', false); $this->assertFalse(config_value('BIBTEXBROWSER_NO_DEFAULT')); ob_start(); default_message(); - $this->assertContains('Congratulations', ob_get_flush()); + $this->assertContains('Congratulations', ob_get_clean()); } + function testInternationalization() { $btb = $this->createDB(); global $BIBTEXBROWSER_LANG; @@ -89,7 +122,7 @@ class BTBTest extends PHPUnit_Framework_TestCase { $d->setDB($btb); ob_start(); $d->display(); - $data = ob_get_flush(); + $data = ob_get_clean(); $this->assertContains('Livres', $data); } @@ -270,6 +303,7 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals("\`a Book",$dis->getTitle()); } + function test_PagedDisplay() { $PAGE_SIZE = 3; bibtexbrowser_configure('BIBTEXBROWSER_DEFAULT_DISPLAY', 'PagedDisplay'); @@ -279,7 +313,7 @@ class BTBTest extends PHPUnit_Framework_TestCase { $d = new Dispatcher(); ob_start(); $d->main(); - $content = "
@@ -735,7 +791,7 @@ usage: notes: method build can be used several times, bibtex entries are accumulated in the builder */ -class BibDBBuilder { +class BibDBBuilder extends ParserDelegate { /** A hashtable from keys to bib entries (BibEntry). */ var $builtdb = array(); @@ -794,7 +850,8 @@ class BibDBBuilder { //print_r($this->builtdb); } - function setEntryField($finalkey,$entryvalue) { + function setEntryField($fieldkey,$entryvalue) { + $fieldkey=trim($fieldkey); // support for Bibtex concatenation // see http://newton.ex.ac.uk/tex/pack/bibtex/btxdoc/node3.html // (?currentEntry->setField($finalkey,$entryvalue); + $this->currentEntry->setField($fieldkey,$entryvalue); } function setEntryType($entrytype) { @@ -850,7 +907,7 @@ class BibDBBuilder { // we format the author names in a special field // to enable search if ($this->currentEntry->hasField('author')) { - $this->currentEntry->setField(Q_INNER_AUTHOR,$this->currentEntry->getFormattedAuthorsImproved()); + $this->currentEntry->setField(Q_INNER_AUTHOR,$this->currentEntry->getFormattedAuthorsString()); } // ignoring jabref comments @@ -868,6 +925,20 @@ class BibDBBuilder { // we add it to the database else { $this->builtdb[$this->currentEntry->getKey()] = $this->currentEntry; + } + } + + function entryValuePart($key, $value, $type) { + if (preg_match('/^author$/i',trim($key)) && strlen($value)>0) { + if ($type == 'CURLYTOP') { + foreach (preg_split('/\s+and\s+/i', $value) as $author) { + if (strlen($author)>0) { + $this->currentEntry->addAuthor($author); + } + } + } else { // 'CURLYONE', nnon-breakable + $this->currentEntry->addAuthor($value); + } } } } // end class BibDBBuilder @@ -1073,6 +1144,10 @@ class BibEntry { /** The location in the original bibtex file (set by addEntry) */ var $order = -1; + /** the authors (split at parsing time) */ + var $author_array = array(); + + /** returns a debug string representation */ function __toString() { return $this->getType()." ".$this->getKey(); @@ -1274,11 +1349,16 @@ class BibEntry { return isset($this->fields[strtolower($name)]); } + /** adds an author to this entry */ + function addAuthor($author) { + $this->author_array[] = trim($author); + } + /** Returns the authors of this entry. If "author" is not given, * return a string 'Unknown'. */ function getAuthor() { if (array_key_exists(AUTHOR, $this->fields)) { - return $this->fields[AUTHOR]; + return getFormattedAuthorsString(); } // 2010-03-02: commented the following, it results in misleading author lists // issue found by Alan P. Sexton @@ -1322,26 +1402,26 @@ class BibEntry { return ''; } - /** Returns the authors of this entry as an array */ + /** Returns the authors of this entry as an array (split by " and ") */ function getRawAuthors() { - $authors = array(); - foreach (preg_split('/ and /i', $this->getAuthor()) as $author) { - $authors[]=$author; - } - return $authors; + return $this->author_array; } /** - * Returns the formated author name w.r.t to the user preference encoded in USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT - */ + * Returns the formated author name w.r.t to the user preference + * encoded in USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT and USE_INITIALS_FOR_NAMES + */ function formatAuthor($author){ - if (USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT) { + if (bibtexbrowser_configuration('USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT')) { return $this->formatAuthorCommaSeparated($author); - } else if (USE_INITIALS_FOR_NAMES) { + } else if (bibtexbrowser_configuration('USE_INITIALS_FOR_NAMES')) { return $this->formatAuthorInitials($author); + } else if (bibtexbrowser_configuration('USE_FIRST_THEN_LAST')) { + return $this->formatAuthorCanonical($author); } - else return $this->formatAuthorCanonical($author); + // otherwise to formatting + else return $author; } /** @@ -1373,24 +1453,23 @@ class BibEntry { } - /** Returns the authors as an array of strings (one string per author) */ - function getFormattedAuthors() { - $authors = array(); - foreach ($this->getRawAuthors() as $author) { - $authors[]=''.$this->formatAuthor($author).''; - } - return $authors; - } + /** @deprecated */ + function formattedAuthors() { return $this->getFormattedAuthorsString(); } + /** @deprecated */ + function getFormattedAuthors() { return $this->getFormattedAuthorsArray(); } + /** @deprecated */ + function getFormattedAuthorsImproved() { return $this->getFormattedAuthorsString(); } - /** @deprecated - * @see getFormattedAuthorsImproved() - */ - function formattedAuthors() { return $this->getFormattedAuthorsImproved(); } - /** Adds to getFormattedAuthors() the home page links and returns a string (not an array). Is configured with BIBTEXBROWSER_AUTHOR_LINKS and USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT. - */ - function getFormattedAuthorsImproved() { - $array_authors = $this->getFormattedAuthors(); + /** Returns the authors as an array of strings (one string per author). + */ + function getFormattedAuthorsArray() { + $array_authors = array(); + + // first we use formatAuthor + foreach ($this->getRawAuthors() as $author) { + $array_authors[]=$this->formatAuthor($author); + } if (BIBTEXBROWSER_AUTHOR_LINKS=='homepage') { foreach ($array_authors as $k => $author) { @@ -1404,13 +1483,31 @@ class BibEntry { } } - if (USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT) {$sep = '; ';} else {$sep = ', ';} - if (FORCE_NAMELIST_SEPARATOR !== '') {$sep = FORCE_NAMELIST_SEPARATOR;} + return $array_authors; + } - return implode($sep ,$array_authors); + /** Adds to getFormattedAuthors() the home page links and returns a string (not an array). Is configured with BIBTEXBROWSER_AUTHOR_LINKS and USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT. + */ + function getFormattedAuthorsString() { + return $this->implodeAuthors($this->getFormattedAuthorsArray()); } + + function implodeAuthors($authors) { + if (count($authors)==0) return ''; + if (count($authors)==1) return $authors[0]; + + $result = ''; - /** adds a link to the author page */ + if (bibtexbrowser_configuration('USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT')) {$sep = '; ';} else {$sep = ', ';} + if (FORCE_NAMELIST_SEPARATOR !== '') {$sep = FORCE_NAMELIST_SEPARATOR;} + for ($i=0;$i$author)); return "$author"; @@ -1477,7 +1574,7 @@ class BibEntry { foreach ($this->getEditors() as $editor) { $editors[]=$this->addHomepageLink($this->formatAuthor($editor)); } - if (USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT) {$sep = '; ';} else {$sep = ', ';} + if (bibtexbrowser_configuration('USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT')) {$sep = '; ';} else {$sep = ', ';} if (FORCE_NAMELIST_SEPARATOR !== '') {$sep = FORCE_NAMELIST_SEPARATOR;} return implode($sep, $editors).', '.(count($editors)>1?'eds.':'ed.'); } @@ -1705,7 +1802,7 @@ class BibEntry { $url_parts[]='rft.date='.s3988($this->getYear()); - foreach ($this->getFormattedAuthors() as $au) $url_parts[]='rft.au='.s3988($au); + foreach ($this->getFormattedAuthorsArray() as $au) $url_parts[]='rft.au='.s3988($au); return ''; @@ -1847,7 +1944,7 @@ function print_footer_layout() { /** this function encapsulates the user-defined name for bib to HTML*/ function bib2html(&$bibentry) { - $function = BIBLIOGRAPHYSTYLE; + $function = bibtexbrowser_configuration('BIBLIOGRAPHYSTYLE'); return $function($bibentry); } @@ -2089,10 +2186,20 @@ function DefaultBibliographyStyle(&$bibentry) { if ($bibentry->hasField('url')) $title = ' '.$title.''; - // author + $coreInfo = $title; + + // adding author info if ($bibentry->hasField('author')) { - $coreInfo = $title . ' ( )';} - else $coreInfo = $title; + $coreInfo .= ' ( )'; + } // core info usually contains title + author $entry[] = $coreInfo; @@ -2189,7 +2296,7 @@ function JanosBibliographyStyle(&$bibentry) { // author if ($bibentry->hasField('author')) { - $entry[] = $bibentry->formattedAuthors(); + $entry[] = $bibentry->getFormattedAuthorsString(); } // title @@ -2299,7 +2406,7 @@ function VancouverBibliographyStyle(&$bibentry) { // author if ($bibentry->hasField('author')) { - $entry[] = $bibentry->formattedAuthors().'. '; + $entry[] = $bibentry->getFormattedAuthorsString().'. '; } // Ensure punctuation mark at title's end @@ -2972,7 +3079,7 @@ class SimpleDisplay { } if (BIBTEXBROWSER_DEBUG) { - echo 'Style: '.BIBLIOGRAPHYSTYLE.'
'; + echo 'Style: '.bibtexbrowser_configuration('BIBLIOGRAPHYSTYLE').'
'; echo 'Order: '.ORDER_FUNCTION.'
'; echo 'Abbrv: '.ABBRV_TYPE.'
'; echo 'Options: '.@implode(',',$this->options).'
'; @@ -3177,7 +3284,7 @@ class BibEntryDisplay { /** 2011/10/02: new display, inspired from Tom Zimmermann's home page */ function displayOnSteroids() { - $subtitle = 'by '.$this->bib->getFormattedAuthorsImproved().''; + $subtitle = 'by '.$this->bib->getFormattedAuthorsString().''; $abstract = ''; if ($this->bib->hasField('abstract')) { @@ -4553,4 +4660,4 @@ class Dispatcher { $class = BIBTEXBROWSER_MAIN;// extension point $main = new $class(); $main->main(); -?> \ No newline at end of file +?>