diff --git a/bibtexbrowser-cli.php b/bibtexbrowser-cli.php new file mode 100644 index 0000000..b53b93d --- /dev/null +++ b/bibtexbrowser-cli.php @@ -0,0 +1,10 @@ + diff --git a/bibtexbrowser-documentation.wiki b/bibtexbrowser-documentation.wiki index 0ec3ff6..5e2116c 100755 --- a/bibtexbrowser-documentation.wiki +++ b/bibtexbrowser-documentation.wiki @@ -505,6 +505,14 @@ You must add a ''@string'' block in the bibtex file, which contains the links. T } +====How to modify bibtex entries on the command line?==== + +Use ''bibtexbrowser-cli.php''. For instance: + +
+php bibtexbrowser-cli.php mybib.bib --id classical --set-title \"a new title\" --id with_abstract --set-title \"a new title\" --set-year 1990 ++ =====Related tools===== Old-fashioned: diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index 83e56fa..e5b2927 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -27,11 +27,14 @@ 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" + return $this->_createDB("@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,pages={1-2}}\n" - ); + ."@article{aKeyA,title={An Article},author={Foo Bar and Jane Doe},volume=5,journal=\"New Results\",year=2009,pages={1-2}}\n"); + } + + function _createDB($content) { + $test_data = fopen('php://memory','x+'); + fwrite($test_data, $content); fseek($test_data,0); $btb = new BibDataBase(); $btb->update_internal("inline", $test_data); @@ -487,6 +490,37 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals('Foo Acé', $authors[1]); $this->assertEquals('Monperrus, Martin', $authors[2]); } + + function test_identity() { + $btb = new BibDataBase(); + $btb->load('bibacid-utf8.bib'); + $nref = count($btb->bibdb); + $bibtex = $btb->toBibtex(); + // reparsing the new content + $btb2 = $this->_createDB($bibtex); + // there is the same number of entries + $this->assertEquals($nref, count($btb2->bibdb)); + $this->assertEquals($bibtex, $btb2->toBibtex()); + } + + function test_cli() { + $test_file="test_cli.bib"; + copy('bibacid-utf8.bib', $test_file); + system('php bibtexbrowser-cli.php '.$test_file." --id classical --set-title \"a new title\""); + $db = new BibDataBase(); + $db->load($test_file); + $this->assertEquals("a new title", $db->getEntryByKey('classical')->getField('title')); + + // multiple changes + system('php bibtexbrowser-cli.php '.$test_file." --id classical --set-title \"a new title\" --id with_abstract --set-title \"a new title\" --set-year 1990"); + $db = new BibDataBase(); + $db->load($test_file); + $this->assertEquals("a new title", $db->getEntryByKey('classical')->getField('title')); + $this->assertEquals("a new title", $db->getEntryByKey('with_abstract')->getField('title')); + $this->assertEquals("1990", $db->getEntryByKey('with_abstract')->getField('year')); + + unlink($test_file); + } } // end class diff --git a/bibtexbrowser.php b/bibtexbrowser.php index 33b3626..5b5e24b 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1905,6 +1905,12 @@ class BibEntry { } // enc class BibEntry +class RawBibEntry extends BibEntry { + function setField($name, $value) { + $this->fields[$name]=$value; + } +} + /** returns an HTML tag depending on BIBTEXBROWSER_LAYOUT e.g.