Example #1
0
  /**
   * Get the bibtex data from an URI
   */
  function &getData($biburis, $timeout = 3600) {
    // Loop over the different given URIs
    $array = explode(",", $biburis);
    $result = array();
    foreach($array as $biburi) {

      // (1) Get the context
      if (!$this->cache[$biburi]) {
	if (strpos($biburi, "http://") === 0) 
	  $bibFile = $this->getCached($biburi, $timeout);
	else {
	  $bibFile = $this->getDataFile("bib/$biburi");
	}
      
	if (!$bibFile || !file_exists($bibFile[0]))
	  continue;	

	$bibFile = $bibFile[0];

	// (2) Parse the BibTeX
	if (file_exists($bibFile)) {
	  $data = file_get_contents($bibFile);

	  if (!empty($data)) {
	    switch($this->options["bibtex_parser"]) {
	    case "papercite":
	      $parser = new BibTexEntries();
	      if (!$parser->parse($data)) {
		$this->cache[$biburi] = false;
		continue;
	      } else {
		$this->cache[$biburi] = &$parser->data;
	      }
	      break;
	    default:
	      $this->_parser = new Structures_BibTex(array('removeCurlyBraces' => true, 'extractAuthors' => true));
	      $this->_parser->loadString($data);
	      $stat = $this->_parser->parse();
	      
	      if ( !$stat )  return  $this->cache[$biburi] = false;
	      $this->cache[$biburi] = &$this->_parser->data;
	    }
	

	    // --- Add custom fields
	    foreach($this->cache[$biburi] as &$entry) {
	      $this->checkFiles($entry, array(array("pdf", "pdf")));
	    }
	  }
	}
      }

      // Add to the list
      if ($this->cache[$biburi]) 
	$result[] = $this->cache[$biburi];
    }
    
    if (sizeof($result) == 0) return false;
    return $result;
 
  }
Example #2
0
 /**
  * Extracting the data of one entry
  *
  * @access private
  * @param string $entry The entry
  * @return array The representation of the entry or false if there is a problem
  */
 static function _postProcessing(&$ret)
 {
     // Process accents
     foreach ($ret as $key => &$value) {
         if ($key != "entrytype" && $key != "bibtex" && $key != "cite") {
             BibTexEntries::process_accents($value);
         }
     }
     // Remove braces and handles capitalization
     foreach (array("title", "booktitle") as $f) {
         if (in_array($f, array_keys($ret))) {
             $ret[$f] = BibTexEntries::formatTitle($ret[$f]);
         }
     }
     // Handling pages
     if (in_array('pages', array_keys($ret))) {
         $matches = array();
         if (preg_match("/^\\s*(\\d+)(?:\\s*--?\\s*(\\d+))?\\s*\$/", $ret['pages'], $matches)) {
             $ret['pages'] = new BibtexPages($matches[1], $matches[2]);
         }
     }
     //Handling the authors
     if (in_array('author', array_keys($ret))) {
         $ret['author'] = BibTexEntries::_extractAuthors($ret['author']);
     }
     //Handling the editors
     if (in_array('editor', array_keys($ret))) {
         $ret['editor'] = BibTexEntries::_extractAuthors($ret['editor']);
     }
 }
Example #3
0
 /**
  * Get the bibtex data from an URI
  */
 function &getData($biburis, $timeout = 3600)
 {
     global $wpdb, $papercite_table_name;
     // Loop over the different given URIs
     $array = explode(",", $biburis);
     $result = array();
     foreach ($array as $biburi) {
         // (1) Get the context
         $data = FALSE;
         if (!$this->cache[$biburi]) {
             if (strpos($biburi, "custom://") === 0) {
                 $data = get_post_custom_values("papercite_" . substr($biburi, 9));
                 if ($data) {
                     $data = $data[0];
                 }
             } else {
                 if (strpos($biburi, "http://") === 0) {
                     $bibFile = $this->getCached($biburi, $timeout);
                 } else {
                     $bibFile = $this->getDataFile("bib/{$biburi}");
                 }
             }
             if ($data === FALSE && !($bibFile && file_exists($bibFile[0]))) {
                 continue;
             }
             // (2) Parse the BibTeX
             if ($data || file_exists($bibFile[0])) {
                 if (!$data) {
                     $fileTS = filemtime($bibFile[0]);
                     // Check if we don't have the data in cache
                     if ($this->useDb()) {
                         // We use entrytype as a timestamp
                         $dbTS = intval($wpdb->get_var($wpdb->prepare("SELECT entrytype FROM {$papercite_table_name} WHERE url=%s and bibtexid=''", "ts://" . $biburi)));
                         if ($dbTS >= $fileTS) {
                             $result[$biburi] = $this->cache[$biburi] = "__DB__";
                             continue;
                         }
                     }
                     $data = file_get_contents($bibFile[0]);
                 }
                 if (!empty($data)) {
                     switch ($this->options["bibtex_parser"]) {
                         case "pear":
                             // Pear parser
                             $this->_parser = new Structures_BibTex(array('removeCurlyBraces' => true, 'extractAuthors' => true));
                             $this->_parser->loadString($data);
                             $stat = $this->_parser->parse();
                             if (!$stat) {
                                 return $this->cache[$biburi] = false;
                             }
                             $this->cache[$biburi] =& $this->_parser->data;
                             break;
                         default:
                             // OSBiB parser
                             $parser = new BibTexEntries();
                             if (!$parser->parse($data)) {
                                 $this->cache[$biburi] = false;
                                 continue;
                             } else {
                                 $this->cache[$biburi] =& $parser->data;
                             }
                             break;
                     }
                     // --- Add custom fields
                     foreach ($this->cache[$biburi] as &$entry) {
                         $this->checkFiles($entry, array(array("pdf", "pdf")));
                     }
                     // Save to DB
                     if (!$stringedFile && $this->useDb()) {
                         // First delete everything
                         $wpdb->query($wpdb->prepare("DELETE FROM {$papercite_table_name} WHERE url=%s", $biburi));
                         $code = true;
                         foreach ($this->cache[$biburi] as &$value) {
                             $statement = $wpdb->prepare("INSERT INTO {$papercite_table_name}(url, bibtexid, entrytype, year, data) VALUES (%s,%s,%s,%s,%s)", $biburi, $value["cite"], $value["entrytype"], $value["year"], maybe_serialize($value));
                             $code = $wpdb->query($statement);
                             if ($code === FALSE) {
                                 break;
                             }
                         }
                         if ($code !== FALSE) {
                             $statement = $wpdb->prepare("INSERT INTO {$papercite_table_name}(url, bibtexid, entrytype) VALUES(%s,%s,%s)", "ts://" . $biburi, "", $fileTS);
                             $code = $wpdb->query($statement);
                         }
                     }
                 }
             }
         }
         // Add to the list
         if ($this->cache[$biburi]) {
             $result[$biburi] = $this->cache[$biburi];
         }
     }
     if (sizeof($result) == 0) {
         return false;
     }
     return $result;
 }
Example #4
0
  /**
   * Extracting the data of one content
   *
   * @access private
   * @param string $entry The entry
   * @return array The representation of the entry or false if there is a problem
   */
  static function _postProcessing(&$ret) {
    // Process accents
    foreach($ret as $key => &$value) 
      if ($key != "bibtex" && $key != "cite")
	BibTexEntries::process_accents($value);
    
    // Handling pages
    if (in_array('pages', array_keys($ret))) {
      $matches = array();
      if (preg_match("/^\s*(\d+)(?:\s*--?\s*(\d+))?\s*$/", $ret['pages'], $matches)) {
	$ret['pages'] = new BibtexPages($matches[1], $matches[2]);
      }
    }

    //Handling the authors
    if (in_array('author', array_keys($ret))) {
      $ret['author'] = BibTexEntries::_extractAuthors($ret['author']);
    }

    //Handling the editors
    if (in_array('editor', array_keys($ret))) {
      $ret['editor'] = BibTexEntries::_extractAuthors($ret['editor']);
    }
    
  }