feat: add function addKeyword

This commit is contained in:
Martin Monperrus
2024-10-04 10:58:04 +02:00
parent 4ed89551b5
commit aa886ffb39
2 changed files with 30 additions and 1 deletions
+24
View File
@@ -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 } // end class
@copy('bibtexbrowser.local.php.bak','bibtexbrowser.local.php'); @copy('bibtexbrowser.local.php.bak','bibtexbrowser.local.php');
+5
View File
@@ -1738,6 +1738,11 @@ class BibEntry {
function getKeywords() { function getKeywords() {
return preg_split('/[,;\\/]/', $this->getField("keywords")); 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? */ /** Returns the value of the given field? */
function getField($name) { function getField($name) {