forked from bpiwowar/papercite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
papercite.php
executable file
·845 lines (684 loc) · 26.6 KB
/
papercite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
<?php
/*
Plugin Name: papercite
Plugin URI: http://www.bpiwowar.net/papercite
Description: papercite enables to add BibTeX entries formatted as HTML in wordpress pages and posts. The input data is the bibtex text file and the output is HTML.
Version: HEAD
Author: Benjamin Piwowarski
Author URI: http://www.bpiwowar.net
*/
/* Copyright 2012 Benjamin Piwowarski (email : benjamim@bpiwowar.net)
Contributors:
- Stefan Aiche: group by year option
- Łukasz Radliński: bug fixes & handling polish characters
- Some parts of the code come from bib2html (version 0.9.3) written by
Sergio Andreozzi.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Options
include("papercite_options.php");
class Papercite {
var $parse = false;
// List of publications for those citations
var $bibshows = array();
// Our caches (bibtex files and formats)
var $cache = array();
// Array of arrays of current citations
var $cites = array();
// Global replacements for cited keys
var $keys = array();
var $keyValues = array();
// bibshow options stack
var $bibshow_options = array();
var $bibshow_tpl_options = array();
// Global counter for unique references of each
// displayed citation (used by bibshow)
var $citesCounter = 0;
// Global counter for unique reference of each
// displayed citation
var $counter = 0;
/** Returns filename of cached version of given url
* @param url The URL
* @param timeout The timeout of the cache
*/
function getCached($url, $timeout = 3600) {
// check if cached file exists
$name = strtolower(preg_replace("@[/:]@","_",$url));
$dir = WP_PLUGIN_DIR . "/papercite/cache";
$file = "$dir/$name.bib";
// check if file date exceeds 60 minutes
if (! (file_exists($file) && (filemtime($file) + $timeout > time()))) {
// not returned yet, grab new version
// since wordpress 2.7, we can use the wp_remote_get function
if (function_exists("wp_remote_get")) {
$body = wp_remote_retrieve_body(wp_remote_get($url));
if ($body) {
$f=fopen($file,"wb");
fwrite($f,$body);
fclose($f);
} else return NULL;
}
else {
$f=fopen($file,"wb");
fwrite($f,file_get_contents($url));
fclose($f);
}
if (!$f) {
echo "Failed to write file " . $file . " - check directory permission according to your Web server privileges.";
return false;
}
}
return array($file, WP_PLUGIN_URL."/papercite/cache/$name.bib");
}
static $bibtex_parsers = array("pear" => "Pear parser", "osbib" => "OSBiB parser");
// Names of the options that can be set
static $option_names = array("format", "timeout", "file", "bibshow_template", "bibtex_template", "bibtex_parser", "use_db");
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" => "pear", "use_db" => false);
/**
* Init is called before the first callback
*/
function init() {
// Get general preferences & page wise preferences
if (!$this->options) {
$this->options = papercite::$default_options;
$pOptions = &get_option('papercite_options');
// Use preferences if set to override default values
if (is_array($pOptions)) {
foreach(self::$option_names as &$name) {
if (array_key_exists($name, $pOptions) && sizeof($pOptions[$name]) > 0) {
$this->options[$name] = $pOptions[$name];
}
}
}
// Use custom field values
$custom_field = get_post_custom_values("papercite_$name");
if (sizeof($custom_field) > 0)
$this->options[$name] = $custom_field[0];
// Upgrade if needed
if ($this->options["bibtex_parser"] == "papercite") {
$this->options["bibtex_parser"] = "osbib";
}
}
}
/**
* Check the different paths where papercite data can be stored
* and return the first match, starting by the preferred ones
* @return either false (no match), or an array with the full
* path and the URL
*/
static function getDataFile($relfile) {
global $wpdb;
// Multi-site case
if (is_multisite()) {
$subpath = '/blogs.dir/'. $wpdb->blogid . "/files/papercite-data/$relfile";
$path = WP_CONTENT_DIR . $subpath;
if (file_exists($path))
return array($path, WP_CONTENT_URL.$subpath);
}
if (file_exists(WP_CONTENT_DIR . "/papercite-data/$relfile"))
return array(WP_CONTENT_DIR . "/papercite-data/$relfile", WP_CONTENT_URL . "/papercite-data/$relfile");
if (file_exists(WP_PLUGIN_DIR . "/papercite/$relfile"))
return array(WP_PLUGIN_DIR . "/papercite/$relfile", WP_PLUGIN_URL . "/papercite/$relfile");
}
/**
* Check if a matching file exists, and add it to the bibtex if so
* @param The key
* @param
*/
function checkFiles(&$entry, $types) {
$id = strtolower(preg_replace("@[/:]@", "-", $entry["cite"]));
foreach($types as &$type) {
$file = papercite::getDataFile("$type[0]/$id.$type[1]");
if ($file) {
$entry[$type[0]] = $file[1];
}
}
}
/**
* Get the bibtex data from an URI
*/
function &getData($biburis, $timeout = 3600) {
global $wpdb, $papercite_table_name;
// Loop over the different given URIs
$array = explode(",", $biburis);
$result = array();
foreach($array as $biburi) {
// (1) Get the context
$data = FALSE;
if (!$this->cache[$biburi]) {
if (strpos($biburi, "custom://") === 0) {
$data = get_post_custom_values("papercite_" . substr($biburi, 9));
if ($data) $data = $data[0];
}
else if (preg_match('#^(ftp|http)s?://#', $biburi) == 1) {
$bibFile = $this->getCached($biburi, $timeout);
} else {
$bibFile = $this->getDataFile("bib/$biburi");
}
if ($data === FALSE && !($bibFile && file_exists($bibFile[0])))
continue;
// (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()) {
// We use entrytype as a timestamp
$dbTS = intval($wpdb->get_var($wpdb->prepare("SELECT entrytype FROM $papercite_table_name WHERE url=%s and bibtexid=''", "ts://" . $biburi)));
if ($dbTS >= $fileTS) {
$result[$biburi] = $this->cache[$biburi] = "__DB__";
continue;
}
}
$data = file_get_contents($bibFile[0]);
}
if (!empty($data)) {
switch($this->options["bibtex_parser"]) {
case "pear": // Pear parser
$this->_parser = new Structures_BibTex(array('removeCurlyBraces' => true, 'extractAuthors' => true));
$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 BibTexEntries();
if (!$parser->parse($data)) {
$this->cache[$biburi] = false;
continue;
} else {
$this->cache[$biburi] = &$parser->data;
}
break;
}
// --- Add custom fields
foreach($this->cache[$biburi] as &$entry) {
$this->checkFiles($entry, array(array("pdf", "pdf")));
}
// Save to DB
if (!$stringedFile && $this->useDb()) {
// First delete everything
$wpdb->query($wpdb->prepare("DELETE FROM $papercite_table_name WHERE url=%s", $biburi));
$code = true;
foreach($this->cache[$biburi] as &$value) {
$statement = $wpdb->prepare("INSERT INTO $papercite_table_name(url, bibtexid, entrytype, year, data) VALUES (%s,%s,%s,%s,%s)",
$biburi, $value["cite"], $value["entrytype"], $value["year"], maybe_serialize($value));
$code = $wpdb->query($statement);
if ($code === FALSE) {
break;
}
}
if ($code !== FALSE) {
$statement = $wpdb->prepare("INSERT INTO $papercite_table_name(url, bibtexid, entrytype) VALUES(%s,%s,%s)", "ts://".$biburi, "", $fileTS);
$code = $wpdb->query($statement);
}
}
}
}
}
// Add to the list
if ($this->cache[$biburi])
$result[$biburi] = $this->cache[$biburi];
}
if (sizeof($result) == 0) return false;
return $result;
}
/** Returns true if papercite uses a database backend */
function useDb() { return $this->options["use_db"]; }
// Get the subset of keys present in the entries
static function getEntriesByKey(&$entries, &$keys) {
global $wpdb, $papercite_table_name;
$a = array();
$dbs = array();
$found = array();
foreach ($entries as $key => &$outer) {
if ($outer == "__DB__") $dbs[] = $key;
else foreach($outer as $entry) {
if (in_array($entry["cite"], $keys)) {
$a[] = $entry;
$found[] = $entry["cite"];
$n = $n + 1;
// We found everything, early break
if ($n == sizeof($keys)) break;
}
}
if ($n == sizeof($keys)) break;
}
// Case where we have to check the db
$unfound = array_diff($keys, $found);
if ($dbs && sizeof($unfound) > 0) {
$dbs = papercite::getDbCond($dbs);
foreach($unfound as &$v) $v = '"' . $wpdb->escape($v) . '"';
$keylist = implode(",", $unfound);
$st = "SELECT data FROM $papercite_table_name WHERE $dbs and bibtexid in ($keylist)";
$val = $wpdb->get_col($st);
if ($val !== FALSE) {
foreach($val as &$data)
$a[] = maybe_unserialize($data);
}
}
return $a;
}
/**
* Get string with author name(s) and make regex of it.
* String with author or a list of authors (passed as parameter to papercite) in the following format:
* -a1|a2|..|an - publications including at least one of these authors
* -a1&a2&..&an - publications including all of these authors
*
* @param unknown $authors - string parsed from papercite after tag: "author="
*/
function _build_authors_regex($authors){
if(empty($authors)){
return $authors;
}else if(!is_string){
echo "Warning: cannot parse option \"authors\", this is specified by string!<br>";// probably useless..
return $authors;
// string contains both: & and | => this is not supported
}else if(preg_match('/^(?=.*\|)(?=.*\&)/i', $authors)){
echo "ERROR: multiple conditions not supported so far: use only \& or \| between authors<br>";
return $authors;
// if string contains & between authors: build regex, in all other cases return the same (| is supported directly by bip2tpl)
}else if(preg_match('/\&/i', $authors)){
// so we want to translate e.g. this: 'nahodil&kadlecek' to this string: '^(?=.*nahodil)(?=.*kadle)'
$connect = ')(?=.*';
$start ='^(?=.*';
$end = ')';
$reg = preg_replace("/\&/i",$connect, $authors);
$reg = $start.$reg.$end;
return $reg;
}
return $authors;
}
/**
* 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";
}
// convert list of authors into regex
$aut_regex = $this->_build_authors_regex($options["author"]);
$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"],
// filtering authors and entrytype goes here
"only" => array('author' => $aut_regex, 'entrytype' => $options["type"]));
$data = null;
// --- Process the commands ---
switch($command) {
// display form, convert bibfilter to bibtex command and recursivelly call the same;-)
case "bibfilter":
// this should return hmtl form and new command composed of (modified) $options_pairs
$converted = convertAddForm($options_pairs);
$variable = $converted[1];
return $converted[0]." ".$this->process($variable);
// 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)".
(current_user_can("edit_post") ? " with name [".htmlspecialchars($options["file"])."]" : "") ."</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
case "bibshow":
$data = $this->getData($options["file"]);
if (!$data) return "<span style='color: red'>[Could not find the bibliography file(s)".
(current_user_can("edit_post") ? " with name [".htmlspecialchars($options["file"])."]" : "") ."</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
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]";
}
}
//! Get a db condition subquery
function getDbCond(&$dbArray) {
global $wpdb;
$dbs = array();
foreach($dbArray as &$db)
$dbs[] = "\"" . $wpdb->escape($db) . "\"";
$dbs = implode(",", $dbs);
if ($dbs) $dbs = "url in ($dbs)";
return $dbs;
}
function end_bibshow() {
global $wpdb, $papercite_table_name;
// 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();
$dbs = papercite::getDbCond($data["__DB__"]);
// Order the citations according to citation order
// (might be re-ordered latter)
foreach($cites as $key => &$cite) {
// Search
if (!$data[$key] && $dbs) {
$val = $wpdb->get_var($wpdb->prepare("SELECT data FROM $papercite_table_name WHERE $dbs and bibtexid=%s", $key));
if ($val !== FALSE) {
$refs[$cite[0]] = maybe_unserialize($val);
}
} else
$refs[$cite[0]] = $data[$key];
$refs[$cite[0]]["pKey"] = $cite[1];
// just in case
$refs[$cite[0]]["cite"] = $key;
}
ksort($refs);
return $this->showEntries(array_values($refs), $tplOptions, true, $options["bibshow_template"], $options["format"], "bibshow");
}
/**
* Show a set of entries
* @param refs An array of references
* @param options The options to pass to bib2tpl
* @param getKeys Keep track of the keys for a final substitution
*/
function showEntries(&$refs, &$options, $getKeys, $mainTpl, $formatTpl, $mode) {
// Get the template files
$mainFile = papercite::getDataFile("/tpl/$mainTpl.tpl");
$formatFile = papercite::getDataFile("/format/$formatTpl.tpl");
// Fallback to defaults if needed
if (!$mainFile)
$mainFile = papercite::getDataFile("/tpl/" .papercite::$default_options["${mode}_template"] .".tpl");
if (!$formatFile)
$formatFile = papercite::getDataFile("/format/" .papercite::$default_options["format"] . ".tpl");
$main = file_get_contents($mainFile[0]);
$format = file_get_contents($formatFile[0]);
$bibtexEntryTemplate = new BibtexEntryFormat($format);
// Gives a distinct ID to each publication (i.e. to display the corresponding bibtex)
// in the reference list
foreach($refs as &$entry) {
$entry["papercite_id"] = $this->counter++;
}
// Convert (also set the citation key)
$bib2tpl = new BibtexConverter($options, $main, $bibtexEntryTemplate);
$bib2tpl->setGlobal("WP_PLUGIN_URL", WP_PLUGIN_URL);
$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"];
}
}
// Process text in order to avoid some unexpected WordPress formatting
return str_replace("\t", ' ', trim($r["text"]));
}
}
// -------------------- Bibfilter command support
/**
* This does two things:
* -dynamically creates html form based on parameters (author and menutype)
* -rebuilds command which is then sent as the bibtex command
*
* @param unknown $pairs pairs in the same format as bibtex uses
* @return multitype:string returns command to be passes to bibtex
*/
function convertAddForm($pairs){
// create form with custom types and authors
generateForm($pairs, $authors, $types);
// if the form is empty, just rebuild the command to: bibtex
if (empty ( $_POST )) {
$out = array("",rebuildCommand($pairs,"",""));
return $out;
// if something is selected, rebuild command with filtered data according to the selection
}else{
$out = array("",rebuildCommand($pairs,$_POST['only_author'],$_POST['only_entrytype']));
return $out;
}
}
/**
* Provide options_pairs and two parameters: $author and $type (got from form selection),
* the same array as is passed to process() method is rebuilt with potentially new values.
*
* @param unknown $options_pairs pairs obtained from process method
* @param unknown $author author to be filtered, if empty, the original values are left intact
* @param unknown $type type of publication to be displayed, if empty, original value remains
*/
function rebuildCommand($options_pairs, $author, $type){
$out = "";
$typefound = 0;
// for author and type, check if is overwritten by selection form, ignore the rest (no-changes)
foreach($options_pairs as $x) {
if($x[1]=="author"){
if(empty($author)){
$out = $out.$x[0]." ";
}else{
$out = $out."author=".$author." ";
}
}else if($x[1] == "type"){
$typefound =1;
if(empty($type)){
$out = $out.$x[0]." ";
}else{
$out = $out."type=".$type." ";
}
// menutypes and sortauthors should not be passed to bibtex command
}else if($x[1] != "menutypes" && $x[1] != "sortauthors"){
$out = $out.$x[0];
}
}
// if type not given in bibfilter command, add the filtered value
if($typefound==0 && !empty($type)){
$out = $out."type=".$type." ";
}
// compose the command to the required format
$out0 = "[bibtex ".$out."]";
$out1 = "bibtex";
$out2 = $out;
$oo = array($out0,$out1,$out2);
return $oo;
}
/**
* Generate form, use javascript to fill authors and types into selectors
* @param unknown $pairs used originally in papercite
* @return string returns nothing, html is just directly added
*/
function generateForm($pairs){
$sortmenu = 0;
foreach($pairs as $x) {
if($x[1]=="author"){
// pass list of authors separated by | .. to the form
$authors = $x[2];
}else if($x[1]=="menutypes"){
// types of publicaitons separated by |
// value and text are separated by "-" and all "_" are replaced by space
$types = $x[2];
}else if($x[1]=="sortauthors"){
$sortmenu = $x[2];
}
}
require_once 'bibfilterForm.html';
return "";
}
// -------------------- Interface with WordPress
// --- Head of the HTML ----
function papercite_head() {
if (!function_exists('wp_enqueue_script')) {
// In case there is no wp_enqueue_script function (WP < 2.6), we load the javascript ourselves
echo "\n" . '<script src="'. get_bloginfo('wpurl') . '/wp-content/plugins/papercite/js/jquery.js" type="text/javascript"></script>' . "\n";
echo '<script src="'. get_bloginfo('wpurl') . '/wp-content/plugins/papercite/js/papercite.js" type="text/javascript"></script>' . "\n";
}
}
// --- Initialise papercite ---
function papercite_init() {
global $papercite;
if (function_exists('wp_enqueue_script')) {
wp_register_script('papercite', get_bloginfo('wpurl') . '/wp-content/plugins/papercite/js/papercite.js', array('jquery'));
wp_enqueue_script('papercite');
}
// Register and enqueue the stylesheet
wp_register_style('papercite_css', WP_PLUGIN_URL . '/papercite/papercite.css' );
wp_enqueue_style('papercite_css');
// Initialise the object
$papercite = new Papercite();
}
// --- Callback function ----
function &papercite_cb($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");
}
// print "<div style='border: 1pt solid blue'>"; print(nl2br(htmlentities($myContent))); print "</div>";
// (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);
// print "<div style='border: 1pt solid black'>"; print(nl2br(htmlentities($text))); print "</div>";
return $text;
}
// --- Add the documentation link in the plugin list
function papercite_row_cb($data, $file) {
if ($file == "papercite/papercite.php") {
$data[] = "<a href='" . WP_PLUGIN_URL . "/papercite/documentation/index.html'>Documentation</a>";
}
return $data;
}
add_filter('plugin_row_meta', 'papercite_row_cb',1,2);
// --- Add the different handlers to WordPress ---
add_action('init', 'papercite_init');
add_action('wp_head', 'papercite_head');
add_filter('the_content', 'papercite_cb', -1);
?>