SimplexmlController.php 44.3 KB
Newer Older
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1
2
<?php

3
declare(strict_types=1);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
4

5
namespace App\Controller;
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
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

use App\Model\SolrDocument;
use DOMDocument;
use DOMXPath;
use League\Flysystem\Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\Routing\Annotation\Route;
use Solarium\Client;
use Symfony\Component\Finder\Finder;

class SimplexmlController extends AbstractController
{
    private Client $client;

    private ?string $teiDir = null;

    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    public function setConfigs(string $teiDir) {
        $this->teiDir = $teiDir;
    }

    public function deleteSolrIndex(): void
    {
        $update = $this->client->createUpdate();
        $update->addDeleteQuery('*:*');
        $update->addCommit();
        $this->client->execute($update);
    }

    private function getId(DOMXPath $xpath): string
    {
        $id = '';
        $idNode = $xpath->query('//tei:text/@xml:id');
        $id = $idNode->item(0)->nodeValue;

        return $id;
    }

    private function getShortTitle(DOMXPath $xpath): string
    {
        $shortTitleNodeList = $xpath->query('//tei:title[@type = "short"]/descendant::text()');
        $shortTitle = '';
        foreach ($shortTitleNodeList as $k => $shortTitleNode) {
            $shortTitle .= $shortTitleNode->data;
        }

        $shortTitle = trim(preg_replace('/\s+/', ' ', $shortTitle));

        return $shortTitle;
    }

    private function getTitle(DOMXPath $xpath): string
    {
        $titleNodeList = $xpath->query('//tei:title[@type = "desc"]/descendant::text()');
        $title = '';
        foreach ($titleNodeList as $k => $titleNode) {
            $title .= $titleNode->data;
        }

        $title = trim(preg_replace('/\s+/', ' ', $title));

        return $title;
    }

    private function getOriginPlace(DOMXPath $xpath): string
    {
        $originPlace = '';
        $originPlaceNode = $xpath->query('//tei:name[@type="place" and @subtype="orn"]');

        if ($originPlaceNode->item(0)) {
            $originPlace = $originPlaceNode->item(0)->nodeValue;
        }

        return $originPlace;
    }

    private function getAuthor(DOMXPath $xpath): string
    {
        $author = '';
        $authorNode = $xpath->query('//tei:name[@type="person" and @subtype="aut"]');
        if ($authorNode->item(0)) {
            $author = $authorNode->item(0)->nodeValue;
            $author = trim(preg_replace('/\s+/', ' ', $author));
        }

        return $author;
    }

    private function getRecipient(DOMXPath $xpath): string
    {
        $recipient = '';
        $recipientNode = $xpath->query('//tei:name[@type="person" and @subtype="rcp"]');
        if ($recipientNode->item(0)) {
            $recipient = $recipientNode->item(0)->nodeValue;
            $recipient = trim(preg_replace('/\s+/', ' ', $recipient));
        }

        return $recipient;
    }

    private function getDestinationPlace(DOMXPath $xpath): string
    {
        $destinationPlace = '';
        $destinationPlaceNode = $xpath->query('//tei:name[@type="place" and @subtype="dtn"]');
        if ($destinationPlaceNode->item(0)) {
            $destinationPlace = $destinationPlaceNode->item(0)->nodeValue;
        }

        return $destinationPlace;
    }

    private function getOriginDate(DOMXPath $xpath): string
    {
        $originDate = '';
        $originDateNode = $xpath->query('//tei:date[@type="orn"]/@ref');
        if ($originDateNode->item(0)) {
            $originDate = $originDateNode->item(0)->nodeValue;
        }

        return $originDate;
    }

    private function getLicense(DOMXPath $xpath): string
    {
        $license = '';
        $licenseNode = $xpath->query('//tei:licence');
        if ($licenseNode->item(0)) {
            $license = $licenseNode->item(0)->nodeValue;
        }

        return $license;
    }

    private function getLanguage(DOMXPath $xpath): string
    {
        $language = '';
        $languageNode = $xpath->query('//tei:text//@xml:lang');
        if ($languageNode->item(0)) {
            $language = $languageNode->item(0)->nodeValue;
        }

        return $language;
    }

    private function getReference(DOMXPath $xpath): string
    {
        $reference = '';
        $referenceNode = $xpath->query('//tei:relatedItem[@type = "letter" and @subtype = "related"]/tei:ref');
        if ($referenceNode->item(0)) {
            $reference = $referenceNode->item(0)->nodeValue;
            $reference = trim(preg_replace('/\s+/', ' ', $reference));
        }

        return $reference;
    }

    private function getResponse(DOMXPath $xpath): string
    {
        $response = '';
        $responseNode = $xpath->query('//tei:relatedItem[@type = "letter" and @subtype = "response"]/tei:ref');
        if ($responseNode->item(0)) {
            $response = $responseNode->item(0)->nodeValue;
            $response = trim(preg_replace('/\s+/', ' ', $response));
        }

        return $response;
    }

    private function getRelatedItems(DOMXPath $xpath): array
    {
        $relatedItems = [];
        $relatedItemNodes = $xpath->query('//tei:relatedItem[@type="letter" and not(@subtype)]//tei:ref');
        if (is_iterable($relatedItemNodes)) {
            foreach ($relatedItemNodes as $relatedItemNode) {
                if (!empty($relatedItemNode->nodeValue)) {
                    $relatedItem = trim(preg_replace('/\s+/', ' ', $relatedItemNode->nodeValue));
                    $relatedItems[] = $relatedItem;
                }
            }
        }

        return $relatedItems;
    }

    private function getRepository(DOMXPath $xpath): string
    {
        $repository = '';
        $repositoryNode = $xpath->query('//tei:repository');
        if ($repositoryNode->item(0)) {
            $repository = $repositoryNode->item(0)->nodeValue;
        }

        return $repository;
    }

    private function getInstitution(DOMXPath $xpath): string
    {
        $institution = '';
        $institutionNode = $xpath->query('//tei:institution');
        if ($institutionNode->item(0)) {
            $institution = $institutionNode->item(0)->nodeValue;
        }

        return $institution;
    }

    private function getSettlement(DOMXPath $xpath): string
    {
        $settlement = '';
        $settlementNode = $xpath->query('//tei:settlement');
        if ($settlementNode->item(0)) {
            $settlement = $settlementNode->item(0)->nodeValue;
        }

        return $settlement;
    }

    private function getCountry(DOMXPath $xpath): string
    {
        $country = '';
        $countryNode = $xpath->query('//tei:country');
        if ($countryNode->item(0)) {
            $country = $countryNode->item(0)->nodeValue;
        }

        return $country;
    }

    private function getSourceDescription(DOMXPath $xpath): string
    {
        $sourceDescription = '';
        $sourceDescriptionNodeList = $xpath->query('//tei:sourceDesc/descendant::text()');
        foreach ($sourceDescriptionNodeList as $sourceDescriptionNode) {
            $sourceDescription .= $sourceDescriptionNode->data;
        }

        if (!empty($sourceDescription)) {
            $sourceDescription = trim(preg_replace('/\s+/', ' ', $sourceDescription));
        }

        return $sourceDescription;
    }

    private function getPublicationDate(DOMXPath $xpath): string
    {
        $publicationDate = '';
        $publicationDateNode = $xpath->query('//date[@type = "orn"]');
        if ($publicationDateNode->item(0)) {
            $publicationDate = $publicationDateNode->item(0)->nodeValue;
        }

        return $publicationDate;
    }

    private function getFulltext(DOMXPath $xpath): string
    {
        $fulltext = '';
        $fulltextNodeList = $xpath->query('//tei:body//tei:div/descendant::text()');
        foreach ($fulltextNodeList as $fulltextNode) {
            $fulltext .= $fulltextNode->data;
        }

        if (!empty($fulltext)) {
            $fulltext = trim(preg_replace('/\s+/', ' ', $fulltext));
        }

        return $fulltext;
    }

    private function getNumberOfPages(DOMXPath $xpath): ?int
    {
        $numberOfPages = null;
        $numberOfPagesNode = $xpath->query('//tei:pb');
        if ($numberOfPagesNode->count()) {
            $numberOfPages = $numberOfPagesNode->count();
        }

        return $numberOfPages;
    }

    private function getGndKeywords(DOMXPath $xpath): array
    {
        $gndKeywords = [];
        $gndKeyWordNodes = $xpath->query('//tei:keywords[@scheme = "#gnd"]/tei:term');
        if (is_iterable($gndKeyWordNodes)) {
            foreach ($gndKeyWordNodes as $gndKeyWordNode) {
                if (!empty($gndKeyWordNode->nodeValue)) {
                    $gndKeyword = trim(preg_replace('/\s+/', ' ', $gndKeyWordNode->nodeValue));
                    $gndKeywords[] = $gndKeyword;
                }
            }
        }

        return $gndKeywords;
    }

    private function getFreeKeywords(DOMXPath $xpath): array
    {
        $freeKeywords = [];
        $freeKeyWordNodes = $xpath->query('//tei:keywords[@scheme = "frei"]/tei:term');
        if (is_iterable($freeKeyWordNodes)) {
            foreach ($freeKeyWordNodes as $freeKeyWordNode) {
                if (!empty($freeKeyWordNode->nodeValue)) {
                    $freeKeyword = trim(preg_replace('/\s+/', ' ', $freeKeyWordNode->nodeValue));
                    $freeKeywords[] = $freeKeyword;
                }
            }
        }

        $freeKeyWordNodes = $xpath->query('//tei:keywords[@scheme = "free"]/tei:term');
        if (is_iterable($freeKeyWordNodes)) {
            foreach ($freeKeyWordNodes as $freeKeyWordNode) {
                if (!empty($freeKeyWordNode->nodeValue)) {
                    $freeKeyword = trim(preg_replace('/\s+/', ' ', $freeKeyWordNode->nodeValue));
                    $freeKeywords[] = $freeKeyword;
                }
            }
        }

        return $freeKeywords;
    }

    private function getShelfmark(DOMXPath $xpath): string
    {
        $shelfmark = '';
        $shelfmarkNode = $xpath->query('//tei:idno');
        if ($shelfmarkNode->item(0)) {
            $shelfmark = trim(preg_replace('/\s+/', ' ', $shelfmarkNode->item(0)->nodeValue));
        }

        return $shelfmark;
    }

    private function getScriptSource(DOMXPath $xpath): string
    {
        $scriptSource = '';
        $supportDescNode = $xpath->query('//tei:supportDesc/tei:extent/text()');
        if (!empty($supportDescNode->item(0)->data)) {
            $scriptSource .= trim(preg_replace('/\s+/', ' ', $supportDescNode->item(0)->data)) . ' ';
        }

        $heightNode = $xpath->query('//tei:supportDesc/tei:extent/tei:dimensions/tei:height');
        if (!empty($heightNode->item(0)->nodeValue)) {
            $height = $heightNode->item(0)->nodeValue;
        }

        $widthNode = $xpath->query('//tei:supportDesc/tei:extent/tei:dimensions/tei:width');
        if (!empty($widthNode->item(0)->nodeValue)) {
            $width = $widthNode->item(0)->nodeValue;
        }

        $unitNode = $xpath->query('//tei:supportDesc/tei:extent/tei:dimensions/@unit');
        if (!empty($unitNode->item(0)->nodeValue)) {
            $unit = $unitNode->item(0)->nodeValue;
        }

        if ((isset($height) && !empty($height)) &&
            (isset($width) && !empty($width)) &&
            (isset($unit) && !empty($unit))) {
            $scriptSource .= $height . ' x ' . $width . ' ' . $unit . '.';
        }

        $bindingDescNode = $xpath->query('//tei:bindingDesc/tei:p/text()');
        if (!empty($bindingDescNode->item(0)->data)) {
            $scriptSource .= ' ' . trim(preg_replace('/\s+/', ' ', $bindingDescNode->item(0)->data));
        }

        return $scriptSource;
    }

    private function getWriters(DOMXPath $xpath): array
    {
        $writers = [];
        $writerNodes = $xpath->query('//tei:handNote');
        if (is_iterable($writerNodes)) {
            foreach ($writerNodes as $writerNode) {
                foreach ($writerNode->attributes as $attribute) {
                    if ('major' === $attribute->nodeValue) {
                        $textAddition = '– (Grundschicht)';
                    }
                }

                if (!empty($writerNode->textContent)) {
                    $writer = trim(preg_replace('/\s+/', ' ', $writerNode->textContent));

                    if (isset($textAddition)) {
                        $writer .= $writer . ' ' . $textAddition;
                    }

                    $writers[] = $writer;
                }
            }
        }

        return $writers;
    }

    private function getImageIds(DOMXPath $xpath): array
    {
        $imageIds = [];
        $imageIdsNodes = $xpath->query('//tei:graphic/@xml:id');
        if (is_iterable($imageIdsNodes)) {
            foreach ($imageIdsNodes as $imageIdsNode) {
                if (!empty($imageIdsNode->nodeValue)) {
                    $imageId = trim(preg_replace('/\s+/', ' ', $imageIdsNode->nodeValue));
                    $imageIds[] = $imageId;
                }
            }
        }

        return $imageIds;
    }

    private function getImageUrls(DOMXPath $xpath): array
    {
        $imageUrls = [];
        $imageUrlssNodes = $xpath->query('//tei:graphic/@url');
        if (is_iterable($imageUrlssNodes)) {
            foreach ($imageUrlssNodes as $imageUrlssNode) {
                if (!empty($imageUrlssNode->nodeValue)) {
                    $imageUrl = trim(preg_replace('/\s+/', ' ', $imageUrlssNode->nodeValue));
                    $imageUrls[] = $imageUrl;
                }
            }
        }

        return $imageUrls;
    }

    private function getGraphics(array $imageIds, array $imageUrls): array
    {
        $graphics = [];
        if (!empty($imageIds) && !empty($imageUrls)) {
            foreach ($imageIds as $key => $imageId) {
                if (!empty($imageId) && !empty($imageUrls[$key])) {
                    $graphics[$imageId] = $imageUrls[$key];
                }
            }
        }

        return $graphics;
    }

    private function getEntities(DOMXPath $xpath): array
    {
        $entities = [];
        $documentGndsNodes = $xpath->query('//tei:text[@xml:lang="ger"]//tei:name');
        $documentEntities = [];
        foreach ($documentGndsNodes as $documentGndsNode) {
            foreach ($documentGndsNode->attributes as $attribute) {
                if (strstr($attribute->nodeValue, 'gnd')) {
                    $gnd = str_replace('gnd:', '', $attribute->nodeValue);
                    $documentEntities[] = $gnd;
                } else {
                    $type = $attribute->nodeValue;;
                }
            }

            if (!empty($documentGndsNode->nodeValue)) {
                $entityName = trim(preg_replace('/\s+/', ' ', $documentGndsNode->nodeValue));
            }

            if (isset($entityName) && isset($type) && isset($gnd)) {
                $entities[] = ['doctype' => 'entity', 'name' => $entityName, 'entity_type' => $type, 'gnd' => $gnd];
            }
        }

        return $entities;
    }

    private function getDoctypeNotes(DOMXPath $xpath, string $id): array
    {
        $doctypeNotes = [];
        $notesNodes = $xpath->query('//tei:text[@xml:lang="ger"]//tei:div//tei:div//tei:note');
        if (is_iterable($notesNodes) && !empty($notesNodes)) {
            $notes = [];
            foreach ($notesNodes as $key => $notesNode) {
                if (!empty($notesNodes->item(0)->nodeValue) && !empty($id)) {
                    $note = trim(preg_replace('/\s+/', ' ', $notesNode->nodeValue));
                    $notes[] = $note;
                    $doctypeNotes[$id][] = ['id' => $id . '_note_' . ++$key, 'article_id' => $id, 'doctype' => 'note', 'note' => $note];
                }
            }
        }

        return $doctypeNotes;
    }

    /**
     * @Route("tei2solr")
     */
    public function tei2solr(): void
    {
505
        $this->client->getEndpoint()->setOptions(['timeout' => 60]);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
506
507
508
509
        $finder = new Finder();
        $finder->files()->in($this->teiDir);

        foreach ($finder as $file) {
asajedi's avatar
asajedi committed
510
            libxml_use_internal_errors(TRUE);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
511
512
513
            $doc = new DOMDocument();
            $doc->load($file->getRealPath());

asajedi's avatar
asajedi committed
514
515
516
            if (!libxml_get_errors()) {
                $xpath = new DOMXPath($doc);
                $xpath->registerNamespace('tei', 'http://www.tei-c.org/ns/1.0');
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
517

asajedi's avatar
asajedi committed
518
519
520
521
                $id = $this->getId($xpath);
                $docType = 'article';
                $shortTitle = $this->getShortTitle($xpath);
                $title = $this->getTitle($xpath);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
522

asajedi's avatar
asajedi committed
523
                $originPlaceGNDNode = $xpath->query('//tei:name[@type="place" and @subtype="orn"]/@ref');
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
524

asajedi's avatar
asajedi committed
525
526
527
                if ($originPlaceGNDNode->item(0)) {
                    $originPlaceGND = $originPlaceGNDNode->item(0)->nodeValue;
                }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
528

asajedi's avatar
asajedi committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
                $originPlace = $this->getOriginPlace($xpath);
                $author = $this->getAuthor($xpath);
                $recipient = $this->getRecipient($xpath);
                $destinationPlace = $this->getDestinationPlace($xpath);
                $originDate = $this->getOriginDate($xpath);
                $license = $this->getLicense($xpath);
                $language = $this->getLanguage($xpath);
                $reference = $this->getReference($xpath);
                $response = $this->getResponse($xpath);
                $relatedItems = $this->getRelatedItems($xpath);
                $repository = $this->getRepository($xpath);
                $institution = $this->getInstitution($xpath);
                $settlement = $this->getSettlement($xpath);
                $country = $this->getCountry($xpath);
                if (isset($repository) && isset($institution) && isset($settlement) && isset($country)) {
                    $institution = $repository . ', ' . $institution . ', ' . $settlement . ' (' . $country . ')';
                    $institution = trim(preg_replace('/\s+/', ' ', $institution));
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
546
547
                }

asajedi's avatar
asajedi committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
                $sourceDescription = $this->getSourceDescription($xpath);
                $publicationDate = $this->getPublicationDate($xpath);
                $fulltext = $this->getFulltext($xpath);
                $numberOfPages = $this->getNumberOfPages($xpath);
                $gndKeywords = $this->getGndKeywords($xpath);
                $freeKeywords = $this->getFreeKeywords($xpath);
                $shelfmark = $this->getShelfmark($xpath);
                $scriptSource = $this->getScriptSource($xpath);
                $writers = $this->getWriters($xpath);
                $imageIds = $this->getImageIds($xpath);
                $imageUrls = $this->getImageUrls($xpath);
                $graphics = $this->getGraphics($imageIds, $imageUrls);
                $entities = $this->getEntities($xpath);
                $doctypeNotes = $this->getDoctypeNotes($xpath, $id);

                $solrDocument = $this->getTextVersions($file->getRealPath(), $graphics);
                $transcription = $solrDocument->getTranscriptedText();
                $pagesTranscription = $solrDocument->getTranscriptedTextArr();
                $editedText = $solrDocument->getEditedText();
                $pagesEdited = $solrDocument->getEditedTextArr();
                $gndsUuids = $solrDocument->getGndsUuids();
                $pagesGndsUuids = $solrDocument->getPagesGndsUuids();

                $update = $this->client->createUpdate();
                $doc = $update->createDocument();

                if (!empty($id)) {
                    $doc->id = $id;
                    $doc->doctype = $docType;
                    $doc->short_title = $shortTitle;
                    $doc->title = $title;

                    if (isset($originPlace) && !empty($originPlace)) {
                        $doc->origin_place = $originPlace;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
583

asajedi's avatar
asajedi committed
584
                    $doc->author = $author;
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
585

asajedi's avatar
asajedi committed
586
587
588
                    if (isset($recipient) && !empty($recipient)) {
                        $doc->recipient = $recipient;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
589

asajedi's avatar
asajedi committed
590
591
592
                    if (isset($originDate) && !empty($originDate)) {
                        $doc->origin_date = $originDate;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
593

asajedi's avatar
asajedi committed
594
595
596
                    if (isset($destinationPlace) && !empty($destinationPlace)) {
                        $doc->destination_place = $destinationPlace;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
597

asajedi's avatar
asajedi committed
598
599
                    $doc->license = $license;
                    $doc->language = $language;
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
600

asajedi's avatar
asajedi committed
601
602
603
                    if (isset($reference) && !empty($reference)) {
                        $doc->reference = $reference;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
604

asajedi's avatar
asajedi committed
605
606
607
                    if (isset($response) && !empty($response)) {
                        $doc->response = $response;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
608

asajedi's avatar
asajedi committed
609
610
611
                    if (isset($relatedItem) && !empty($relatedItem)) {
                        $doc->related_items = $relatedItems;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
612

asajedi's avatar
asajedi committed
613
614
615
                    if (isset($institution) && !empty($institution)) {
                        $doc->institution = $institution;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
616

asajedi's avatar
asajedi committed
617
618
619
                    if (isset($sourceDescription) && !empty($sourceDescription)) {
                        $doc->source_description = $sourceDescription;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
620

asajedi's avatar
asajedi committed
621
622
623
                    if (isset($publicationDate) && !empty($publicationDate)) {
                        $doc->article_pub_date = $publicationDate;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
624

asajedi's avatar
asajedi committed
625
626
627
                    if (isset($fulltext) && !empty($fulltext)) {
                        $doc->fulltext = $fulltext;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
628

asajedi's avatar
asajedi committed
629
630
631
                    if (isset($numberOfPages) && !empty($numberOfPages)) {
                        $doc->number_of_pages = $numberOfPages;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
632

asajedi's avatar
asajedi committed
633
634
635
                    if (isset($gndKeywords) && !empty($gndKeywords)) {
                        $doc->gnd_keyword = $gndKeywords;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
636

asajedi's avatar
asajedi committed
637
638
639
                    if (isset($freeKeywords) && !empty($freeKeywords)) {
                        $doc->free_keyword = $freeKeywords;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
640

asajedi's avatar
asajedi committed
641
642
643
                    if (isset($shelfmark) && !empty($shelfmark)) {
                        $doc->shelfmark = $shelfmark;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
644

asajedi's avatar
asajedi committed
645
646
647
                    if (isset($scriptSource) && !empty($scriptSource)) {
                        $doc->script_source = $scriptSource;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
648

asajedi's avatar
asajedi committed
649
650
651
                    if (isset($writers) && !empty($writers)) {
                        $doc->writer = $writers;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
652

asajedi's avatar
asajedi committed
653
654
655
                    if (isset($imageIds) && !empty($imageIds)) {
                        $doc->image_ids = $imageIds;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
656

asajedi's avatar
asajedi committed
657
658
659
                    if (isset($imageUrls) && !empty($imageUrls)) {
                        $doc->image_urls = $imageUrls;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
660

asajedi's avatar
asajedi committed
661
662
663
                    if (isset($documentEntities) && !empty($documentEntities)) {
                        $doc->entities = $documentEntities;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
664

asajedi's avatar
asajedi committed
665
666
667
                    if (isset($notes) && !empty($notes)) {
                        $doc->notes = $notes;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
668

asajedi's avatar
asajedi committed
669
670
671
672
                    if (isset($transcription) && !empty($transcription)) {
                        $doc->transcripted_text = $transcription;
                        $doc->edited_text = $editedText;
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
673

asajedi's avatar
asajedi committed
674
675
676
677
678
679
680
681
682
683
684
685
686
687
                    if (!empty($numberOfPages) && intval($numberOfPages)) {
                        for ($i = 1; $i <= $numberOfPages; $i++) {
                            $update1 = $this->client->createUpdate();
                            $childDoc = $update1->createDocument();
                            $childDoc->id = $id . '_page' . $i;
                            $childDoc->article_id = $id;
                            $childDoc->article_title = $title;
                            $childDoc->doctype = 'page';
                            $childDoc->page_number = $i;
                            $childDoc->language = $language;

                            if (isset($imageUrls[$i - 1]) && !empty($imageUrls[$i - 1])) {
                                $childDoc->image_url = $imageUrls[$i - 1];
                            }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
688

asajedi's avatar
asajedi committed
689
690
691
                            if (isset($pagesTranscription[$i - 1]) && !empty($pagesTranscription[$i - 1])) {
                                $childDoc->transcripted_text = $pagesTranscription[$i - 1];
                            }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
692

asajedi's avatar
asajedi committed
693
694
695
                            if (isset($pagesEdited[$i - 1]) && !empty($pagesEdited[$i - 1])) {
                                $childDoc->edited_text = $pagesEdited[$i - 1];
                            }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
696

asajedi's avatar
asajedi committed
697
698
699
700
701
702
703
                            if (isset($pagesGndsUuids[$i - 1]) && !empty(($pagesGndsUuids[$i - 1]))) {
                                $childDoc->entities = array_values($pagesGndsUuids[$i - 1]);
                                $childDoc->annotation_ids = array_keys($pagesGndsUuids[$i - 1]);
                            }

                            $update->addDocument($childDoc);
                        }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
704
705
                    }

asajedi's avatar
asajedi committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
                    $update->addDocument($doc);
                    $update->addCommit();
                    $this->client->execute($update);

                    if (isset($doctypeNotes) && is_iterable($doctypeNotes)) {
                        foreach ($doctypeNotes as $doctypeNoteArr) {
                            foreach ($doctypeNoteArr as $doctypeNote) {
                                if (!empty($doctypeNote['id'])) {
                                    $update = $this->client->createUpdate();
                                    $doc = $update->createDocument();
                                    $doc->id = $doctypeNote['id'];
                                    $doc->article_id = $doctypeNote['article_id'];
                                    $doc->doctype = $doctypeNote['doctype'];
                                    $doc->note = $doctypeNote['note'];

                                    $update->addDocument($doc);
                                    $update->addCommit();

                                    $this->client->execute($update);
                                }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
726
727
728
729
730
                            }
                        }
                    }
                }

asajedi's avatar
asajedi committed
731
732
733
734
735
                if (isset($entities) && is_iterable($entities)) {
                    foreach ($entities as $entity) {
                        if (!empty($entity['gnd'])) {
                            $localFilePath = './../data/gnd-files/' . $entity['gnd'] . '.json';
                            if (!file_exists($localFilePath)) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
736

asajedi's avatar
asajedi committed
737
                                $remoteFilePath = 'https://lobid.org/gnd/' . $entity['gnd'] . '.json';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
738

asajedi's avatar
asajedi committed
739
740
                                try {
                                    $fileContent = @file_get_contents($remoteFilePath, true);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
741

asajedi's avatar
asajedi committed
742
743
744
745
746
                                    if ($fileContent == false) {
                                        throw new Exception($localFilePath);
                                    }
                                } catch (Exception $e) {
                                    echo $e->getMessage();
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
747
748
                                }

asajedi's avatar
asajedi committed
749
750
                                $filesystem = new Filesystem();
                                $filesystem->dumpFile($localFilePath, $fileContent);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
751

asajedi's avatar
asajedi committed
752
753
754
755
                                $gndArr = json_decode($fileContent);
                            } else {
                                $gndArr = json_decode(file_get_contents($localFilePath));
                            }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
756

asajedi's avatar
asajedi committed
757
758
759
                            if (isset($gndArr->preferredName) && !empty($gndArr->preferredName)) {
                                $preferredName = $gndArr->preferredName;
                            }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
760

asajedi's avatar
asajedi committed
761
762
763
                            if (isset($gndArr->variantName) && !empty($gndArr->variantName)) {
                                $variantNames = $gndArr->variantName;
                            }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
764

asajedi's avatar
asajedi committed
765
766
767
768
769
770
                            $update = $this->client->createUpdate();
                            $doc = $update->createDocument();
                            $doc->id = $entity['gnd'];
                            $doc->entity_name = $entity['name'];
                            $doc->doctype = $entity['doctype'];
                            $doc->entitytype = $entity['entity_type'];
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
771

asajedi's avatar
asajedi committed
772
773
774
                            if (isset($preferredName) && !empty($preferredName)) {
                                $doc->mostly_use_name = $preferredName;
                            }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
775

asajedi's avatar
asajedi committed
776
777
778
                            if (is_iterable($variantNames) && !empty($variantNames)) {
                                $doc->alternatively_name = $variantNames;
                            }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
779

asajedi's avatar
asajedi committed
780
781
                            $update->addDocument($doc);
                            $update->addCommit();
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
782

asajedi's avatar
asajedi committed
783
784
                            $this->client->execute($update);
                        }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
785
786
                    }
                }
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
            } else {
                $filesystem = new Filesystem();
                $teiImportLogFile = './data/log/teiImportLogs.txt';

                if (!$filesystem->exists($teiImportLogFile)) {
                    $filesystem->mkdir('./data/log');
                    $filesystem->touch($teiImportLogFile);
                }

                $errors = [];
                foreach (libxml_get_errors() as $key => $error) {
                    if (0 === $key) {
                        $errors[] = explode('/', $error->file)[4].PHP_EOL;
                        $errors[] = '--------------------'.PHP_EOL;
                    }

                    $errors[] = $error->message;
                }

                $filesystem->appendToFile($teiImportLogFile, implode('', $errors));

                libxml_clear_errors();
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
            }
        }
    }

    public function getUuid()
    {
        $uuid = uuid_create(UUID_TYPE_RANDOM);

        return $uuid;
    }

    public function getPagesNodes($pagesNode, &$elements): array
    {
        if (isset($pagesNode->nodeName)) {
            if ('pb' === $pagesNode->nodeName) {
                $elements[][] = $pagesNode;
            } else {
                if (!empty($elements)) {
                    $max = max(array_keys($elements));
                    $elements[$max][] = $pagesNode;
                }
            }

            if ($pagesNode->childNodes) {
                foreach ($pagesNode->childNodes as $childNode) {
                    $this->getPagesNodes($childNode, $elements);
                }
            }
        }

        return $elements;
    }

    public function getNodeChilds($pagesNode, &$ele)
    {
        if (isset($pagesNode->childNodes)) {

            foreach ($pagesNode->childNodes as $childNode) {

                $ele[] = $childNode;

                if ($childNode->childNodes) {

                    foreach ($childNode->childNodes as $childChildNode) {

                        $ele[] = $childChildNode;

                        $this->getNodeChilds($childChildNode, $ele);
                    }
                }
            }
        }

        return $ele;
    }

    public function getTextVersions(string $filePath, array $graphics): SolrDocument
    {
asajedi's avatar
asajedi committed
867
868
        libxml_use_internal_errors(TRUE);
        $solrDocument = new SolrDocument();
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
869
870
871
        $doc = new DOMDocument();
        $doc->load($filePath);

872
873
874
        $xpath = new DOMXPath($doc);
        $xpath->registerNamespace('tei', 'http://www.tei-c.org/ns/1.0');
        $pagesNodes = $xpath->query('//tei:body');
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
875

876
877
878
879
        $elements = [];
        foreach ($pagesNodes as $pagesNode) {
            $elements = $this->getPagesNodes($pagesNode, $elements);
        }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
880

881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
        $editedTextArr = [];
        $transcriptedTextArr = [];
        $gndsUuids = [];
        $pagesGndsUuids = [];

        foreach ($elements as $k => $myelement) {
            $renditions = [];
            $myeditedText = '';
            $mytranscriptedText = '';

            foreach ($myelement as $element) {
                if (isset($element->nodeName) && (
                        $element->nodeName === 'p' ||
                        $element->nodeName === 'dateline' ||
                        $element->nodeName === 'address' ||
                        $element->nodeName === 'closer' ||
                        $element->nodeName === 'list'
                    )) {

                    $ele = [];
                    $el = $this->getNodeChilds($element, $ele);
                    $tText = '';
                    $eText = '';
                    $n = 0;
                    $liNumber = 1;

                    foreach ($el as $e) {
                        if ($e->nodeName === '#text'
                            && ($e->parentNode->nodeName !== 'abbr' && ($e->nodeName !== 'del' || $e->parentNode->nodeName !== 'add'))
                            && $e->parentNode->nodeName !== 'note'

                        ) {

                            if (isset($add) && !empty($add)) {
                                $tText .= ' &lang;' . $e->data . ' ' . $add;

                                $eText .= ' ' . $e->data;

                                $add = '';
                            } elseif (isset($del) && !empty($del)) {
                                $tText .= ' &#x5B;' . $e->data . ' ' . $del;

                                $del = '';
                            } elseif (isset($li) && !empty($li) && !empty($e->data) && 'item' === $e->parentNode->nodeName) {

                                if (isset($italic) && true === $italic) {
                                    $tText .= $li . $e->data . '</li>';
                                    $eText .= $li . $e->data . '</li>';
                                    $italic = false;
                                } else {
                                    $tText .= $li . $e->data;
                                    $eText .= $li . $e->data;
                                }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
934

935
936
                                $li = '';
                            } elseif ('hi' === $e->parentNode->nodeName && isset($e->parentNode->attributes[0])) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
937

938
                                $hi = explode(':', $e->parentNode->attributes[0]->value);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
939

940
941
942
                                if (isset($hi[1]) && !empty($hi[1]) && 'superscript' === $hi[1]) {
                                    $tText .= '<sup>' . $e->data . '</sup>';
                                } elseif (isset($hi[1]) && !empty($hi[1]) && 'italic' === $hi[1]) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
943

944
                                    $tText .= '<i>' . $e->data . '</i>';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
945

946
947
948
949
950
951
952
953
                                    $italic = true;
                                }
                            } elseif (isset($gnd) && true === $gnd) {
                                $eText .= $e->data . '</span>';
                                $tText .= $e->data;
                                $gnd = false;
                            } else {
                                $eText .= $e->data;
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
954

955
956
957
                                if (isset($renditions) && !empty($renditions)) {
                                    $classOpeningTag = '';
                                    $classEndTag = '';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
958

959
960
961
                                    foreach ($renditions as $rendition) {
                                        if ('italic' === $rendition) {
                                            $class = 'i';
asajedi's avatar
asajedi committed
962

963
964
965
966
967
968
                                        } elseif ('underline' === $rendition) {
                                            $class = 'u';
                                        }
                                        if (isset($class) && !empty($class)) {
                                            $classOpeningTag .= '<' . $class . '>';
                                            $classEndTag .= '</' . $class . '>';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
969
970
                                        }

971
                                        $renditions = [];
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
972
                                    }
973
                                }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
974

975
976
977
978
                                if (isset($classOpeningTag) && !empty($classOpeningTag)) {
                                    $tText .= $classOpeningTag;
                                    $classOpeningTag = '';
                                }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
979

980
                                $tText .= $e->data;
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
981

982
983
984
                                if (isset($classEndTag) && !empty($classEndTag)) {
                                    $tText .= $classEndTag;
                                    $classEndTag = '';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
985
986
                                }

987
988
989
990
991
992
993
                            }
                        } elseif ('item' === $e->nodeName) {
                            if ($liNumber++ === 1) {
                                $li = '<ul><li>';
                            } else {
                                $li = '<li>';
                            }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
994

995
996
997
998
                        } elseif ('add' === $e->nodeName) {
                            if ('rdg' === $e->parentNode->nodeName) {

                                if (!empty($e->attributes)) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
999

1000
                                    $pattern = '/^#[a-z_]*$/i';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1001

1002
                                    foreach ($e->attributes as $attribute) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1003

1004
1005
                                        if ('hand' === $attribute->nodeName) {
                                            $match = preg_match($pattern, $attribute->nodeValue, $matches);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1006

1007
1008
                                            if ($match) {
                                                $add = '<i>erg. ' . str_replace('_', ' ', trim($attribute->nodeValue, '#')) . '</i>&rang; ';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1009
1010
                                            }
                                        }
asajedi's avatar
asajedi committed
1011
                                    }
1012
1013
1014

                                } else {
                                    $add = '<i>erg.</i>&rang; ';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1015
                                }
1016
1017
1018
1019
                            }
                        } elseif ('del' === $e->nodeName) {
                            if ('rdg' === $e->parentNode->nodeName) {
                                if (!empty($e->attributes)) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1020

1021
                                    foreach ($e->attributes as $attribute) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1022

1023
1024
                                        if ('hand' === $attribute->nodeName) {
                                            $del = '<i>str. ' . str_replace('_', ' ', trim($attribute->nodeValue, '#')) . '</i>&#x5D;';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1025
                                        }
asajedi's avatar
asajedi committed
1026
                                    }
1027
1028
1029

                                } else {
                                    $del = '<i>str.</i>&#x5D;';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1030
                                }
1031
1032
                            }
                        } elseif ('handShift' === $e->nodeName && 'signed' === $e->parentNode->nodeName) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1033

1034
                            if (!empty($e->attributes)) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1035

1036
                                $pattern = '/^#[a-z_]*$/i';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1037

1038
                                foreach ($e->attributes as $attribute) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1039

1040
1041
                                    if ('scribeRef' === $attribute->nodeName) {
                                        $match = preg_match($pattern, $attribute->nodeValue, $matches);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1042

1043
1044
                                        if ($match) {
                                            $add = '<i>sign. ' . str_replace('_', ' ', trim($attribute->nodeValue, '#')) . '</i>&rang; ';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1045
1046
1047
                                        }
                                    }
                                }
1048
1049
1050
1051
1052
                            }
                        } elseif ('lb' === $e->nodeName) {
                            $tText .= '<br>';
                            $eText .= ' ';
                        } elseif ('addrLine' === $e->nodeName) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1053

1054
1055
1056
1057
                            if ($n++ > 0) {
                                $eText .= '<br>';
                                $tText .= '<br>';
                            }
asajedi's avatar
asajedi committed
1058

1059
1060
1061
1062
1063
                        } elseif ('signed' === $e->nodeName) {
                            $tText .= '<br><br>';
                            $eText .= '<br><br>';
                        } elseif ('hi' === $e->nodeName) {
                            foreach ($e->attributes as $attribute) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1064

1065
                                if (isset(explode(':', $attribute->value)[1])) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1066

1067
1068
1069
                                    $rend = explode(':', $attribute->value)[1];
                                    if ('superscript' !== $rend && 'italic' !== $rend) {
                                        $renditions[] = explode(':', $attribute->value)[1];
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1070
1071
1072
                                    }
                                }
                            }
1073
1074
1075
1076
1077
1078
                        } elseif ('name' === $e->nodeName && (isset($e->attributes[1]->value) && !empty($e->attributes[1]->value)) && (isset($e->attributes[0]->value) && !empty($e->attributes[0]->value))) {
                            $uuid = $this->getUuid();
                            $gndsUuids[$uuid] = str_replace('gnd:', '', $e->attributes[1]->value);
                            $pagesGndsUuids[$k][$uuid] = str_replace('gnd:', '', $e->attributes[1]->value);
                            $eText .= '<span class="' . $e->attributes[0]->value . '" id="' . $uuid . '">';
                            $gnd = true;
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1079
                        }
1080
                    }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1081

1082
1083
                    $eText = preg_replace('~\x{00AD}~u', '-', $eText);
                    $pattern = '/(\w+)-\s(\w)/i';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1084

1085
1086
1087
1088
1089
1090
1091
                    $eText = preg_replace_callback(
                        $pattern,
                        function ($match) {
                            return $match[1] . $match[2];
                        },
                        $eText
                    );
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1092

1093
1094
                    $myeditedText .= '<p>' . $eText . '</p>';
                    $mytranscriptedText .= '<p>' . $tText . '</p>';
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1095

1096
1097
1098
1099
1100
                } elseif ('pb' === $element->nodeName) {
                    foreach ($element->attributes as $key => $attribute) {
                        if ('facs' === $attribute->name) {
                            if (isset($graphics[trim($attribute->value, '#')])) {
                                $graphic = $graphics[trim($attribute->value, '#')];
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1101

1102
1103
                                if (str_ends_with($graphic, '.jpg')) {
                                    $graphic = substr($graphic, 0, strlen($graphic) - 4);
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1104
                                }
asajedi's avatar
asajedi committed
1105
                            }
1106
1107
                        } elseif ('n' === $attribute->name) {
                            $pageNumber = $attribute->value;
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1108
1109
                        }

1110
1111
1112
1113
1114
1115
1116
                        if (!empty($pageNumber) && !empty($graphic)) {
                            $href = '<a href="/' . $graphic . '" target="_blank"/>' . $pageNumber . '</a>';
                        } elseif (!empty($pageNumber) && empty($graphic)) {
                            $href = $pageNumber;
                        } else {
                            $href = '';
                        }
asajedi's avatar
asajedi committed
1117
1118
                    }

1119
1120
1121
1122
1123
                    $tText = $href;
                    $eText = $href;
                    $myeditedText .= '<p>' . $eText . '</p>';
                    $mytranscriptedText .= '<p>' . $tText . '</p>';
                }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1124
1125
            }

1126
1127
1128
            $editedTextArr[] = $myeditedText;
            $transcriptedTextArr[] = $mytranscriptedText;
        }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1129

1130
1131
1132
1133
        $transcriptedText = '';
        foreach ($transcriptedTextArr as $mytranscriptedText) {
            $transcriptedText .= $mytranscriptedText;
        }
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1134

1135
1136
1137
        $editedText = '';
        foreach ($editedTextArr as $myeditedText) {
            $editedText .= $myeditedText;
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1138
1139
        }

1140
1141
1142
1143
1144
1145
1146
        $solrDocument->setTranscriptedText($transcriptedText);
        $solrDocument->setTranscriptedTextArr($transcriptedTextArr);
        $solrDocument->setEditedText($editedText);
        $solrDocument->setEditedTextArr($editedTextArr);
        $solrDocument->setGndsUuids($gndsUuids);
        $solrDocument->setPagesGndsUuids($pagesGndsUuids);

Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1147
1148
1149
1150
1151
1152
1153
1154
        return $solrDocument;
    }

    /**
     * @Route("gitlab")
     */
    public function fetchTeis(): void
    {
1155
1156
1157
1158
1159
1160
        $filesystem = new Filesystem();
        if ($filesystem->exists($this->teiDir)) {
            $filesystem->mkdir($this->teiDir);
        }

        for ($i = 1; $i <= 150; $i++) {
Ali Reza Sajedi's avatar
Ali Reza Sajedi committed
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
            try {
                $files = file_get_contents('https://gitlab.gwdg.de/api/v4/projects/3451/repository/tree?access_token=ordUT8XGFzf-HMsXvCXU&path=TEI_doc_bearb&per_page=10&page=' . $i);

                $files = json_decode($files, true);

                foreach ($files as $file) {

                    $fileData = file_get_contents('https://gitlab.gwdg.de/api/v4/projects/3451/repository/files/TEI_doc_bearb%2F' . $file['name'] . '?access_token=ordUT8XGFzf-HMsXvCXU&ref=master');

                    $fileData = json_decode($fileData, true);

                    $filesystem = new Filesystem();

                    try {
                        $filesystem->dumpFile('./data/gitlab/' . $file['name'], base64_decode($fileData['content']));
                    } catch (FileException $exception) {
                        echo $file['name'] . ' could not be imported.';
                    }

                }

            } catch (FileException $exception) {
                echo 'Files could not be imported from gitlab';
            }
        }
    }

}