/** * Main entry point: Handles a match in the post */ function process(&$matches) { global $wpdb, $papercite_table_name; $debug = false; $post = null; //print(get_post($post)->post_modified_gmt); // --- Initialisation --- // Includes once the bibtex parser require_once dirname(__FILE__) . "/lib/BibTex_" . $this->options["bibtex_parser"] . ".php"; // Includes once the converter require_once "bib2tpl/bibtex_converter.php"; // Get the options $command = $matches[1]; // Get all the options pairs and store them // in the $options array $options_pairs = array(); preg_match_all("/\\s*(?:([\\w-_]+)=(\\S+))(\\s+|\$)/", $matches[2], $options_pairs, PREG_SET_ORDER); // Set preferences, by order of increasing priority // (0) Set in // (1) From the preferences // (2) From the custom fields // (3) From the general options // $this->options has already processed the steps 0-2 $options = $this->options; // Gets the options from the command foreach ($options_pairs as $x) { if ($x[1] == "template") { // Special case of template: should overwrite the corresponding command template $options["{$command}_{$x['1']}"] = $x[2]; } else { $options[$x[1]] = $x[2]; } } // --- Compatibility issues if (array_key_exists("groupByYear", $options) && strtoupper($options["groupByYear"]) == "TRUE") { $options["group"] = "year"; $options["group_order"] = "desc"; } $tplOptions = array("anonymous-whole" => true, "group" => $options["group"], "group_order" => $options["group_order"], "sort" => $options["sort"], "order" => $options["order"], "key_format" => $options["key_format"]); $data = null; // --- Process the commands --- switch ($command) { // bibtex command: case "bibtex": // --- Filter the data $entries = $this->getData($options["file"], $options["timeout"]); if (!$entries) { return "<span style='color: red'>[Could not find the bibliography file(s)]</span>"; } if (array_key_exists('key', $options)) { // Select only specified entries $keys = split(",", $options["key"]); $a = array(); $n = 0; $result = papercite::getEntriesByKey($entries, $keys); } else { // Based on the entry types $allow = $options["allow"]; $deny = $options["deny"]; $allow = $allow ? split(",", $allow) : array(); $deny = $deny ? split(",", $deny) : array(); $result = array(); $dbs = array(); foreach ($entries as $key => &$outer) { if ($outer == "__DB__") { $dbs[] = $key; } else { foreach ($outer as &$entry) { $t =& $entry["entrytype"]; if ((sizeof($allow) == 0 || in_array($t, $allow)) && (sizeof($deny) == 0 || !in_array($t, $deny))) { $result[] = $entry; } } } } // Add DB entries if ($dbs) { $dbCond = $this->getDbCond($dbs); // Construct the query 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) . ")" : ""; $st = "SELECT data FROM {$papercite_table_name} WHERE {$dbCond} {$denyCond} {$allowCond}"; $rows = $wpdb->get_col($st); if ($rows) { foreach ($rows as $data) { $result[] = maybe_unserialize($data); } } } } return $this->showEntries($result, $tplOptions, false, $options["bibtex_template"], $options["format"], "bibtex"); // bibshow / bibcite commands // bibshow / bibcite commands case "bibshow": $data = $this->getData($options["file"]); if (!$data) { return "<span style='color: red'>[Could not find the bibliography file(s)]</span>"; } // TODO: replace this by a method call $refs = array("__DB__" => array()); foreach ($data as $bib => &$outer) { // If we have a database backend for a bibtex, use it if ($outer == "__DB__") { array_push($refs["__DB__"], $bib); } else { foreach ($outer as &$entry) { $key = $entry["cite"]; $refs[$key] =& $entry; } } } $this->bibshow_tpl_options[] = $tplOptions; $this->bibshow_options[] = $options; array_push($this->bibshows, &$refs); $this->cites[] = array(); break; // Just cite // Just cite case "bibcite": if (sizeof($this->bibshows) == 0) { return "[<span title=\"Unkown reference: {$options['key']}\">?</span>]"; } $keys = preg_split("/,/", $options["key"]); $cites =& $this->cites[sizeof($this->cites) - 1]; $returns = ""; foreach ($keys as $key) { if ($returns) { $returns .= ", "; } // First, get the corresponding entry $num = $cites[$key]; // Did we already cite this? if (!$num) { // no, register this $id = "BIBCITE%%" . $this->citesCounter . "%"; $this->citesCounter++; $num = sizeof($cites); $cites[$key] = array($num, $id); } else { // yes, just copy the id $id = $num[1]; } $returns .= "{$id}"; } return "[{$returns}]"; case "/bibshow": return $this->end_bibshow(); default: return "[error in papercite: unhandled]"; } }
/** * Main entry point: Handles a match in the post */ function process(&$matches) { $debug = false; $post = null; //print(get_post($post)->post_modified_gmt); // --- Initialisation --- // Includes once switch($this->options["bibtex_parser"]) { case "papercite": // Use papercite parser require_once(dirname(__FILE__) . "/lib/BibTex_parser.php"); break; default: // Use the slightly modified BibTex parser from PEAR. require_once(dirname(__FILE__) . '/lib/BibTex.php'); break; } require_once("bib2tpl/bibtex_converter.php"); // Get the options $command = $matches[1]; // Get all the options pairs and store them // in the $options array $options_pairs = array(); preg_match_all("/\s*(?:([\w-_]+)=(\S+))(\s+|$)/", $matches[2], $options_pairs, PREG_SET_ORDER); // Set preferences, by order of increasing priority // (0) Set in // (1) From the preferences // (2) From the custom fields // (3) From the general options // $this->options has already processed the steps 0-2 $options = $this->options; // Gets the options from the command foreach($options_pairs as $x) { if ($x[1] == "template") { // Special case of template: should overwrite the corresponding command template $options["${command}_$x[1]"] = $x[2]; } else $options[$x[1]] = $x[2]; } // --- Compatibility issues if (array_key_exists("groupByYear", $options) && (strtoupper($options["groupByYear"]) == "TRUE")) { $options["group"] = "year"; $options["group_order"] = "desc"; } $tplOptions = array( "anonymous-whole" => true, // for compatibility in the output "group" => $options["group"], "group_order" => $options["group_order"], "sort" => $options["sort"], "order" => $options["order"], "key_format" => $options["key_format"]); $data = null; // --- Process the commands --- switch($command) { /* "bibtext" command */ case "bibtex": // --- Filter the data $entries = $this->getData($options["file"], $options["timeout"]); if (!$entries) return "<span style='color: red'>[Could not find the bibliography file(s)]</span>"; if (array_key_exists('key', $options)) { // Select only specified entries $keys = split(",", $options["key"]); $a = array(); $n = 0; $result = papercite::getEntriesByKey($entries, $keys); } else { // Based on the entry types $allow = $options["allow"]; $deny = $options["deny"]; if ($allow || $deny) { $allow = $allow ? split(",",$allow) : false; $deny = $deny ? split(",", $deny) : false; $result = array(); // TODO: replace this by a method call foreach($entries as &$outer) { foreach($outer as &$entry) { $t = &$entry["entrytype"]; if ((!$allow || in_array($t, $allow)) && (!$deny || !in_array($t, $deny))) { $result[] = $entry; } } } } else { $result = array(); foreach($entries as &$outer) { foreach($outer as &$entry) { $result[] = $entry; } } } } return $this->showEntries($result, $tplOptions, false, $options["bibtex_template"], $options["format"], "bibtex"); /* bibshow / bibcite commands */ case "bibshow": $data = $this->getData($options["file"]); if (!$data) return "<span style='color: red'>[Could not find the bibliography file(s)]</span>"; // TODO: replace this by a method call $refs = array(); foreach($data as $outer) { foreach($outer as &$entry) { $key = $entry["cite"]; $refs[$key] = &$entry; } } $this->bibshow_tpl_options[] = $tplOptions; $this->bibshow_options[] = $options; array_push($this->bibshows, &$refs); $this->cites[] = array(); break; // Just cite case "bibcite": if (sizeof($this->bibshows) == 0) return "[<span title=\"Unkown reference: $options[key]\">?</span>]"; $keys = preg_split("/,/",$options["key"]); $refs = &$this->bibshows[sizeof($this->bibshows)-1]; $cites = &$this->cites[sizeof($this->cites)-1]; $returns = ""; foreach($keys as $key) { if ($returns) $returns .= ", "; // First, get the corresponding entry if (array_key_exists($key, $refs)) { $num = $cites[$key]; // Did we already cite this? if (!$num) { // no, register this $id = "BIBCITE%%%" . $this->citesCounter; $this->citesCounter++; $num = sizeof($cites); $cites[$key] = array($num, $id); } $returns .= "$id"; } else { $returns .= "<span title=\"Unkown reference: $key\">?</span>"; } } return "[$returns]"; case "/bibshow": // select from cites if (sizeof($this->bibshows) == 0) return ""; // Remove the array from the stack $data = &array_pop($this->bibshows); $cites = &array_pop($this->cites); $tplOptions = &array_pop($this->bibshow_tpl_options); $options = &array_pop($this->bibshow_options); $refs = array(); // Order the citations according to citation order // (might be re-ordered latter) foreach($data as $key => &$entry) { $num = $cites[$key]; if ($num) { $refs[$num[0]] = $entry; $refs[$num[0]]["pKey"] = $num[1]; } } ksort($refs); return $this->showEntries(array_values($refs), $tplOptions, true, $options["bibshow_template"], $options["format"], "bibshow"); default: return "[error in papercite: unhandled]"; } }
/** Get entries fullfilling a condition (bibtex & bibfilter) */ function getEntries($options) { global $wpdb, $papercite_table_name; // --- Filter the data $entries = $this->getData($options["file"], $options); if ($entries === FALSE) { $this->addMessage("[Could not find the bibliography file(s) with name [" . htmlspecialchars($options["file"]) . "]"); return false; } if (array_key_exists('key', $options)) { // Select only specified entries $keys = preg_split("-,-", $options["key"]); $a = array(); $n = 0; $result = papercite::getEntriesByKey($entries, $keys); if (array_key_exists("allow", $options) || array_key_exists("deny", $options) || array_key_exists("author", $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", ""); $allow = $allow ? preg_split("-,-", $allow) : array(); $deny = $deny ? preg_split("-,-", $deny) : array(); $author_matcher = new PaperciteAuthorMatcher(Papercite::array_get($options, "author", "")); $result = array(); $dbs = array(); foreach ($entries as $key => &$outer) { if (is_array($outer) && $outer[0] == "__DB__") { $dbs[] = $outer[1]; } 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)) { $result[] = $entry; } } } } // --- 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) . '"'; } $allowCond = $allow ? "and entrytype in (" . implode(",", $allow) . ")" : ""; foreach ($deny as &$v) { $v = '"' . $wpdb->escape($v) . '"'; } $denyCond = $deny ? "and entrytype not in (" . implode(",", $deny) . ")" : ""; // Retrieve and filter further $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); if ($author_matcher->matches($entry) && Papercite::userFiltersMatch($options["filters"], $entry)) { $result[] = $entry; } } } } } return $result; }