From 8afa8153173f0427b827ab47367182e6fea0f2b4 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Date: Tue, 5 Aug 2014 13:27:51 +1200 Subject: [PATCH 1/2] Update with bibfilter: add a new bibtex template Allow search for publication year, keyword, Improve the search filters example: [bibfilter file=bib.bib group=year group_order=desc allow=inproceedings,article,book,inbook,incollection,techreport,mastersthesis,phdthesis,unpublished author=Nguyen|Gimel'farb|Delmas sortauthors=1 keyword="3D content,Web browsers,World Wide Web,Internet,Online front-ends,Stereo image,3D image,3D reconstruction,Image analysis,Vision,Binocular,Stereopsis,Calibration" ] --- papercite.php | 150 +++++++++++++++++++++++++++++++++-------- tpl/advance-bibtex.tpl | 38 +++++++++++ 2 files changed, 159 insertions(+), 29 deletions(-) create mode 100644 tpl/advance-bibtex.tpl diff --git a/papercite.php b/papercite.php index 36656d9..3da3bf5 100755 --- a/papercite.php +++ b/papercite.php @@ -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)){ @@ -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; } @@ -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!
";// 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 { @@ -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"; $file = "$dir/$name.bib"; // check if file date exceeds 60 minutes @@ -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"); @@ -225,7 +261,6 @@ function init() { static function getCustomDataDirectory() { - global $wpdb; $url = WP_CONTENT_URL; if (is_multisite()) { $subpath = '/blogs.dir/'. $wpdb->blogid . "/files"; @@ -599,6 +634,7 @@ function _process(&$matches) { * @options The options of the command */ function processCommand($command, $options) { + //print_r($options); global $wpdb, $papercite_table_name; // --- Process the commands --- @@ -632,6 +668,7 @@ function processCommand($command, $options) { $refs[$key] = &$entry; } } + $this->bibshow_tpl_options[] = $this->getBib2TplOptions($options); $this->bibshow_options[] = $options; @@ -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); // --- Filter the data $entries = $this->getData($options["file"], $options); if ($entries === FALSE) { @@ -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(); @@ -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; } @@ -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) { // Get the template files $mainFile = $this->getDataFile("$mainTpl", "tpl", "tpl", "MIMETYPE", $goptions, true); $formatFile = $this->getDataFile("$formatTpl", "tpl", "format", "MIMETYPE", $goptions, true); + + // Fallback to defaults if needed if (!$mainFile) @@ -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 @@ -909,28 +963,39 @@ 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(); ?> -
+ @@ -938,39 +1003,66 @@ function bibfilter($options){ - + + + + + + + + + + + + +
Type:
Year: +
Keywords:
 
showEntries($result, $options, $this->getBib2TplOptions($options), false, $options["bibtex_template"], $options["format"], "bibtex"); } diff --git a/tpl/advance-bibtex.tpl b/tpl/advance-bibtex.tpl new file mode 100644 index 0000000..cfae056 --- /dev/null +++ b/tpl/advance-bibtex.tpl @@ -0,0 +1,38 @@ +@{group@ +@?groupkey@ +

@groupkey@

+@;groupkey@ + +@}group@ From 8083774bf78e5a25939279f43d8d4157b39f80b8 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Date: Wed, 6 Aug 2014 11:51:07 +1200 Subject: [PATCH 2/2] Revert "Update with bibfilter: add a new bibtex template" This reverts commit 8afa8153173f0427b827ab47367182e6fea0f2b4. --- papercite.php | 150 ++++++++--------------------------------- tpl/advance-bibtex.tpl | 38 ----------- 2 files changed, 29 insertions(+), 159 deletions(-) delete mode 100644 tpl/advance-bibtex.tpl diff --git a/papercite.php b/papercite.php index 3da3bf5..36656d9 100755 --- a/papercite.php +++ b/papercite.php @@ -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)){ @@ -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; } @@ -87,42 +87,6 @@ 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!
";// 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 { @@ -161,7 +125,7 @@ class Papercite { function getCached($url, $timeout = 3600) { // check if cached file exists $name = strtolower(preg_replace("@[/:]@","_",$url)); - $dir = WP_PLUGIN_DIR . "/papercite/cache"; + $dir = plugins_dir_path(__FILE__) . "/papercite/cache"; $file = "$dir/$name.bib"; // check if file date exceeds 60 minutes @@ -197,7 +161,7 @@ function getCached($url, $timeout = 3600) { } } - return array($file, WP_PLUGIN_URL."/papercite/cache/$name"); + return array($file, plugins_url()."/papercite/cache/$name"); } static $bibtex_parsers = array("pear" => "Pear parser", "osbib" => "OSBiB parser"); @@ -261,6 +225,7 @@ function init() { static function getCustomDataDirectory() { + global $wpdb; $url = WP_CONTENT_URL; if (is_multisite()) { $subpath = '/blogs.dir/'. $wpdb->blogid . "/files"; @@ -634,7 +599,6 @@ function _process(&$matches) { * @options The options of the command */ function processCommand($command, $options) { - //print_r($options); global $wpdb, $papercite_table_name; // --- Process the commands --- @@ -668,7 +632,6 @@ function processCommand($command, $options) { $refs[$key] = &$entry; } } - $this->bibshow_tpl_options[] = $this->getBib2TplOptions($options); $this->bibshow_options[] = $options; @@ -741,7 +704,6 @@ static function userFiltersMatch($filters, $entry) /** Get entries fullfilling a condition (bibtex & bibfilter) */ function getEntries($options) { global $wpdb, $papercite_table_name; - //print_r($options); // --- Filter the data $entries = $this->getData($options["file"], $options); if ($entries === FALSE) { @@ -761,19 +723,13 @@ 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", "")); - - $keyword_matcher = new PaperciteKeywordMatcher(Papercite::array_get($options, "keyword", "")); + $author_matcher = new PaperciteAuthorMatcher(Papercite::array_get($options, "author", "")); $result = array(); $dbs = array(); @@ -797,25 +753,20 @@ 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 $getYearCond"; - //print($st); - $rows = $wpdb->get_col($st); + $st = "SELECT data FROM $papercite_table_name WHERE $dbCond $denyCond $allowCond"; + $rows = $wpdb->get_col($st); if ($rows) foreach($rows as $data) { $entry = maybe_unserialize($data); - //print_r($entry); - if ($author_matcher->matches($entry) && $keyword_matcher->matches($entry) && Papercite::userFiltersMatch($options["filters"], $entry)) + if ($author_matcher->matches($entry) && Papercite::userFiltersMatch($options["filters"], $entry)) $result[] = $entry; } } } - + return $result; } @@ -894,12 +845,10 @@ 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) { // Get the template files $mainFile = $this->getDataFile("$mainTpl", "tpl", "tpl", "MIMETYPE", $goptions, true); $formatFile = $this->getDataFile("$formatTpl", "tpl", "format", "MIMETYPE", $goptions, true); - - // Fallback to defaults if needed if (!$mainFile) @@ -936,18 +885,15 @@ 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 @@ -963,39 +909,28 @@ 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(); ?> -
+ @@ -1003,66 +938,39 @@ function bibfilter($options){ - - + - - - - - - - - - - -
Type:
Year: -
Keywords:
 
showEntries($result, $options, $this->getBib2TplOptions($options), false, $options["bibtex_template"], $options["format"], "bibtex"); } diff --git a/tpl/advance-bibtex.tpl b/tpl/advance-bibtex.tpl deleted file mode 100644 index cfae056..0000000 --- a/tpl/advance-bibtex.tpl +++ /dev/null @@ -1,38 +0,0 @@ -@{group@ -@?groupkey@ -

@groupkey@

-@;groupkey@ - -@}group@