allow combined width/height param in {{filepath:}}
authorumherirrender <umherirrender_de.wp@web.de>
Wed, 25 Jul 2012 15:31:47 +0000 (17:31 +0200)
committerumherirrender <umherirrender_de.wp@web.de>
Wed, 25 Jul 2012 15:31:47 +0000 (17:31 +0200)
Using the same regex like [[File:|]]
With heigth, the width inside the thumb link can be calculated, if the
height not fit in the width.

Change-Id: If188d923d6cd25ea6a5118098f3a513ca5135d43

includes/parser/CoreParserFunctions.php
includes/parser/Parser.php

index 912de41..4d5ebe6 100644 (file)
@@ -754,40 +754,34 @@ class CoreParserFunctions {
        }
 
        // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
+       // or {{filepath|300px}}, {{filepath|200x300px}}, {{filepath|nowiki|200x300px}}, {{filepath|200x300px|nowiki}}
        public static function filepath( $parser, $name='', $argA='', $argB='' ) {
                $file = wfFindFile( $name );
-               $size = '';
-               $argA_int = intval( $argA );
-               $argB_int = intval( $argB );
-
-               if ( $argB_int > 0 ) {
-                       // {{filepath: | option | size }}
-                       $size = $argB_int;
-                       $option = $argA;
-
-               } elseif ( $argA_int > 0 ) {
-                       // {{filepath: | size [|option] }}
-                       $size = $argA_int;
-                       $option = $argB;
+               $isNowiki = false;
 
+               if( $argA == 'nowiki' ) {
+                       // {{filepath: | option [| size] }}
+                       $isNowiki = true;
+                       $parsedWidthParam = $parser->parseWidthParam( $argB );
                } else {
-                       // {{filepath: [|option] }}
-                       $option = $argA;
+                       // {{filepath: [| size [|option]] }}
+                       $parsedWidthParam = $parser->parseWidthParam( $argA );
+                       $isNowiki = ($argB == 'nowiki');
                }
 
                if ( $file ) {
                        $url = $file->getFullUrl();
 
                        // If a size is requested...
-                       if ( is_integer( $size ) ) {
-                               $mto = $file->transform( array( 'width' => $size ) );
+                       if ( count( $parsedWidthParam ) ) {
+                               $mto = $file->transform( $parsedWidthParam );
                                // ... and we can
                                if ( $mto && !$mto->isError() ) {
                                        // ... change the URL to point to a thumbnail.
                                        $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE );
                                }
                        }
-                       if ( $option == 'nowiki' ) {
+                       if ( $isNowiki ) {
                                return array( $url, 'nowiki' => true );
                        }
                        return $url;
index 7991ca6..5fcc962 100644 (file)
@@ -5036,27 +5036,22 @@ class Parser {
 
                                # Special case; width and height come in one variable together
                                if ( $type === 'handler' && $paramName === 'width' ) {
-                                       $m = array();
-                                       # (bug 13500) In both cases (width/height and width only),
-                                       # permit trailing "px" for backward compatibility.
-                                       if ( preg_match( '/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
-                                               $width = intval( $m[1] );
-                                               $height = intval( $m[2] );
+                                       $parsedWidthParam = $this->parseWidthParam( $value );
+                                       if( isset( $parsedWidthParam['width'] ) ) {
+                                               $width = $parsedWidthParam['width'];
                                                if ( $handler->validateParam( 'width', $width ) ) {
                                                        $params[$type]['width'] = $width;
                                                        $validated = true;
                                                }
+                                       }
+                                       if( isset( $parsedWidthParam['height'] ) ) {
+                                               $height = $parsedWidthParam['height'];
                                                if ( $handler->validateParam( 'height', $height ) ) {
                                                        $params[$type]['height'] = $height;
                                                        $validated = true;
                                                }
-                                       } elseif ( preg_match( '/^[0-9]*\s*(?:px)?\s*$/', $value ) ) {
-                                               $width = intval( $value );
-                                               if ( $handler->validateParam( 'width', $width ) ) {
-                                                       $params[$type]['width'] = $width;
-                                                       $validated = true;
-                                               }
-                                       } # else no validation -- bug 13436
+                                       }
+                                       # else no validation -- bug 13436
                                } else {
                                        if ( $type === 'handler' ) {
                                                # Validate handler parameter
@@ -5775,4 +5770,32 @@ class Parser {
        function isValidHalfParsedText( $data ) {
                return isset( $data['version'] ) && $data['version'] == self::HALF_PARSED_VERSION;
        }
+
+       /**
+        * Parsed a width param of imagelink like 300px or 200x300px
+        *
+        * @param $value String
+        *
+        * @return array
+        * @since 1.20
+        */
+       public function parseWidthParam( $value ) {
+               $parsedWidthParam = array();
+               if( $value === '' ) {
+                       return $parsedWidthParam;
+               }
+               $m = array();
+               # (bug 13500) In both cases (width/height and width only),
+               # permit trailing "px" for backward compatibility.
+               if ( preg_match( '/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
+                       $width = intval( $m[1] );
+                       $height = intval( $m[2] );
+                       $parsedWidthParam['width'] = $width;
+                       $parsedWidthParam['height'] = $height;
+               } elseif ( preg_match( '/^[0-9]*\s*(?:px)?\s*$/', $value ) ) {
+                       $width = intval( $value );
+                       $parsedWidthParam['width'] = $width;
+               }
+               return $parsedWidthParam;
+       }
 }