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