/**
  * 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
  */
 function _postProcessing(&$ret)
 {
     // First post processing: Process accents, transform bibtex types
     foreach ($ret as $key => &$value) {
         switch ($key) {
             case "bibtex":
             case "cite":
                 break;
             case "entrytype":
                 if ($value == "conference") {
                     $value = "inproceedings";
                 }
                 break;
             default:
                 PaperciteBibTexEntries::process_accents($value);
         }
     }
     // Remove braces and handles capitalization
     foreach (array("title", "booktitle", "journal") as $f) {
         if (in_array($f, array_keys($ret))) {
             $ret[$f] = $this->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 PaperciteBibtexPages($matches[1], sizeof($matches) > 2 ? $matches[2] : "");
         }
     }
     //Handling the authors
     if (in_array('author', array_keys($ret))) {
         $ret['author'] = PaperciteBibTexEntries::_extractAuthors($ret['author']);
     }
     //Handling the editors
     if (in_array('editor', array_keys($ret))) {
         $ret['editor'] = PaperciteBibTexEntries::_extractAuthors($ret['editor']);
     }
 }
Example #2
0
 /**
  * Get the bibtex data from an URI
  */
 function getData($biburis, $options)
 {
     global $wpdb, $papercite_table_name, $papercite_table_name_url;
     $timeout = $options["timeout"];
     $processtitles = $options["process_titles"];
     $sslverify = $options["ssl_check"];
     // Loop over the different given URIs
     $bibFile = false;
     $array = explode(",", $biburis);
     $result = array();
     foreach ($array as $biburi) {
         // (1) Get the context
         $data = FALSE;
         $stringedFile = false;
         $custom_prefix = "custom://";
         // Handles custom:// by adding the post number
         if (papercite::startsWith($biburi, $custom_prefix)) {
             $stringedFile = true;
             $key = substr($biburi, strlen($custom_prefix));
             $biburi = "post://" . get_the_ID() . "/" . $key;
             $data = get_post_custom_values("papercite_{$key}");
             if ($data) {
                 $data = $data[0];
             }
         }
         if (!Papercite::array_get($this->cache, $biburi, false)) {
             if ($stringedFile) {
                 // do nothing
             } else {
                 if (preg_match('#^(ftp|http)s?://#', $biburi) == 1) {
                     $bibFile = $this->getCached($biburi, $timeout, $sslverify);
                 } else {
                     $biburi = preg_replace("#\\.bib\$#", "", $biburi);
                     $bibFile = $this->getDataFile("{$biburi}", "bib", "bib", "application/x-bibtex", $options);
                 }
             }
             if ($data === FALSE && !($bibFile && file_exists($bibFile[0]))) {
                 continue;
             }
             // Customize URIs depending on parsing options
             $biburi .= $processtitles ? "#pt=1" : "#pt=0";
             // (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()) {
                         $oldurlid = -1;
                         // We use entrytype as a timestamp
                         $row = $wpdb->get_row($wpdb->prepare("SELECT urlid, ts FROM {$papercite_table_name_url} WHERE url=%s", $biburi));
                         if ($row) {
                             $oldurlid = $row->urlid;
                             if ($row->ts >= $fileTS) {
                                 $result[$biburi] = $this->cache[$biburi] = array("__DB__", $row->urlid);
                                 continue;
                             }
                         }
                     }
                     $data = file_get_contents($bibFile[0]);
                 }
                 if (!empty($data)) {
                     switch ($this->options["bibtex_parser"]) {
                         case "pear":
                             // Pear parser
                             $this->_parser = new PaperciteStructures_BibTex(array('removeCurlyBraces' => true, 'extractAuthors' => true, 'processTitles' => $processtitles));
                             $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 PaperciteBibTexEntries();
                             $parser->processTitles($processtitles);
                             if (!$parser->parse($data)) {
                                 $this->cache[$biburi] = false;
                                 continue;
                             } else {
                                 $this->cache[$biburi] =& $parser->data;
                             }
                             break;
                     }
                     // Save to DB
                     if (!$stringedFile && $this->useDb()) {
                         // First delete everything
                         if ($oldurlid >= 0) {
                             $wpdb->query($wpdb->prepare("DELETE FROM {$papercite_table_name} WHERE urlid=%d", $oldurlid));
                             if ($code === FALSE) {
                                 break;
                             }
                         } else {
                             $code = $wpdb->query($wpdb->prepare("INSERT INTO {$papercite_table_name_url}(url, ts) VALUES (%s, 0)", $biburi));
                             if ($code === FALSE) {
                                 break;
                             }
                             $oldurlid = $wpdb->insert_id;
                         }
                         $code = true;
                         foreach ($this->cache[$biburi] as &$value) {
                             $year = is_numeric($value["year"]) ? intval($value["year"]) : -1;
                             $statement = $wpdb->prepare("REPLACE {$papercite_table_name}(urlid, bibtexid, entrytype, year, data) VALUES (%s,%s,%s,%s,%s)", $oldurlid, $value["cite"], $value["entrytype"], $year, maybe_serialize($value));
                             $code = $wpdb->query($statement);
                             if ($code === FALSE) {
                                 break;
                             }
                         }
                         if ($code !== FALSE) {
                             $statement = $wpdb->prepare("REPLACE INTO {$papercite_table_name_url}(url, urlid, ts) VALUES(%s,%s,%s)", $biburi, $oldurlid, $fileTS);
                             $code = $wpdb->query($statement);
                         }
                     }
                 }
             }
         }
         // end bibtex processing (not in cache)
         // Add to the list
         if (Papercite::array_get($this->cache, $biburi, false)) {
             $result[$biburi] = $this->cache[$biburi];
         }
     }
     // end loop over URIs
     return $result;
 }