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

Update with bibfilter: add a new bibtex template #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 121 additions & 29 deletions papercite.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
*/
class PaperciteAuthorMatcher {
function __construct($authors){
// Each element of this array is alternative match
// Each element of this array is alternative match
$this->filters = Array();

if (!isset($authors) || empty($authors)){
Expand All @@ -71,8 +71,8 @@ function matches(&$entry) {
foreach($this->filters as &$filter) {
foreach($filter->creators as $author) {
$ok = false;
foreach($eAuthors->creators as $eAuthor) {
if ($author["surname"] === $eAuthor["surname"]) {
foreach($eAuthors->creators as $eAuthor) {
if (($author["surname"]) === $eAuthor["surname"]) {
$ok = true;
break;
}
Expand All @@ -87,6 +87,42 @@ function matches(&$entry) {
}
}

class PaperciteKeywordMatcher {
function __construct($keywords){
// Each element of this array is alternative match
$this->filters = Array();

if (!isset($keywords) || empty($keywords)){
} else if(!is_string($keywords)){
echo "Warning: cannot parse option \"keywords\", this is specified by string!<br>";// probably useless..
} else {
require_once(dirname(__FILE__) . "/lib/bibtex_common.php");
foreach(preg_split("-\\;-", $keywords) as $conjonction) {
$this->keywordFind = $conjonction;
}
}
}

function matches(&$entry) {
$ok = true;
$eKeywords = &$entry["keywords"];

$allKeywordFind = explode(",", $this->keywordFind);
if(sizeof($allKeywordFind) > 1) return true;

foreach(explode(",", $this->keywordFind) as $keywordFind) {
foreach(preg_split("-\\;-", $eKeywords) as $keywords) {
$ok = false;
if(strtolower($keywords) === strtolower($keywordFind)){
$ok = true;
break;
}
}
}
return $ok;
}
}

class Papercite {


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand Down Expand Up @@ -125,7 +161,7 @@ class Papercite {
function getCached($url, $timeout = 3600) {
// check if cached file exists
$name = strtolower(preg_replace("@[/:]@","_",$url));
$dir = plugins_dir_path(__FILE__) . "/papercite/cache";
$dir = WP_PLUGIN_DIR . "/papercite/cache";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

$file = "$dir/$name.bib";

// check if file date exceeds 60 minutes
Expand Down Expand Up @@ -161,7 +197,7 @@ function getCached($url, $timeout = 3600) {
}
}

return array($file, plugins_url()."/papercite/cache/$name");
return array($file, WP_PLUGIN_URL."/papercite/cache/$name");
}

static $bibtex_parsers = array("pear" => "Pear parser", "osbib" => "OSBiB parser");
Expand Down Expand Up @@ -225,7 +261,6 @@ function init() {


static function getCustomDataDirectory() {
global $wpdb;
$url = WP_CONTENT_URL;
if (is_multisite()) {
$subpath = '/blogs.dir/'. $wpdb->blogid . "/files";
Expand Down Expand Up @@ -599,6 +634,7 @@ function _process(&$matches) {
* @options The options of the command
*/
function processCommand($command, $options) {
//print_r($options);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove

global $wpdb, $papercite_table_name;

// --- Process the commands ---
Expand Down Expand Up @@ -632,6 +668,7 @@ function processCommand($command, $options) {
$refs[$key] = &$entry;
}
}


$this->bibshow_tpl_options[] = $this->getBib2TplOptions($options);
$this->bibshow_options[] = $options;
Expand Down Expand Up @@ -704,6 +741,7 @@ static function userFiltersMatch($filters, $entry)
/** Get entries fullfilling a condition (bibtex & bibfilter) */
function getEntries($options) {
global $wpdb, $papercite_table_name;
//print_r($options);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove debugging information

// --- Filter the data
$entries = $this->getData($options["file"], $options);
if ($entries === FALSE) {
Expand All @@ -723,13 +761,19 @@ function getEntries($options) {
$this->addMessage("[papercite] Filtering by (key argument) is compatible with filtering by type or author (allow, deny, author arguments)", E_USER_NOTICE);
}
} else {
// Based on the entry types
$allow = Papercite::array_get($options, "allow", "");
$deny = Papercite::array_get($options, "deny", "");
// Based on the entry types
$allow = Papercite::array_get($options, "allow", "");
$deny = Papercite::array_get($options, "deny", "");

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

$getYear = Papercite::array_get($options, "year", "");
//print($getYear); //return;

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

$keyword_matcher = new PaperciteKeywordMatcher(Papercite::array_get($options, "keyword", ""));

$result = array();
$dbs = array();
Expand All @@ -753,20 +797,25 @@ function getEntries($options) {
// Handles year and entry type by direct SQL
foreach($allow as &$v) $v = '"' . $wpdb->escape($v) . '"';
$allowCond = $allow ? "and entrytype in (" . implode(",",$allow) . ")" : "";

foreach($deny as &$v) $v = '"' . $wpdb->escape($v) . '"';
$denyCond = $deny ? "and entrytype not in (" . implode(",",$deny) . ")" : "";

$getYearCond = $getYear ? "and year = " . $getYear . "" : "";

// Retrieve and filter further
$st = "SELECT data FROM $papercite_table_name WHERE $dbCond $denyCond $allowCond";
$rows = $wpdb->get_col($st);
$st = "SELECT data FROM $papercite_table_name WHERE $dbCond $denyCond $allowCond $getYearCond";
//print($st);
$rows = $wpdb->get_col($st);
if ($rows) foreach($rows as $data) {
$entry = maybe_unserialize($data);
if ($author_matcher->matches($entry) && Papercite::userFiltersMatch($options["filters"], $entry))
//print_r($entry);
if ($author_matcher->matches($entry) && $keyword_matcher->matches($entry) && Papercite::userFiltersMatch($options["filters"], $entry))
$result[] = $entry;
}
}
}

return $result;
}

Expand Down Expand Up @@ -845,10 +894,12 @@ function end_bibshow() {
* @param options The options to pass to bib2tpl
* @param getKeys Keep track of the keys for a final substitution
*/
function showEntries($refs, $goptions, $options, $getKeys, $mainTpl, $formatTpl, $mode) {
function showEntries($refs, $goptions, $options, $getKeys, $mainTpl, $formatTpl, $mode) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no change here - remove the trailing spaces

// Get the template files
$mainFile = $this->getDataFile("$mainTpl", "tpl", "tpl", "MIMETYPE", $goptions, true);
$formatFile = $this->getDataFile("$formatTpl", "tpl", "format", "MIMETYPE", $goptions, true);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove blank lines



// Fallback to defaults if needed
if (!$mainFile)
Expand Down Expand Up @@ -885,15 +936,18 @@ function showEntries($refs, $goptions, $options, $getKeys, $mainTpl, $formatTpl,
$this->checkFiles($ref, $goptions);
}



$r = $bib2tpl->display($refs);

// If we need to get the citation key back
if ($getKeys) {
foreach($refs as &$group)
foreach($group as &$ref) {
$this->keys[] = $ref["pKey"];
$this->keyValues[] = $ref["key"];
}
foreach($refs as &$group){
foreach($group as &$ref) {
$this->keys[] = $ref["pKey"];
$this->keyValues[] = $ref["key"];
}
}
}

// Process text in order to avoid some unexpected WordPress formatting
Expand All @@ -909,68 +963,106 @@ function showEntries($refs, $goptions, $options, $getKeys, $mainTpl, $formatTpl,
* @return multitype:string The output of the bibfilter shortcode
*/
function bibfilter($options){
// create form with custom types and authors
// create form with custom types and authors
global $post;

$selected_author = false;
$selected_type = false;
$selected_year = false;
$selected_keyword = false;

$original_authors = Papercite::array_get($options, "author", "");
$original_allow = Papercite::array_get($options, "allow", "");
$original_year = Papercite::array_get($options, "year", "");
$original_keyword = Papercite::array_get($options, "keyword", "");


if (isset($_POST) && (papercite::array_get($_POST, "papercite_post_id", 0) == $post->ID)) {
if (isset($_POST["papercite_author"]) && !empty($_POST["papercite_author"]))
$selected_author = ($options["author"] = $_POST["papercite_author"]);

if (isset($_POST["papercite_allow"]) && !empty($_POST["papercite_allow"]))
$selected_type = ($options["allow"] = $_POST["papercite_allow"]);
$selected_type = ($options["allow"] = $_POST["papercite_allow"]);

if (isset($_POST["papercite_year"]) && !empty($_POST["papercite_year"]))
$selected_year = ($options["year"] = $_POST["papercite_year"]);

if (isset($_POST["papercite_keywords"]) && !empty($_POST["papercite_keywords"]))
$selected_keyword = ($options["keyword"] = $_POST["papercite_keywords"]);

}
$result = $this->getEntries($options);
ob_start();
?>
<form method="post" accept-charset="UTF-8">
<form method="post">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UTF-8 was needed to handle properly the form

<input type="hidden" name="papercite_post_id" value="<?php echo $post->ID?>">
<table style="border-top: solid 1px #eee; border-bottom: solid 1px #eee; width: 100%">
<tr>
<td>Authors:</td>
<td><select name="papercite_author" id="papercite_author">
<option value="">ALL</option>
<?php
//$original_authors = "Nguyen|Delmas|Gong|Gimel'farb";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

$authors = preg_split("#\s*\\|\s*#", $original_authors);
if (Papercite::array_get($options, "sortauthors", 0))
sort($authors);

foreach($authors as $author) {
print "<option value=\"".htmlentities($author, ENT_QUOTES, "UTF-8")."\"";
if ($selected_author == $author)
print "<option value=\"".htmlentities($author)."\"";
//print addslashes($selected_author)." == $author";
if ($selected_author == addslashes($author))
print " selected=\"selected\"";
print ">$author</option>";
}
?>
</select></td>

</tr>
<tr>
<td>Type:</td>
<td><select name="papercite_allow" id="papercite_type">
<option value="">ALL</option>
<?php
$types = preg_split("#\s*,\s*#", $original_allow);
foreach($types as $type) {
print "<option value=\"".htmlentities($type, ENT_QUOTES, "UTF-8")."\"";
print "<option value=\"".htmlentities($type)."\"";
if ($selected_type == $type)
print " selected=\"selected\"";
print ">" . papercite_bibtype2string($type) . "</option>";
}
?>
</select></td>
</tr>
<tr>
<td>Year:</td>
<td><input type="text" name="papercite_year" id="papercite_year" value="<?php echo $selected_year; ?>" />
</td>
</tr>
<tr>
<td>Keywords:</td>
<td><select name="papercite_keywords" id="papercite_keywords">
<option value="">ALL</option>
<?php
$keywords = preg_split("#\s*,\s*#", $original_keyword);
sort($keywords);
foreach($keywords as $keyword) {
print "<option value=\"".htmlentities($keyword)."\"";
if ($selected_keyword == $keyword)
print " selected=\"selected\"";
print ">" . ($keyword) . "</option>";
}
?>
</select></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Filter" /></td>
</tr>
</table>
</form>

<?php

//print_r($options);
return ob_get_clean() . $this->showEntries($result, $options, $this->getBib2TplOptions($options), false, $options["bibtex_template"], $options["format"], "bibtex");
}

Expand Down
38 changes: 38 additions & 0 deletions tpl/advance-bibtex.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@{group@
@?groupkey@
<h3 class="papercite">@groupkey@</h3>
@;groupkey@
<ul class="papercite_bibliography">
@{entry@
<li>
@?pdf@ <a href="@pdf@" title='Download PDF' class='papercite_pdf'><img src='@WP_PLUGIN_URL@/papercite/img/pdf.png' alt="[PDF]"/></a>@;pdf@
@#entry@
@?doi@
<a href='http://dx.doi.org/@doi@' class='papercite_doi' title='View on publisher site'>doi:@doi@</a>
@;doi@

<br/>
<a href="javascript:void(0)" id="papercite_@papercite_id@" class="papercite_toggle">[BibTeX]</a>
@?abstract@
<a href="javascript:void(0)" id="papercite_abstract_@papercite_id@" class="papercite_toggle">[Abstract]</a>
@;abstract@

@?pdf@
<a href="@pdf@" title='Download PDF' class='papercite_pdf'>[Download PDF]</a>
@;pdf@

@?url@
<a href="@url@" title='External URL link' class='papercite_pdf'>[External URL link]</a>
@;url@

@?abstract@
<blockquote class="papercite_bibtex" id="papercite_abstract_@papercite_id@_block">@abstract@</blockquote>
@;abstract@

<div class="papercite_bibtex" id="papercite_@papercite_id@_block">
<pre><code class="tex bibtex">@bibtex@</code></pre>
</div>
</li>
@}entry@
</ul>
@}group@