r81189 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81188‎ | r81189 | r81190 >
Date:13:54, 29 January 2011
Author:happy-melon
Status:ok
Tags:
Comment:
Follow-up r81074:
* subclass ConvertError and ExprError from MWException
* invoking the parser for error messages clears state and exposes strip markers, and the error messages should be in the content language anyway
* clean up the magic word list
Modified paths:
  • /trunk/extensions/ParserFunctions/Convert.php (modified) (history)
  • /trunk/extensions/ParserFunctions/Expr.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/Expr.php
@@ -46,7 +46,7 @@
4747 define( 'EXPR_POW', 35 );
4848 define( 'EXPR_PI', 36 );
4949
50 -class ExprError extends Exception {
 50+class ExprError extends MWException {
5151 public function __construct( $msg, $parameter = '' ) {
5252 $this->message = '<strong class="error">' . wfMsgForContent( "pfunc_expr_$msg", htmlspecialchars( $parameter ) ) . '</strong>';
5353 }
Index: trunk/extensions/ParserFunctions/Convert.php
@@ -4,11 +4,12 @@
55 die( 'This file is a MediaWiki extension, it is not a valid entry point' );
66 }
77
8 -class ConvertError extends Exception {
 8+class ConvertError extends MWException {
99 public function __construct( $msg /*...*/ ) {
1010 $args = func_get_args();
1111 array_shift( $args );
12 - $this->message = '<strong class="error">' . wfMsgExt( "pfunc-convert-$msg", 'parseinline', $args ) . '</strong>';
 12+ array_map( 'htmlspecialchars', $args );
 13+ $this->message = '<strong class="error">' . wfMsgForContent( "pfunc-convert-$msg", $args ) . '</strong>';
1314 }
1415 }
1516
@@ -74,48 +75,62 @@
7576 $string = trim( array_shift( $args ) );
7677
7778 # Process the rest of the args
78 - $sourceUnit =& MagicWord::get( 'sourceunit' );
79 - $targetUnit =& MagicWord::get( 'targetunit' );
80 - $linkUnit =& MagicWord::get( 'linkunit' );
81 - $dp =& MagicWord::get( 'decimalplaces' );
82 - $sf =& MagicWord::get( 'significantfigures' );
83 - $abbr =& MagicWord::get( 'abbreviate' );
84 - $raw =& MagicWord::get( 'rawsuffix' );
 79+ static $magicWords = array(
 80+ 'sourceunit' => null,
 81+ 'targetunit' => null,
 82+ 'linkunit' => null,
 83+ 'decimalplaces' => null,
 84+ 'significantfigures' => null,
 85+ 'abbreviate' => null,
 86+ 'rawsuffix' => null,
 87+ );
 88+ if( !is_object( $magicWords ) ){
 89+ foreach( $magicWords as $key => &$val ){
 90+ $magicWords[$key] =& MagicWord::get( $key );
 91+ }
 92+ # The $magicWords[key]->function() syntax doesn't work, so cast to
 93+ # object so we can use $magicWords->key->function() instead
 94+ $magicWords = (object)$magicWords;
 95+ }
8596
8697 $n = 0; # Count of unnamed parameters
8798 foreach ( $args as $arg ) {
8899 $parts = array_map( 'trim', explode( '=', $arg, 2 ) );
89100 if ( count( $parts ) == 2 ) {
90101 # Found "="
91 - if ( $sourceUnit->matchStartAndRemove( $parts[0] ) ) {
92 - if( $targetUnit->matchStartAndRemove( $parts[1] ) ){
 102+ if ( $magicWords->sourceunit->matchStartAndRemove( $parts[0] ) ) {
 103+ if( $magicWords->targetunit->matchStartAndRemove( $parts[1] ) ){
93104 $this->targetUnit =& $this->sourceUnit;
94105 } else {
95106 $this->sourceUnit = new ConvertUnit( $parts[1] );
96107 }
97108
98 - } elseif ( $targetUnit->matchStartAndRemove( $parts[0] ) ) {
99 - if( $sourceUnit->matchStartAndRemove( $parts[1] ) ){
 109+ } elseif ( $magicWords->targetunit->matchStartAndRemove( $parts[0] ) ) {
 110+ if( $magicWords->sourceunit->matchStartAndRemove( $parts[1] ) ){
100111 $this->targetUnit =& $this->sourceUnit;
101112 } else {
102113 $this->targetUnit = new ConvertUnit( $parts[1] );
103114 }
104115
105 - } elseif( $dp->matchStartAndRemove( $parts[0] ) ) {
 116+ } elseif( $magicWords->decimalplaces->matchStartAndRemove( $parts[0] ) ) {
106117 $this->decimalPlaces = intval( $parts[1] );
107118
108 - } elseif( $sf->matchStartAndRemove( $parts[0] ) ) {
 119+ } elseif( $magicWords->significantfigures->matchStartAndRemove( $parts[0] ) ) {
109120 # It doesn't make any sense to have negative sig-figs
110121 if( intval( $parts[1] ) > 0 ){
111122 $this->significantFigures = intval( $parts[1] );
112123 }
113124 }
114 - } elseif( $linkUnit->matchStartAndRemove( $parts[0] ) ) {
 125+
 126+ } elseif( $magicWords->linkunit->matchStartAndRemove( $parts[0] ) ) {
115127 $this->link = true;
116 - } elseif( $abbr->matchStartAndRemove( $parts[0] ) ) {
 128+
 129+ } elseif( $magicWords->abbreviate->matchStartAndRemove( $parts[0] ) ) {
117130 $this->abbreviate = true;
118 - } elseif( $raw->matchStartAndRemove( $parts[0] ) ) {
 131+
 132+ } elseif( $magicWords->rawsuffix->matchStartAndRemove( $parts[0] ) ) {
119133 $this->raw = true;
 134+
120135 } elseif( $parts[0] != '' && !$n++ && !$this->targetUnit instanceof ConvertUnit ){
121136 # First unnamed parameter = output unit
122137 $this->targetUnit = new ConvertUnit( $parts[0] );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r81074(bug 235) parser function for conversion of units of measurement....happy-melon00:13, 27 January 2011

Status & tagging log