Esempio n. 1
0
 /**
  * Import data from a file
  *
  * @param   array  $file
  * @return  array
  */
 public function onImport($file, $scope = NULL, $scope_id = NULL)
 {
     //file type
     $active = 'bib';
     //get the file extension
     $extension = $file->getClientOriginalExtension();
     //make sure we have a .bib file
     if ($active != $extension) {
         return;
     }
     //include bibtex file
     include_once PATH_CORE . DS . 'components' . DS . 'com_citations' . DS . 'helpers' . DS . 'BibTex.php';
     //create bibtex object
     $bibtex = new Structures_BibTex();
     //feed bibtex lib the file
     $bibtex->loadFile($file->getPathname());
     //parse file
     $bibtex->parse();
     //get parsed citations
     $citations = $bibtex->data;
     //fix authors
     for ($i = 0; $i < count($citations); $i++) {
         $authors = array();
         $auths = isset($citations[$i]['author']) ? $citations[$i]['author'] : '';
         if ($auths != '') {
             foreach ($auths as $a) {
                 if (isset($a['jr']) && $a['jr'] != '') {
                     $authors[] = $a['last'] . ' ' . $a['jr'] . ', ' . $a['first'];
                 } else {
                     $authors[] = $a['last'] . ', ' . $a['first'];
                 }
             }
             $citations[$i]['author'] = implode('; ', $authors);
         }
         //end if
     }
     //array to hold final citataions
     $final = array();
     //check for duplicates
     for ($i = 0; $i < count($citations); $i++) {
         $duplicate = $this->checkDuplicateCitation($citations[$i], $scope, $scope_id);
         if ($duplicate) {
             $citations[$i]['duplicate'] = $duplicate;
             $final['attention'][] = $citations[$i];
         } else {
             $citations[$i]['duplicate'] = 0;
             $final['no_attention'][] = $citations[$i];
         }
     }
     return $final;
 }
 static function __readBibFile($full_filename)
 {
     require_once 'Modules/Bibliographic/lib/PEAR_BibTex_1.0.0RC5/Structures/BibTex.php';
     $bibtex_reader = new Structures_BibTex();
     //Loading and parsing the file example.bib
     $ret = $bibtex_reader->loadFile($full_filename);
     $bibtex_reader->setOption("extractAuthors", false);
     $bibtex_reader->parse();
     // Remove library-bug: if there is no cite, the library mixes up the key for the type and the first attribute.
     // It also shows an empty and therefore unwanted cite in the array.
     //
     // The cite is the text coming right after the type. Example:
     // @book {cite,
     // author = { "...."},
     foreach ($bibtex_reader->data as $key => $entry) {
         if (empty($entry['cite'])) {
             unset($bibtex_reader->data[$key]['cite']);
             foreach ($entry as $attr_key => $attribute) {
                 if (strpos($attr_key, '{') !== false) {
                     unset($bibtex_reader->data[$key][$attr_key]);
                     $attr_key_exploaded = explode('{', $attr_key);
                     $bibtex_reader->data[$key]['entryType'] = trim($attr_key_exploaded[0]);
                     $bibtex_reader->data[$key][trim($attr_key_exploaded[1])] = $attribute;
                 }
             }
         }
     }
     return $bibtex_reader->data;
 }
 /**
  * @param $full_filename
  *
  * @return array
  */
 public static function __readBibFile($full_filename)
 {
     self::__setCharsetToUtf8($full_filename);
     require_once 'Modules/Bibliographic/lib/PEAR_BibTex_1.0.0RC5/Structures/BibTex.php';
     $bibtex_reader = new Structures_BibTex();
     //Loading and parsing the file example.bib
     $bibtex_reader->loadFile($full_filename);
     //replace bibtex special chars with the real characters
     $bibtex_reader->content = self::__convertBibSpecialChars($bibtex_reader->content);
     $bibtex_reader->setOption('extractAuthor', false);
     $bibtex_reader->parse();
     // Remove library-bug: if there is no cite, the library mixes up the key for the type and the first attribute.
     // It also shows an empty and therefore unwanted cite in the array.
     //
     // The cite is the text coming right after the type. Example:
     // @book {cite,
     // author = { "...."},
     foreach ($bibtex_reader->data as $key => $entry) {
         if (empty($entry['cite'])) {
             unset($bibtex_reader->data[$key]['cite']);
             foreach ($entry as $attr_key => $attribute) {
                 if (strpos($attr_key, '{') !== false) {
                     unset($bibtex_reader->data[$key][$attr_key]);
                     $attr_key_exploaded = explode('{', $attr_key);
                     $bibtex_reader->data[$key]['entryType'] = trim($attr_key_exploaded[0]);
                     $bibtex_reader->data[$key][trim($attr_key_exploaded[1])] = $attribute;
                 }
             }
         }
         // formating the author to the following type of string
         // Smith, John / Comte, Gabriel / von Gunten Jr, Thomas
         foreach ($entry as $attr_key => $attribute) {
             if ($attr_key == 'author' && is_array($attribute)) {
                 $attribute_string = array();
                 foreach ($attribute as $author_key => $author) {
                     $lastname = array($author['von'], $author['last'], $author['jr']);
                     $attribute_string[$author_key] = implode(' ', array_filter($lastname));
                     if (!empty($author['first'])) {
                         $attribute_string[$author_key] .= ', ' . $author['first'];
                     }
                 }
                 $bibtex_reader->data[$key][$attr_key] = implode(' / ', $attribute_string);
             }
         }
     }
     return $bibtex_reader->data;
 }
<?php

error_reporting(E_ALL);
require_once 'PEAR.php';
require_once 'Structures/BibTex.php';
$bibtex = new Structures_BibTex();
//Loading and parsing the file example.bib
$ret = $bibtex->loadFile('example.bib');
if (PEAR::isError($ret)) {
    print $ret->getMessage();
    die;
}
$bibtex->parse();
//Creating an entry
$addarray = array();
$addarray['entryType'] = 'Article';
$addarray['cite'] = 'art2';
$addarray['title'] = 'Titel2';
$addarray['author'][0]['first'] = 'John';
$addarray['author'][0]['last'] = 'Doe';
$addarray['author'][1]['first'] = 'Jane';
$addarray['author'][1]['last'] = 'Doe';
//Adding the entry
$bibtex->addEntry($addarray);
//Printing the result
echo "Converting This Array:\n\n";
echo "<pre>";
print_r($bibtex->data);
echo "\nInto this:\n\n";
echo $bibtex->bibTex();
echo "<hr />";
<?php

require_once 'BibTex.php';
$bibtex = new Structures_BibTex();
$bibtex->setOption('extractAuthors', false);
$ret = $bibtex->loadFile('publis/Gramfort.bib');
if (PEAR::isError($ret)) {
    die($ret->getMessage());
}
$bibtex->parse();
function clean_bibtex_string($str)
{
    $bad_chars = array('{', '}');
    return str_replace($bad_chars, "", $str);
}
function br2nl($str)
{
    return preg_replace('/<br(\\s*)>/i', '\\n', $str);
}
function short_name($name)
{
    $l = explode(',', $name);
    if (count($l) > 1) {
        $first = trim($l[1]);
        $last = $l[0];
    } else {
        $l = explode(' ', $name);
        $first = $l[0];
        $last = $l[1];
    }
    $sname = $first[0] . '. ' . $last;