Spaces to tabs
[lhc/web/wiklou.git] / includes / ImageFunctions.php
1 <?php
2 /**
3 * Global functions related to images
4 *
5 * @file
6 */
7
8 /**
9 * Determine if an image exists on the 'bad image list'.
10 *
11 * The format of MediaWiki:Bad_image_list is as follows:
12 * * Only list items (lines starting with "*") are considered
13 * * The first link on a line must be a link to a bad image
14 * * Any subsequent links on the same line are considered to be exceptions,
15 * i.e. articles where the image may occur inline.
16 *
17 * @param $name string the image name to check
18 * @param $contextTitle Title: the page on which the image occurs, if known
19 * @return bool
20 */
21 function wfIsBadImage( $name, $contextTitle = false ) {
22 static $badImages = false;
23 wfProfileIn( __METHOD__ );
24
25 # Handle redirects
26 $redirectTitle = RepoGroup::singleton()->checkRedirect( Title::makeTitle( NS_FILE, $name ) );
27 if( $redirectTitle ) {
28 $name = $redirectTitle->getDbKey();
29 }
30
31 # Run the extension hook
32 $bad = false;
33 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
34 wfProfileOut( __METHOD__ );
35 return $bad;
36 }
37
38 if( !$badImages ) {
39 # Build the list now
40 $badImages = array();
41 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
42 foreach( $lines as $line ) {
43 # List items only
44 if ( substr( $line, 0, 1 ) !== '*' ) {
45 continue;
46 }
47
48 # Find all links
49 $m = array();
50 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
51 continue;
52 }
53
54 $exceptions = array();
55 $imageDBkey = false;
56 foreach ( $m[1] as $i => $titleText ) {
57 $title = Title::newFromText( $titleText );
58 if ( !is_null( $title ) ) {
59 if ( $i == 0 ) {
60 $imageDBkey = $title->getDBkey();
61 } else {
62 $exceptions[$title->getPrefixedDBkey()] = true;
63 }
64 }
65 }
66
67 if ( $imageDBkey !== false ) {
68 $badImages[$imageDBkey] = $exceptions;
69 }
70 }
71 }
72
73 $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
74 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
75 wfProfileOut( __METHOD__ );
76 return $bad;
77 }
78
79 /**
80 * Calculate the largest thumbnail width for a given original file size
81 * such that the thumbnail's height is at most $maxHeight.
82 * @param $boxWidth Integer Width of the thumbnail box.
83 * @param $boxHeight Integer Height of the thumbnail box.
84 * @param $maxHeight Integer Maximum height expected for the thumbnail.
85 * @return Integer.
86 */
87 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
88 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
89 $roundedUp = ceil( $idealWidth );
90 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
91 return floor( $idealWidth );
92 else
93 return $roundedUp;
94 }