00001 <?php
00010 function wfScaleSVGUnit( $length, $viewportSize=512 ) {
00011 static $unitLength = array(
00012 'px' => 1.0,
00013 'pt' => 1.25,
00014 'pc' => 15.0,
00015 'mm' => 3.543307,
00016 'cm' => 35.43307,
00017 'in' => 90.0,
00018 'em' => 16.0,
00019 'ex' => 12.0,
00020 '' => 1.0,
00021 );
00022 $matches = array();
00023 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
00024 $length = floatval( $matches[1] );
00025 $unit = $matches[2];
00026 if( $unit == '%' ) {
00027 return $length * 0.01 * $viewportSize;
00028 } else {
00029 return $length * $unitLength[$unit];
00030 }
00031 } else {
00032
00033 return floatval( $length );
00034 }
00035 }
00036
00037 class XmlSizeFilter {
00038 const DEFAULT_WIDTH = 512;
00039 const DEFAULT_HEIGHT = 512;
00040 var $first = true;
00041 var $width = self::DEFAULT_WIDTH;
00042 var $height = self::DEFAULT_HEIGHT;
00043 function filter( $name, $attribs ) {
00044 if( $this->first ) {
00045 $defaultWidth = self::DEFAULT_WIDTH;
00046 $defaultHeight = self::DEFAULT_HEIGHT;
00047 $aspect = 1.0;
00048 $width = null;
00049 $height = null;
00050
00051 if( isset( $attribs['viewBox'] ) ) {
00052
00053 $viewBox = preg_split( '/\s+/', trim( $attribs['viewBox'] ) );
00054 if( count( $viewBox ) == 4 ) {
00055 $viewWidth = wfScaleSVGUnit( $viewBox[2] );
00056 $viewHeight = wfScaleSVGUnit( $viewBox[3] );
00057 if( $viewWidth > 0 && $viewHeight > 0 ) {
00058 $aspect = $viewWidth / $viewHeight;
00059 $defaultHeight = $defaultWidth / $aspect;
00060 }
00061 }
00062 }
00063 if( isset( $attribs['width'] ) ) {
00064 $width = wfScaleSVGUnit( $attribs['width'], $defaultWidth );
00065 }
00066 if( isset( $attribs['height'] ) ) {
00067 $height = wfScaleSVGUnit( $attribs['height'], $defaultHeight );
00068 }
00069
00070 if( !isset( $width ) && !isset( $height ) ) {
00071 $width = $defaultWidth;
00072 $height = $width / $aspect;
00073 } elseif( isset( $width ) && !isset( $height ) ) {
00074 $height = $width / $aspect;
00075 } elseif( isset( $height ) && !isset( $width ) ) {
00076 $width = $height * $aspect;
00077 }
00078
00079 if( $width > 0 && $height > 0 ) {
00080 $this->width = intval( round( $width ) );
00081 $this->height = intval( round( $height ) );
00082 }
00083
00084 $this->first = false;
00085 }
00086 }
00087 }
00088
00098 function wfGetSVGsize( $filename ) {
00099 $filter = new XmlSizeFilter();
00100 $xml = new XmlTypeCheck( $filename, array( $filter, 'filter' ) );
00101 if( $xml->wellFormed ) {
00102 return array( $filter->width, $filter->height, 'SVG',
00103 "width=\"$filter->width\" height=\"$filter->height\"" );
00104 }
00105
00106 return false;
00107 }
00108
00122 function wfIsBadImage( $name, $contextTitle = false ) {
00123 static $badImages = false;
00124 wfProfileIn( __METHOD__ );
00125
00126 # Run the extension hook
00127 $bad = false;
00128 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
00129 wfProfileOut( __METHOD__ );
00130 return $bad;
00131 }
00132
00133 if( !$badImages ) {
00134 # Build the list now
00135 $badImages = array();
00136 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
00137 foreach( $lines as $line ) {
00138 # List items only
00139 if ( substr( $line, 0, 1 ) !== '*' ) {
00140 continue;
00141 }
00142
00143 # Find all links
00144 $m = array();
00145 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
00146 continue;
00147 }
00148
00149 $exceptions = array();
00150 $imageDBkey = false;
00151 foreach ( $m[1] as $i => $titleText ) {
00152 $title = Title::newFromText( $titleText );
00153 if ( !is_null( $title ) ) {
00154 if ( $i == 0 ) {
00155 $imageDBkey = $title->getDBkey();
00156 } else {
00157 $exceptions[$title->getPrefixedDBkey()] = true;
00158 }
00159 }
00160 }
00161
00162 if ( $imageDBkey !== false ) {
00163 $badImages[$imageDBkey] = $exceptions;
00164 }
00165 }
00166 }
00167
00168 $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
00169 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
00170 wfProfileOut( __METHOD__ );
00171 return $bad;
00172 }
00173
00182 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
00183 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
00184 $roundedUp = ceil( $idealWidth );
00185 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
00186 return floor( $idealWidth );
00187 else
00188 return $roundedUp;
00189 }