-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.php
452 lines (439 loc) · 16.8 KB
/
build.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
<?php
/**
* Génère les formats détachés et le site statique basique sur Dramacode
*/
// cli usage
Dramacode::deps();
set_time_limit(-1);
if (php_sapi_name() == "cli") {
Dramacode::cli();
}
class Dramacode
{
static $sets = array(
"bibdramatique" => array(
"glob" => '../bibdramatique/*_*.xml',
"publisher" => "CELLF, Bibliothèque dramatique",
"identifier" => "http://bibdramatique.paris-sorbonne.fr/%s",
"source" => "http://dramacode.github.io/bibdramatique/%s.xml",
),
"moliere" => array(
"glob" => '../moliere/*_*.xml',
"publisher" => 'OBVIL, projet Molière',
"identifier" => "http://obvil.paris-sorbonne.fr/corpus/moliere/%s",
"source" => "http://dramacode.github.io/moliere/%s.xml",
),
// en dernier, ne doit pas écraser les collections ci-dessus
"tc" => array(
"glob" => '../tcp5/*_*.xml',
"publisher" => "Théâtre Classique",
"identifier" => "http://theatre-classique.fr/pages/programmes/edition.php?t=../documents/%s.xml",
"source" => "http://dramacode.github.io/tcp5/%s.xml",
),
);
static $formats = array(
'epub' => array( "ext"=>'.epub', "mime"=>"application/epub+zip", "title"=>"Livre électronique" ),
'kindle' => array( "ext"=>'.mobi', "mime"=>"application/x-mobipocket-ebook", "title"=>"Livre électronique" ),
'markdown' => array( "ext"=>'.txt', "mime"=>"text/markdown; charset=UTF-8", "title"=>"Texte brut" ),
'iramuteq' => array( "ext"=>'.txt', "mime"=>"text/plain; charset=UTF-8", "title"=>"Texte brut avec métadonnées au format Iramuteq" ),
'naked' => array( "ext"=>'.txt', "mime"=>"text/plain; charset=UTF-8", "label"=>"paroles", "title"=>"Texte dit, sans didascalies ou intitulés structurants" ),
'txm' => array( "ext"=>'.xml', "mime"=>"text/xml; charset=UTF-8", "label"=>"TXM", "title"=>"Texte structuré en répliques, sans didascalies" ),
'html' => array( "ext"=>'.html', "mime"=>"text/html; charset=UTF-8", "title"=>"Page web complète avec table des matières"),
'article' => array( "ext"=>'.html', "mime"=>"text/html; charset=UTF-8", "label"=>"fragment html", "title"=>"Page web insérable dans un site web"),
);
/** petite base sqlite pour conserver la mémoire des doublons etc */
static $create = "
PRAGMA encoding = 'UTF-8';
PRAGMA page_size = 8192;
CREATE TABLE play (
-- une pièce
id INTEGER, -- rowid auto
setcode TEXT, -- code de set
code TEXT, -- nom de fichier sans extension
filemtime INTEGER, -- date de dernière modification du fichier pour update
publisher TEXT, -- nom de l’institution qui publie
identifier TEXT, -- uri on publisher
source TEXT, -- XML TEI refercenced URI
creator TEXT, -- auteur
title TEXT, -- titre
date INTEGER, -- année, généralement la publication papier est la seule date sûre
acts INTEGER, -- nombre d’actes, essentiellement 5, 3, 1 ; ajuster pour les prologues
verse BOOLEAN, -- uniquement si majoritairement en vers, ne pas cocher si chanson mêlée à de la prose
genrecode TEXT, -- comedy|tragedy
genre TEXT, -- genre tel que dans le titre “Comédie héroïque avec danse et musique”
PRIMARY KEY(id ASC)
);
CREATE UNIQUE INDEX play_code ON play(code);
CREATE INDEX play_creator_date ON play(creator, date, title);
CREATE INDEX play_date_creator ON play(date, creator, title);
CREATE INDEX play_setcode ON play(setcode);
";
/** Lien à une base SQLite */
public $pdo;
/** Requête d’insertion d’une pièce */
private $_insert;
/** Test de date d’une pièce */
private $_sqlmtime;
/** Pièce XML/TEI en cours de traitement */
private $_dom;
/** Processeur xpath */
private $_xpath;
/** Processeur xslt */
private $_xslt;
/** Vrai si dépendances vérifiées et chargées */
private static $_deps;
/** A logger, maybe a stream or a callable, used by self::log() */
private static $_logger;
/** Log level */
public static $debug = true;
/**
* Constructeur de la base
*/
public function __construct( $sqlitefile, $logger="php://output" ) {
if (is_string($logger)) $logger = fopen($logger, 'w');
self::$_logger = $logger;
$this->connect($sqlitefile);
// create needed folders
foreach (self::$formats as $format => $row) {
if (!file_exists($dir = dirname(__FILE__).'/'.$format)) {
mkdir($dir, 0775, true);
@chmod($dir, 0775); // let @, if www-data is not owner but allowed to write
}
}
}
/**
*
*/
/**
* Produire les exports depuis le fichier XML
*/
public function add( $srcfile, $setcode=null, $priority=0 ) {
$set = self::$sets[$setcode];
if (!isset($set['predir'])) $set['predir'] = ''; // used for Théâtre Classique
$srcname = pathinfo($srcfile, PATHINFO_FILENAME);
$srcmtime = filemtime($srcfile);
$this->_sqlmtime->execute(array($srcname));
list( $basemtime ) = $this->_sqlmtime->fetch();
// TODO optimize on dates
$teinte = new Teinte_Doc($srcfile);
// file already exist, negative priority for this one, do nothing
if ( $basemtime && $priority < 0 );
// priority > 0 or newer, force update
if ( $basemtime < $srcmtime || $priority > 0) $this->insert($teinte, $setcode);
// Specific Théâtre Classique
if ($setcode == 'tc') {
$teinte->pre(dirname(__FILE__).'/tc-norm.xsl');
}
$echo = "";
foreach (self::$formats as $format => $row) {
$dir = $set['predir'].$format;
$destfile = dirname(__FILE__).'/'.$dir.'/'.$srcname.$row["ext"];
if ( !file_exists($destfile) ); // work to be done
else if ( $priority < 0 ) continue; // low priority, do not replace
else if ( $priority > 0 ); // force, do work
else if ( $srcmtime < filemtime($destfile) ) continue; // freshness, no work
// delete destfile if exists ?
$echo .= " ".$format;
// TODO git $destfile
if ($format == 'html') $teinte->html($destfile, 'http://oeuvres.github.io/Teinte/');
else if ($format == 'article') $teinte->article($destfile);
else if ($format == 'markdown') $teinte->markdown($destfile);
else if ($format == 'iramuteq') $teinte->iramuteq($destfile);
else if ($format == 'naked') $teinte->naked($destfile);
else if ($format == 'txm') $teinte->txm($destfile);
else if ($format == 'epub') {
$livre = new Livrable_Tei2epub($teinte->dom(), self::$_logger);
$livre->epub($destfile);
}
// mobi sould be done after epub
if ($format == 'kindle') {
$epubfile = dirname(__FILE__).'/'.$set['predir'].'epub/'.$srcname.".epub";
$mobifile = dirname(__FILE__).'/'.$set['predir'].'kindle/'.$srcname.".mobi";
Livrable_Tei2epub::mobi($epubfile, $mobifile);
}
else if ($format == 'docx') {
$echo .= " docx";
Toff_Tei2docx::docx($srcfile, $destfile);
}
}
if ($echo) self::log(E_USER_NOTICE, $srcfile.$echo."\n");
}
/**
* Insertion de la pièce
*/
private function insert( $teinte, $setcode ) {
// supprimer la pièce, des triggers doivent normalement supprimer la cascade.
$this->pdo->exec("DELETE FROM play WHERE code = ".$this->pdo->quote($teinte->filename()));
// globa TEI meta
$meta = $teinte->meta();
// acts
$xpath = $teinte->xpath();
$meta['acts'] = $xpath->evaluate("count(/*/tei:text/tei:body//tei:*[@type='act'])");
if (!$meta['acts']) $meta['acts'] = $xpath->evaluate("count(/*/tei:text/tei:body/*[tei:div|tei:div2])");
if (!$meta['acts']) $meta['acts'] = 1;
// verse
$l = $xpath->evaluate("count(//tei:sp/tei:l)");
$p = $xpath->evaluate("count(//tei:sp/tei:p)");
if ($l > 2*$p) $meta['verse'] = true;
else if ($p > 2*$l) $meta['verse'] = false;
else $meta['verse'] = null;
// genre
$genre = $genrecode = null;
$nl = $xpath->evaluate("/*/tei:teiHeader//tei:term[@type='genre']");
if ($nl->length) {
$n = $nl->item(0);
$genrecode = $n->getAttribute ('subtype');
$genre = $n->nodeValue;
}
if (!isset(self::$sets[$setcode]['identifier']))
$identifier = null;
else if ( $setcode == 'tc' )
$identifier = sprintf ( self::$sets[$setcode]['identifier'], strtoupper( $teinte->filename() ) );
else
$identifier = sprintf ( self::$sets[$setcode]['identifier'], $teinte->filename() );
$values = array(
$setcode,
$teinte->filename(),
$teinte->filemtime(),
self::$sets[$setcode]['publisher'],
$identifier,
sprintf ( self::$sets[$setcode]['source'], $teinte->filename() ),
$meta['byline'],
$meta['title'],
$meta['date'],
$meta['acts'],
$meta['verse'],
$genrecode,
$genre,
);
$this->_insert->execute( $values );
}
/**
* Ligne bibliographique pour une pièce
*/
public function bibl($play) {
if (is_string($play)) {
$playcode = $this->pdo->quote($playcode);
$play = $this->pdo->query("SELECT * FROM play WHERE code = $playcode")->fetch();
}
$bibl = $play['creator'].', '.$play['title'].' ('.$play['date'];
if ($play['genre'] == 'tragedy') $bibl .= ', tragédie';
else if ($play['genre'] == 'comedy') $bibl .= ', comédie';
$bibl .= ', '.$play['acts'].(($play['acts']>2)?" actes":" acte");
$bibl .= ', '.(($play['verse'])?"vers":"prose");
$bibl .= ')';
return $bibl;
}
/**
* Sortir le catalogue en table html
*/
public function table() {
echo '<table class="sortable">
<thead>
<tr>
<th>N°</th>
<th>Éditeur</th>
<th>Auteur</th>
<th>Date</th>
<th>Titre</th>
<th>Genre</th>
<th>Actes</th>
<th>Vers</th>
<th>Téléchargements</th>
</tr>
</thead>
';
$i = 1;
foreach ($this->pdo->query("SELECT * FROM play ORDER BY creator, date") as $play) {
$set = self::$sets[$play['setcode']];
echo "\n <tr>\n";
echo " <td>$i</td>\n";
if ($play['identifier']) echo ' <td><a href="'.$play['identifier'].'">'.$play['publisher']."</a></td>\n";
else echo ' <td>'.$play['publisher']."</td>\n";
echo ' <td>'.$play['creator']."</td>\n";
echo ' <td>'.$play['date']."</td>\n";
if ($play['identifier']) echo ' <td><a href="'.$play['identifier'].'">'.$play['title']."</a></td>\n";
else echo ' <td>'.$play['title']."</td>\n";
echo ' <td>';
if ($play['genrecode'] == 'tragedy') echo 'Tragédie';
else if ($play['genrecode'] == 'comedy') echo 'Comédie';
else echo $play['genre'];
echo " </td>\n";
echo " <td>".$play['acts']."</td>\n";
echo " <td>".(($play['verse'])?"vers":"prose")."</td>\n";
// downloads
echo ' <td>';
if ($play['source']) echo '<a href="'.$play['source'].'">TEI</a>';
$sep = ", ";
foreach ( self::$formats as $key=>$format ) {
if (isset($set['predir'])) $dir = $set['predir'].$key;
else $dir = $key;
if ( !isset( $format['label'] ) ) $format['label'] = $key;
echo $sep.'<a title="'.$format['title'].'" type="'.$format['mime'].'" href="'.$dir.'/'.$play['code'].$format["ext"].'">'.$format['label'].'</a>';
}
echo " </td>\n";
echo " </tr>\n";
$i++;
}
echo "\n</table>\n";
}
/**
* Sortir le catalogue en table html
*/
public function csv() {
$i = 1;
echo "CODE\tAUTEU\tDATE\tTITRE\tGENRE\tVERS\tACTES\tÉDITION\n";
foreach ($this->pdo->query("SELECT * FROM play ORDER BY creator, date") as $play) {
$set = self::$sets[$play['setcode']];
echo $play['code'];
echo "\t";
echo $play['creator'];
echo "\t";
echo $play['date'];
echo "\t";
echo $play['title'];
echo "\t";
if ($play['genrecode'] == 'tragedy') echo 'Tragédie';
else if ($play['genrecode'] == 'comedy') echo 'Comédie';
else echo $play['genre'];
echo "\t";
echo $play['verse']?"vers":"prose";
echo "\t";
echo $play['acts'];
echo "\t";
echo $play['identifier'];
echo "\n";
$i++;
}
}
/**
* Connexion à la base
*/
private function connect($sqlite) {
$dsn = "sqlite:" . $sqlite;
// si la base n’existe pas, la créer
if (!file_exists($sqlite)) {
if (!file_exists($dir = dirname($sqlite))) {
mkdir($dir, 0775, true);
@chmod($dir, 0775); // let @, if www-data is not owner but allowed to write
}
$this->pdo = new PDO($dsn);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
@chmod($sqlite, 0775);
$this->pdo->exec(Dramacode::$create);
}
else {
$this->pdo = new PDO($dsn);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
// table temporaire en mémoire
$this->pdo->exec("PRAGMA temp_store = 2;");
$this->_insert = $this->pdo->prepare("
INSERT INTO play (setcode, code, filemtime, publisher, identifier, source, creator, title, date, acts, verse, genrecode, genre)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
");
$this->_sqlmtime = $this->pdo->prepare("SELECT filemtime FROM play WHERE code = ?");
}
static function deps() {
if(self::$_deps) return;
// Deps
$inc = dirname(__FILE__).'/../Livrable/Tei2epub.php';
if (!file_exists($inc)) {
echo "Impossible de trouver ".realpath(dirname(__FILE__).'/../')."/Livrable/
Vous pouvez le télécharger sur https://github.com/oeuvres/Livrable\n";
exit();
}
else {
include_once($inc);
}
$inc = dirname(__FILE__).'/../Teinte/Doc.php';
if (!file_exists($inc)) {
echo "Impossible de trouver ".realpath(dirname(__FILE__).'/../')."/Teinte/
Vous pouvez le télécharger sur https://github.com/oeuvres/Teinte\n";
exit();
}
else {
include_once($inc);
}
self::$_deps=true;
}
/**
* Custom error handler
* May be used for xsl:message coming from transform()
* To avoid Apache time limit, php could output some bytes during long transformations
*/
static function log( $errno, $errstr=null, $errfile=null, $errline=null, $errcontext=null) {
$errstr=preg_replace("/XSLTProcessor::transform[^:]*:/", "", $errstr, -1, $count);
if ($count) { // is an XSLT error or an XSLT message, reformat here
if(strpos($errstr, 'error')!== false) return false;
else if ($errno == E_WARNING) $errno = E_USER_WARNING;
}
// not a user message, let work default handler
else if ($errno != E_USER_ERROR && $errno != E_USER_WARNING && $errno != E_USER_NOTICE ) return false;
// a debug message in normal mode, do nothing
if ($errno == E_USER_NOTICE && !self::$debug) return true;
if (!self::$_logger);
else if (is_resource(self::$_logger)) fwrite(self::$_logger, $errstr);
else if ( is_string(self::$_logger) && function_exists(self::$_logger)) call_user_func(self::$_logger, $errstr);
}
static function epubcheck($glob) {
echo "epubcheck epub/*.epub\n";
foreach(glob($glob) as $file) {
echo $file;
// validation
$cmd = "java -jar ".dirname(__FILE__)."/epubcheck/epubcheck.jar ".$file;
$last = exec ($cmd, $output, $status);
echo ' '.$status."\n";
if ($status) rename($file, dirname($file).'/_'.basename($file));
}
}
/**
* Command line API
*/
static function cli() {
$timeStart = microtime(true);
$usage = "\n usage : php -f ".basename(__FILE__)." base.sqlite set\n";
array_shift($_SERVER['argv']); // shift first arg, the script filepath
$sqlite = 'dramacode.sqlite';
// pas d’argument, on démarre sur les valeurs par défaut
if (!count($_SERVER['argv'])) {
$base = new Dramacode($sqlite, STDERR);
$filelist = array();
foreach(self::$sets as $setcode=>$setrow) {
echo $setcode."\n";
if (!is_array( $setrow['glob'] )) $setrow['glob']=array( $setrow['glob'] );
foreach( $setrow['glob'] as $glob ) {
foreach( glob($glob) as $file ) {
// attention au "livret" de Molière
if ( strpos( $file, "-livret") !== false ) continue;
$filename = basename( $file );
// first win
if ( isset( $filelist[$filename] ) ) continue;
$filelist[$filename] = true;
$base->add( $file, $setcode);
}
}
}
ob_start();
include( 'index.php' );
$html = ob_get_contents();
ob_end_clean();
file_put_contents( "index.html", $html );
exit();
}
if ($_SERVER['argv'][0] == 'epubcheck') {
Dramacode::epubcheck('epub/*.epub');
exit();
}
// des arguments, on joue plus fin
$base = new Dramacode($sqlite, STDERR);
if (!count($_SERVER['argv'])) exit("\n Quel set insérer ?\n");
$setcode = array_shift($_SERVER['argv']);
foreach(split(" ", self::$sets[$setcode]['glob']) as $glob) {
foreach(glob($glob) as $file) {
$base->add($file, $setcode);
}
}
}
}
?>