r96933 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96932‎ | r96933 | r96934 >
Date:00:36, 13 September 2011
Author:reedy
Status:ok
Tags:
Comment:
REL1_18: MFT r96499, r96502, r96505
Modified paths:
  • /branches/REL1_18/extensions/ParserFunctions (modified) (history)
  • /branches/REL1_18/extensions/ParserFunctions/Convert.php (modified) (history)
  • /branches/REL1_18/extensions/ParserFunctions/ParserFunctions.php (modified) (history)
  • /branches/REL1_18/extensions/ParserFunctions/convertTests.txt (modified) (history)

Diff [purge]

Index: branches/REL1_18/extensions/ParserFunctions/convertTests.txt
@@ -1,3 +1,7 @@
 2+!! functionhooks
 3+convert
 4+!! endfunctionhooks
 5+
26 !! test
37 Simple conversion
48 !! input
@@ -3,5 +7,5 @@
48 {{#convert: 10 m | km }}
59 !!result
6 -<p>0.01 kilometers
 10+<p>0.01 kilometres
711 </p>
812 !! end
@@ -19,14 +23,14 @@
2024 *{{#convert: 10.0E2 km | m }}
2125 *{{#convert: 10.0E2.5 km | m }}
2226 !! result
23 -<ul><li>0.01 kilometers
24 -</li><li>0.01kilometers
25 -</li><li>10,000 meters
26 -</li><li>10,000-meters
27 -</li><li>1,000,000 meters
28 -</li><li>100 meters
29 -</li><li>1,000,000 meters
30 -</li><li>1,000,000.5,000 meters
 27+<ul><li>0.01 kilometres
 28+</li><li>0.01kilometres
 29+</li><li>10,000 metres
 30+</li><li>10,000-metres
 31+</li><li>1,000,000 metres
 32+</li><li>100 metres
 33+</li><li>1,000,000 metres
 34+</li><li>1,000,000.5,000 metres
3135 </li></ul>
3236
3337 !! end
@@ -46,17 +50,17 @@
4751 *{{#convert: 19 m | km }}
4852 *{{#convert: 20 m | km }}
4953 !! result
50 -<ul><li>0.01 kilometers
51 -</li><li>0.011 kilometers
52 -</li><li>0.012 kilometers
53 -</li><li>0.013 kilometers
54 -</li><li>0.014 kilometers
55 -</li><li>0.015 kilometers
56 -</li><li>0.016 kilometers
57 -</li><li>0.017 kilometers
58 -</li><li>0.018 kilometers
59 -</li><li>0.019 kilometers
60 -</li><li>0.02 kilometers
 54+<ul><li>0.01 kilometres
 55+</li><li>0.011 kilometres
 56+</li><li>0.012 kilometres
 57+</li><li>0.013 kilometres
 58+</li><li>0.014 kilometres
 59+</li><li>0.015 kilometres
 60+</li><li>0.016 kilometres
 61+</li><li>0.017 kilometres
 62+</li><li>0.018 kilometres
 63+</li><li>0.019 kilometres
 64+</li><li>0.02 kilometres
6165 </li></ul>
6266
6367 !! end
@@ -73,14 +77,14 @@
7478 *{{#convert: 10.6 m | km }}
7579 *{{#convert: 10.7 m | km }}
7680 !! result
77 -<ul><li>0.0100 kilometers
78 -</li><li>0.0101 kilometers
79 -</li><li>0.0102 kilometers
80 -</li><li>0.0103 kilometers
81 -</li><li>0.0104 kilometers
82 -</li><li>0.0105 kilometers
83 -</li><li>0.0106 kilometers
84 -</li><li>0.0107 kilometers
 81+<ul><li>0.0100 kilometres
 82+</li><li>0.0101 kilometres
 83+</li><li>0.0102 kilometres
 84+</li><li>0.0103 kilometres
 85+</li><li>0.0104 kilometres
 86+</li><li>0.0105 kilometres
 87+</li><li>0.0106 kilometres
 88+</li><li>0.0107 kilometres
8589 </li></ul>
8690
8791 !! end
@@ -90,7 +94,7 @@
9195 !! input
9296 {{#convert: 25, 26, 27, 28, 29, and 30 km }}
9397 !! result
94 -<p>25,000, 26,000, 27,000, 28,000, 29,000, and 30,000 meters
 98+<p>25,000, 26,000, 27,000, 28,000, 29,000, and 30,000 metres
9599 </p>
96100 !! end
97101
@@ -99,7 +103,7 @@
100104 !! input
101105 {{#convert: 25, 26, 27, 28, 29, and 30 miles }}
102106 !! result
103 -<p>40,000, 42,000, 43,000, 45,000, 47,000, and 50,000 meters
 107+<p>40,000, 42,000, 43,000, 45,000, 47,000, and 50,000 metres
104108 </p>
105109 !! end
106110
@@ -108,7 +112,7 @@
109113 !! input
110114 {{#convert:35000, 35E3, 35.0E3, 350E2, 3.500E4, 35000E0, 350000E-1 m | km }}
111115 !! result
112 -<p>35, 35, 35.0, 35, 35.00, 35, 35 kilometers
 116+<p>35, 35, 35.0, 35, 35.00, 35, 35 kilometres
113117 </p>
114118 !! end
115119
@@ -120,10 +124,10 @@
121125 *{{#convert: 25 | #sourceunit = km | #sourceunit = mm }}
122126 *{{#convert: 25 | #sourceunit = km | cm }}
123127 !! result
124 -<ul><li>25,000 meters
125 -</li><li>25,000 meters
126 -</li><li>0.025 meters
127 -</li><li>2,500,000 centimeters
 128+<ul><li>25,000 metres
 129+</li><li>25,000 metres
 130+</li><li>0.025 metres
 131+</li><li>2,500,000 centimetres
128132 </li></ul>
129133
130134 !! end
@@ -142,16 +146,16 @@
143147 *{{#convert: 1 mi | #sf = 5 }}
144148 *{{#convert: 1 mi | #sf = -8 }}
145149 !! result
146 -<ul><li>1,609 meters
147 -</li><li>1,609.3 meters
148 -</li><li>1,600 meters
149 -</li><li>1,609.344 meters
150 -</li><li>0 meters
151 -</li><li>2,000 meters
152 -</li><li>2,000 meters
153 -</li><li>1,610 meters
154 -</li><li>1,609.3 meters
155 -</li><li>2,000 meters
 150+<ul><li>1,609 metres
 151+</li><li>1,609.3 metres
 152+</li><li>1,600 metres
 153+</li><li>1,609.344 metres
 154+</li><li>0 metres
 155+</li><li>2,000 metres
 156+</li><li>2,000 metres
 157+</li><li>1,610 metres
 158+</li><li>1,609.3 metres
 159+</li><li>2,000 metres
156160 </li></ul>
157161
158162 !! end
@@ -186,8 +190,8 @@
187191 *{{#convert: 25 km | #targetunit = #sourceunit }}
188192 *{{#convert: 25 km | #sourceunit = #targetunit }}
189193 !! result
190 -<ul><li>25 kilometers
191 -</li><li>25 kilometers
 194+<ul><li>25 kilometres
 195+</li><li>25 kilometres
192196 </li></ul>
193197
194198 !! end
@@ -196,14 +200,14 @@
197201 #language
198202 !! input
199203 *{{#convert: 10 m | km | #language=en-gb }}
200 -*{{#convert: 10m | km | #language = en-gb }}
201 -*{{#convert: 10 km | m |#language=en-gb}}
202 -*{{#convert: 10 Pa | mmHg | #language = en-gb }}
 204+*{{#convert: 10m | km | #language = en }}
 205+*{{#convert: 10 km | m |#language=en}}
 206+*{{#convert: 10 Pa | mmHg | #language = fr }}
203207 !! result
204208 <ul><li>0.01 kilometres
205 -</li><li>0.01kilometres
206 -</li><li>10,000 metres
207 -</li><li>0.1 millimetres of mercury
 209+</li><li>0.01kilometers
 210+</li><li>10,000 meters
 211+</li><li>0.1 millimètre de mercure
208212 </li></ul>
209213
210214 !! end
@@ -217,10 +221,10 @@
218222 *{{#convert: 10 Pa | mmHg }}
219223 *{{#convert: 10 pa | mmHg }}
220224 !! result
221 -<ul><li>0.01 meters
222 -</li><li>10,000,000 meters
223 -</li><li>0.01 megameters
224 -</li><li>0.1 millimeters of mercury
 225+<ul><li>0.01 metres
 226+</li><li>10,000,000 metres
 227+</li><li>0.01 megametres
 228+</li><li>0.1 millimetres of mercury
225229 </li><li><strong class="error">Error: unknown unit "pa"</strong>
226230 </li></ul>
227231
Index: branches/REL1_18/extensions/ParserFunctions/Convert.php
@@ -24,8 +24,14 @@
2525 # A regex *FRAGMENT* which matches SI prefixes
2626 const PREFIX_REGEX = '[YZEPTGMkh(da)dcm\x{03BC}\x{00B5}npfazy]?';
2727
28 - # ConvertUnit objects
 28+ /**
 29+ * @var ConvertUnit
 30+ */
2931 protected $sourceUnit;
 32+
 33+ /**
 34+ * @var ConvertUnit
 35+ */
3036 protected $targetUnit;
3137
3238 # Whether to abbreviate the output unit
@@ -48,6 +54,9 @@
4955 # The last value converted, which will be used for PLURAL evaluation
5056 protected $lastValue;
5157
 58+ /**
 59+ * Reset the parser so it isn't contaminated by the results of previous parses
 60+ */
5261 public function clearState(){
5362 # Make sure we break any references set up in the parameter passing below
5463 unset( $this->sourceUnit );
@@ -176,6 +185,11 @@
177186 return $this->processString( $string );
178187 }
179188
 189+ /**
 190+ * Find the unit at the end of the string and load $this->sourceUnit with an appropriate
 191+ * ConvertUnit, or throw an exception if the unit is unrecognised.
 192+ * @param $string
 193+ */
180194 protected function deduceSourceUnit( $string ){
181195 # Get the unit from the end of the string
182196 $matches = array();
@@ -191,6 +205,7 @@
192206 /**
193207 * Identify the values to be converted, and convert them
194208 * @param $string String
 209+ * @return String
195210 */
196211 protected function processString( $string ){
197212 # Replace values
@@ -216,7 +231,7 @@
217232 * Express a value in the $sourceUnit in terms of the $targetUnit, preserving
218233 * an appropriate degree of accuracy.
219234 * @param $value String
220 - * @return void
 235+ * @return String
221236 */
222237 public function convert( $value ){
223238 global $wgContLang;
@@ -374,6 +389,11 @@
375390 public $value;
376391 protected $name;
377392
 393+ /**
 394+ * Constructor
 395+ * @param $var ConvertDimension|Int a dimension constant or existing unit
 396+ * @param $var2 ConvertDimension|Int optionally another dimension constant for a compound unit $var/$var2
 397+ */
378398 public function __construct( $var, $var2=null ){
379399 static $legalDimensionsFlip;
380400
@@ -418,8 +438,8 @@
419439 if( in_array( $this->value, array_keys( self::$legalDimensions ) ) ){
420440 $this->name = self::$legalDimensions[$this->value];
421441 $this->compoundName = array(
422 - self::$legalDimensions[$var],
423 - self::$legalDimensions[$var2],
 442+ self::$legalDimensions[$dim],
 443+ self::$legalDimensions[$dim2],
424444 );
425445 } else {
426446 # Some combinations of units are fine (carats per bushel is a perfectly good,
@@ -442,6 +462,8 @@
443463
444464 /**
445465 * Get the name, or names, of the dimension
 466+ * @param $expandCompound Bool Whether to return a string instead of an array of strings in
 467+ * case of a compound unit
446468 * @return String|Array of String
447469 */
448470 public function getName( $expandCompound = false ){
@@ -630,7 +652,9 @@
631653
632654 /***************** MEMBER VARIABLES *****************/
633655
634 - # @var ConvertDimension
 656+ /**
 657+ * @var ConvertDimension
 658+ */
635659 public $dimension;
636660
637661 # What number you need to multiply this unit by to get the equivalent
@@ -665,6 +689,10 @@
666690 $this->parseUnit( $rawUnit );
667691 }
668692
 693+ /**
 694+ * Parse a raw unit string, and populate member variables
 695+ * @param $rawUnit String
 696+ */
669697 protected function parseUnit( $rawUnit ){
670698
671699 # Do mappings like 'mph' --> 'mi/h'
@@ -724,12 +752,39 @@
725753 }
726754 }
727755
 756+ /**
 757+ * Get the mathematical factor which will convert a measurement in this unit into a
 758+ * measurement in the SI base unit for the dimension
 759+ * @return double
 760+ */
728761 public function getConversion(){
729 - return $this->prefix
730 - ? $this->conversion * self::$prefixes[$this->prefix][0]
731 - : $this->conversion;
 762+ return $this->conversion * $this->getPrefixConversion();
732763 }
733764
 765+ /**
 766+ * Get the conversion factor associated with the prefix(es) in the unit
 767+ * @return double
 768+ */
 769+ public function getPrefixConversion(){
 770+ if( !$this->prefix ){
 771+ return 1;
 772+ } elseif( is_array( $this->prefix ) ){
 773+ $x = $this->prefix[0] !== null
 774+ ? self::$prefixes[$this->prefix[0]][0]
 775+ : 1;
 776+ if( $this->prefix[1] !== null ){
 777+ $x *= self::$prefixes[$this->prefix[1]][0];
 778+ }
 779+ return $x;
 780+ } else {
 781+ return self::$prefixes[$this->prefix][0];
 782+ }
 783+ }
 784+
 785+ /**
 786+ * Get a regular expression which will match keywords for this unit
 787+ * @return String
 788+ */
734789 public function getRegex(){
735790 return $this->regex;
736791 }
@@ -738,6 +793,7 @@
739794 * Get the text of the unit
740795 * @param $value String number for PLURAL support
741796 * @param $link Bool
 797+ * @param $abbreviate Bool
742798 * @param $language Language
743799 * @return String
744800 */
@@ -785,7 +841,19 @@
786842 return trim( $msgText );
787843 }
788844
789 - protected function getTextFromMessage( $dimension, $unit, $prefix, $number, $link, $abbreviate, $language ){
 845+ /**
 846+ * Retrieve the unit text from actual messages
 847+ * @param $dimension String
 848+ * @param $unit String
 849+ * @param $prefix String
 850+ * @param $string String
 851+ * @param $number String the actual value (for {{PLURAL}} etc)
 852+ * @param $link Bool
 853+ * @param $abbreviate Bool
 854+ * @param $language Language|Bool|null
 855+ * @return String
 856+ */
 857+ protected function getTextFromMessage( $dimension, $unit, $prefix, $string, $number, $link, $abbreviate, $language ){
790858 $abbr = $abbreviate ? '-abbr' : '';
791859 $prefix = $prefix === null
792860 ? ''
Index: branches/REL1_18/extensions/ParserFunctions/ParserFunctions.php
@@ -29,6 +29,23 @@
3030 */
3131 $wgPFEnableStringFunctions = false;
3232
 33+/**
 34+ * Enable Convert parser for converting between units of measurement
 35+ */
 36+$wgPFEnableConvert = false;
 37+
 38+/**
 39+ * The language for 'en' is actually 'en-us', which insists on using non-canonical translations
 40+ * of the SI base units ("meter" rather than "metre" and "liter" rather than "litre"). We
 41+ * can avoid contaminatng dialects by internally mapping languages by default; this is
 42+ * configurable so you can remove it if you like, or add other maps if that's useful.
 43+ * Essentially, if your wiki's $wgContLang appears as a key in this array, the value is
 44+ * what is used as the default language for {{#convert}} output.
 45+ */
 46+$wgPFUnitLanguageVariants = array(
 47+ 'en' => 'en-gb'
 48+);
 49+
3350 /** REGISTRATION */
3451 $wgExtensionCredits['parserhook'][] = array(
3552 'path' => __FILE__,
@@ -53,7 +70,7 @@
5471 $wgHooks['ParserFirstCallInit'][] = 'wfRegisterParserFunctions';
5572
5673 function wfRegisterParserFunctions( $parser ) {
57 - global $wgPFEnableStringFunctions;
 74+ global $wgPFEnableStringFunctions, $wgPFEnableConvert;
5875
5976 if ( defined( get_class( $parser ) . '::SFH_OBJECT_ARGS' ) ) {
6077 // These functions accept DOM-style arguments
@@ -77,7 +94,6 @@
7895 $parser->setFunctionHook( 'timel', 'ExtParserFunctions::localTime' );
7996 $parser->setFunctionHook( 'rel2abs', 'ExtParserFunctions::rel2abs' );
8097 $parser->setFunctionHook( 'titleparts', 'ExtParserFunctions::titleparts' );
81 - $parser->setFunctionHook( 'convert', 'ExtParserFunctions::convert' );
8298
8399 // String Functions
84100 if ( $wgPFEnableStringFunctions ) {
@@ -91,5 +107,9 @@
92108 $parser->setFunctionHook( 'urldecode', 'ExtParserFunctions::runUrlDecode' );
93109 }
94110
 111+ if( $wgPFEnableConvert ) {
 112+ $parser->setFunctionHook( 'convert', 'ExtParserFunctions::convert' );
 113+ }
 114+
95115 return true;
96116 }
Property changes on: branches/REL1_18/extensions/ParserFunctions
___________________________________________________________________
Modified: svn:mergeinfo
97117 Merged /trunk/extensions/ParserFunctions:r96499,96502,96505

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r96499Follow-up r81074:...happy-melon21:06, 7 September 2011
r96502Follow up r81074: implement a language map per Tim's suggestion (no more Amer...happy-melon21:28, 7 September 2011
r96505FU 96499, r81074: fix parser tests to reflect the change in default dialect, ...happy-melon21:39, 7 September 2011

Status & tagging log