| Index: trunk/extensions/ParserFunctions/Convert.php |
| — | — | @@ -24,8 +24,14 @@ |
| 25 | 25 | # A regex *FRAGMENT* which matches SI prefixes |
| 26 | 26 | const PREFIX_REGEX = '[YZEPTGMkh(da)dcm\x{03BC}\x{00B5}npfazy]?'; |
| 27 | 27 | |
| 28 | | - # ConvertUnit objects |
| | 28 | + /** |
| | 29 | + * @var ConvertUnit |
| | 30 | + */ |
| 29 | 31 | protected $sourceUnit; |
| | 32 | + |
| | 33 | + /** |
| | 34 | + * @var ConvertUnit |
| | 35 | + */ |
| 30 | 36 | protected $targetUnit; |
| 31 | 37 | |
| 32 | 38 | # Whether to abbreviate the output unit |
| — | — | @@ -48,6 +54,9 @@ |
| 49 | 55 | # The last value converted, which will be used for PLURAL evaluation |
| 50 | 56 | protected $lastValue; |
| 51 | 57 | |
| | 58 | + /** |
| | 59 | + * Reset the parser so it isn't contaminated by the results of previous parses |
| | 60 | + */ |
| 52 | 61 | public function clearState(){ |
| 53 | 62 | # Make sure we break any references set up in the parameter passing below |
| 54 | 63 | unset( $this->sourceUnit ); |
| — | — | @@ -176,6 +185,11 @@ |
| 177 | 186 | return $this->processString( $string ); |
| 178 | 187 | } |
| 179 | 188 | |
| | 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 | + */ |
| 180 | 194 | protected function deduceSourceUnit( $string ){ |
| 181 | 195 | # Get the unit from the end of the string |
| 182 | 196 | $matches = array(); |
| — | — | @@ -191,6 +205,7 @@ |
| 192 | 206 | /** |
| 193 | 207 | * Identify the values to be converted, and convert them |
| 194 | 208 | * @param $string String |
| | 209 | + * @return String |
| 195 | 210 | */ |
| 196 | 211 | protected function processString( $string ){ |
| 197 | 212 | # Replace values |
| — | — | @@ -216,7 +231,7 @@ |
| 217 | 232 | * Express a value in the $sourceUnit in terms of the $targetUnit, preserving |
| 218 | 233 | * an appropriate degree of accuracy. |
| 219 | 234 | * @param $value String |
| 220 | | - * @return void |
| | 235 | + * @return String |
| 221 | 236 | */ |
| 222 | 237 | public function convert( $value ){ |
| 223 | 238 | global $wgContLang; |
| — | — | @@ -374,6 +389,11 @@ |
| 375 | 390 | public $value; |
| 376 | 391 | protected $name; |
| 377 | 392 | |
| | 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 | + */ |
| 378 | 398 | public function __construct( $var, $var2=null ){ |
| 379 | 399 | static $legalDimensionsFlip; |
| 380 | 400 | |
| — | — | @@ -418,8 +438,8 @@ |
| 419 | 439 | if( in_array( $this->value, array_keys( self::$legalDimensions ) ) ){ |
| 420 | 440 | $this->name = self::$legalDimensions[$this->value]; |
| 421 | 441 | $this->compoundName = array( |
| 422 | | - self::$legalDimensions[$var], |
| 423 | | - self::$legalDimensions[$var2], |
| | 442 | + self::$legalDimensions[$dim], |
| | 443 | + self::$legalDimensions[$dim2], |
| 424 | 444 | ); |
| 425 | 445 | } else { |
| 426 | 446 | # Some combinations of units are fine (carats per bushel is a perfectly good, |
| — | — | @@ -442,6 +462,8 @@ |
| 443 | 463 | |
| 444 | 464 | /** |
| 445 | 465 | * 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 |
| 446 | 468 | * @return String|Array of String |
| 447 | 469 | */ |
| 448 | 470 | public function getName( $expandCompound = false ){ |
| — | — | @@ -630,7 +652,9 @@ |
| 631 | 653 | |
| 632 | 654 | /***************** MEMBER VARIABLES *****************/ |
| 633 | 655 | |
| 634 | | - # @var ConvertDimension |
| | 656 | + /** |
| | 657 | + * @var ConvertDimension |
| | 658 | + */ |
| 635 | 659 | public $dimension; |
| 636 | 660 | |
| 637 | 661 | # What number you need to multiply this unit by to get the equivalent |
| — | — | @@ -665,6 +689,10 @@ |
| 666 | 690 | $this->parseUnit( $rawUnit ); |
| 667 | 691 | } |
| 668 | 692 | |
| | 693 | + /** |
| | 694 | + * Parse a raw unit string, and populate member variables |
| | 695 | + * @param $rawUnit String |
| | 696 | + */ |
| 669 | 697 | protected function parseUnit( $rawUnit ){ |
| 670 | 698 | |
| 671 | 699 | # Do mappings like 'mph' --> 'mi/h' |
| — | — | @@ -724,12 +752,39 @@ |
| 725 | 753 | } |
| 726 | 754 | } |
| 727 | 755 | |
| | 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 | + */ |
| 728 | 761 | public function getConversion(){ |
| 729 | | - return $this->prefix |
| 730 | | - ? $this->conversion * self::$prefixes[$this->prefix][0] |
| 731 | | - : $this->conversion; |
| | 762 | + return $this->conversion * $this->getPrefixConversion(); |
| 732 | 763 | } |
| 733 | 764 | |
| | 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 | + */ |
| 734 | 789 | public function getRegex(){ |
| 735 | 790 | return $this->regex; |
| 736 | 791 | } |
| — | — | @@ -739,6 +794,7 @@ |
| 740 | 795 | * @param $string String Original text, with the number converted |
| 741 | 796 | * @param $value String number for PLURAL support |
| 742 | 797 | * @param $link Bool |
| | 798 | + * @param $abbreviate Bool |
| 743 | 799 | * @param $language Language |
| 744 | 800 | * @return String |
| 745 | 801 | */ |
| — | — | @@ -787,6 +843,18 @@ |
| 788 | 844 | return trim( $msgText ); |
| 789 | 845 | } |
| 790 | 846 | |
| | 847 | + /** |
| | 848 | + * Retrieve the unit text from actual messages |
| | 849 | + * @param $dimension String |
| | 850 | + * @param $unit String |
| | 851 | + * @param $prefix String |
| | 852 | + * @param $string String |
| | 853 | + * @param $number String the actual value (for {{PLURAL}} etc) |
| | 854 | + * @param $link Bool |
| | 855 | + * @param $abbreviate Bool |
| | 856 | + * @param $language Language|Bool|null |
| | 857 | + * @return String |
| | 858 | + */ |
| 791 | 859 | protected function getTextFromMessage( $dimension, $unit, $prefix, $string, $number, $link, $abbreviate, $language ){ |
| 792 | 860 | $abbr = $abbreviate ? '-abbr' : ''; |
| 793 | 861 | $prefix = $prefix === null |
| Index: trunk/extensions/ParserFunctions/ParserFunctions.php |
| — | — | @@ -29,6 +29,11 @@ |
| 30 | 30 | */ |
| 31 | 31 | $wgPFEnableStringFunctions = false; |
| 32 | 32 | |
| | 33 | +/** |
| | 34 | + * Enable Convert parser for converting between units of measurement |
| | 35 | + */ |
| | 36 | +$wgPFEnableConvert = false; |
| | 37 | + |
| 33 | 38 | /** REGISTRATION */ |
| 34 | 39 | $wgExtensionCredits['parserhook'][] = array( |
| 35 | 40 | 'path' => __FILE__, |
| — | — | @@ -53,7 +58,7 @@ |
| 54 | 59 | $wgHooks['ParserFirstCallInit'][] = 'wfRegisterParserFunctions'; |
| 55 | 60 | |
| 56 | 61 | function wfRegisterParserFunctions( $parser ) { |
| 57 | | - global $wgPFEnableStringFunctions; |
| | 62 | + global $wgPFEnableStringFunctions, $wgPFEnableConvert; |
| 58 | 63 | |
| 59 | 64 | if ( defined( get_class( $parser ) . '::SFH_OBJECT_ARGS' ) ) { |
| 60 | 65 | // These functions accept DOM-style arguments |
| — | — | @@ -77,7 +82,6 @@ |
| 78 | 83 | $parser->setFunctionHook( 'timel', 'ExtParserFunctions::localTime' ); |
| 79 | 84 | $parser->setFunctionHook( 'rel2abs', 'ExtParserFunctions::rel2abs' ); |
| 80 | 85 | $parser->setFunctionHook( 'titleparts', 'ExtParserFunctions::titleparts' ); |
| 81 | | - $parser->setFunctionHook( 'convert', 'ExtParserFunctions::convert' ); |
| 82 | 86 | |
| 83 | 87 | // String Functions |
| 84 | 88 | if ( $wgPFEnableStringFunctions ) { |
| — | — | @@ -91,5 +95,9 @@ |
| 92 | 96 | $parser->setFunctionHook( 'urldecode', 'ExtParserFunctions::runUrlDecode' ); |
| 93 | 97 | } |
| 94 | 98 | |
| | 99 | + if( $wgPFEnableConvert ) { |
| | 100 | + $parser->setFunctionHook( 'convert', 'ExtParserFunctions::convert' ); |
| | 101 | + } |
| | 102 | + |
| 95 | 103 | return true; |
| 96 | 104 | } |