-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bibfile.php
562 lines (497 loc) · 14.4 KB
/
Bibfile.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
<?php
if (!defined('MEDIAWIKI'))
die();
/**
* Bibfile
*
* @addtogroup Extensions
* @package Bibwiki
*
* @link http://www.plaschg.net/bibwiki Homepage
* @link http://www.plaschg.net/bibwiki/docs Code documentation
* @author Wolfgang Plaschg <wpl@gmx.net>
* @copyright Copyright (C) 2007 Wolfgang Plaschg
*
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*/
include_once(dirname(__FILE__)."/Misc.php");
/**
* Class for accessing .bib files.
*/
class Bibfile {
var $mFilename;
var $mHandle;
var $mIsOpen;
var $mPosition;
var $mFilter;
var $mFilterArray;
var $mKeys;
var $mDoubleKeys;
var $mMacros;
/**
* insertNew() and saveChanges() store their generated BibTeX key
* here. This is needed for the "Location:" redirect.
*
* @access private
*/
var $mTmpCiteKey;
function getCiteKeyOfLastCommand() {
return $this->mTmpCiteKey;
}
function open() {
if ($this->mIsOpen)
$this->close();
$this->mPosition = 0;
$this->mKeys = array();
$this->mDoubleKeys = array();
$this->mIsOpen = false;
if (file_exists($this->getAbsoluteName()) == false)
return false;
if (is_readable($this->getAbsoluteName()) == false)
return false;
$this->mHandle = @fopen($this->getAbsoluteName(), "r");
if ($this->mHandle == false)
return false;
$this->mIsOpen = true;
return true;
}
static function checkBibfilename($filename) {
global $wgDefaultBib, $wgRequest, $wgUser, $wgBibPath;
#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' );
if ($filename == "") $filename = $wgDefaultBib;
if ($wgUser->isAnon() == false)
$userdir = "(".$wgUser->getName()."\\".DIRECTORY_SEPARATOR.")?";
if (preg_match("/^".$userdir.'[\w_\-]+\.bib$/', $filename) == false or
strlen($filename) > 255 or
file_exists(bwMakePath($wgBibPath, $filename)) == false or
is_readable(bwMakePath($wgBibPath, $filename)) == false
)
$filename = $wgDefaultBib;
return $filename;
}
function setFilter($filter="") {
$this->mFilter = bwDiacriticsSimplify($filter);
$this->mFilterArray = array();
if ($this->mFilter != "" and strpos($this->mFilter, " ") !== false)
$this->mFilterArray = explode(" ", $this->mFilter);
elseif ($this->mFilter != "")
$this->mFilterArray = array($this->mFilter);
}
function init($filename="", $filter="") {
$this->close();
$this->mHandle = 0;
$this->mIsOpen = false;
$this->mPosition = 0;
$this->mFilter = "";
$this->mFilterArray = array();
$this->mMacros = array();
$this->mKeys = array();
$this->mDoubleKeys = array();
$this->setFilter($filter);
$this->mFilename = $this->checkBibfilename($filename);
}
function close() {
if ($this->mIsOpen) {
$this->mPosition = 0;
$this->mKeys = array();
$this->mDoubleKeys = array();
@fclose($this->mHandle);
$this->mIsOpen = false;
}
}
/**
* Returns true if there are no more bibtex records that pass the filter.
*/
function nomoreFilteredItems() {
if (!$this->mIsOpen)
return true;
if (feof($this->mHandle)) return true;
$filepos = ftell($this->mHandle);
$counter = $this->mPosition;
$rv = ($this->nextFilteredRecord() === false);
fseek($this->mHandle, $filepos);
$this->mPosition = $counter;
return $rv;
}
function getName() {
return $this->mFilename;
}
function getAbsoluteName() {
global $wgBibPath;
#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' );
return bwMakePath($wgBibPath, $this->mFilename);
}
function getPosition() {
return $this->mPosition;
}
function isStartOfBibItem($s) {
if (strpos(trim($s), "@") === 0 and
stristr($s, "@String") === false and
stristr($s, "@Comment") === false and
stristr($s, "@Preamble") === false)
return true;
return false;
}
/**
* Extracts the BibTeX key from $s.
*
* @param string $s A plain BibTeX record
* @return string
*/
function parseCiteKey($s) {
$matches = array();
if (preg_match("/^\s*@\s*\w+\s*[{(]{1,1}\s*(.+)\s*,/U", $s, $matches) === false)
return false;
return $matches[1];
}
/**
* Reads the keys of a bibfile to $this->mKeys and double keys to
* $this->mDoubleKeys. Returns an array of all keys. Doesn't touch
* internal file handle.
*
* @return array
*/
function getKeys() {
if (count($this->mKeys) == 0) {
if ($fh = @fopen($this->getAbsoluteName(),'r')) {
while (!feof($fh)) {
$s = fgets($fh);
if ($this->isStartOfBibitem($s)) {
$key = $this->parseCiteKey($s);
if ($key != false and $key != "") {
if (isset($this->mKeys[$key]))
$this->mKeys[$key]++;
else
$this->mKeys[$key] = 1;
if ($this->mKeys[$key] > 1)
$this->mDoubleKeys[$key] = 1;
}
}
}
fclose($fh);
}
}
return array_keys($this->mKeys);
}
function getMacros() {
return $this->mMacros;
}
function getDoubleKeys() {
if (count($this->mKeys) == 0)
$this->getKeys();
return array_keys($this->mDoubleKeys);
}
/**
* parse a string definition in key/string pair
*
* @param string $s the string definition " @string{<key> = <string>} "
*/
function parseStringDefinition($s) {
$tmp = str_replace("@String", "", $s);
$tmp = trim($tmp, " \t\n\r");
$tmp = Bibitem::removeDelimiters($tmp);
$parts = explode('=', $tmp, 2);
if (count($parts) == 2) {
$key = trim($parts[0], " \t\n\r");
$val = trim($parts[1], " \t\n\r");
$val = Bibitem::removeDelimiters($val);
if ($key != "" and $val != "")
return array("key" => mb_strtolower($key), "string" => $val);
else
return false;
}
return false;
}
/**
* Gets the next unfiltered BibTeX record
*
* Doesn't increase $this->mPosition!
*
* @todo Parses string definitions too, but expects them to be in one line!
* @return array <code>array("key" => $key, "record" => $bibtexrecord)</code>
*/
function nextRecord() {
if (!$this->mIsOpen) return false;
$filepos = ftell($this->mHandle);
do {
# go to the the start of the next bibtex record
$s = fgets($this->mHandle);
if (feof($this->mHandle)) {
# leave everything untouched
fseek($this->mHandle, $filepos);
return false;
}
# if we stepped over a string defintion store it for future use
if (strpos(trim($s), "@String") === 0) {
/**
* @todo make better multi-line @string parsing
*/
$s = trim($s);
while (substr($s,-1) != "}") {
if (feof($this->mHandle)) return false;
$s .= fgets($this->mHandle);
$s = trim($s);
}
$strdef = $this->parseStringDefinition($s);
if (is_array($strdef)) {
$this->mMacros[$strdef["key"]] = $strdef["string"];
# set filepos after string defintion
$filepos = ftell($this->mHandle);
}
}
} while (!$this->isStartOfBibItem($s));
$key = $this->parseCiteKey($s);
if ($key === false) {
# no key, so leave everything untouched
fseek($this->mHandle, $filepos);
return false;
}
# ok, we have a bibtex record
# grab everything until the start of the end of the record,
# a single } or ) in a line
$record = $s;
do {
$filepos = ftell($this->mHandle);
$s = fgets($this->mHandle);
if (strpos(trim($s), "}") === 0 or
strpos(trim($s), ")") === 0) {
$record .= $s;
break;
}
/*
if ($this->isStartOfBibItem($s) or
strpos(trim($s), "@String") === 0) {
#ups, too far
fseek($this->mHandle, $filepos);
break;
}
*/
$record .= $s;
} while (!feof($this->mHandle));
return array("key" => $key, "record" => bwToUtf8($record));
}
/**
* Return a record from the file.
*/
function loadRecord($citekey, $nr=1) {
#start from beginning
if ($this->mIsOpen === true)
$this->close();
$this->open();
$found = false;
$key_cnt = 0;
$lwrcitekey = strtolower($citekey);
$rv = false;
do {
$rec = $this->nextRecord();
if ($rec === false) break;
if (bwStrEqual(strtolower($rec["key"]), $lwrcitekey)) {
#found
$key_cnt++;
if ($key_cnt == $nr) {
$rv = $rec["record"];
$found = true;
}
}
} while (!$found);
$this->close();
return $rv;
}
/**
* Returns true if $bibtexrecord passes through the filter.
*
* @param string $bibtexrecord a plain text bibtex record
* @return boolean
*/
function passThroughFilter($bibtexrecord) {
$found = true;
if (count($this->mFilterArray) > 0) {
$reduced_bibtexrecord = preg_replace("/\W+/", "", $bibtexrecord);
#takes too long $bibtexrecord = bwTeXToHTML($bibtexrecord);
#$bibtexrecord = bwToUtf8($bibtexrecord);
$bibtexrecord = bwDiacriticsSimplify($bibtexrecord);
foreach ($this->mFilterArray as $filter) {
$reduced_filter = preg_replace("/\W+/", "", $filter);
if ($filter <> "" and
stristr($bibtexrecord, $filter) === false and
stristr($reduced_bibtexrecord, $reduced_filter) === false
) {
$found = false;
break;
}
}
}
return $found;
}
/**
* Return the next bibtex record that passes the filter.
* Increases $this->mPosition.
*
* @return string The bibtex record
*/
function nextFilteredRecord() {
if ($this->mIsOpen === false) return false;
if (feof($this->mHandle)) return false;
do {
$data = $this->nextRecord();
if ($data == false) return false;
$passed = $this->passThroughFilter($data["record"]);
} while (!$passed);
$this->mPosition++;
return $data["record"];
}
function backup() {
global $wgKeepBackups, $wgBackupPath;
#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' );
if ($wgKeepBackups > 0) {
copy($this->getAbsoluteName(), bwMakePath($wgBackupPath, $this->getName()).".".(time()));
$this->removeOldBackups();
}
}
function removeOldBackups() {
global $wgBackupPath, $wgKeepBackups;
#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' );
$dir = dir(rtrim($wgBackupPath, DIRECTORY_SEPARATOR));
$files = array();
while (($f = $dir->read()) !== false) {
if ($f != "." and $f != "..") {
#truncate timestamp from filename
$fname = preg_replace("/\.\d+$/", "", $f);
# print "#".$fname."#<br>";
# print $f."<br>";
$files[$fname][] = $f;
}
}
# print "<br>====<br>";
$dir->close();
foreach($files as $f) {
# print count($f)."<br>";
rsort($f);
while(count($f) > $wgKeepBackups) {
$fn = array_pop($f);
# print $fn."<br>";
unlink(bwMakePath($wgBackupPath, $fn));
}
}
}
function formatRecordForWriting($record) {
$bibitem = new Bibitem;
$bibitem->set($record);
$bibitem->correctUserErrors();
if (!$bibitem->parse()) return false;
$record = $bibitem->formatForWriting($this->getKeys());
$this->mTmpCiteKey = $bibitem->getCiteKey();
return $record;
}
/**
* Inserts $record at the beginning of the file.
*
* @param string $record A plain BibTeX record.
* @return boolean Success status
*/
function insertRecord($record) {
global $wgRequest, $wgBackupPath, $wgKeepBackups,
$wgConvertAnsiToTeX;
#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' );
$in = @fopen($this->getAbsoluteName(),'r');
$out = @fopen($this->getAbsoluteName().".tmp",'w');
$record = $this->formatRecordForWriting($record);
if ($record == false) return false;
if ($in and $out) {
$inserted = false;
while (!feof($in)) {
$sz = fgets($in);
if (!$inserted and $this->isStartOfBibItem($sz)) {
fputs($out, $record."\n");
$inserted = true;
}
fputs($out, $sz);
}
if (!$inserted) fputs($out, $record."\n");
fclose($in);
fclose($out);
if (file_exists($this->getAbsoluteName().".bak"))
unlink($this->getAbsoluteName().".bak");
rename($this->getAbsoluteName(), $this->getAbsoluteName().".bak");
rename($this->getAbsoluteName().".tmp", $this->getAbsoluteName());
$this->backup();
}
else return false;
return true;
}
/**
* @todo Make this a method of Bibfile.
*/
function saveChanges($key, $keynr, $record) {
global $wgOut, $wgRequest, $wgBackupPath, $wgKeepBackups,
$wgConvertAnsiToTeX;
#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');
$in = @fopen($this->getAbsoluteName(), 'r');
$out = @fopen($this->getAbsoluteName().".tmp", 'w');
if ($record != "") {
$record = $this->formatRecordForWriting($record);
if ($record == false) return false;
}
if ($in and $out) {
$inserted = false;
$in_record = false;
$lwrkey = strtolower($key);
$tmplwrkey = "";
$tmpkeynr = 0;
while (!feof($in)) {
$sz = fgets($in);
if (!$inserted and
$this->isStartOfBibItem($sz)) {
$tmplwrkey = strtolower($this->parseCiteKey($sz));
if ($tmplwrkey == $lwrkey) {
$tmpkeynr++;
if ($tmpkeynr == $keynr) {
fputs($out, $record);
$inserted = true;
$in_record = true;
}
}
}
if ($in_record) {
if (trim($sz) == "}")
$in_record = false;
}
else
fputs($out, $sz);
}
fclose($in);
fclose($out);
if (file_exists($this->getAbsoluteName().".bak"))
unlink($this->getAbsoluteName().".bak");
rename($this->getAbsoluteName(), $this->getAbsoluteName().".bak");
rename($this->getAbsoluteName().".tmp", $this->getAbsoluteName());
$this->backup();
}
else return false;
}
}
?>