r110312 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110311‎ | r110312 | r110313 >
Date:18:31, 30 January 2012
Author:grafzahl
Status:deferred
Tags:
Comment:
MIDI overriding support
Modified paths:
  • /trunk/extensions/Score/Score.body.php (modified) (history)
  • /trunk/extensions/Score/Score.i18n.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Score/Score.i18n.php
@@ -38,6 +38,7 @@
3939 'score-getcwderr' => 'Unable to obtain current working directory',
4040 'score-invalidlang' => 'Invalid score language lang="<nowiki>$1</nowiki>". Currently recognised languages are lang="lilypond" (the default) and lang="ABC".',
4141 'score-invalidoggoverride' => 'The file "<nowiki>$1</nowiki>" you specified with override_ogg is invalid. Please specify the file name only, omit <nowiki>[[…]]</nowiki> and the "{{ns:file}}:" prefix.',
 42+ 'score-midioverridenotfound' => 'The file "<nowiki>$1</nowiki>" you specified with override_midi could not be found. Please specify the file name only, omit <nowiki>[[…]]</nowiki> and the "{{ns:file}}:" prefix.',
4243 'score-noabcinput' => 'ABC source file $1 could not be created.',
4344 'score-noimages' => 'No score images were generated. Please check your score code.',
4445 'score-noinput' => 'Failed to create LilyPond input file $1.',
@@ -73,6 +74,7 @@
7475 'score-getcwderr' => 'Displayed if the extension cannot obtain the current working directory.',
7576 'score-invalidlang' => 'Displayed if the lang="…" attribute contains an unrecognised score language. $1 is the unrecognised language.',
7677 'score-invalidoggoverride' => 'Displayed if the file specified with the override_ogg="…" attribute is invalid. $1 is the value of the override_ogg attribute.',
 78+ 'score-midioverridenotfound' => 'Displayed if the file specified with the override_midi="…" attribute could not be found. $1 is the value of the override_midi attribute.',
7779 'score-noabcinput' => 'Displayed if an ABC source file could not be created for lang="ABC". $1 is the path to the file that could not be created.',
7880 'score-noimages' => 'Displayed if no score images were rendered.',
7981 'score-noinput' => 'Displayed if the LilyPond input file cannot be created. $1 is the path to the input file.',
Index: trunk/extensions/Score/Score.body.php
@@ -212,6 +212,24 @@
213213 throw new ScoreException( wfMessage( 'score-invalidlang', htmlspecialchars( $options['lang'] ) ) );
214214 }
215215
 216+ /* Override MIDI file? */
 217+ if ( array_key_exists( 'override_midi', $args ) ) {
 218+ $file = wfFindFile( $args['override_midi'] );
 219+ if ( $file === false ) {
 220+ throw new ScoreException( wfMessage( 'score-midioverridenotfound', htmlspecialchars( $args['override_midi'] ) ) );
 221+ }
 222+ $options['override_midi'] = true;
 223+ $options['midi_file'] = $file;
 224+ $options['midi_path'] = $file->getLocalRefPath();
 225+ /* Set OGG stuff in case Vorbis rendering is requested */
 226+ $sha1 = $file->getSha1();
 227+ $oggRel = self::LILYPOND_DIR_NAME . "/{$sha1[0]}/{$sha1[0]}{$sha1[1]}/$sha1.ogg";
 228+ $options['ogg_path'] = "$wgUploadDirectory/$oggRel";
 229+ $options['ogg_url'] = "$wgUploadPath/$oggRel";
 230+ } else {
 231+ $options['override_midi'] = false;
 232+ }
 233+
216234 /* image file path and URL prefixes */
217235 $imageCacheName = wfBaseConvert( sha1( $code ), 16, 36, 31 );
218236 $imagePrefixEnd = self::LILYPOND_DIR_NAME . '/'
@@ -226,8 +244,11 @@
227245 $options['link_midi'] = false;
228246 }
229247 if ( $options['link_midi'] ) {
230 - $options['midi_path'] = "{$options['image_path_prefix']}.midi";
231 - $options['midi_url'] = "{$options['image_url_prefix']}.midi";
 248+ if ( $options['override_midi'] ) {
 249+ $options['midi_url'] = $options['midi_file']->getUrl();
 250+ } else {
 251+ $options['midi_url'] = "{$options['image_url_prefix']}.midi";
 252+ }
232253 }
233254
234255 /* Override OGG file? */
@@ -257,11 +278,17 @@
258279 if ( $options['generate_vorbis'] && ( $options['override_ogg'] !== false ) ) {
259280 throw new ScoreException( wfMessage( 'score-vorbisoverrideogg' ) );
260281 }
261 - if ( $options['generate_vorbis'] ) {
 282+ if ( $options['generate_vorbis'] && !$options['override_midi'] ) {
262283 $options['ogg_path'] = "{$options['image_path_prefix']}.ogg";
263284 $options['ogg_url'] = "{$options['image_url_prefix']}.ogg";
264285 }
265286
 287+ /* Generate MIDI? */
 288+ $options['generate_midi'] = ( $options['override_midi'] === false ) && ( $options['link_midi'] || $options['generate_vorbis'] );
 289+ if ( $options['generate_midi'] && !array_key_exists( 'midi_path', $options ) ) {
 290+ $options['midi_path'] = "{$options['image_path_prefix']}.midi";
 291+ }
 292+
266293 /* Raw rendering? */
267294 if ( array_key_exists( 'raw', $args ) ) {
268295 $options['raw'] = $args['raw'];
@@ -287,6 +314,9 @@
288315 * * factory_directory: string Path to directory in which files
289316 * may be generated without stepping on someone else's
290317 * toes. The directory may not exist yet. Required.
 318+ * * generate_midi: bool Whether to create a MIDI file, either
 319+ * to subsequently generate a vorbis file, or to provide
 320+ * a link to the MIDI file. Required.
291321 * * generate_vorbis: bool Whether to create an Ogg/Vorbis file in
292322 * an OggHandler. If set to true, the override_ogg option
293323 * must be set to false. Required.
@@ -295,8 +325,10 @@
296326 * * image_url_prefix: string Prefix to the image URL. Required.
297327 * * lang: string Score language. Required.
298328 * * link_midi: bool Whether to link to a MIDI file. Required.
299 - * * midi_path: string Local MIDI path. Required if the link_midi
300 - * option is set to true, ignored otherwise.
 329+ * * midi_file: MIDI file object.
 330+ * * midi_path: string Local MIDI path. Required if the link_midi,
 331+ * override_midi, or the generate_vorbis option is set to
 332+ * true, ignored otherwise.
301333 * * midi_url: string MIDI URL. Required if the link_midi option
302334 * is set to true, ignored otherwise.
303335 * * ogg_name: string Name of the OGG file. Required if the
@@ -307,6 +339,8 @@
308340 * * ogg_url: string Ogg/Vorbis URL. Required if the
309341 * generate_vorbis option is set to true, ignored
310342 * otherwise.
 343+ * * override_midi: bool Whether to use a user-provided MIDI file.
 344+ * Required.
311345 * * override_ogg: bool Whether to generate a wikilink to a
312346 * user-provided OGG file. If set to true, the vorbis
313347 * option must be set to false. Required.
@@ -326,10 +360,6 @@
327361 /* Generate PNG and MIDI files if necessary */
328362 $imagePath = "{$options['image_path_prefix']}.png";
329363 $multi1Path = "{$options['image_path_prefix']}-1.png";
330 - if ( !$options['link_midi'] && $options['generate_vorbis'] && !file_exists( $options['ogg_path'] ) ) {
331 - /* We need a MIDI file to generate a Vorbis file */
332 - $options['midi_path'] = "{$options['factory_directory']}/file.midi";
333 - }
334364 if ( ( !file_exists( $imagePath ) && !file_exists( $multi1Path ) )
335365 || ( array_key_exists( 'midi_path', $options ) && !file_exists( $options['midi_path'] ) ) ) {
336366 self::generatePngAndMidi( $code, $options );
@@ -401,9 +431,9 @@
402432 *
403433 * @param $code string Score code.
404434 * @param $options array Rendering options. They are the same as for
405 - * Score::generateHTML() with the exception that the link_midi
406 - * option is ignored and the midi file is generated if the
407 - * midi_path option is present.
 435+ * Score::generateHTML(). The MIDI file specified by the midi_path
 436+ * option is generated if and only if the generate_midi option
 437+ * evaluates to true.
408438 *
409439 * @throws ScoreException on error.
410440 */
@@ -417,7 +447,7 @@
418448 $multiFormat = "{$options['image_path_prefix']}-%d.png";
419449
420450 /* delete old files if necessary */
421 - if ( array_key_exists( 'midi_path', $options ) ) {
 451+ if ( $options['generate_midi'] ) {
422452 self::cleanupFile( $options['midi_path'] );
423453 }
424454 self::cleanupFile( $image );
@@ -477,7 +507,7 @@
478508 if ( $rc2 != 0 ) {
479509 self::throwCallException( wfMessage( 'score-compilererr' ), $output );
480510 }
481 - if ( array_key_exists( 'midi_path', $options ) && !file_exists( $factoryMidi ) ) {
 511+ if ( $options['generate_midi'] && !file_exists( $factoryMidi ) ) {
482512 throw new ScoreException( wfMessage( 'score-nomidi' ) );
483513 }
484514
@@ -496,7 +526,7 @@
497527
498528 /* move files to proper places */
499529 $rc = true;
500 - if ( array_key_exists( 'midi_path', $options ) ) {
 530+ if ( $options['generate_midi'] ) {
501531 self::renameFile( $factoryMidi, $options['midi_path'] );
502532 }
503533 if ( file_exists( $factoryImageTrimmed ) ) {
@@ -512,9 +542,8 @@
513543 *
514544 * @param $lilypondCode string Simple LilyPond code.
515545 * @param $options array Rendering options. The are the same as for
516 - * Score::generateHTML() except that the link_midi option is
517 - * ignored and a MIDI block is embedded if and only if the
518 - * midi_path option is present.
 546+ * Score::generateHTML(). A MIDI block is embedded if and only if
 547+ * the generate_midi option evaluates to true.
519548 *
520549 * @return string Raw lilypond code.
521550 *
@@ -540,7 +569,7 @@
541570 . "\\score {\n"
542571 . $lilypondCode
543572 . "\t\\layout { }\n"
544 - . ( array_key_exists( 'midi_path', $options ) ? "\t\\midi { }\n" : "" )
 573+ . ( $options['generate_midi'] ? "\t\\midi { }\n" : '' )
545574 . "}\n";
546575 return $raw;
547576 }

Status & tagging log