-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGalenus.php
210 lines (187 loc) · 6.78 KB
/
Galenus.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
<?php declare(strict_types=1);
/**
* Part of galenus-verbatim https://github.com/galenus-verbatim/galenus
* Copyright (c) 2021 Nathalie Rousseau
* MIT License https://opensource.org/licenses/mit-license.php
*/
/**
* This class requires verbatim, https://github.com/galenus-verbatim/varbatim
* Specific code for Galenus https://galenus-verbatim.huma-num.fr/
*/
declare(strict_types=1);
require_once(__DIR__ . '/vendor/autoload.php');
// require_once(dirname(__DIR__) . '/verbatim/Verbatim.php');
use Oeuvres\Kit\{Filesys, Log, Xt};
use GalenusVerbatim\Verbatim\{Verbatim};
Galenus::init();
class Galenus
{
/** configuration parameters */
static public $config = [
'win' => false,
];
/**
* Init static things
*/
static public function init()
{
$config_file = __DIR__ . '/config.php';
if (file_exists($config_file)) {
$conf = include($config_file);
self::$config = array_merge(self::$config, $conf);
}
}
static function zotero_xmlnorm($rdf_file)
{
// wash a bit rdf before
$xml = file_get_contents($rdf_file);
// Extra tag
$xml = preg_replace(
'@</dc:description>@',
"\n$0",
$xml,
);
$xml = preg_replace_callback(
array(
'@(CTS URN|Original Title|French Title|English Title|English Short Title) *: *(.*)@'
),
function ($matches) {
$tag = preg_replace('/ +/', '-', trim($matches[1]));
$tag = strtolower($tag);
return "<z:$tag>$matches[2]</z:$tag>";
},
$xml
);
// Entities in notes
$xml = preg_replace_callback(
array(
'@(<bib:Memo[^>]*>\s*<rdf:value>)([^<]*?)(</rdf:value>\s*</bib:Memo>)@',
),
function ($matches) {
// pb with double decoding
$html = str_replace(
['&lt;', '&gt;', '&gt;'],
['‹', '›', '&'],
$matches[2]
);
$html = html_entity_decode($html, ENT_QUOTES | ENT_SUBSTITUTE | ENT_XHTML);
$html = html_entity_decode($html);
return $matches[1] . $html . $matches[3];
},
$xml
);
return $xml;
}
/**
*
*/
static function zotero(
$rdf_file = __DIR__ . "/Galenus-verbatim.rdf",
$force = false
) {
if (!file_exists($rdf_file)) {
Log::error($rdf_file . " not found for a Zotero RDF export.");
return;
}
// test freshness
// this file knows last generation
$editiones_file = __DIR__ . "/html_cache/editiones.html";
if ($force) {
Log::info('Rebuild, force');
}
else if (!file_exists($editiones_file)) {
Log::info('New build, file not found ' . $editiones_file);
} else if (filemtime($editiones_file) < filemtime($rdf_file)) {
Log::info('Rebuild, new zotero rdf export');
} else if (filemtime($editiones_file) + 10 < filemtime(Verbatim::db_file())) {
Log::info('Rebuild, new verbapie database');
} else {
return; // OK
}
Log::info('Generate resources from Zotero rdf export ' . $rdf_file);
// normalize xml of html oddiities
$xml = self::zotero_xmlnorm($rdf_file);
// for debug, verify xml
file_put_contents($rdf_file .".xml", $xml);
$dom = Xt::loadXml($xml);
/* editiones */
$editiones = Xt::transformToXml(
__DIR__ . "/build/galenzot_editiones.xsl",
$dom
);
Verbatim::$pdo->beginTransaction();
$re = '@<section id="([^"]+)"[^>]*>.*?</section>@s';
preg_match_all($re, $editiones, $matches);
$cts = $matches[1];
$bibl = $matches[0];
$sql = "UPDATE edition SET bibl = ? WHERE cts = ?;";
$ins = Verbatim::$pdo->prepare($sql);
$sel = Verbatim::$pdo->prepare("SELECT COUNT(*) FROM edition WHERE cts = ?;");
for ($i = 0, $max = count($bibl); $i < $max; $i++) {
$sel->execute(array($cts[$i]));
list($num) = $sel->fetch();
if (!$num) {
Log::info('CTS not found in database: ' . $cts[$i]);
continue;
}
$ins->execute(array($bibl[$i], $cts[$i]));
}
Verbatim::$pdo->commit();
/* opera */
$html = Xt::transformToXml(
__DIR__ . "/build/galenzot_opera_navs.xsl",
$dom
);
Filesys::mkdir(__DIR__ . "/html_cache/");
file_put_contents(__DIR__ . "/html_cache/opera_navs.html", $html);
$opera_html = Xt::transformToXml(
__DIR__ . "/build/galenzot_opera_bib.xsl",
$dom
);
file_put_contents(__DIR__ . "/html_cache/opera_bib.html", $opera_html);
$html = Xt::transformToXml(
__DIR__ . "/build/galenzot_checks.xsl",
$dom
);
file_put_contents(__DIR__ . "/html_cache/zotero_checks.html", $html);
// load opus records
Log::info('Load database with Zotero records');
Verbatim::$pdo->exec("DELETE FROM opus;");
Verbatim::$pdo->beginTransaction();
$re = '@<section id="([^"]+)".*?</section>@s';
preg_match_all($re, $opera_html, $matches);
$cts = $matches[1];
$bibl = $matches[0];
$sql = "INSERT INTO opus (cts, bibl) VALUES (?, ?);";
$insOpus = Verbatim::$pdo->prepare($sql);
for ($i = 0, $max = count($bibl); $i < $max; $i++) {
$insOpus->execute(array($cts[$i], $bibl[$i]));
}
Verbatim::$pdo->commit();
Log::info('Database, optimize');
Verbatim::$pdo->exec("PRAGMA auto_vacuum = FULL");
// generate sitemap.xml
self::sitemap();
// finish with that, will be the timestamp
Filesys::mkdir(dirname($editiones_file));
file_put_contents($editiones_file, $editiones);
Log::info('End');
}
static function sitemap($sitemap_file = __DIR__ . '/sitemap.xml')
{
$stream = fopen($sitemap_file, "w");
$url_format = self::$config['url.doc'];
fwrite($stream, '<?xml version="1.0" encoding="UTF-8"?>' . "\n");
fwrite($stream, '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n");
$stmt = Verbatim::$pdo->prepare("SELECT cts FROM doc;");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
fwrite($stream,
' <url><loc>'
. sprintf($url_format, $row['cts'])
. '</loc></url>' . "\n"
);
}
fwrite($stream, '</urlset>' . "\n");
}
}