-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibbrowser.php
173 lines (140 loc) · 5.36 KB
/
bibbrowser.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
<?php
defined("PmWiki") or die;
$RecipeInfo['BibBrowse']['Version'] = '2022-12-07';
@define("BIBBROWSE_SORT", [
"academic" => "bibBrowseAcademicOrder"
]);
@define("BIBBROWSE_DEFAULT_SORT", "bibBrowseAcademicOrder");
@define("BIBBROWSE_TYPE_WEIGHTS", [
"book" => 10,
"phdthesis" => 20,
"proceedings" => 30,
"booklet" => 40,
"inbook" => 50,
"article" => 60,
"incollection" => 70,
"inproceedings" => 80,
"manual" => 90,
"mastersthesis" => 90,
"techreport" => 90,
"unpublished" => 100
]);
@define("BIBBROWSE_MONTH_WEIGHTS", [
"jan" => 1, "january" => 1, "1" => 1,
"feb" => 2, "february" => 2, "2" => 2,
"mar" => 3, "march" => 3, "3" => 3,
"apr" => 4, "april" => 4, "4" => 4,
"may" => 5, "may" => 5, "5" => 5,
"jun" => 6, "june" => 6, "6" => 6,
"jul" => 7, "july" => 7, "7" => 7,
"aug" => 8, "august" => 8, "8" => 8,
"sep" => 9, "september" => 9, "9" => 9,
"oct" => 10, "october" => 10, "10" => 10,
"nov" => 11, "november" => 11, "11" => 11,
"dec" => 12, "december" => 12, "12" => 12
]);
@define("BIBBROWSE_DEFAULT_WEIGHT", 100);
@define("BIBBROWSE_HTML_PREFIX_LIST", '<ol class="bibbrowse-list">');
@define("BIBBROWSE_HTML_POSTFIX_LIST", "</ol>");
@define("BIBBROWSE_HTML_PREFIX_ENTRY", '<li class="text-justify">');
@define("BIBBROWSE_HTML_POSTFIX_ENTRY", "</li>");
@define("BIBBROWSE_HTML_SEPARATOR", "");
global $db;
if (!$db) {
// Initialize
$db = new BibDataBase();
Markup('bibtexbrowseyear', '<split','/\\(:bibyear (\\d{4}):\\)/', 'bibYear');
Markup('bibtexbrowserel', '<split','/\\(:bibyear ([-+]?[0-9]{1,2}):\\)/', 'bibYearRel');
Markup('bibtexbrowselast', '<split','/\\(:biblast (\\S+) ([0-9]{1,2}):\\)/', 'bibLast');
Markup('bibtexbrowseauthor', '<split','/\\(:bibauth (\\S+):\\)/', 'bibAuthor');
Markup('bibtexbrowseeditor', '<split','/\\(:bibedit (\\S+):\\)/', 'bibEditor');
Markup('bibtexbrowseauthtype','<split','/\\(:bibauth (\\S+) (\\S+):\\)/', 'bibAuthor');
Markup('bibtexbrowsetype', '<split','/\\(:bibtype (\\S+):\\)/', 'bibType');
Markup('bibtexbrowser', '<split','/\\(:bib (.*):\\)/', 'bibQuery');
}
if (!function_exists('bibBrowsePrint')) {
function bibBrowsePrint($entries)
{
return BIBBROWSE_HTML_PREFIX_LIST
. implode(
BIBBROWSE_HTML_SEPARATOR,
array_map(function ($entry) {
return BIBBROWSE_HTML_PREFIX_ENTRY . $entry->toHTML() . BIBBROWSE_HTML_POSTFIX_ENTRY;
}, $entries)
)
. BIBBROWSE_HTML_POSTFIX_LIST;
}
}
function bibBrowseAcademicOrder($a, $b)
{
$r = (($a->getYear() < $b->getYear()) ? 1 : (($a->getYear() > $b->getYear()) ? -1 : 0));
if ($r == 0) $r = ((getTypeWeight($a) < getTypeWeight($b)) ? -1 : ((getTypeWeight($a) > getTypeWeight($b)) ? 1 : 0));
if ($r == 0) $r = ((getMonthWeight($a) < getMonthWeight($b)) ? 1 : ((getMonthWeight($a) > getMonthWeight($b)) ? -1 : 0));
if ($r == 0) $r = ((getDayWeight($a) < getDayWeight($b)) ? 1 : ((getDayWeight($a) > getDayWeight($b)) ? -1 :0));
if ($r == 0) $r = strcmp($a->getFormattedAuthorsString(), $b->getFormattedAuthorsString());
return $r;
}
function getTypeWeight($e) {
return BIBBROWSE_TYPE_WEIGHTS[$e->getType()] ?? BIBBROWSE_DEFAULT_WEIGHT;
}
function getMonthWeight($e) {
return BIBBROWSE_MONTH_WEIGHTS[strtolower($e->getField('Month'))] ?? 0;
}
function getDayWeight($e) {
return ($e->hasField('Day') ? intval($e->getField('Day')) : 0);
}
function loadBib(string $bib) {
global $db;
$db->load($bib);
}
function bibYear($m) {
$entries = search(array('year'=>$m[1]));
return Keep(bibBrowsePrint($entries));
}
function bibYearRel($m) {
$entries = search(array('year'=>date("Y") + $m[1]));
return Keep(bibBrowsePrint($entries));
}
function bibLast($m) {
$entries = search(array('author'=>$m[1], 'limit'=>$m[2]));
return Keep(bibBrowsePrint($entries));
}
function bibAuthor($m) {
$entries = search(array('author'=>$m[1], 'type'=>(isset($m[2]) ? $m[2] : '.*')));
return Keep(bibBrowsePrint($entries));
}
function bibEditor($m) {
$entries = search(array('editor'=>$m[1], 'type'=>(isset($m[2]) ? $m[2] : '.*')));
return Keep(bibBrowsePrint($entries));
}
function bibType($m) {
$entries = search(array('type'=>$m[1]));
return Keep(bibBrowsePrint($entries));
}
function bibQuery($m) {
$entries = search(query($m));
return Keep(bibBrowsePrint($entries));
}
function query($m) {
preg_match_all("/\s*([a-z]+)\s*=\s*\"([^\"]+)\"/i", $m[1], $query);
$query = array_combine($query[1], $query[2]);
$yearOffset = [];
if (isset($query['year']) && preg_match('/(\s*[\+|\-]\d+)/', $query['year'], $yearOffset)) {
$query['year'] = date("Y") + $yearOffset[0];
}
return $query;
}
function search($query) {
global $db;
$limit = $query['limit'] ?? 0;
unset($query['limit']);
$sort = BIBBROWSE_SORT[$query['sort'] ?? null] ?? BIBBROWSE_DEFAULT_SORT;
unset($query['sort']);
$entries = $db->multisearch($query);
uasort($entries, $sort);
if ($limit > 0) {
$partioned_entries = array_chunk($entries, $limit, true);
$entries = $partioned_entries[0];
}
return $entries;
}