forked from VolksmissionFreudenstadt/SongFormatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SongFormat.php
executable file
·297 lines (267 loc) · 7.86 KB
/
SongFormat.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
#!/usr/bin/env php5
<?php
//// Configuration
// Define the tags assigned to each language in a song.
// The curly brackets {} will be added automatically, so 'sp1'
// will become {sp1}. If you assign an empty string '', no tags
// will be added to the corresponding language lines.
$languageColors=array(
1=> '',
2=> 'sp2',
3=> 'sp3',
4=> 'sp4',
5=> 'sp5',
6=> 'sp6',
7=> 'sp7',
8=> 'sp8',
9=> 'sp9',
);
// Define the string to look for when extracting CCLI license no
// information from the text. This is language-dependend. The default
// string used here is the one from the German SongSelect site:
define('CCLI_STRING', 'CCLI-Liednummer');
// Define whether to do the title processing specific to Volksmission
// Freudenstadt. Specifically, this will eliminate any song numbers in
// the formats 'PJ-nnn' or 'n - ' prefixed to a song title.
define('VMFDS_FORMATTING', TRUE);
// Define whether to convert the original .sng file to utf8. Note that
// .sng files from a Windows platform will probably need to be converted,
// and failure to do so will result in strange characters in the
// song titles in OpenLP
define('UTF8_ENCODE', TRUE);
// -- nothing to configure below this line
///////////////////////////////////////////////////////////////////////
error_reporting(E_ERROR);
define('VERSION', strftime('%Y%m%d.%H.%M', getlastmod()));
define('CRLF', "\r\n");
//////////////////////////////////////////////////////////////////////
/**
* Songbeamer .sng song
*
* @author Christoph Fischer <[email protected]>
*/
class SBSong {
protected $filename;
public $title;
protected $songConfig;
protected $parts;
protected $divider="\n";
/**
* Create a new instance of the SBSong class
*
* The constructor will immediately import the base file, do some title
* formatting and CCLI number extraction. It also does utf8 conversion.
*
* @param string The .sng file name
* @returns void
*/
public function __construct ($filename) {
$this->import($filename);
}
/**
* Import a song from a .sng file
*
* @param string .sng file name
* @returns void
*/
protected function import($filename) {
$this->filename=$filename;
$raw = file_get_contents($filename);
if (UTF8_ENCODE) $raw=utf8_encode($raw);
$this->identifyDivider($raw);
$this->parts=explode('---'.$this->divider, $raw);
$this->importConfig($this->parts[0]);
unset($this->parts[0]);
$this->formatTitle();
$this->getLicense($raw);
}
/**
* Extract license information
*
* Some songs simply copied from www.songselect.com have an empty
* CCLI number field, but have a line containing the CCLI number at
* the end of the text. This method will look for this line and
* extract its contents into the correct data field.
*
* Songs without a CCLI license number will be listed in a file
* called unlicensed.txt in the current folder.
*
* @param string Raw .sng file contents
* @returns void
*/
protected function getLicense($raw) {
if (!$this->songConfig['CCLI']) {
$lines = explode($this->divider, $raw);
foreach ($lines as $key => $line) {
if (substr($line, 0, strlen(CCLI_STRING))==CCLI_STRING) {
$this->songConfig['CCLI'] = str_replace(CCLI_STRING.' ', '', $line);
}
}
}
// write a list of all songs without license no.
if (!$this->songConfig['CCLI']) {
$fp = fopen('unlicensed.txt', 'a');
fwrite($fp, $this->filename.CRLF);
fclose($fp);
}
}
/**
* Remove PJ-nnn and initial song numbers from the title
*
* @param string Song title
* @returns string Song title without the number parts
*/
protected function removeTitleParts($t) {
if (VMFDS_FORMATTING) {
// get rid of PJ-...
if (substr($t, 0, 2)=='PJ') $t = substr($t, 9);
// get rid of initial song numbers
$tmp = explode(' - ', $t);
if (count($tmp)) {
if (is_numeric($tmp[0])) $t = str_replace($tmp[0].' - ', '', $t);
}
}
return $t;
}
/**
* Format the song title
*
* If no song title is present, the file name is used as title
*
* @params void
* @returns void
*/
protected function formatTitle() {
$t = $this->removeTitleParts($this->songConfig['Title']);
if (!(trim($t))) {
// no title? get it from filename
$t = $this->filename;
$t = $this->removeTitleParts(str_replace('.sng', '', str_replace('.RR', '', $t)));
}
// compromise needed? Rather a strange title than an empty one
if (!trim($t)) $t = $this->songConfig['Title'];
$this->songConfig['Title'] = $t;
$this->title = $t;
}
/**
* Identify the line break character
*
* This function checks whether \r\n (Windows standard) or \n
* (Unix standard) was used as the line break control character
* in the source file.
*
* @param string Raw .sng file text
* @returns void
*/
protected function identifyDivider($raw) {
$lines=explode("\n", $raw);
if (substr($lines[0], -1) == "\r") $this->divider="\r\n";
}
/**
* Import configuration from .sng file
*
* This method imports the configuration from the .sng file's
* header into an array.
*
* @param string Raw text of the .sng file header
* @returns void
*/
protected function importConfig($rawPart) {
$lines = explode($this->divider, $rawPart);
foreach ($lines as $line) {
if (trim($line)) {
$line = substr($line, 1);
$cfg=explode('=', $line);
$this->songConfig[trim($cfg[0])]=trim($cfg[1]);
}
}
}
/**
* Tag the different languages in a song
*
* @param array languageColors array assigning a tag to each language number
* @returns void
*/
public function processLanguages($colors) {
if ($this->songConfig['LangCount']>1) {
foreach ($this->parts as $pk => $part) {
$lines = explode($this->divider, $part);
$idx = 1;
foreach ($lines as $key => $line) {
if (!$this->skipLine($line)) {
$manual=false;
// check for manually set index
if (substr($line, 0, 2)=='##') {
$idx = substr($line, 2, 1);
$line = substr($line, 4);
$manual = true;
}
// tag color
if ($idx>1) {
if ($colors[$idx]) {
$lines[$key] = '{'.$colors[$idx].'}'
.$line
.'{/'.$colors[$idx].'}';
}
}
if (!$manual)
if ($idx==$this->songConfig['LangCount']) $idx=1; else $idx++;
}
}
$this->parts[$pk]=join($this->divider, $lines);
}
}
}
/**
* Checks whether a line should be skipped
*
* Lines containing song-part keywords like verse, refrain, ...
* should be skipped and not counted as a new language line
*
* @param string Line from a song
* @returns bool True when the line should be skipped
*/
protected function skipLine($line) {
$line = explode(' ', $line);
$keyWord=$line[0];
$f=in_array($keyWord, array('unbekannt', 'unbenannt', 'unknown', 'intro', 'vers', 'verse', 'strophe',
'refrain', 'chorus', 'pre-bridge', 'bridge', 'ending', 'pre-refrain', 'pre-chorus',
'pre-coda', 'zwischenspiel', 'interlude', 'coda', 'teil', 'part', '$$m=', '#h',
));
return $f;
}
/**
* Write the song back to a .sng file
*
* @param void
* @returns void
*/
public function write() {
// create config block
foreach ($this->songConfig as $key=> $value) {
$cfg .='#'.$key.'='.$value.$this->divider;
}
$raw = join($this->divider.'---'.$this->divider, $this->parts);
$fp=fopen($this->filename, 'w');
fwrite($fp, $cfg.'---'.$this->divider);
fwrite($fp, $raw);
fclose($fp);
}
}
///////////////////////////////////////////////////////////////////////
echo 'SongFormatter v'.VERSION.CRLF;
echo '(c) Volksmission Freudenstadt'.CRLF;
echo CRLF;
if ($handle=opendir('.')) {
while (false !== ($file = readdir($handle))) {
if (pathinfo($file, PATHINFO_EXTENSION)=='sng') {
$song=new SBSong($file);
echo 'Converting song "'.$song->title.'"... ';
$song->processLanguages($languageColors);
$song->write();
echo 'OK'.CRLF;
}
}
closedir($handle);
}
echo 'Done.'.CRLF;