-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBibitemDetailedPrinter.php
316 lines (284 loc) · 11.3 KB
/
BibitemDetailedPrinter.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
<?php
if (!defined('MEDIAWIKI'))
die();
/**
* BibitemCompactPrinter
*
* @addtogroup Extensions
* @package Bibwiki
*
* @author Wolfgang Plaschg <[email protected]>
* @copyright Copyright (C) 2007 Wolfgang Plaschg
*
* @link http://www.plaschg.net/bibwiki Homepage
* @link http://www.plaschg.net/bibwiki/docs Code documentation
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*/
require_once(dirname( __FILE__ ) ."/Bibitem.php");
require_once(dirname( __FILE__ ) ."/Misc.php");
/**
* Prettyprints a given Bibitem depending of the bibliographic type.
*/
class BibitemDetailedPrinter {
/**
* The bibitem
*
* @var object
*/
var $mBibitem;
/**
* Stores the query string (Usually this is "f=<filename>").
* Needed for building the hyperlinks of authors, titles etc.
*
* @var string
*/
var $mBibfileQuery;
/**
* @var array
*/
var $mWikiKeys;
function BibitemDetailedPrinter($bibfileQuery="", $wikikeys=array()) {
$this->mBibfileQuery = $bibfileQuery;
$this->mWikiKeys = $wikikeys;
}
/**
* The main routine. Returns a string with the prettyprinted bibitem.
*
* @param object $this->mBibitem
* @return string
*/
function prettyPrint($bibitem) {
$this->mBibitem = $bibitem;
#$type = mb_strtolower($this->mBibitem->getType());
#if ($type == "book") return $this->printBook();
#elseif ($type == "article") return $this->printArticle();
#elseif ($type == "incollection") return $this->printIncollection();
#elseif ($type == "inbook") return $this->printInbook();
return $this->printMisc();
}
/**
* Split and wrap an entry of a BibTeX record.
* @todo rewrite
*/
function splitAndWrapLine($keynr) {
global $wgBreakLines, $wgLineBreakAt;
#Load Settings
if (file_exists(dirname( __FILE__ ) . '/BibwikiSettings.Default.php'))
include( dirname( __FILE__ ) . '/BibwikiSettings.Default.php' );
if (file_exists(dirname( __FILE__ ) . '/BibwikiSettings.php'))
include( dirname( __FILE__ ) . '/BibwikiSettings.php' );
$key = $this->mBibitem->getKey($keynr);
$pre = "";
$middle = "";
$post = "";
$rv = array();
if ($wgBreakLines == false) {
# just reformat with given linebreaks
$lines = $this->mBibitem->getPrettyVal($keynr);
$lines = explode("\n", $lines);
$linecnt = 0;
foreach($lines as $line) {
if ($line != "")
{
$linecnt++;
if ($linecnt == 1) {
$middle .= trim($line, " \n\r\t");
}
else
$middle .= sprintf("\n %-12s %s", "", trim($line, " \n\r\t,"));
}
}
}
else {
# complete reformat -- wrap lines longer than $wgLineBreakAt
$value = $this->mBibitem->getPrettyVal($keynr);
$sep = " ";
if (strtolower($key)=="url" and strpos($value, "/") > 0) {
$chunks = preg_split('/(\/)/', $value, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$sep = "";
}
else {
$chunks = preg_split('/\s+/', $value, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$sep = " ";
}
$line = "";
$linecnt = 0;
$chunkcnt = 0;
foreach ($chunks as $chunk) {
$chunkcnt++;
if ($line <> "" and
bwStrlenWithoutHTML($line.$sep.$chunk) > $wgLineBreakAt) {
$linecnt++;
if ($linecnt == 1)
$middle = trim($line, " \n\r\t,");
else
$middle .= sprintf("\n %-12s %s", "", trim($line, " \n\r\t,"));
$line = "";
}
if ($chunkcnt == 1)
$line = $chunk;
else
$line .= $sep.$chunk;
}
$linecnt++;
if ($linecnt == 1)
$middle = trim($line, " \n\r\t,");
else
$middle .= sprintf("\n %-12s %s", "", trim($line, " \n\r\t,"));
}
$delims = Bibitem::getDelimiters($this->mBibitem->getVal($keynr));
$pre = sprintf(" %-12s = %s", $key, $delims["left"]);
$post = sprintf("%s,\n", $delims["right"]);
return array($pre, $middle, $post);
}
function printMisc() {
global $wgRequest, $wgDownloadsUrl, $wgISBNLinkTags, $wgTitleLink,
$wgAuthorLink, $wgISBNLink, $wgUser, $wgURLReverseReplacements,
$wgValueDelimRight, $wgTitleLinkTags;
#LoadSettings
if (file_exists(dirname( __FILE__ ) . '/BibwikiSettings.Default.php'))
include( dirname( __FILE__ ) . '/BibwikiSettings.Default.php' );
if (file_exists(dirname( __FILE__ ) . '/BibwikiSettings.php'))
include( dirname( __FILE__ ) . '/BibwikiSettings.php' );
$output .= # $this->mBibfile->getPosition()."/".$bibs_printed.": ". #debug
"@".$this->mBibitem->getType().'{<a name="'.$this->mBibitem->getCiteKey().'" />';
/* NS_BIB if ($page_exists["Bib:".$this->mBibitem->getCiteKey()] > 0)
$output .= $wgUser->getSkin()->makeKnownLink("Bib:".$this->mBibitem->getCiteKey(), $this->mBibitem->getCiteKey());
else
$output .= $wgUser->getSkin()->makeBrokenLink("Bib:".$this->mBibitem->getCiteKey(), $this->mBibitem->getCiteKey());
*/
if ($this->mWikiKeys[$this->mBibitem->getCiteKey()] > 0)
$output .= $wgUser->getSkin()->makeKnownLink($this->mBibitem->getCiteKey(), $this->mBibitem->getCiteKey());
else
$output .= $wgUser->getSkin()->makeBrokenLink($this->mBibitem->getCiteKey(), $this->mBibitem->getCiteKey());
$output .= ",\n";
for($keynr=0; $keynr < $this->mBibitem->getValueCount(); $keynr++) {
$key = $this->mBibitem->getKey($keynr);
list($pre, $val, $post) = $this->splitAndWrapLine($keynr);
#$wgOut->addHTML($pre);
#$wgOut->addHTML($val);
#$wgOut->addHTML($post."<br/>");
if (bwStrEqual($key, "crossref") and $val != "") {
$output .= $pre.'<a href="'.Bibliography::getLocalURL(array('view=detailed', 'startkey='.$val, $this->mBibfileQuery)).'">'.$val.'</a>'.$post;
}
elseif (bwStrEqual($key, "doi") and $val != "") {
$output .= $pre.'<a target="doi" href="http://dx.doi.org/'.$val.'">'.$val.'</a>'.$post;
}
elseif (bwStrEqual($key, "arxiv") and $val != "") {
$output .= $pre.'<a target="arxiv" href="http://arxiv.org/abs/'.$val.'">'.$val.'</a>'.$post;
}
elseif (bwStrEqual($key, "isbn") and $val != "" and !empty($wgISBNLinkTags)) {
$post = trim($post, "\n");
$isbn = str_replace("-", "", $val);
$linktags = array();
foreach($wgISBNLinkTags as $t) {
$href = str_replace("\$isbn", $isbn, $t["href"]);
$href = str_replace("\$self", Bibliography::getLocalURL(), $href);
$linktags[] = '<span class="bibeditsection"><a href="'.$href.'" target="'.$t["target"].'">'.$t["text"].'</a></span>';
}
$linktags = implode(' <span class="bibeditsection">|</span> ', $linktags);
if ($wgISBNLink != "") {
$href = str_replace("\$isbn", $isbn, $wgISBNLink);
$output .= $pre.'<a class="invisible" href="'.$href.'" target="aleph">'.$val.'</a>'.$post.' '.$linktags."\n";
}
else {
$output .= $pre.$val.$post.' '.$linktags."\n";
}
}
elseif ((bwStrEqual($key, "author") or bwStrEqual($key, "editor"))
and $val != "" and $wgAuthorLink != ""
) {
$authors = explode(" and", $val);
foreach ($authors as $a) {
$a = trim($a);
if ($a != "others" and $a != "") {
$pos = strpos($val, $a);
$len = strlen($a);
$as = $a;
#$as = str_replace("~", " ", $as);
$as_parts = bwParseAuthor($as);
#$as = str_replace("{", "", $as);
#$as = str_replace("}", "", $as);
$no_utf8_as = urlencode($as);
$utf8_as = urlencode($as);
$href = str_replace('$author', $no_utf8_as, $wgAuthorLink);
$href = str_replace('$utf8_author', $utf8_as, $href);
$href = str_replace('$firstname_initial', $as_parts["firstname_initial"], $href);
$href = str_replace('$firstnames_initials', $as_parts["firstnames_initials"], $href);
$href = str_replace('$firstname_normalized', $as_parts["firstname_simplified"], $href);
$href = str_replace('$firstnames_normalized', $as_parts["firstnames_simplified"], $href);
$href = str_replace('$middlepart_normalized', $as_parts["middlepart_simplified"], $href);
$href = str_replace('$surname_normalized', $as_parts["surname_simplified"], $href);
$href = str_replace('$firstname', $as_parts["firstname"], $href);
$href = str_replace('$middlepart', $as_parts["middlepart"], $href);
$href = str_replace('$surname', $as_parts["surname"], $href);
$val = substr_replace($val, '<a href="'.$href.'" target="aleph">'.$a.'</a>', $pos, $len);
}
}
$output .= $pre.$val.$post;
}
elseif ((bwStrEqual($key, "keywords") or bwStrEqual($key, "tags")) and $val != "") {
$keywords = preg_split("/(\s+)/", $val, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$m = "";
foreach ($keywords as $keyword) {
if (preg_match("/\s+/", $keyword))
$m .= $keyword;
else {
$keyword = str_replace('<', "", $keyword);
$keyword = str_replace('>', "", $keyword);
$keyword = trim($keyword, " ,;<>");
if ($keyword != "") {
$m .= '<a class="invisible" href="'.
Bibliography::getLocalURL(
array('action='.$wgRequest->getVal("action"),
'keyword='.$keyword,
$this->mBibfileQuery
)
).'">'.$keyword.'</a>';
}
}
}
$output .= $pre.$m.$post;
}
elseif (bwStrEqual($key, "title") and $val != "" and $wgTitleLink != "") {
$val_with_reduced_spaces = preg_replace("/\s+/", " ", $val);
$val_with_reduced_spaces = bwRemoveHTML($val_with_reduced_spaces);
$as = urlencode($val_with_reduced_spaces);
$utf8_as = urlencode(bwHTMLDecode($val_with_reduced_spaces));
$href = str_replace('$title', $as, $wgTitleLink);
$href = str_replace('$utf8_title', $utf8_as, $href);
$output .= $pre.'<a href="'.$href.'" target="aleph">'.$val.'</a>'.$post;
}
elseif (bwStrEqual($key, "url") and $val != "") {
$url = $val;
# delete indent spaces of wrapped urls
$url = bwRemoveHTML($url);
$url = preg_replace("/\\n\s+/", "", $url);
foreach($wgURLReverseReplacements as $from => $to) {
$url = preg_replace($from, $to, $url);
}
$output .= $pre.'<a target="extern" class="invisible" href="'.$url.'">'.$val.'</a>'.$post;
}
elseif ((bwStrEqual($key, "docname") or
bwStrEqual($key, "file") or
bwStrEqual($key, "pdf")) and $val != ""
and $wgDownloadsUrl != "") {
$post = trim($post, "\n");
$url = $val;
$url = bwRemoveHTML($url);
# reverse the replacements
foreach($wgURLReverseReplacements as $from => $to)
$url = str_replace($from, $to, $url);
$url = preg_replace("/\s{2,}/", " ", $url);
$js_url = str_replace("'", "\\'", $url);
$rename_link = '';
if (Bibliography::userIsAllowedToEdit())
$rename_link = ' <span class="bibeditsection">[<span class="renamesection" onClick="return rename(\''.$js_url.'\',\''.$this->mBibitem->getCiteKey().'\',\''.$keycounter[$this->mBibitem->getCiteKey()].'\');">'.wfMsg('bibwiki_rename').'</span>]</span>';
$output .= $pre.'<a class="invisible" target="docname" href="'.$wgDownloadsUrl.'/'.$url.'">'.$val.'</a>'.$post.$rename_link."\n";
}
else
$output .= $pre.$val.$post;
}
return $output;
}
}