r107165 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107164‎ | r107165 | r107166 >
Date:17:35, 23 December 2011
Author:grafzahl
Status:ok
Tags:
Comment:
Collect options in an options array
Modified paths:
  • /trunk/extensions/Score/Score.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Score/Score.body.php
@@ -151,31 +151,33 @@
152152 */
153153 public static function render( $code, array $args, Parser $parser, PPFrame $frame ) {
154154 try {
 155+ $options = array();
 156+
155157 /* Score language selection */
156158 if ( array_key_exists( 'lang', $args ) ) {
157 - $lang = $args['lang'];
 159+ $options['lang'] = $args['lang'];
158160 } else {
159 - $lang = 'lilypond';
 161+ $options['lang'] = 'lilypond';
160162 }
161 - if ( !in_array( $lang, self::$supportedLangs ) ) {
162 - throw new ScoreException( wfMessage( 'score-invalidlang', $lang ) );
 163+ if ( !in_array( $options['lang'], self::$supportedLangs ) ) {
 164+ throw new ScoreException( wfMessage( 'score-invalidlang', $options['lang'] ) );
163165 }
164166
165167 /* Midi rendering? */
166168 if ( array_key_exists( 'midi', $args ) ) {
167 - $linkMidi = $args['midi'];
 169+ $options['midi'] = $args['midi'];
168170 } else {
169 - $linkMidi = false;
 171+ $options['midi'] = false;
170172 }
171173
172174 /* Raw rendering? */
173175 if ( array_key_exists( 'raw', $args ) ) {
174 - $rawLilypond = $args['raw'];
 176+ $options['raw'] = $args['raw'];
175177 } else {
176 - $rawLilypond = false;
 178+ $options['raw'] = false;
177179 }
178180
179 - $html = self::generateHTML( $code, $lang, $linkMidi, $rawLilypond );
 181+ $html = self::generateHTML( $code, $options );
180182 } catch ( ScoreException $e ) {
181183 $html = "$e";
182184 }
@@ -187,15 +189,16 @@
188190 * Generates the HTML code for a score tag.
189191 *
190192 * @param $code score code.
191 - * @param $lang score language the code is in.
192 - * @param $linkMidi whether to generate a link to a MIDI file.
193 - * @param $rawLilypond whether to assume raw LilyPond code (ignored if $lang is not 'lilypond').
 193+ * @param $options array of music rendering options. Available options keys are:
 194+ * * lang: score language,
 195+ * * midi: whether to link to a MIDI file,
 196+ * * raw: whether to assume raw LilyPond code.
194197 *
195198 * @return HTML.
196199 *
197200 * @throws ScoreException if an error occurs.
198201 */
199 - private static function generateHTML( $code, $lang, $linkMidi, $rawLilypond ) {
 202+ private static function generateHTML( $code, $options ) {
200203 global $wgUploadDirectory, $wgUploadPath;
201204
202205 /* Various paths and file names */
@@ -221,8 +224,8 @@
222225 }
223226
224227 /* Generate PNG and MIDI files if necessary */
225 - if ( ( !file_exists( $image ) && !file_exists( $multi1 ) ) || ( $linkMidi && !file_exists( $midi ) ) ) {
226 - self::generatePngAndMidi( $code, $lang, $linkMidi, $rawLilypond, $filePrefix );
 228+ if ( ( !file_exists( $image ) && !file_exists( $multi1 ) ) || ( $options['midi'] && !file_exists( $midi ) ) ) {
 229+ self::generatePngAndMidi( $code, $options, $filePrefix );
227230 }
228231
229232 /* return output link(s) */
@@ -244,7 +247,7 @@
245248 self::debug( "No output images $image or $multi1!\n" );
246249 $link = 'No image';
247250 }
248 - if ( $linkMidi ) {
 251+ if ( $options['midi'] ) {
249252 $link = Html::rawElement( 'a', array( 'href' => $midiPath ), $link );
250253 }
251254
@@ -255,14 +258,12 @@
256259 * Generates score PNG file(s) and possibly a MIDI file.
257260 *
258261 * @param $code score code.
259 - * @param $lang language the score code is in.
260 - * @param $generateMidi whether to generate a MIDI file.
261 - * @param $rawLilypond whether to assume raw LilyPond code (ignored if $lang is not 'lilypond').
 262+ * @param $options rendering options, see Score::generateHTML() for explanation.
262263 * @param $filePrefix prefix for the generated files.
263264 *
264265 * @throws ScoreException on error.
265266 */
266 - private static function generatePngAndMidi( $code, $lang, $generateMidi, $rawLilypond, $filePrefix ) {
 267+ private static function generatePngAndMidi( $code, $options, $filePrefix ) {
267268 global $wgTmpDirectory, $wgLilyPond, $wgScoreTrim;
268269
269270 wfProfileIn( __METHOD__ );
@@ -301,11 +302,11 @@
302303 $factoryMultiTrimmedFormat = "$factoryDirectory/file-%d-trimmed.png";
303304
304305 /* Determine which LilyPond code to use */
305 - if ( $lang == 'lilypond' ) {
306 - if ( $rawLilypond ) {
 306+ if ( $options['lang'] == 'lilypond' ) {
 307+ if ( $options['raw'] ) {
307308 $lilypondCode = $code;
308309 } else {
309 - $lilypondCode = self::embedLilypondCode( $code, $generateMidi );
 310+ $lilypondCode = self::embedLilypondCode( $code, $options );
310311 }
311312 } else {
312313 wfSuppressWarnings();
@@ -313,7 +314,7 @@
314315 wfRestoreWarnings();
315316 if ( $lilypondCode === false ) {
316317 /* (re-)generate .ly file */
317 - $lilypondCode = self::generateLilypond( $code, $lang, $filePrefix, $factoryDirectory );
 318+ $lilypondCode = self::generateLilypond( $code, $options, $filePrefix, $factoryDirectory );
318319 }
319320 }
320321
@@ -392,13 +393,13 @@
393394 * Embeds simple LilyPond code in a score block.
394395 *
395396 * @param $lilypondCode
396 - * @param $renderMidi
 397+ * @param $options
397398 *
398399 * @return Raw lilypond code.
399400 *
400401 * @throws ScoreException if determining the LilyPond version fails.
401402 */
402 - private static function embedLilypondCode( $lilypondCode, $renderMidi ) {
 403+ private static function embedLilypondCode( $lilypondCode, $options ) {
403404 /* Get LilyPond version if we don't know it yet */
404405 if ( self::$lilypondVersion === null ) {
405406 self::getLilypondVersion();
@@ -418,7 +419,7 @@
419420 . "\\score {\n"
420421 . $lilypondCode
421422 . "\t\\layout { }\n"
422 - . ( $renderMidi ? "\t\\midi { }\n" : "" )
 423+ . ( $options['midi'] ? "\t\\midi { }\n" : "" )
423424 . "}\n";
424425 return $raw;
425426 }
@@ -427,7 +428,7 @@
428429 * Generates LilyPond code.
429430 *
430431 * @param $code score code.
431 - * @param $lang language the score code is in (a supported language other than 'lilypond').
 432+ * @param $options rendering options, see Score::generateHTML() for explanation.
432433 * @param $filePrefix prefix for the generated file.
433434 * @param $factoryDirectory directory of the working environment.
434435 *
@@ -435,10 +436,10 @@
436437 *
437438 * @throws ScoreException if an error occurs.
438439 */
439 - private static function generateLilypond( $code, $lang, $filePrefix, $factoryDirectory ) {
 440+ private static function generateLilypond( $code, $options, $filePrefix, $factoryDirectory ) {
440441 $ly = "$filePrefix.ly";
441442
442 - switch ( $lang ) {
 443+ switch ( $options['lang'] ) {
443444 case 'ABC':
444445 $lilypondCode = self::generateLilypondFromAbc( $code, $factoryDirectory );
445446 break;

Status & tagging log