-
Notifications
You must be signed in to change notification settings - Fork 2
/
makedb.php
77 lines (70 loc) · 2.34 KB
/
makedb.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
<?php
/**
* Parse BibTeX objects and write them to a SQLite database.
*
* @package ExternBib
* @author Olaf Lenz
* @author Michael Kuron
* @author Jean-Noël Grad
* @copyright 2011-2014,2016,2022 The Authors
* @license https://opensource.org/licenses/BSD-3-Clause New BSD License
* @link https://github.com/olenz/externbib
*/
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'include' . DIRECTORY_SEPARATOR . 'PEAR.php');
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'include' . DIRECTORY_SEPARATOR . 'tex2html.php');
require_once("bibdb.php");
/**
* Parse a BibTeX file and populate its content in the default table of a
* SQLite database. LaTeX math, macros and escape sequences are converted
* to HTML on a "best effort" basis.
*
* @param SQLite3 $db The SQLite database.
* @param mixed $bibfile The BibTeX file path or file handle.
* @param Structures_BibTex $bibparser The BibTeX parser.
* @param int $modes LaTeX-to-HTML conversions to apply.
* @param bool $verbose Whether to print progress to screen.
* @return void
*/
function populate_db($db, $bibfile, $bibparser, $modes, $verbose) {
$filedesc = "file object";
if (is_string($bibfile)) {
$filedesc = $bibfile;
} elseif (is_resource($bibfile)) {
$filedesc = "file handle";
}
if ($verbose) {
echo "Loading $filedesc...\n";
}
$ret = $bibparser->loadFile($bibfile);
if (PEAR::isError($ret)) {
die($ret->getMessage());
}
if ($verbose) {
echo "Parsing $filedesc...\n";
}
$bibparser->parse();
if ($bibparser->hasWarning()) {
foreach ($bibparser->warnings as $warning) {
error_log("WARNING: " . $warning['warning'] . " (line \"" . $warning['entry'] . "\")");
}
}
if ($verbose) {
echo "Found " . $bibparser->amount() . " entries.\n";
}
foreach ($bibparser->data as $entry) {
// create associative array
$key = $entry['cite'];
// UTF8-ify and trim entry
$cleanentry = array();
foreach ($entry as $k=>$v) {
$v = trim($v);
if ($modes != 0 && $v != "" && $k != "fullEntry" && $k != "url" && $k != "eprint" && $k != "e-print") {
$v = convert_latex_string($v, $modes);
}
$cleanentry[$k] = $v;
}
$value = serialize($cleanentry);
bibdb_insert($key, $value, $db);
}
}
?>