-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
143 lines (117 loc) · 5.24 KB
/
index.php
File metadata and controls
143 lines (117 loc) · 5.24 KB
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
<?php
$rootPath = __DIR__;
$storagePath = $rootPath . '/storage';
$exportPath = $rootPath . '/output';
$songbookName = 'SongBookName'; //Change this
if (!file_exists($exportPath)) mkdir($exportPath);
if (!file_exists($storagePath)) mkdir($storagePath);
if (!file_exists($exportPath . '/plain_text')) mkdir($exportPath . '/plain_text');
if (!file_exists($exportPath . '/xml')) mkdir($exportPath . '/xml');
try {
if (!file_exists($storagePath . '/Songs.json')) throw new Exception('Import file is not exists');
$data = json_decode(file_get_contents($storagePath . '/Songs.json'), true);
$songs = [];
foreach ($data as $song) {
if (isset($song['songbook_id']) && $song['songbook_id'] != 6) continue;
$song_text = $song['song_text'];
$title = $song['title'];
$number = $song['number'];
/* get the xml printed */
file_put_contents($exportPath . '/plain_text' . '/' . $song['number'] . '-' . $song['title'] . '.txt', $song_text);
$songStructure = [
'title' => $title,
'number' => $number,
'parts' => []
];
$verses = explode('Куплет', $song_text);
$countChoruses = 0;
$countVerses = 0;
foreach ($verses as $index => $vers) {
if (empty($vers)) {
continue;
}
$case1 = strpos($vers, 'Припев') !== false;
$case2 = strpos($vers, 'Приспів') !== false;
if ($case1 || $case2) {
$key = $case2 ? 'Приспів' : 'Припев';
$searchChorus = explode($key, $vers);
$songStructure['parts'][] = [
'type' => 'Verse',
'number' => $countVerses + 1,
'data' => trim($searchChorus[0])
];
$songStructure['parts'][] = [
'type' => 'Chorus',
'number' => $countChoruses + 1,
'data' => trim($searchChorus[1])
];
$countChoruses++;
$countVerses++;
} else {
$songStructure['parts'][] = [
'type' => 'Verse',
'number' => $countVerses + 1,
'data' => trim($vers)
];
$countVerses++;
}
}
$songs[] = $songStructure;
}
foreach ($songs as $song) {
$domtree = new DOMDocument('1.0', 'UTF-8');
$verseOrders = [];
/* create the root element of the xml tree */
$songElement = $domtree->createElement("song");
$songElement->setAttribute('xmlns', 'http://openlyrics.info/namespace/2009/song');
$songElement->setAttribute('version', '0.8');
$songElement->setAttribute('createdIn', 'OpenLP 2.4.6');
$songElement->setAttribute('modifiedIn', 'OpenLP 2.4.6');
$songElement->setAttribute('modifiedDate', '2020-05-09T22:15:25');
/* append it to the document created */
$songElement = $domtree->appendChild($songElement);
$properties = $domtree->createElement("properties");
$lyrics = $domtree->createElement('lyrics');
foreach ($song['parts'] as $part) {
$lines = $domtree->createElement('lines', $part['data']);
if ($part['type'] == 'Verse') {
$verseOrders[] = 'v' . $part['number'];
$verse = $domtree->createElement('verse');
$verse->setAttribute('name', 'v' . $part['number']);
$verse->appendChild($lines);
$lyrics->appendChild($verse);
}
if ($part['type'] == 'Chorus') {
$verseOrders[] = 'c' . $part['number'];
$verse = $domtree->createElement('chorus');
$verse->setAttribute('name', 'c' . $part['number']);
$verse->appendChild($lines);
$lyrics->appendChild($verse);
}
}
$verseOrder = $domtree->createElement('verseOrder', implode(' ', $verseOrders));
//Authors
$authors = $domtree->createElement('authors');
$authors->appendChild($domtree->createElement('author', $songbookName));
$title = $domtree->createElement('title', $song['number'] . '.' . $song['title']);
$title2 = $domtree->createElement('title', $song['number']);
$titles = $domtree->createElement('titles');
$titles->appendChild($title);
$titles->appendChild($title2);
$songbooks = $domtree->createElement('songbooks');
$songbook = $domtree->createElement('songbook');
$songbook->setAttribute('name', $songbookName);
$songbook->setAttribute('entry', $song['number']);
$songbooks->appendChild($songbook);
$properties->appendChild($titles);
$properties->appendChild($verseOrder);
$properties->appendChild($authors);
$properties->appendChild($songbooks);
$songElement->appendChild($properties);
$songElement->appendChild($lyrics);
file_put_contents($exportPath . '/xml/' . $song['number'] . '-' . $song['title'] . '.xml', $domtree->saveXML());
}
print_r('Job done!'. PHP_EOL);
} catch (Exception $exception) {
print_r($exception->getMessage() . PHP_EOL);
}