-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbib_stuff.php
1347 lines (1158 loc) · 46.2 KB
/
bib_stuff.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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
require "config.php";
require $bib_connect_file;
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
// Cached DB connection
$last_dbconn = NULL;
// Month names
// NB: There are 2 options for "undefined" corresponding to indices 0 and 13.
// Only matters if you care where an item appears when sorting by month.
$bib_month_names = array("0"=>"", "1"=>"January", "2"=>"February", "3"=>"March", "4"=>"April", "5"=>"May", "6"=>"June", "7"=>"July", "8"=>"August", "9"=>"September", "10"=>"October", "11"=>"November", "12"=>"December", "13"=>"");
// Main database fields for bibliography items (excludes "key", "cats")
$bib_item_fields = array("type", "address", "author", "booktitle", "chapter", "edition", "editor", "institution", "journal", "month", "note", "number", "organization", "pages", "publisher", "school", "series", "title", "type2", "volume", "year", "url", "filename", "abstract", "links", "synopsis");
// Subset of database fields (from $bib_item_fields above) that are standard bibtex fields
$bib_item_fields_bibtex = array("type", "address", "author", "booktitle", "chapter", "edition", "editor", "institution", "journal", "month", "note", "number", "organization", "pages", "publisher", "school", "series", "title", "type2", "volume", "year");
// Bibliography item types (à la bibtex)
$bib_item_types = array("inproceedings","article","book","inbook","phdthesis","mastersthesis","techreport","unpublished","proceedings");
// Default database fields to use for searching
$bib_list_search_fields_default = array("key", "address", "author", "booktitle", "chapter", "editor", "institution", "journal", "month", "note", "number", "organization", "pages", "publisher", "school", "series", "title", "type2", "volume", "year", "synopsis");
// Storage for info about a bib item list
$bib_list_item_link = "";
$bib_list_item_start = "";
$bib_list_item_end = "";
$bib_list_block_start = "";
$bib_list_block_end = "";
$bib_list_sections = NULL;
$bib_list_search_enabled = false;
$bib_list_search_string = NULL;
$bib_list_search_fields = NULL;
$bib_list_search_words = NULL;
$bib_list_search_preg = NULL;
$bib_list_search_sql = NULL;
$bib_list_counter = 1;
$bib_list_alt = 1;
$bib_list_item_extra_links = NULL;
//-----------------------------------------------------------------------------
// General utiltity functions
//-----------------------------------------------------------------------------
// Extract a variable from POST/GET
function extract_var_from_post_get($var)
{
$res = "";
if (isset($_POST[$var])) {
$res = $_POST[$var];
}
else if (isset($_GET[$var])) {
$res = $_GET[$var];
}
if (is_string($res)) {
$res = stripslashes($res);
}
return $res;
}
//-----------------------------------------------------------------------------
// Extract multiple variables from POST/GET
function extract_vars_from_post_get(&$res, $arr)
{
foreach ($arr as $var) {
$res[$var] = "";
if (isset($_POST[$var])) {
$res[$var] = $_POST[$var];
}
else if (isset($_GET[$var])) {
$res[$var] = $_GET[$var];
}
if (is_string($res[$var])) {
$res[$var] = stripslashes($res[$var]);
}
}
return $res;
}
//-----------------------------------------------------------------------------
// Extract "key" variable from POST/GET
function extract_key_from_post_get($field = "key")
{
// get variable
$key = extract_var_from_post_get($field);
// replace ' 's with '+'s because they get mangled in the url
$key = preg_replace("/ /", "+", $key);
return $key;
}
//-----------------------------------------------------------------------------
// Check validity of a key
function is_key_valid($key)
{
return (preg_match("/^[A-Za-z0-9_\-+]+$/", $key) === 1);
}
//-----------------------------------------------------------------------------
// Get a connection to the database, setting up if needed
function bib_db_connection()
{
global $last_dbconn;
$dbconn = $last_dbconn ? $last_dbconn : bib_connect_to_db();
if (!$dbconn) { bib_log_error("Couldn't connect to database"); return NULL; }
$last_dbconn = $dbconn;
return $dbconn;
}
//-----------------------------------------------------------------------------
// Log an error message to the error log.
// If $dbconn is provided, last DB error will also be logged.
function bib_log_error($err_msg, $dbconn = NULL)
{
global $bib_error_file;
// open log file
$fout = fopen($bib_error_file, "a");
if ($fout) {
// write date/time
fwrite($fout, "\nTime: ".date("D M j G:i:s T Y")."\n");
// write error message
fwrite($fout, "Error: ".$err_msg.".\n");
// if a db error, write db error message too
if ($dbconn) fwrite($fout, "Postgresql error: ".pg_last_error($dbconn)."\n");
// write calling url
fwrite($fout, "URL: ".$_SERVER["PHP_SELF"]);
if (array_key_exists("QUERY_STRING", $_SERVER)) if ($_SERVER["QUERY_STRING"]) fwrite($fout, "?".$_SERVER["QUERY_STRING"]);
fwrite($fout, "\n");
// close log file
fclose($fout);
}
}
//-----------------------------------------------------------------------------
// Execute an arbitrary SQL query and return results in an array.
// Result is NULL in case of error.
// By default array is 2D but if there is only one column it is 1D.
function bib_db_query($query)
{
// query database
$dbconn = bib_db_connection();
$result = @pg_query($dbconn, $query);
if (!$result) { bib_log_error("Couldn't execute query \"$query\"", $dbconn); return NULL; }
// initialise result array
$res = array();
// for a single column of results, build a 1D array
if (pg_num_fields($result) <= 1) {
while($row = pg_fetch_array($result)) {
// store single row item in array, de-escaping if necessary
$res[] = (is_string($row[0])) ? stripslashes($row[0]) : $row[0];
}
}
// for multiple columns, build a 2D array
else {
$res = array();
while($row = pg_fetch_array($result)) {
// de-escape any strings
foreach ($row as $key => $val) {
if (is_string($val))
$row[$key] = stripslashes($val);
}
// store row in array
$res[] = $row;
}
}
// return result
return $res;
}
//-----------------------------------------------------------------------------
// Execute an arbitrary SQL update query and return number of affected rows.
// Result is NULL in case of error.
function bib_db_update_query($query)
{
// query database
$dbconn = bib_db_connection();
$result = @pg_query($dbconn, $query);
if (!$result) { bib_log_error("Couldn't execute query \"$query\"", $dbconn); return NULL; }
// return result
return pg_affected_rows($result);
}
//-----------------------------------------------------------------------------
// Do escaping of a string for inclusion in a DB query.
// This is just a wrapper for pg_escape_string, setting up a new DB connection if needed.
function bib_db_escape_string($string)
{
return pg_escape_string(bib_db_connection(), $string);
}
//-----------------------------------------------------------------------------
// Get array of category names.
// If anything goes wrong, just return empty array.
function bib_get_cat_names()
{
$result = bib_db_query("SELECT name,description FROM bib_cats ORDER BY cat_group, name");
if (!$result) $result = array();
return $result;
}
// Get array of category names for one group.
// If anything goes wrong, just return empty array.
function bib_get_cat_names_for_group($group)
{
$group = bib_db_escape_string($group);
$result = bib_db_query("SELECT name,description FROM bib_cats WHERE cat_group='$group' ORDER BY name");
if (!$result) $result = array();
return $result;
}
// Get array of category groups.
// If anything goes wrong, just return empty array.
function bib_get_cat_groups()
{
$result = bib_db_query("SELECT DISTINCT cat_group FROM bib_cats");
if (!$result) $result = array();
return $result;
}
//-----------------------------------------------------------------------------
// Construct an SQL clause with which to select bib items
// First argument is field.
// Subsequent arguments are possible values for field, e.g.:
// bib_build_sql_clause("type", "article", "inproceedings").
// If first argument is "!", clause is negated, e.g.:
// bib_build_sql_clause("type", "!", "techreport", "unpublished").
function bib_build_sql_clause($args)
{
global $bib_item_fields;
// check arguments for errors
if (count($args) == 0) {
bib_log_error("No field provided to construct SQL clause");
return "";
}
$field = $args[0];
if (!(in_array($field, $bib_item_fields) || $field == "cat" || $field == "key")) {
bib_log_error("No such field \"".$field."\"");
return "";
}
if (count($args) == 1) {
bib_log_error("Incomplete information to construct SQL clause");
return "";
}
// check for negation
if ($args[1] == "!") {
$negate = true;
$start = 2;
} else {
$negate = false;
$start = 1;
}
// collate elements of list to build clause
$n = count($args);
$clauses = array();
for ($i = $start; $i < $n; $i++) {
if ($field != "cat") {
$clauses []= "bib_items.$field='".bib_db_escape_string($args[$i])."'";
} else {
$clauses []= "position('|".bib_db_escape_string($args[$i])."|' in cats)>0";
}
}
// compose elements
$clause = implode(" OR ", $clauses);
if ($negate) $clause = "NOT(".$clause.")";
return $clause;
}
//-----------------------------------------------------------------------------
// Do some processing of the search string
function bib_build_search_info($search, $fields)
{
global $bib_item_fields;
global $bib_month_names;
global $bib_list_search_enabled;
global $bib_list_search_string;
global $bib_list_search_fields;
global $bib_list_search_words;
global $bib_list_search_preg;
global $bib_list_search_sql;
$search = trim($search);
if ($search == NULL || $search == "") {
$bib_list_enabled = false;
$bib_list_search_string = NULL;
$bib_list_search_fields = NULL;
$bib_list_search_words = NULL;
$bib_list_search_preg = NULL;
$bib_list_search_sql = NULL;
} else {
// set enabled flag
$bib_list_search_enabled = true;
// store search string
$bib_list_search_string = trim($search);
// store search fields
$bib_list_search_fields = $fields;
// separate search string into words - note more complex if there are "s
if (!strstr($bib_list_search_string, "\"")) {
$bib_list_search_words = explode(" ", $bib_list_search_string);
}
else {
// split up into blocks in/out-side quotes
$tmp = split("\"", trim($bib_list_search_string));
$n = count($tmp);
$bib_list_search_words = array();
for ($i = 0; $i <= ((int)($n/2)); $i++) {
// split odd elements of list - i.e. stuff outside quotes
$tmp2 = explode(" ", trim($tmp[2*$i]));
foreach ($tmp2 as $tmp3) if ($tmp3 != "") $bib_list_search_words[] = $tmp3;
// don't split even elements of list - i.e. stuff inside quotes
if (2*$i+1 < $n) if ($tmp[2*$i+1] != "") $bib_list_search_words[] = $tmp[2*$i+1];
}
}
// build regexp to search for *any* of these words (in html)
$bib_list_search_preg = "";
foreach ($bib_list_search_words as $word)
$bib_list_search_preg = ($bib_list_search_preg==""?"":$bib_list_search_preg."|").preg_quote(htmlentities($word, ENT_COMPAT, "UTF-8"), "/");
// build sql query to search for *all* of these words
$bib_list_search_sql = "";
foreach ($bib_list_search_words as $word) {
$clause = "key ~* '".bib_db_escape_string(preg_quote($word, "/"))."'";
foreach ($bib_item_fields as $field) {
if (in_array($field, $bib_list_search_fields)) {
if ($field == "month") {
$months = array();
foreach ($bib_month_names as $i => $month) if (stristr($month, $word)) $months[] = "month=$i";
if ($months) $clause = $clause." OR ".implode(" OR ", $months);
} else if ($field == "year") {
$clause = $clause." OR CAST($field AS TEXT) ~* '".bib_db_escape_string(preg_quote($word, "/"))."'";
} else {
$clause = $clause." OR $field ~* '".bib_db_escape_string(preg_quote($word, "/"))."'";
}
}
}
$bib_list_search_sql = ($bib_list_search_sql==""?"":$bib_list_search_sql." AND ")."(".$clause.")";
}
}
}
//-----------------------------------------------------------------------------
// Functions to access/modify bibliography items
//-----------------------------------------------------------------------------
// Fetch bibliography item.
// Returns array of fields.
// If anything goes wrong, return NULL.
function bib_fetch_item($key)
{
// check key is valid
if (!is_key_valid($key)) {
bib_log_error("Cannot fetch item \"$key\" - key is invalid");
return NULL;
}
// query database
$key = bib_db_escape_string($key);
$result = bib_db_query("SELECT * FROM bib_items WHERE key='$key';");
if (!$result) return NULL;
$item = $result[0];
// expand category information
$item["cats"] = explode("|", $item["cats"]);
return $item;
}
//-----------------------------------------------------------------------------
// Add a new bibliography item.
// Returns any error message, empty return denotes success.
function bib_add_item($key, $type)
{
global $bib_item_types;
// check key is valid
if (!is_key_valid($key)) {
return "Key \"$key\" is invalid";
}
// check type is present/valid
if (!in_array($type, $bib_item_types)) {
return "Invalid type \"$type\"";
}
// update db
$key = bib_db_escape_string($key);
$type = bib_db_escape_string($type);
$result = bib_db_update_query("INSERT INTO bib_items (key, type, month) VALUES ('$key', '$type', '13')");
if ($result != 1) {
return "Could not add item \"$key\" - duplicate key perhaps";
}
}
//-----------------------------------------------------------------------------
// Delete a bibliography item.
// Returns any error message, empty return denotes success.
function bib_delete_item($key)
{
// check key is valid
if (!is_key_valid($key)) {
return "Key \"$key\" is invalid";
}
// update db
$key = bib_db_escape_string($key);
$result = bib_db_update_query("DELETE FROM bib_items WHERE key='$key'");
if ($result != 1) {
return "Could not delete item \"$key\"";
}
}
// Rename a new bibliography item.
// Returns any error message, empty return denotes success.
function bib_rename_item($key, $newkey)
{
// check keys are valid
if (!is_key_valid($key)) {
return "Key \"$key\" is invalid";
}
if (!is_key_valid($newkey)) {
return "New key \"$newkey\" is invalid";
}
// update db
$key = bib_db_escape_string($key);
$newkey = bib_db_escape_string($newkey);
$result = bib_db_update_query("UPDATE bib_items SET key='$newkey' WHERE key='$key'");
if ($result != 1) {
return "Could not rename item \"$key\" to \"$newkey\" - duplicate key perhaps";
}
}
//-----------------------------------------------------------------------------
// Update bibliography item.
// Takes array of fields $item.
// This should also include an index "key" with the key.
// Category information can also be included as an array under index "cats".
// Returns any error message, empty return denotes success.
function bib_update_item($item)
{
global $bib_item_fields;
// check key is present and valid
if (!array_key_exists("key", $item)) {
return "No key supplied";
}
if (!is_key_valid($item["key"])) {
return "Key \"".$item["key"]."\" is invalid";
}
// do some checks on fields supplied
if (array_key_exists("year", $item)) if ($item["year"]) if (!is_numeric($item["year"])) return "Invalid year";
if (array_key_exists("month", $item)) if ($item["month"]) if (!is_numeric($item["month"])) return "Invalid month";
// build update query
$set = "";
// go through all the normal fields
foreach ($item as $item_key => $item_val) {
if (in_array($item_key, $bib_item_fields)) {
if ($set != "") $set .= ", ";
if (is_string($item_val)) $item_val = bib_db_escape_string($item_val);
if (($item_key == "month" || $item_key == "year") && $item_val == "") {
$set .= "$item_key=NULL";
} else {
$set .= "$item_key='$item_val'";
}
}
}
// then add category info
if (array_key_exists("cats", $item)) {
if ($set != "") $set .= ", ";
if ($item["cats"]) {
foreach ($item["cats"] as $i => $cat) if (is_string($cat)) $item["cats"][$i] = bib_db_escape_string($cat);
$set .= "cats='|".implode("|", $item["cats"])."|'";
} else {
$set .= "cats=''";
}
}
// build/execute whole query
if (strlen($set) > 0) {
$item["key"] = bib_db_escape_string($item["key"]);
$result = bib_db_update_query("UPDATE bib_items SET $set WHERE key='".$item["key"]."'");
if ($result != 1) {
return "Could not update item \"$key\"";
}
}
}
//-----------------------------------------------------------------------------
// Gets title of bibliography item.
// We only really need a function for this because of "inbook"'s use of the "title" field
function bib_get_title($item)
{
if (!$item) return "";
return ($item["type"] != "inbook") ? $item["title"] : $item["chapter"];
}
//-----------------------------------------------------------------------------
// Display bibliography item.
// Item details passed in in array $item.
// Link for each item (optional) is given by $link, where %k expands to the key.
function bib_display_item($item, $link = NULL, $indent = 0)
{
global $bib_month_names;
global $bib_list_search_enabled;
global $bib_list_search_fields;
global $bib_list_search_preg;
global $bib_list_item_extra_links;
$tabs = str_repeat("\t", $indent);
// html-ise everything
// (note: some fields, e.g. abstract, links, are html anyway and shouldn't be html-ised again,
// but none of those are displayed by this function)
// also convert months to month names
foreach ($item as $index => $val) {
if ($index !== "month") {
if (is_string($val)) $item[$index] = htmlentities($val, ENT_COMPAT, "UTF-8");
} else {
$item[$index] = htmlentities($bib_month_names[$val], ENT_COMPAT, "UTF-8");
}
}
// remove any trailing full stop on "note" (will add anyway)
if (strlen($item["note"]) > 0) if (substr($item["note"], strlen($item["note"])-1) == ".") $item["note"] = substr($item["note"], 0, strlen($item["note"])-1);
// create search-highlighted version of $item
// (nb: don't just overwrite because for some things, e.g. anchors, links, we don't want it)
if ($bib_list_search_enabled) {
foreach ($item as $item_index => $item_val) {
if (in_array($item_index, $bib_list_search_fields))
$item_hi[$item_index] = preg_replace("/$bib_list_search_preg/i", "<span class=\"bib-highlight\">\\0</span>", $item_val);
else
$item_hi[$item_index] = $item_val;
}
}
else {
$item_hi = $item;
}
// key
echo "$tabs<a name=\"".$item["key"]."\"></a>\n";
echo "$tabs<span class=\"bib-key\">[".$item_hi["key"]."]</span>\n";
// author
echo "$tabs<span class=\"bib-auth\">";
if ($item_hi["author"])
echo $item_hi["author"].".";
if (($item["type"] == "book" || $item["type"] == "proceedings") && $item_hi["editor"]) {
if ($item_hi["author"]) echo " ";
echo $item_hi["editor"]." (editor";
if (strstr($item_hi["editor"], " and ")) echo "s";
echo "),";
}
echo "</span>\n";
// title
$title = ($item["type"] != "inbook") ? $item_hi["title"] : $item_hi["chapter"];
if ($link) {
$link = preg_replace("/%k/", $item["key"], $link);
echo "$tabs<span class=\"bib-title\"><a href=\"$link\">".$title."</a>.</span>\n";
} else {
echo "$tabs<span class=\"bib-title\">".$title.".</span>\n";
}
// src
$src = "";
switch ($item["type"]) {
case "inproceedings":
$src .= "In ";
if ($item_hi["editor"]) {
$src .= $item_hi["editor"]." (editor";
if (strstr($item_hi["editor"], " and ")) $src .= "s";
$src .= "), ";
}
$src .= "<em>".$item_hi["booktitle"]."</em>";
if ($item_hi["volume"] && $item_hi["series"]) $src .= ", volume ".$item_hi["volume"]." of ".$item_hi["series"];
if ($item_hi["pages"]) $src .= ", pages ".$item_hi["pages"];
if ($item_hi["organization"]) $src .= ", ".$item_hi["organization"];
if ($item_hi["publisher"]) $src .= ", ".$item_hi["publisher"];
if ($item_hi["note"]) $src .= ". ".$item_hi["note"];
break;
case "article":
$src .= "<em>".$item_hi["journal"]."</em>";
if ($item_hi["volume"]) $src .= ", ".$item_hi["volume"];
if ($item_hi["number"]) $src .= "(".$item_hi["number"].")";
if ($item_hi["pages"]) $src .= ", pages ".$item_hi["pages"];
if ($item_hi["publisher"]) $src .= ", ".$item_hi["publisher"];
if ($item_hi["note"]) $src .= ". ".$item_hi["note"];
break;
case "book":
case "proceedings":
if ($item_hi["volume"] && $item_hi["series"]) $src .= "Volume ".$item_hi["volume"]." of ".$item_hi["series"].". ";
if ($item_hi["publisher"]) $src .= $item_hi["publisher"];
if ($item_hi["note"]) $src .= ". ".$item_hi["note"];
break;
case "inbook":
$src .= "In ";
if ($item_hi["editor"]) {
$src .= $item_hi["editor"]." (editor";
if (strstr($item_hi["editor"], " and ")) $src .= "s";
$src .= "), ";
}
$src .= "<em>".$item_hi["title"]."</em>";
if ($item_hi["volume"] && $item_hi["series"]) $src .= ", volume ".$item_hi["volume"]." of ".$item_hi["series"];
if ($item_hi["pages"]) $src .= ", pages ".$item_hi["pages"];
if ($item_hi["organization"]) $src .= ", ".$item_hi["organization"];
if ($item_hi["publisher"]) $src .= ", ".$item_hi["publisher"];
if ($item_hi["note"]) $src .= ". ".$item_hi["note"];
break;
case "phdthesis":
$src .= ($item_hi["type2"] ? $item_hi["type2"] : "Ph.D. thesis").", ".$item_hi["school"];
if ($item_hi["note"]) $src .= ". ".$item_hi["note"];
break;
case "mastersthesis":
$src .= ($item_hi["type2"] ? $item_hi["type2"] : "Masters thesis").", ".$item_hi["school"];
if ($item_hi["note"]) $src .= ". ".$item_hi["note"];
break;
case "techreport":
$src .= ($item_hi["type2"] ? $item_hi["type2"] : "Technical report")." ".$item_hi["number"].", ".$item_hi["institution"];
if ($item_hi["note"]) $src .= ". ".$item_hi["note"];
break;
case "unpublished":
if ($item_hi["note"]) $src .= $item_hi["note"];
break;
default:
bib_log_error("Unknown bibliography item type \"".$item["type"]."\"");
break;
}
if ($src) echo "$tabs<span class=\"bib-src\">$src.</span>\n";
// date
echo "$tabs<span class=\"bib-date\">";
if ($item_hi["month"]) echo "<span class=\"bib-month\">".$item_hi["month"]." </span>";
echo "<span class=\"bib-year\">".$item_hi["year"].".</span></span>\n";
// files
$item_files = bib_get_item_files($item);
//if (count($item_files) > 0) {
echo "$tabs<span class=\"bib-files\">\n";
foreach ($item_files as $item_file) {
echo "$tabs\t[<a href=\"".$item_file["url"]."\">".$item_file["ext"]."</a>]\n";
}
echo "$tabs</span>\n";
//}
// url
if ($item_hi["url"]) {
echo "$tabs<span class=\"bib-url\"><a href=\"".$item_hi["url"]."\">".$item_hi["url"]."</a></span>\n";
}
// print any extra links requested
if ($bib_list_item_extra_links) {
echo "\t<span class=\"bib-extralinks\">\n";
foreach ($bib_list_item_extra_links as $link_name => $link_url) {
echo "\t\t[<a href=\"".preg_replace("/%k/", $item["key"], $link_url)."\">".$link_name."</a>]\n";
}
echo "\t</span>\n";
}
// print synopsis
if ($item_hi["synopsis"]) {
echo "$tabs<span class=\"bib-synop\">[".$item_hi["synopsis"]."]</span>\n";
}
}
function bib_display_item_from_key($key, $link = NULL, $indent = 0)
{
$item = bib_fetch_item($key);
if ($key != NULL && $key != "") bib_display_item($item, $link, $indent);
}
//-----------------------------------------------------------------------------
// Display bibliography item.
// Item details passed in in array $item.
// Link for each item (optional) is given by $link, where %k expands to the key.
function bib_display_item_detailed($item, $link = NULL, $indent = 0)
{
global $bib_images_dir;
global $bib_images_url;
$table_alt = 2;
echo "<div class=\"bibbox\"><table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n";
echo "\n<tr class=\"bibbox-row-alt$table_alt\">\n<td class=\"bibbox-td\">\n";
echo "<div class=\"bibbox-details\">\n";
bib_display_item($item, "");
echo "</div>\n";
$item_files = bib_get_item_files($item);
if ($item_files) if (count($item_files) > 0) {
echo "</td>\n</tr>\n";
echo "\n<tr class=\"bibbox-row-alt".($table_alt = 3 - $table_alt)."\">\n<td class=\"bibbox-td\">\n";
echo "<div class=\"bibbox-downloads\">\n";
echo "<b>Downloads:</b>\n";
foreach ($item_files as $item_file) {
switch ($item_file["ext"]) {
case "ps": $image = "ps.png"; break;
case "ps.gz": $image = "ps.png"; break;
case "pdf": $image = "pdf.png"; break;
case "bib": $image = "tex.png"; break;
default: $image = "file.gif"; break;
}
echo " ";
echo "<a href=\"".$item_file["url"]."\">";
echo "<img border=\"0\" src=\"".$bib_images_url."/$image\" alt=\"".$item_file["ext"]."\" style=\"vertical-align:middle;\">";
echo "</a> ";
echo "<b>".$item_file["ext"]."</b> ";
if (array_key_exists("filesize", $item_file)) if ($item_file["filesize"]) echo "(".$item_file["filesize"].") ";
echo "\n";
}
echo "</div>";
}
$notes = array();
if (array_key_exists("links", $item)) if ($item["links"]) {
// replace citations of form [] with links
$thispage = htmlspecialchars($_SERVER["PHP_SELF"]);
$item["links"] = preg_replace("/\[([A-Za-z0-9_\-+]+)\]/", "[<a href=\"$thispage?key=\\1\">\\1</a>]", $item["links"]);
$notes[] = $item["links"];
}
if (array_key_exists("publisher", $item)) if (substr_count($item["publisher"], "Springer")) {
$title = ($item["type"] != "inbook") ? $item["title"] : $item["chapter"];
$url = "https://link.springer.com/search?query=".rawurlencode($title);
$notes[] = "The original publication is available at <a href=\"".$url."\">link.springer.com</a>.";
}
if ((array_key_exists("series", $item) && (strcasecmp($item["series"], "Electronic Notes in Theoretical Computer Science") === 0 || strcasecmp($item["series"], "ENTCS") === 0))
|| (array_key_exists("journal", $item) && (strcasecmp($item["journal"], "Electronic Notes in Theoretical Computer Science") === 0 || strcasecmp($item["journal"], "ENTCS") === 0))) {
$notes[] = "ENTCS is available at <a href=\"http://www.sciencedirect.com/science/journal/15710661\">www.sciencedirect.com/science/journal/15710661</a>.";
}
if (count($notes) > 0) {
echo "</td>\n</tr>\n";
echo "\n<tr class=\"bibbox-row-alt".($table_alt = 3 - $table_alt)."\">\n<td class=\"bibbox-td\">\n";
echo "<div class=\"bibbox-notes\">\n";
echo "<b>Notes:</b>\n";
foreach ($notes as $note) {
echo $note."\n";
}
echo "</div>";
}
/*if (array_key_exists("links", $item)) if ($item["links"]) {
echo "</td>\n</tr>\n";
echo "\n<tr class=\"bibbox-row-alt".($table_alt = 3 - $table_alt)."\">\n<td class=\"bibbox-td\">\n";
echo "<div class=\"bibbox-links\">\n";
// replace citations of form [] with links
$thispage = htmlspecialchars($_SERVER["PHP_SELF"]);
$item["links"] = preg_replace("/\[([A-Za-z0-9_\-+]+)\]/", "[<a href=\"$thispage?key=\\1\">\\1</a>]", $item["links"]);
echo $item["links"]."\n";
echo "</div>";
}*/
echo "</td>\n</tr>\n";
echo "\n<tr class=\"bibbox-row-alt".($table_alt = 3 - $table_alt)."\">\n<td class=\"bibbox-td\">\n";
echo "<div class=\"bibbox-searches\">\n";
echo "<b>Links:</b>\n";
$title = ($item["type"] != "inbook") ? $item["title"] : $item["chapter"];
$url = "http://www.google.com/search?q=\"".rawurlencode($title)."\"&ie=UTF-8&oe=UTF-8&hl=en&btnG=Search";
echo "[<a href=\"".htmlentities($url, ENT_COMPAT, "UTF-8")."\">Google</a>]\n";
$url = "http://scholar.google.com/scholar?q=\"".rawurlencode($title)."\"&ie=UTF-8&oe=UTF-8&hl=en&btnG=Search";
echo "[<a href=\"".htmlentities($url, ENT_COMPAT, "UTF-8")."\">Google Scholar</a>]\n";
//$url = "http://www.google.com/search?q=site:citeseer.ist.psu.edu+".rawurlencode($title)."&ie=UTF-8&oe=UTF-8&hl=en&btnG=Search";
//echo "[<a href=\"".htmlentities($url, ENT_COMPAT, "UTF-8")."\">CiteSeer</a>]\n";
echo "</div>";
if (array_key_exists("url", $item)) if ($item["url"]) {
echo "</td>\n</tr>\n";
echo "\n<tr class=\"bibbox-row-alt".($table_alt = 3 - $table_alt)."\">\n<td class=\"bibbox-td\">\n";
echo "<div class=\"bibbox-url\">\n";
echo "<b>Available from:</b> \n";
echo "<a href=\"".htmlentities($item["url"], ENT_COMPAT, "UTF-8")."\">".htmlentities($item["url"], ENT_COMPAT, "UTF-8")."</a>\n";
echo "</div>";
}
if (array_key_exists("abstract", $item)) if ($item["abstract"]) {
echo "</td>\n</tr>\n";
echo "\n<tr class=\"bibbox-row-alt".($table_alt = 3 - $table_alt)."\">\n<td class=\"bibbox-td\">\n";
echo "<div class=\"bibbox-abstract\">\n";
if ($item["type"] == "book" || $item["type"] == "inbook")
if (file_exists($bib_images_dir."/".$item["key"].".gif"))
echo "<img style=\"border:0;float:left;margin:0px 10px 10px 10px;\" src=\"".$bib_images_url."/bib/".$item["key"].".gif\" alt=\"Front cover\">\n";
if (file_exists($bib_images_dir."/".$item["key"].".jpg"))
echo "<img style=\"border:0;float:left;margin:0px 10px 10px 10px;\" src=\"".$bib_images_url."/bib/".$item["key"].".jpg\" alt=\"Front cover\">\n";
if (file_exists($bib_images_dir."/".$item["key"].".png"))
echo "<img style=\"border:0;float:left;margin:0px 10px 10px 10px;\" src=\"".$bib_images_url."/bib/".$item["key"].".png\" alt=\"Front cover\">\n";
echo "<b>Abstract.</b>\n".$item["abstract"]."\n";
echo "</div>\n";
}
echo "</td>\n</tr>\n\n</table></div>\n";
}
//-----------------------------------------------------------------------------
// Get local files associated with bibliography item
// ($item only actually needs to contain "filename" and "key")
// (can specify if bibtex should be included too)
function bib_get_item_files($item, $bibtex = true)
{
global $bib_files_dir;
global $bib_bibtex_dir;
global $bib_files_url;
global $bib_bibtex_url;
$item_files = array();
if (array_key_exists("filename", $item)) if ($item["filename"]) {
foreach (array("ps", "ps.gz", "pdf") as $ext) {
$filename = "$bib_files_dir/".$item["filename"].".$ext";
if (file_exists($filename)) {
$url = $bib_files_url."/".$item["filename"].".$ext";
$filesize = filesize($filename)/1024;
$filesize = ($filesize > 1024) ? (round($filesize/1024, 2)." MB") : (round($filesize)." KB");
$item_files []= array("filename"=>$filename, "url"=>$url, "ext"=>$ext, "filesize"=>$filesize);
}
}
}
if ($bibtex) {
$filename = "$bib_bibtex_dir/".$item["key"].".bib";
if (file_exists($filename)) {
$url = $bib_bibtex_url."/".$item["key"].".bib";
$item_files []= array("filename"=>$filename, "url"=>$url, "ext"=>"bib");
}
}
return $item_files;
}
//-----------------------------------------------------------------------------
// Main API for creating/displaying lists of bibliography items
//-----------------------------------------------------------------------------
// Create a new list of bibliography items.
function bib_new_list($search = NULL, $fields = NULL)
{
global $bib_list_sections;
global $bib_list_search_fields_default;
$bib_list_sections = array();
bib_new_section();
// if no list of search fields specified, use some sensible defaults
if ($fields == NULL) $fields = $bib_list_search_fields_default;
// if search info provided, do some processing of it
if ($search != NULL) if ($search != "") bib_build_search_info($search, $fields);
}
// Create a new section within the current list of bibliography items.
function bib_new_section($header = "", $headerlink = "")
{
global $bib_list_sections;
// initialise section:
// select is "false" - section is initially empty
// list of sort operations is empty
$section = array("header"=>$header, "headerlink"=>$headerlink, "select"=>"false", "sortby_list"=>array());
$bib_list_sections []= $section;
}
//-----------------------------------------------------------------------------
// Add to the current list of bibliography items.
// First argument is field to select on.
// Subsequent arguments are possible values for field, e.g.:
// bib_add("type", "article", "inproceedings").
// If first argument is "!", selection is negated, e.g.:
// bib_add("type", "!", "techreport", "unpublished").
function bib_add()
{
global $bib_list_sections;
// construct corresponding sql clause
$clause = bib_build_sql_clause(func_get_args());
// modify select info for current section
$section = &$bib_list_sections[count($bib_list_sections)-1];
$section["select"] = "(".$section["select"].") OR (".$clause.")";
}
//-----------------------------------------------------------------------------
// Add to the current list of bibliography items based on an sql expression.
function bib_add_sql($sql)
{
global $bib_list_sections;
// modify select info for current section
$section = &$bib_list_sections[count($bib_list_sections)-1];
$section["select"] = "(".$section["select"].") OR (".$sql.")";
}
//-----------------------------------------------------------------------------
// Add all bibliography items to the current list.
function bib_add_all()
{
global $bib_list_sections;
// modify select info for current section
$section = &$bib_list_sections[count($bib_list_sections)-1];
$section["select"] = "true";
}
//-----------------------------------------------------------------------------
// Filter the current list of bibliography items.
// First argument is field to filter on.
// Subsequent arguments are possible values for field, e.g.:
// bib_filter("type", "article", "inproceedings").
// If first argument is "!", filter is negated, e.g.:
// bib_filter("type", "!", "techreport", "unpublished").
function bib_filter()
{
global $bib_list_sections;
// construct corresponding sql clause
$clause = bib_build_sql_clause(func_get_args());
// modify select info for current section
$section = &$bib_list_sections[count($bib_list_sections)-1];
$section["select"] = "(".$section["select"].") AND (".$clause.")";
}
//-----------------------------------------------------------------------------
// Filter the current list of bibliography items based on an sql expression.
function bib_filter_sql($sql)
{
global $bib_list_sections;
// modify select info for current section
$section = &$bib_list_sections[count($bib_list_sections)-1];
$section["select"] = "(".$section["select"].") AND (".$sql.")";
}
//-----------------------------------------------------------------------------
// Sort the current list of bibliography items.