From 178b85b3cc21191292f2434adc46fff3e61b86a0 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Fri, 22 May 2015 22:51:16 +0200 Subject: [PATCH] adds support for dynamic Bibtex data --- bibtexbrowser-documentation.wiki | 32 ++++++++++++++++++++-------- bibtexbrowser.php | 36 ++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/bibtexbrowser-documentation.wiki b/bibtexbrowser-documentation.wiki index e991cdc..aa8f0f3 100644 --- a/bibtexbrowser-documentation.wiki +++ b/bibtexbrowser-documentation.wiki @@ -286,7 +286,7 @@ This comment field can also be used to add acceptance rates and impact factors. ====How to localize bibtexbrowser?==== Add in ''bibtexbrowser.local.php'':
-<?
+<?php
 global $BIBTEXBROWSER_LANG;
 $BIBTEXBROWSER_LANG=array();
 $BIBTEXBROWSER_LANG['Refereed Conference Papers']="Conférences avec comité de lecture";
@@ -298,7 +298,7 @@ $BIBTEXBROWSER_LANG['Year']="Année";
 ====How to change the default frame?====
 Add in ''bibtexbrowser.local.php'':
 
-<?
+<?php
 // or any valid query: year=2010, author=Jane, etc.
 @define('BIBTEXBROWSER_DEFAULT_FRAME','all');
 ?>
@@ -306,7 +306,7 @@ Add in ''bibtexbrowser.local.php'':
 
 ====How to order a list of Bibtex entries?====
 
-<?
+<?php
 $_GET['library']=1;
 require_once('bibtexbrowser.php');
 $db = new BibDataBase();
@@ -340,13 +340,27 @@ define('BIBTEXBROWSER_ROBOTS_NOINDEX', true);
 
 ====How to load Bibtex from a dynamic string?====
 
-You can use the special ''php://memory'' feature as follows:
+You can use the special ''php://memory'' feature in a new dispatcher.
+Add in ''bibtexbrowser.local.php'':
+
+define("BIBTEXBROWSER_MAIN", 'DynamicDispatcher');
+
+ +and then in ''bibtexbrowser.after.php'':
-$test_data = fopen('php://memory','x+');
-fwrite($test_data, "@book{aKey,title={A Book},author={Jan Doe},publisher={Springer},year=2009}\n");
-fseek($test_data,0);
-$this->btb = new BibDataBase();
-$this->btb->update_internal("inline", $test_data);
+<?php
+class DynamicDispatcher extends Dispatcher {
+  function getDB() {
+    $data = fopen('php://memory','x+');
+    $dynamic_string = "@book{aKey,title={A Book},author={Jan Doe},publisher={Springer},year=2009}\n";
+    fwrite($data, $dynamic_string);
+    fseek($data,0);
+    $db = new BibDataBase();
+    $db->update_internal("inline", $data);
+    return $db;
+  }
+}
+?>
 
diff --git a/bibtexbrowser.php b/bibtexbrowser.php index e39e06a..1df6804 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -2233,13 +2233,12 @@ usage: $_GET['all']=1; include( 'bibtexbrowser.php' ); setDB(); - new IndependentYearMenu(); + new IndependentYearMenu($_GET[Q_DB]);
*/ class IndependentYearMenu { - function IndependentYearMenu() { - if (!isset($_GET[Q_DB])) {die('Did you forget to call setDB() before instantiating this class?');} - $yearIndex = $_GET[Q_DB]->yearIndex(); + function IndependentYearMenu($db) { + $yearIndex = $db->yearIndex(); echo '
Year: '; $formatedYearIndex = array(); $formatedYearIndex[] = ''.*')).'>All'; @@ -2329,7 +2328,7 @@ class MenuManager { } /** sets the database that is used to create the menu */ - function setDB(&$db) { + function setDB($db) { $this->db =$db; return $this; } @@ -3947,8 +3946,22 @@ class Dispatcher { */ var $wrapper = BIBTEXBROWSER_DEFAULT_TEMPLATE; + /** The BibDataBase object */ + var $db = null; + function Dispatcher() {} + /** returns the underlying BibDataBase object */ + function getDB() { + // by default set it from $_GET[Q_FILE] + // first we set the database (load from disk or parse the bibtex file) + if ($this->db == null) { + list($db, $parsed, $updated, $saved) = _zetDB($_GET[Q_FILE]); + $this->db = $db; + } + return $this->db; + } + function main() { // are we in test mode, or libray mode // then this file is just a library @@ -3961,9 +3974,6 @@ class Dispatcher { if (!isset($_GET[Q_FILE])) { die('$_GET[\''.Q_FILE.'\'] is not set!'); } - // first we set the database (load from disk or parse the bibtex file) - if (!isset($_GET[Q_DB]) || !$_GET[Q_DB]->is_already_loaded($_GET[Q_FILE])) { setDB(); } - // is the publication list included in another page? // strtr is used for Windows where __FILE__ contains C:\toto and SCRIPT_FILENAME contains C:/toto (bug reported by Marco) // realpath is required if the path contains sym-linked directories (bug found by Mark Hereld) @@ -3988,7 +3998,7 @@ class Dispatcher { unset($this->query[Q_ALL]); } - $selectedEntries = $_GET[Q_DB]->multisearch($this->query); + $selectedEntries = $this->getDB()->multisearch($this->query); if (count($selectedEntries)==0) { $this->displayer = 'NotFoundDisplay'; @@ -4080,7 +4090,7 @@ class Dispatcher { function year() { // we may want the latest if ($_GET[Q_YEAR]=='latest') { - $years = $_GET[Q_DB]->yearIndex(); + $years = $this->getDB()->yearIndex(); $_GET[Q_YEAR]=array_shift($years); } $this->query[Q_YEAR]=$_GET[Q_YEAR]; @@ -4165,7 +4175,7 @@ class Dispatcher { function menu() { $menu = createMenuManager(); - $menu->setDB($_GET[Q_DB]); + $menu->setDB($this->getDB()); $fun = $this->wrapper; $fun($menu); return 'END_DISPATCH'; @@ -4195,8 +4205,8 @@ class Dispatcher { function key() { $entries = array(); // case 1: this is a single key - if ($_GET[Q_DB]->contains($_GET[Q_KEY])) { - $entries[] = $_GET[Q_DB]->getEntryByKey($_GET[Q_KEY]); + if ($this->getDB()->contains($_GET[Q_KEY])) { + $entries[] = $this->getDB()->getEntryByKey($_GET[Q_KEY]); if (isset($_GET['astext'])) { $bibdisplay = new BibtexDisplay(); $bibdisplay->setEntries($entries);