(bug 19441) Updated date formatting for Lithuanian
[mediawiki.git] / includes / ImageFunctions.php
blob5f01ab6ef8c13d79eb71bda983f04ea4727de4f8
1 <?php
2 /**
3 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
4 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
6 * @param $length String: CSS/SVG length.
7 * @param $viewportSize: Float optional scale for percentage units...
8 * @return float: length in pixels
9 */
10 function wfScaleSVGUnit( $length, $viewportSize=512 ) {
11 static $unitLength = array(
12 'px' => 1.0,
13 'pt' => 1.25,
14 'pc' => 15.0,
15 'mm' => 3.543307,
16 'cm' => 35.43307,
17 'in' => 90.0,
18 'em' => 16.0, // fake it?
19 'ex' => 12.0, // fake it?
20 '' => 1.0, // "User units" pixels by default
22 $matches = array();
23 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
24 $length = floatval( $matches[1] );
25 $unit = $matches[2];
26 if( $unit == '%' ) {
27 return $length * 0.01 * $viewportSize;
28 } else {
29 return $length * $unitLength[$unit];
31 } else {
32 // Assume pixels
33 return floatval( $length );
37 class XmlSizeFilter {
38 const DEFAULT_WIDTH = 512;
39 const DEFAULT_HEIGHT = 512;
40 var $first = true;
41 var $width = self::DEFAULT_WIDTH;
42 var $height = self::DEFAULT_HEIGHT;
43 function filter( $name, $attribs ) {
44 if( $this->first ) {
45 $defaultWidth = self::DEFAULT_WIDTH;
46 $defaultHeight = self::DEFAULT_HEIGHT;
47 $aspect = 1.0;
48 $width = null;
49 $height = null;
51 if( isset( $attribs['viewBox'] ) ) {
52 // min-x min-y width height
53 $viewBox = preg_split( '/\s+/', trim( $attribs['viewBox'] ) );
54 if( count( $viewBox ) == 4 ) {
55 $viewWidth = wfScaleSVGUnit( $viewBox[2] );
56 $viewHeight = wfScaleSVGUnit( $viewBox[3] );
57 if( $viewWidth > 0 && $viewHeight > 0 ) {
58 $aspect = $viewWidth / $viewHeight;
59 $defaultHeight = $defaultWidth / $aspect;
63 if( isset( $attribs['width'] ) ) {
64 $width = wfScaleSVGUnit( $attribs['width'], $defaultWidth );
66 if( isset( $attribs['height'] ) ) {
67 $height = wfScaleSVGUnit( $attribs['height'], $defaultHeight );
70 if( !isset( $width ) && !isset( $height ) ) {
71 $width = $defaultWidth;
72 $height = $width / $aspect;
73 } elseif( isset( $width ) && !isset( $height ) ) {
74 $height = $width / $aspect;
75 } elseif( isset( $height ) && !isset( $width ) ) {
76 $width = $height * $aspect;
79 if( $width > 0 && $height > 0 ) {
80 $this->width = intval( round( $width ) );
81 $this->height = intval( round( $height ) );
84 $this->first = false;
89 /**
90 * Compatible with PHP getimagesize()
91 * @todo support gzipped SVGZ
92 * @todo check XML more carefully
93 * @todo sensible defaults
95 * @param $filename String: full name of the file (passed to php fopen()).
96 * @return array
98 function wfGetSVGsize( $filename ) {
99 $filter = new XmlSizeFilter();
100 $xml = new XmlTypeCheck( $filename, array( $filter, 'filter' ) );
101 if( $xml->wellFormed ) {
102 return array( $filter->width, $filter->height, 'SVG',
103 "width=\"$filter->width\" height=\"$filter->height\"" );
106 return false;
110 * Determine if an image exists on the 'bad image list'.
112 * The format of MediaWiki:Bad_image_list is as follows:
113 * * Only list items (lines starting with "*") are considered
114 * * The first link on a line must be a link to a bad image
115 * * Any subsequent links on the same line are considered to be exceptions,
116 * i.e. articles where the image may occur inline.
118 * @param $name string the image name to check
119 * @param $contextTitle Title: the page on which the image occurs, if known
120 * @return bool
122 function wfIsBadImage( $name, $contextTitle = false ) {
123 static $badImages = false;
124 wfProfileIn( __METHOD__ );
126 # Handle redirects
127 $redirectTitle = RepoGroup::singleton()->checkRedirect( Title::makeTitle( NS_FILE, $name ) );
128 if( $redirectTitle ) {
129 $name = $redirectTitle->getDbKey();
132 # Run the extension hook
133 $bad = false;
134 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
135 wfProfileOut( __METHOD__ );
136 return $bad;
139 if( !$badImages ) {
140 # Build the list now
141 $badImages = array();
142 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
143 foreach( $lines as $line ) {
144 # List items only
145 if ( substr( $line, 0, 1 ) !== '*' ) {
146 continue;
149 # Find all links
150 $m = array();
151 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
152 continue;
155 $exceptions = array();
156 $imageDBkey = false;
157 foreach ( $m[1] as $i => $titleText ) {
158 $title = Title::newFromText( $titleText );
159 if ( !is_null( $title ) ) {
160 if ( $i == 0 ) {
161 $imageDBkey = $title->getDBkey();
162 } else {
163 $exceptions[$title->getPrefixedDBkey()] = true;
168 if ( $imageDBkey !== false ) {
169 $badImages[$imageDBkey] = $exceptions;
174 $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
175 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
176 wfProfileOut( __METHOD__ );
177 return $bad;
181 * Calculate the largest thumbnail width for a given original file size
182 * such that the thumbnail's height is at most $maxHeight.
183 * @param $boxWidth Integer Width of the thumbnail box.
184 * @param $boxHeight Integer Height of the thumbnail box.
185 * @param $maxHeight Integer Maximum height expected for the thumbnail.
186 * @return Integer.
188 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
189 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
190 $roundedUp = ceil( $idealWidth );
191 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
192 return floor( $idealWidth );
193 else
194 return $roundedUp;