r107083 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107082‎ | r107083 | r107084 >
Date:18:31, 22 December 2011
Author:grafzahl
Status:resolved
Tags:
Comment:
* Changing ScoreException to expect a Message object, suggested by Nikerabbit.
* Eliminating ScoreCallException in favour of this new scheme.
Modified paths:
  • /trunk/extensions/Score/Score.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Score/Score.body.php
@@ -32,16 +32,14 @@
3333 */
3434 class ScoreException extends Exception {
3535 /**
36 - * Error message parameters
 36+ * Constructor.
 37+ *
 38+ * @param $message Message to create error message from. Should have one $1 parameter.
 39+ * @param $code optionally, an error code.
 40+ * @param $previous Exception that caused this exception.
3741 */
38 - private $parameters;
39 -
40 - /**
41 - * Empty constructor.
42 - */
43 - public function __construct( $message ) {
44 - $this->parameters = array_slice( func_get_args(), 1 );
45 - parent::__construct( $message );
 42+ public function __construct( $message, $code = 0, Exception $previous = null ) {
 43+ parent::__construct( $message->inContentLanguage()->parse(), $code, $previous );
4644 }
4745
4846 /**
@@ -54,66 +52,41 @@
5553 return Html::rawElement(
5654 'span',
5755 array( 'class' => 'error' ),
58 - wfMessage( $this->getMessage() )
59 - ->inContentLanguage()
60 - ->params( $this->parameters )
61 - ->parse()
 56+ $this->getMessage()
6257 );
6358 }
6459 }
6560
6661 /**
67 - * Score call exception.
68 - * This is a type of exception thrown when a call to some binary used by the
69 - * Score extension fails.
 62+ * Score class
7063 */
71 -class ScoreCallException extends ScoreException {
 64+class Score {
7265 /**
73 - * Error message returned from the call.
 66+ * LilyPond version string.
 67+ * It defaults to null and is set the first time it is required.
7468 */
75 - private $callErrMsg;
 69+ private static $lilypondVersion = null;
7670
7771 /**
78 - * Constructs a new ScoreCallException.
 72+ * Throws proper ScoreException in case of failed shell executions.
7973 *
80 - * @param $message Message key to be used. It should accept one
81 - * parameter, the error message returned from the binary.
82 - * @param $callErrMsg Raw error message returned by the binary.
83 - */
84 - public function __construct( $message, $callErrMsg ) {
85 - $this->callErrMsg = $callErrMsg;
86 - parent::__construct( $message );
87 - }
88 -
89 - /**
90 - * Auto-renders exception as HTML error message in the wiki's content
91 - * language.
 74+ * @param $message Message to display.
 75+ * @param $output collected output from wfShellExec().
9276 *
93 - * @return error message HTML.
 77+ * @throws ScoreException always.
9478 */
95 - public function __toString() {
96 - return Html::rawElement(
97 - 'span',
98 - array( 'class' => 'error' ),
99 - wfMessage( $this->getMessage() )
100 - ->inContentLanguage()
101 - ->rawParams( Html::rawElement( 'pre', array(), strip_tags( $this->callErrMsg ) ) )
102 - ->parse()
 79+ private function throwCallException( $message, $output ) {
 80+ throw new ScoreException(
 81+ $message->rawParams(
 82+ Html::rawElement( 'pre',
 83+ array(),
 84+ strip_tags( $output )
 85+ )
 86+ )
10387 );
10488 }
105 -}
10689
107 -/**
108 - * Score class
109 - */
110 -class Score {
11190 /**
112 - * LilyPond version string.
113 - * It defaults to null and is set the first time it is required.
114 - */
115 - private static $lilypondVersion = null;
116 -
117 - /**
11891 * Determines the version of LilyPond in use and writes the version
11992 * string to self::$lilypondVersion.
12093 *
@@ -123,19 +96,19 @@
12497 global $wgLilyPond;
12598
12699 if ( !is_executable( $wgLilyPond ) ) {
127 - throw new ScoreException( 'score-notexecutable', $wgLilyPond );
 100+ throw new ScoreException( wfMessage( 'score-notexecutable', $wgLilyPond ) );
128101 }
129102
130103 $cmd = wfEscapeShellArg( $wgLilyPond ) . ' --version 2>&1'; // FIXME: 2>&1 is not portable
131 - $stdout = wfShellExec( $cmd, $rc );
 104+ $output = wfShellExec( $cmd, $rc );
132105 if ( $rc != 0 ) {
133 - throw new ScoreCallException( 'score-versionerr', $stdout );
 106+ self::throwCallException( wfMessage( 'score-versionerr' ), $output );
134107 }
135108
136 - $n = sscanf( $stdout, 'GNU LilyPond %s', self::$lilypondVersion );
 109+ $n = sscanf( $output, 'GNU LilyPond %s', self::$lilypondVersion );
137110 if ( $n != 1 ) {
138111 self::$lilypondVersion = null;
139 - throw new ScoreCallException( 'score-versionerr', $stdout );
 112+ self::throwCallException( wfMessage( 'score-versionerr' ), $output );
140113 }
141114 }
142115
@@ -159,7 +132,7 @@
160133 $factoryDirectory = $wgTmpDirectory . "/$factoryPrefix$fuzz";
161134 $rc = wfMkdirParents( $factoryDirectory, 0700, __METHOD__ );
162135 if ( !$rc ) {
163 - throw new ScoreException( 'score-nofactory' );
 136+ throw new ScoreException( wfMessage( 'score-nofactory' ) );
164137 }
165138 } catch ( ScoreException $e ) {
166139 return $e;
@@ -193,7 +166,7 @@
194167 }
195168 $rc = file_put_contents( $lilypondFile, $lilypondCode );
196169 if ( $rc === false ) {
197 - throw new ScoreException( 'score-noinput', $lilypondFile );
 170+ throw new ScoreException( wfMessage( 'score-noinput', $lilypondFile ) );
198171 }
199172 break;
200173 case 'ABC':
@@ -201,7 +174,7 @@
202175 self::runAbc2Ly( $code, $factoryDirectory );
203176 break;
204177 default:
205 - throw new ScoreException( 'score-invalidlang', $lang );
 178+ throw new ScoreException( wfMessage( 'score-invalidlang', $lang ) );
206179 }
207180
208181 $html = self::runLilypond( $factoryDirectory, $renderMidi, $altText );
@@ -271,12 +244,12 @@
272245 /* Create ABC input file */
273246 $rc = file_put_contents( $abcFile, ltrim( $code ) ); // abc2ly is picky about whitespace at the start of the file
274247 if ( $rc === false ) {
275 - throw new ScoreException( 'score-noabcinput', $abcFile );
 248+ throw new ScoreException( wfMessage( 'score-noabcinput', $abcFile ) );
276249 }
277250
278251 /* Convert to LilyPond file */
279252 if ( !is_executable( $wgAbc2Ly ) ) {
280 - throw new ScoreException( 'score-abc2lynotexecutable', $wgAbc2Ly );
 253+ throw new ScoreException( wfMessage( 'score-abc2lynotexecutable', $wgAbc2Ly ) );
281254 }
282255
283256 $cmd = wfEscapeShellArg( $wgAbc2Ly )
@@ -286,25 +259,25 @@
287260 . ' 2>&1'; // FIXME: this last bit is not portable
288261 $output = wfShellExec( $cmd, $rc );
289262 if ( $rc != 0 ) {
290 - throw new ScoreCallException( 'score-abcconversionerr', $output );
 263+ self::throwCallException( wfMessage( 'score-abcconversionerr' ), $output );
291264 }
292265 if ( !file_exists( $lyFile ) ) {
293266 /* Occasionally, abc2ly will return exit code 0 but not create an output file */
294 - throw new ScoreCallException( 'score-abcconversionerr', $output );
 267+ self::throwCallException( wfMessage( 'score-abcconversionerr' ), $output );
295268 }
296269
297270 /* The output file has a tagline which should be removed in a wiki context */
298271 $lyData = file_get_contents( $lyFile );
299272 if ( $lyData === false ) {
300 - throw new ScoreException( 'score-readerr', $lyFile );
 273+ throw new ScoreException( wfMessage( 'score-readerr', $lyFile ) );
301274 }
302275 $lyData = preg_replace( '/^(\s*tagline\s*=).*/m', '$1 ##f', $lyData );
303276 if ( $lyData === null ) {
304 - throw new ScoreException( 'score-pregreplaceerr' );
 277+ throw new ScoreException( wfMessage( 'score-pregreplaceerr' ) );
305278 }
306279 $rc = file_put_contents( $lyFile, $lyData );
307280 if ( $rc === false ) {
308 - throw new ScoreException( 'score-noinput', $lyFile );
 281+ throw new ScoreException( wfMessage( 'score-noinput', $lyFile ) );
309282 }
310283 }
311284
@@ -335,7 +308,7 @@
336309 $lilypondDir = 'lilypond';
337310 $md5 = md5_file( $lilypondFile );
338311 if ( $md5 === false ) {
339 - throw new ScoreException( 'score-noinput', $lilypondFile );
 312+ throw new ScoreException( wfMessage( 'score-noinput', $lilypondFile ) );
340313 }
341314 $rel = $lilypondDir . '/' . $md5; // FIXME: Too many files in one directory?
342315 $filePrefix = "$wgUploadDirectory/$rel";
@@ -376,28 +349,28 @@
377350 $rc = $rc && unlink( $f );
378351 }
379352 if ( !$rc ) {
380 - throw new ScoreException( 'score-cleanerr' );
 353+ throw new ScoreException( wfMessage( 'score-cleanerr' ) );
381354 }
382355
383356 /* create output directory if necessary */
384357 if ( !file_exists( "$wgUploadDirectory/$lilypondDir" ) ) {
385358 $rc = wfMkdirParents( "$wgUploadDirectory/$lilypondDir", null, __METHOD__ );
386359 if ( !$rc ) {
387 - throw new ScoreException( 'score-nooutput', $lilypondDir );
 360+ throw new ScoreException( wfMessage( 'score-nooutput', $lilypondDir ) );
388361 }
389362 }
390363
391364 /* generate lilypond output files in working environment */
392365 $oldcwd = getcwd();
393366 if ( $oldcwd === false ) {
394 - throw new ScoreException( 'score-getcwderr' );
 367+ throw new ScoreException( wfMessage( 'score-getcwderr' ) );
395368 }
396369 $rc = chdir( $factoryDirectory );
397370 if ( !$rc ) {
398 - throw new ScoreException( 'score-chdirerr', $factoryDirectory );
 371+ throw new ScoreException( wfMessage( 'score-chdirerr', $factoryDirectory ) );
399372 }
400373 if ( !is_executable( $wgLilyPond ) ) {
401 - throw new ScoreException( 'score-notexecutable', $wgLilyPond );
 374+ throw new ScoreException( wfMessage( 'score-notexecutable', $wgLilyPond ) );
402375 }
403376 $cmd = wfEscapeShellArg( $wgLilyPond )
404377 . " -dsafe='#t' -dbackend=eps --png --header=texidoc "
@@ -406,10 +379,10 @@
407380 $output = wfShellExec( $cmd, $rc2 );
408381 $rc = chdir( $oldcwd );
409382 if ( !$rc ) {
410 - throw new ScoreException( 'score-chdirerr', $oldcwd );
 383+ throw new ScoreException( wfMessage( 'score-chdirerr', $oldcwd ) );
411384 }
412385 if ( $rc2 != 0 ) {
413 - throw new ScoreCallException( 'score-compilererr', $output );
 386+ self::throwCallException( wfMessage( 'score-compilererr' ), $output );
414387 }
415388
416389 /* trim output images if wanted */
@@ -437,7 +410,7 @@
438411 $rc = $rc && rename( $f, sprintf( $multiFormat, $i ) );
439412 }
440413 if ( !$rc ) {
441 - throw new ScoreException( 'score-renameerr' );
 414+ throw new ScoreException( wfMessage( 'score-renameerr' ) );
442415 }
443416
444417 } catch ( ScoreException $e ) {
@@ -503,7 +476,7 @@
504477 . ' 2>&1'; // FIXME: not portable
505478 $output = wfShellExec( $cmd, $rc );
506479 if ( $rc != 0 ) {
507 - throw new ScoreCallException( 'score-trimerr', $output );
 480+ self::throwCallException( wfMessage( 'score-trimerr' ), $output );
508481 }
509482 }
510483

Follow-up revisions

RevisionCommit summaryAuthorDate
r107091Forgot static keywordgrafzahl18:56, 22 December 2011

Status & tagging log