From 3abf3f0b9b60e020cc4cc76cae4d051f25d07802 Mon Sep 17 00:00:00 2001 From: Matthieu Guillaumin Date: Wed, 27 Feb 2013 11:04:29 +0100 Subject: [PATCH] introduced Q_ASSOCKEYS and improves Q_KEYS and Q_BIBLIOGRAPHY --- bibtexbrowser.bibliography.php | 14 +++++----- bibtexbrowser.local.php | 14 +++------- bibtexbrowser.php | 47 +++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/bibtexbrowser.bibliography.php b/bibtexbrowser.bibliography.php index 97902e2..61040e6 100644 --- a/bibtexbrowser.bibliography.php +++ b/bibtexbrowser.bibliography.php @@ -1,6 +1,7 @@ , one can cite +/* Example: As shown in , one can use bibtex within HTML/PHP. */ function cite() { global $citations; @@ -38,19 +39,16 @@ function cite() { // prepare bibtexbrowser query function make_bibtexbrowser_bibliography_keys() { global $citations; - $keylist = array(); - foreach ( $citation as $entry => $ref ) { - $keylist["$ref"] = $entry; // make sure keys are strings - } - return json_encode($keylist) ; + return json_encode(array_flip($citations)) ; } function make_bibliography() { global $_GET; $_GET = array(); $_GET['bib']='mg.bib'; - $_GET['bibliography']=1; + $_GET['bibliography']=1; // sets assoc_keys $_GET['keys']=make_bibtexbrowser_bibliography_keys(); + //print_r($_GET); include( 'bibtexbrowser.php' ); } diff --git a/bibtexbrowser.local.php b/bibtexbrowser.local.php index 5e017a7..1f68dbf 100644 --- a/bibtexbrowser.local.php +++ b/bibtexbrowser.local.php @@ -119,38 +119,30 @@ function MGBibliographyStyle(&$bibentry) { $result .= '
'; return $result; -} +} // end style function /** Class to display a bibliography of a page. */ class BibliographyDisplay { - function setDB(&$bibdatabase) { - $this->setEntries($bibdatabase->bibdb); - } + function setDB(&$bibdatabase) { $this->setEntries($bibdatabase->bibdb); } /** sets the entries to be shown */ - function setEntries(&$entries) { - $this->entries = $entries; - } + function setEntries(&$entries) { $this->entries = $entries; } function setTitle($title) { $this->title = $title; return $this; } function getTitle() { return @$this->title ; } /** Displays a set of bibtex entries in an HTML table */ function display() { - ksort($this->entries); // sort the keys, not the values - layoutHeaderHTML(); foreach ($this->entries as $ref => $bib) { $bib->setIndex($ref); $bib->toHTML(); } // end foreach layoutFooterHTML(); - } // end function } // end class - ?> diff --git a/bibtexbrowser.php b/bibtexbrowser.php index e5d8c14..2521ed5 100644 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -100,7 +100,8 @@ define('BIBTEXBROWSER','v20130206'); @define('Q_ALL', 'all'); @define('Q_ENTRY', 'entry'); @define('Q_KEY', 'key'); -@define('Q_KEYS', 'keys'); // select entries using a JSON array of bibtex keys (if an associative array is used, the keys of this array are used as item abbrv) +@define('Q_KEYS', 'keys'); // select entries using a JSON array of bibtex keys +@define('Q_ASSOCKEYS', 'assoc_keys'); // consider Q_KEYS as an associative array, and use the keys of Q_KEYS as item abbrv @define('Q_SEARCH', 'search'); @define('Q_EXCLUDE', 'exclude'); @define('Q_RESULT', 'result'); @@ -3000,24 +3001,21 @@ class BibDataBase { if (isset($query[Q_ALL])) return array_values($this->bibdb); if (array_key_exists( Q_KEYS, $query )) { - $keylist = array(); - $is_assoc = (bool)count(array_filter(array_keys($query[Q_KEYS]), 'is_string')); - foreach ($query[Q_KEYS] as $ref => $key) { - if ($is_assoc) { - $keylist[$ref] = $key; - } else { - $keylist[] = $key; - } - } + $keylist = (array) $query[Q_KEYS]; + $reflist = array_flip($keylist); + $is_assoc = array_key_exists( Q_ASSOCKEYS, $query ); //array_values($query[Q_KEYS]) !== $query[Q_KEYS]; + //if ($is_assoc) echo "Assoc"; + //print_r($keylist); } else { $is_assoc = false; } + unset($query[Q_ASSOCKEYS]); // not used for filtering the bibtex entries $result = array(); foreach ($this->bibdb as $bib) { $entryisselected = true; - $akey = -1; + $akey = ''; foreach ($query as $field => $fragment) { $field = strtolower($field); if ($field==Q_SEARCH) { @@ -3046,12 +3044,11 @@ class BibDataBase { } else if ($field==Q_KEYS) { if ( ! in_array( $bib->getKey(), $keylist ) ) { - if ($is_assoc) { - $akey = array_search( $bib->getKey(), $keylist ); - } + //echo $bib->getKey() . " not in list
\n"; $entryisselected = false; break; - } + } + //echo $bib->getKey() . " in list
\n"; } else { if (!$bib->hasPhrase($fragment, $field)) { @@ -3062,13 +3059,16 @@ class BibDataBase { } if ($entryisselected) { - if ($akey<0) { - $akey = count($result)+1; + if ( $is_assoc ) { + $result[$reflist[$bib->getKey()]] = $bib; + } else { + $result[] = $bib; } - $result[$akey] = $bib; - } + } else { + //echo "entry ".$bib->getKey()." not selected\n"; + } } - + //foreach ($result as $ref=>$bib) {echo $ref." => ".$bib->getKey()." ";} return $result; } } // end class @@ -3739,7 +3739,8 @@ class Dispatcher { } function bibliography() { - $this->displayer='BibliographyDisplay'; + $this->displayer='BibliographyDisplay'; + $this->query[Q_ASSOCKEYS]=1; } function layout() { $this->query[LAYOUT]=$_GET[LAYOUT]; } @@ -3752,6 +3753,10 @@ class Dispatcher { $this->query[Q_KEYS]=$_GET[Q_KEYS]; } + function assoc_keys() { + $this->query[Q_ASSOCKEYS]=$_GET[Q_ASSOCKEYS]; + } + /** is used to remotely analyzed a situation */ function diagnosis() { header('Content-type: text/plain');