Browse Source

feat: add function addKeyword

pull/134/head
Martin Monperrus 9 months ago
parent
commit
aa886ffb39
No known key found for this signature in database GPG Key ID: 7D398AD45AEEEC93
  1. 24
      BibtexbrowserTest.php
  2. 7
      bibtexbrowser.php

24
BibtexbrowserTest.php

@ -970,6 +970,30 @@ class BibtexbrowserTest extends PHPUnit_Framework_TestCase {
}
function test_keyword() {
bibtexbrowser_configure('BIBLIOGRAPHYSTYLE','DefaultBibliographyStyle');
$bibtex = "@misc{doe2004,title={foo bar title}, publisher={publisher}, howpublished={howpublished}} @misc{doe2,title={baz}, keywords={bar}, howpublished={howpublished}}";
$test_data = fopen('php://memory','x+');
fwrite($test_data, $bibtex);
fseek($test_data,0);
$db = new BibDataBase();
$db->update_internal("inline", $test_data);
// print_r($db);
// no keyword
$entry = array_values($db->bibdb)[0];
$entry->addKeyword("foo");
$this->assertEquals('foo', $entry->getField("keywords"));
// already one keyword
$entry = array_values($db->bibdb)[1];
$entry->addKeyword("foo");
$this->assertEquals('bar;foo', $entry->getField("keywords"));
}
} // end class
@copy('bibtexbrowser.local.php.bak','bibtexbrowser.local.php');

7
bibtexbrowser.php

@ -1738,7 +1738,12 @@ class BibEntry {
function getKeywords() {
return preg_split('/[,;\\/]/', $this->getField("keywords"));
}
function addKeyword($new_keyword) {
$r = $this->getField('keywords');
if ($r == null || strlen($r) == 0) return $this->setField('keywords', $new_keyword);
return $this->setField('keywords', $r.";".$new_keyword);
}
/** Returns the value of the given field? */
function getField($name) {
// 2010-06-07: profiling showed that this is very costly

Loading…
Cancel
Save