r106499 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106498‎ | r106499 | r106500 >
Date:04:20, 17 December 2011
Author:bawolff
Status:ok
Tags:
Comment:
Some fixes for minor issues:
* $wgLilyPond needs to default to something (even if we expect user to set it)
* Make $wgScoreTrim default to $wgUseImageMagick (seemed like a good idea. I didn't like the check to error out if $wgUseImageMacgick was false, because someone could have image magick installed, but not want to use it for image rendering)
* Returning false from ParserFirstCallInit is a _really_ bad idea (it will stop all other extensions from loading, and its kind of non-transparent to end user why the extension just didn't load). Switch the checks in there to be in the main rendering code. (also, checking if $wgLilyPond is executable every time parser is loaded even if not parsing a <score> tag, seemed unnessary)
* For messages: Since this is a parser hook, its stored in parser cache, so must use the Wikis content language, not the user language (or next person will get errors in wrong language)
*Some of the messages should use parameters (This is especially important for the Page: 1 message, since message may need plural process and digit conversion in some langs)
*Wrap Most of the error messages in <Span class="error">. This is what many things put error messages in, and makes it red. (I didn't do that for the comile-error message, perhaps it should also do that, at least for the intro part)
Modified paths:
  • /trunk/extensions/Score/Score.body.php (modified) (history)
  • /trunk/extensions/Score/Score.i18n.php (modified) (history)
  • /trunk/extensions/Score/Score.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Score/Score.i18n.php
@@ -37,26 +37,29 @@
3838 $messages['en'] = array(
3939 'score-chdirerr' => 'Unable to change directory',
4040 'score-cleanerr' => 'Unable to clean out old files before re-rendering',
41 - 'score-compilererr' => 'Unable to compile LilyPond input file:',
 41+ 'score-compilererr' => 'Unable to compile LilyPond input file:
 42+$1',
4243 'score-desc' => 'MediaWiki tag extension for rendering musical scores with LilyPond',
4344 'score-getcwderr' => 'Unable to obtain current working directory',
4445 'score-nooutput' => 'Failed to create LilyPond image dir',
4546 'score-nofactory' => 'Failed to create LilyPond factory dir',
4647 'score-noinput' => 'Failed to create LilyPond input file',
47 - 'score-page' => 'Page',
 48+ 'score-page' => 'Page $1',
4849 'score-renameerr' => 'Error moving score files to upload directory',
4950 'score-trimerr' => 'Image could not be trimmed. Set $wgScoreTrim=false if this problem persists.',
 51+ 'score-notexecutable' => 'Could not execute LilyPond. Make sure <code>$wgLilyPond</code> is set correctly.',
5052 );
5153
5254 /* Descriptish */
5355 $messages['qqq'] = array(
5456 'score-chdirerr' => 'Displayed if the extension cannot change its working directory.',
5557 'score-cleanerr' => 'Displayed if an old file cleanup operation fails.',
56 - 'score-compilererr' => 'Displayed if the LilyPond code could not be compiled.',
 58+ 'score-compilererr' => 'Displayed if the LilyPond code could not be compiled. $1 is the error (generally big block of text in a pre tag)',
5759 'score-desc' => '{{desc}}',
5860 'score-getcwderr' => 'Displayed if the extension cannot obtain the CWD.',
5961 'score-noinput' => 'Displayed if the LilyPond input file cannot be created.',
60 - 'score-page' => 'The word "Page" as used in pagination.',
 62+ 'score-page' => 'The word "Page" as used in pagination. $1 is the page number',
6163 'score-renameerr' => 'Displayed if moving the resultant files from the working environment to the upload directory fails.',
6264 'score-trimerr' => 'Displayed if the extension failed to trim an output image.',
 65+ 'score-notexecutable' => 'Displayed if LilyPond binary can\'t be executed.',
6366 );
Index: trunk/extensions/Score/Score.php
@@ -42,8 +42,11 @@
4343 * Configuration
4444 */
4545
46 -/* Whether to trim the score images. Requires ImageMagick. Default is yes. */
47 -$wgScoreTrim = true;
 46+/* Whether to trim the score images. Requires ImageMagick.
 47+ * Default is $wgUseImageMagick and set in efScoreExtension */
 48+$wgScoreTrim = null;
 49+/* Path of lilypond executable */
 50+$wgLilyPond = '/usr/bin/lilypond';
4851
4952 /*
5053 * Extension credits
@@ -72,20 +75,12 @@
7376 * @return true if initialisation was successful, false otherwise.
7477 */
7578 function efScoreExtension( Parser &$parser ) {
76 - global $wgUseImageMagick, $wgLilyPond, $wgScoreTrim;
 79+ global $wgUseImageMagick, $wgScoreTrim;
7780
78 - if ( !is_executable( $wgLilyPond ) ) {
79 - wfDebugLog( 'Score', "Set LilyPond file \$wgLilyPond=$wgLilyPond is not executable.\n" );
80 - return false;
 81+ if ( $wgScoreTrim === null ) {
 82+ // Default to if we use Image Magick, since it requires Image Magick.
 83+ $wgScoreTrim = $wgUseImageMagick;
8184 }
82 - if ( !isset( $wgScoreTrim ) ) {
83 - wfDebugLog( 'Score', "Required global variable \$wgScoreTrim not set.\n" );
84 - return false;
85 - }
86 - if ( $wgScoreTrim && !$wgUseImageMagick ) {
87 - wfDebugLog( 'Score', "Score trimming requested, but ImageMagick is unavailable.\n" );
88 - return false;
89 - }
9085
9186 $parser->setHook( 'score', 'Score::render' );
9287
Index: trunk/extensions/Score/Score.body.php
@@ -54,6 +54,7 @@
5555 * @return Image link HTML, and possibly anchor to MIDI file.
5656 */
5757 public static function render( $lilypondCode, array $args, Parser $parser, PPFrame $frame ) {
 58+
5859 if ( array_key_exists( 'midi', $args ) ) {
5960 $renderMidi = $args['midi'];
6061 } else {
@@ -190,6 +191,9 @@
191192 if ( !$rc ) {
192193 throw new ScoreException( 'score-chdirerr' );
193194 }
 195+ if ( !is_executable( $wgLilyPond ) ) {
 196+ throw new ScoreException( 'score-notexecutable' );
 197+ }
194198 $cmd = $wgLilyPond
195199 . " -dsafe='#t' -dbackend=eps --png --header=texidoc "
196200 . wfEscapeShellArg( $lilypondFile )
@@ -202,8 +206,11 @@
203207 if ( $rc2 != 0 ) {
204208 self::eraseFactory( $factoryDirectory );
205209 wfProfileOut( __METHOD__ );
206 - $msg = wfMsgHtml( 'score-compilererr' )
207 - . ' <pre>' . strip_tags( $output ) . "\n</pre>\n";
 210+ $msg = wfMessage( 'score-compilererr' )
 211+ ->inContentLanguage()
 212+ ->rawParams(
 213+ ' <pre>' . strip_tags( $output ) . "\n</pre>\n"
 214+ );
208215 return $msg;
209216 }
210217
@@ -248,23 +255,37 @@
249256 } catch (ScoreException $e) {
250257 self::eraseFactory( $factoryDirectory );
251258 wfProfileOut( __METHOD__ );
252 - return wfMsgHtml( $e->getMessage() );
 259+ return Html::rawElement(
 260+ 'span',
 261+ array( 'class' => 'error' ),
 262+ wfMessage( $e->getMessage() )->inContentLanguage()->parse()
 263+ );
253264 }
254265 wfProfileOut( __METHOD__ );
255266 }
256267
257268 /* return output link(s) */
258269 if ( file_exists( $image ) ) {
 270+ if ( $altText ) {
 271+ $alt = $altText;
 272+ } else {
 273+ $alt = wfMessage( 'score-page' )->inContentLanguage()->numParams( '1' )->plain();
 274+ }
259275 $link = Html::rawElement('img', array(
260276 'src' => $imagePath,
261 - 'alt' => ( $altText ? $altText : wfMsg('score-page') . ' 1' )
 277+ 'alt' => $alt,
262278 ));
263279 } elseif ( file_exists( $multi1 ) ) {
264280 $link = '';
265281 for ($i = 1; file_exists( sprintf( $multiFormat, $i ) ); ++$i ) {
 282+ if ( $altText ) {
 283+ $alt = $altText;
 284+ } else {
 285+ $alt = wfMessage( 'score-page' )->inContentLanguage()->numParams( $i )->plain();
 286+ }
266287 $link .= Html::rawElement( 'img', array(
267288 'src' => sprintf( $multiPathFormat, $i ),
268 - 'alt' => wfMsg('score-page') . " $i"
 289+ 'alt' => $alt,
269290 ));
270291 }
271292 } else {

Status & tagging log