Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added filtering for firstnames #106

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion bib2tpl/bibtex_converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class BibtexConverter
* Global variables that can be accessed in the template
*/
var $_globals;

private $count=0;

/**
* Constructor.
Expand Down Expand Up @@ -137,6 +139,7 @@ function BibtexConverter($options=array(), &$template, &$entry_template)

'highlight' => ''
);


// Overwrite specified options
foreach ( $this->_options as $key => $value )
Expand Down Expand Up @@ -510,7 +513,7 @@ function _callback($match) {
$groupPosition = 0;
foreach($this->_group as &$entry) {
if ($limit > 0 && $limit <= $this->count) {
// Stop if we reached the limit
// Stop if we reached the limit
break;
}
$groupPosition++;
Expand All @@ -520,6 +523,7 @@ function _callback($match) {
$this->_entry = $entry;
$entries .= preg_replace_callback(BibtexConverter::$mainPattern, array($this, "_callback"), $this->full_entry_tpl);
$this->_globals["positionInList"]++;
$this->count++;
}
unset($this->_entry);
return $entries . $match[2];
Expand Down
142 changes: 115 additions & 27 deletions papercite.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,49 @@ function matches(&$entry) {
$eAuthors = &$entry["author"];
foreach($this->filters as &$filter) {
foreach($filter->creators as $author) {
if(!empty($author['firstname'])||!empty($author['initials']))
{
if(!empty($author['firstname']))
{
$FirstNameInitial=$author['firstname']{0};
}
else
{
$FirstNameInitial=$author['initials'];
}
$IgnoreFirstname=false;
}
else
{
$IgnoreFirstname=true;
}
$ok = false;
foreach($eAuthors->creators as $eAuthor) {
if ($author["surname"] === $eAuthor["surname"]) {
$ok = true;
break;
}
}

if ($eAuthors&&is_array($eAuthors->creators)){
foreach($eAuthors->creators as $eAuthor)
{
if ($author["surname"] === $eAuthor["surname"])
{
if($IgnoreFirstname)
{
$ok = true;break;
}
else
{// If filter contains full firstname und entry has full name, full name must match!
if(!empty($eAuthor['firstname'])&&!empty($author['firstname']))
{
if($author['firstname'] === $eAuthor["firstname"]){$ok = true;break;}
}
else //otherwise at least initials must match
{
if(!empty($eAuthor["firstname"])&&($FirstNameInitial===$eAuthor["firstname"]{0})){$ok = true;break;}
if(!empty($eAuthor["initials"])&&($FirstNameInitial===$eAuthor["initials"])){$ok = true;break;}
if(empty($eAuthor["initials"]) && empty($eAuthor["firstname"])){$ok = true;break;}//Nothing to match, return ok
}
}
}
}//endforeach
}//end if -forearch
// Author was not found in publication
if (!$ok) break;
}
Expand Down Expand Up @@ -174,15 +210,15 @@ function getCached($url, $timeout = 3600, $sslverify = false) {

// Names of the options that can be set
static $option_names = array("format", "timeout", "file", "bibshow_template", "bibtex_template", "bibtex_parser",
"use_db", "auto_bibshow", "use_media", "use_files", "skip_for_post_lists", "process_titles", "checked_files", "show_links", "highlight", "ssl_check");
"use_db", "auto_bibshow", "use_media", "use_files", "skip_for_post_lists", "process_titles", "checked_files", "show_links", "highlight", "ssl_check", "limit");

// Default value of options
static $default_options =
array("format" => "ieee", "group" => "none", "order" => "desc", "sort" => "none", "key_format" => "numeric",
"bibtex_template" => "default-bibtex", "bibshow_template" => "default-bibshow", "bibtex_parser" => "osbib", "use_db" => false,
"auto_bibshow" => false, "use_media" => false, "use_files" => true, "skip_for_post_lists" => false, "group_order" => "", "timeout" => 3600, "process_titles" => true,
"checked_files" => array(array("pdf", "pdf", "", "pdf", "application/pdf")), "show_links" => true, "highlight" => "",
"ssl_check" => false);
"ssl_check" => false,"limit"=>0);
/**
* Init is called before the first callback
*/
Expand Down Expand Up @@ -422,15 +458,18 @@ function getData($biburis, $options) {
if ($data || file_exists($bibFile[0])) {
if (!$data) {
$fileTS = filemtime($bibFile[0]);
// Check if we don't have the data in cache

// 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));
$statement=$wpdb->prepare("SELECT urlid, ts FROM $papercite_table_name_url WHERE url=%s",$biburi);
$row = $wpdb->get_row($statement);
if ($row) {
$oldurlid = $row->urlid;
if ($row->ts >= $fileTS) {
echo "<!-- using cache-->";
$result[$biburi] = $this->cache[$biburi] = array("__DB__", $row->urlid);
continue;
}
Expand All @@ -440,6 +479,7 @@ function getData($biburis, $options) {
$data = file_get_contents($bibFile[0]);
}


if (!empty($data)) {
switch($this->options["bibtex_parser"]) {
case "pear": // Pear parser
Expand All @@ -463,11 +503,11 @@ function getData($biburis, $options) {
break;
}


// Save to DB
if (!$stringedFile && $this->useDb()) {
// First delete everything
if ($oldurlid >= 0) {
//echo "delete old database";
$wpdb->query($wpdb->prepare("DELETE FROM $papercite_table_name WHERE urlid=%d", $oldurlid));
if ($code === FALSE)
break;
Expand All @@ -480,11 +520,18 @@ function getData($biburis, $options) {

$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));
$dateexplode= explode('-',$value["date"]);
if(empty($dateexplode[0])){$dateexplode[1]=$value["year"];}
if(empty($dateexplode[1])){$dateexplode[1]=1;}//default month
if(empty($dateexplode[2])){$dateexplode[2]=1;}//default day
$date=date("Y-m-d",mktime(0, 0, 0,$dateexplode[1] , $dateexplode[2], $dateexplode[0]));
$statement = $wpdb->prepare("REPLACE $papercite_table_name(urlid, bibtexid, entrytype, year, data) VALUES (%s,%s,%s,%s,%s)", $oldurlid, $value["cite"], $value["entrytype"], $date,maybe_serialize($value));
$code = $wpdb->query($statement);
//echo mb_detect_encoding(maybe_serialize($value));
// echo "<p>".$statement."<br>.".$code.".</p>";
//$wpdb->print_error();
if ($code === FALSE) {
echo "Failed entry:<p>".$statement."<br>.".$code.".</p>";
break;
}
}
Expand Down Expand Up @@ -535,7 +582,7 @@ static function getEntriesByKey(&$entries, &$keys) {
$unfound = array_diff($keys, $found);
if ($dbs && sizeof($unfound) > 0) {
$dbs = papercite::getDbCond($dbs);
foreach($unfound as &$v) $v = '"' . $wpdb->escape($v) . '"';
foreach($unfound as &$v) $v = '"' . esc_sql($v) . '"';
$keylist = implode(",", $unfound);
$st = "SELECT data FROM $papercite_table_name WHERE $dbs and bibtexid in ($keylist)";
$val = $wpdb->get_col($st);
Expand Down Expand Up @@ -593,9 +640,9 @@ function _process(&$matches) {

// Get all the options pairs and store them
// in the $options array

$options_pairs = array();
preg_match_all("/\s*([\w-:_]+)=(?:([^\"]\S*)|\"([^\"]+)\")(?:\s+|$)/", sizeof($matches) > 2 ? $matches[2] : "", $options_pairs, PREG_SET_ORDER);

// print "<pre>";
// print htmlentities(print_r($options_pairs,true));
// print "</pre>";
Expand All @@ -612,7 +659,7 @@ function _process(&$matches) {

foreach($options_pairs as $x) {
$value = $x[2] . (sizeof($x) > 3 ? $x[3] : "");

if ($x[1] == "template")
{
// Special case of template: should overwrite the corresponding command template
Expand All @@ -622,7 +669,7 @@ function _process(&$matches) {
{
$options["filters"][substr($x[1],7)] = $value;
}
else
else
{
$options[$x[1]] = $value;
}
Expand Down Expand Up @@ -772,9 +819,11 @@ function getEntries($options) {
// Based on the entry types
$allow = Papercite::array_get($options, "allow", "");
$deny = Papercite::array_get($options, "deny", "");
$ignore = Papercite::array_get($options, "ignore", "");
$allow = $allow ? preg_split("-,-",$allow) : Array();
$deny = $deny ? preg_split("-,-", $deny) : Array();

$ignore = $ignore ? preg_split("-,-",$ignore) : Array();

$author_matcher = new PaperciteAuthorMatcher(Papercite::array_get($options, "author", ""));

$result = array();
Expand All @@ -785,7 +834,9 @@ function getEntries($options) {
else
foreach($outer as &$entry) {
$t = &$entry["entrytype"];
if ((sizeof($allow)==0 || in_array($t, $allow)) && (sizeof($deny)==0 || !in_array($t, $deny)) && $author_matcher->matches($entry) && Papercite::userFiltersMatch($options["filters"], $entry)) {
$k = &$entry["cite"];

if ((sizeof($allow)==0 || in_array($t, $allow)) && (sizeof($deny)==0 || !in_array($t, $deny)) && $author_matcher->matches($entry) && Papercite::userFiltersMatch($options["filters"], $entry)&&!in_array($k, $ignore)) {
$result[] = $entry;
}
}
Expand All @@ -795,15 +846,16 @@ function getEntries($options) {
// --- Add entries from database
if ($dbs) {
$dbCond = $this->getDbCond($dbs);

// Handles year and entry type by direct SQL
foreach($allow as &$v) $v = '"' . $wpdb->escape($v) . '"';
foreach($allow as &$v) $v = '"' . esc_sql($v) . '"';
$allowCond = $allow ? "and entrytype in (" . implode(",",$allow) . ")" : "";
foreach($deny as &$v) $v = '"' . $wpdb->escape($v) . '"';
foreach($deny as &$v) $v = '"' . esc_sql($v) . '"';
$denyCond = $deny ? "and entrytype not in (" . implode(",",$deny) . ")" : "";

foreach($ignore as &$v) $v = '"' . esc_sql($v) . '"';
$ignoreCond = $ignore ? "and bibtexid not in (" . implode(",",$ignore) . ")" : "";
// Retrieve and filter further
$st = "SELECT data FROM $papercite_table_name WHERE $dbCond $denyCond $allowCond";
$st = "SELECT data FROM $papercite_table_name WHERE $dbCond $denyCond $allowCond $ignoreCond";
$rows = $wpdb->get_col($st);
if ($rows) foreach($rows as $data) {
$entry = maybe_unserialize($data);
Expand Down Expand Up @@ -845,7 +897,7 @@ static function getDbCond(&$dbArray) {

$dbs = array();
foreach($dbArray as &$db)
$dbs[] = "\"" . $wpdb->escape($db) . "\"";
$dbs[] = "\"" . esc_sql($db) . "\"";
$dbs = implode(",", $dbs);
if ($dbs) $dbs = "urlid in ($dbs)";

Expand Down Expand Up @@ -1063,6 +1115,42 @@ function papercite_init() {
}

// --- Callback function ----
function papercite_staff($myContent) {

// Init
$papercite = &$GLOBALS["papercite"];

// Fixes issue #39 (maintenance mode support)
if(!is_object($papercite))
return $myContent;

$papercite->init();

// Database support if needed
if ($papercite->options["use_db"]) {
require_once(dirname(__FILE__) . "/papercite_db.php");
}

// (0) Skip processing on this page?
if ($papercite->options['skip_for_post_lists'] && !is_single() ) {//&& !is_page()

return preg_replace("/\[\s*((?:\/)bibshow|bibshow|bibcite|bibtex)(?:\s+([^[]+))?]/", '', $myContent);
}
// (1) First phase - handles everything but bibcite keys
$text = preg_replace_callback("/\[\s*((?:\/)bibshow|bibshow|bibcite|bibtex|bibfilter)(?:\s+([^[]+))?]/",
array($papercite, "process"), $myContent);

// (2) Handles missing bibshow tags
while (sizeof($papercite->bibshows) > 0)
$text .= $papercite->end_bibshow();


// (3) Handles custom keys in bibshow and return
$text = str_replace($papercite->keys, $papercite->keyValues, $text);
//echo $text;
return $text;
}

function &papercite_cb($myContent) {
// Init
$papercite = &$GLOBALS["papercite"];
Expand Down
8 changes: 4 additions & 4 deletions papercite_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ function papercite_install($force = false) {

$sql = "CREATE TABLE $papercite_table_name (
urlid INT UNSIGNED NOT NULL,
bibtexid VARCHAR(200) CHARSET ASCII NOT NULL,
entrytype VARCHAR(80) CHARSET ASCII NOT NULL,
year SMALLINT,
bibtexid VARCHAR(200) CHARSET UTF8 NOT NULL,
entrytype VARCHAR(80) CHARSET UTF8 NOT NULL,
year DATE,
data TEXT NOT NULL,
PRIMARY KEY id (urlid, bibtexid),
INDEX year (year),
Expand Down Expand Up @@ -105,4 +105,4 @@ function papercite_install($force = false) {



?>
?>
4 changes: 2 additions & 2 deletions papercite_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ function papercite_use_db() {
print "<div class='papercite_info'>" . $wpdb->get_var("SELECT count(*) FROM $papercite_table_name") . " entries in the database</div>";
print "<div class='papercite_info'>Cached bibtex files: " .
implode(", ", $wpdb->get_col("SELECT url from $papercite_table_name_url")) . "</div>";
print "<div style='margin: 10px 0 10px'><span class='papercite_link' id='papercite_clear_db'>Clear cache</a></div>";
print "<div style='margin: 10px 0 10px'><span class='papercite_link' id='papercite_clear_db'>Click here to Clear cache</a></div>";
}

echo "<input type='radio' id='papercite_use_db' " . ($option ? " checked='checked' " : "") . " value='yes' name='papercite_options[use_db]' /> Yes ";
echo "<h3>Use DB?</h3> <input type='radio' id='papercite_use_db' " . ($option ? " checked='checked' " : "") . " value='yes' name='papercite_options[use_db]' /> Yes ";
echo "<input type='radio' id='papercite_use_db' " . (!$option ? " checked='checked' " : "") . "value='no' name='papercite_options[use_db]' /> No";

}
Expand Down