Revert r44257 "Use boolean searching with '?' char"
[lhc/web/wiklou.git] / includes / ImageFunctions.php
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
5 *
6 * @param $length String: CSS/SVG length.
7 * @return Integer: length in pixels
8 */
9 function wfScaleSVGUnit( $length ) {
10 static $unitLength = array(
11 'px' => 1.0,
12 'pt' => 1.25,
13 'pc' => 15.0,
14 'mm' => 3.543307,
15 'cm' => 35.43307,
16 'in' => 90.0,
17 '' => 1.0, // "User units" pixels by default
18 '%' => 2.0, // Fake it!
19 );
20 $matches = array();
21 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
22 $length = floatval( $matches[1] );
23 $unit = $matches[2];
24 return round( $length * $unitLength[$unit] );
25 } else {
26 // Assume pixels
27 return round( floatval( $length ) );
28 }
29 }
30
31 class XmlSizeFilter {
32 var $first = true;
33 var $width = 256;
34 var $height = 256;
35 function filter( $name, $attribs ) {
36 if( $this->first ) {
37 if( isset( $attribs['width'] ) ) {
38 $this->width = wfScaleSVGUnit( $attribs['width'] );
39 }
40 if( isset( $attribs['height'] ) ) {
41 $this->height = wfScaleSVGUnit( $attribs['height'] );
42 }
43 $this->first = false;
44 }
45 }
46 }
47
48 /**
49 * Compatible with PHP getimagesize()
50 * @todo support gzipped SVGZ
51 * @todo check XML more carefully
52 * @todo sensible defaults
53 *
54 * @param $filename String: full name of the file (passed to php fopen()).
55 * @return array
56 */
57 function wfGetSVGsize( $filename ) {
58 $filter = new XmlSizeFilter();
59 $xml = new XmlTypeCheck( $filename, array( $filter, 'filter' ) );
60 if( $xml->wellFormed ) {
61 return array( $filter->width, $filter->height, 'SVG',
62 "width=\"$filter->width\" height=\"$filter->height\"" );
63 }
64
65 return false;
66 }
67
68 /**
69 * Determine if an image exists on the 'bad image list'.
70 *
71 * The format of MediaWiki:Bad_image_list is as follows:
72 * * Only list items (lines starting with "*") are considered
73 * * The first link on a line must be a link to a bad image
74 * * Any subsequent links on the same line are considered to be exceptions,
75 * i.e. articles where the image may occur inline.
76 *
77 * @param $name string the image name to check
78 * @param $contextTitle Title: the page on which the image occurs, if known
79 * @return bool
80 */
81 function wfIsBadImage( $name, $contextTitle = false ) {
82 static $badImages = false;
83 wfProfileIn( __METHOD__ );
84
85 # Run the extension hook
86 $bad = false;
87 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
88 wfProfileOut( __METHOD__ );
89 return $bad;
90 }
91
92 if( !$badImages ) {
93 # Build the list now
94 $badImages = array();
95 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
96 foreach( $lines as $line ) {
97 # List items only
98 if ( substr( $line, 0, 1 ) !== '*' ) {
99 continue;
100 }
101
102 # Find all links
103 $m = array();
104 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
105 continue;
106 }
107
108 $exceptions = array();
109 $imageDBkey = false;
110 foreach ( $m[1] as $i => $titleText ) {
111 $title = Title::newFromText( $titleText );
112 if ( !is_null( $title ) ) {
113 if ( $i == 0 ) {
114 $imageDBkey = $title->getDBkey();
115 } else {
116 $exceptions[$title->getPrefixedDBkey()] = true;
117 }
118 }
119 }
120
121 if ( $imageDBkey !== false ) {
122 $badImages[$imageDBkey] = $exceptions;
123 }
124 }
125 }
126
127 $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
128 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
129 wfProfileOut( __METHOD__ );
130 return $bad;
131 }
132
133 /**
134 * Calculate the largest thumbnail width for a given original file size
135 * such that the thumbnail's height is at most $maxHeight.
136 * @param $boxWidth Integer Width of the thumbnail box.
137 * @param $boxHeight Integer Height of the thumbnail box.
138 * @param $maxHeight Integer Maximum height expected for the thumbnail.
139 * @return Integer.
140 */
141 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
142 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
143 $roundedUp = ceil( $idealWidth );
144 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
145 return floor( $idealWidth );
146 else
147 return $roundedUp;
148 }