* Allow passing in a blacklist into wfIsBadImage()
[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|bool the page on which the image occurs, if known
19 * @param $blacklist string wikitext of a file blacklist
20 * @return bool
21 */
22 function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
23 static $badImageCache = null; // based on bad_image_list msg
24 wfProfileIn( __METHOD__ );
25
26 # Handle redirects
27 $redirectTitle = RepoGroup::singleton()->checkRedirect( Title::makeTitle( NS_FILE, $name ) );
28 if( $redirectTitle ) {
29 $name = $redirectTitle->getDbKey();
30 }
31
32 # Run the extension hook
33 $bad = false;
34 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
35 wfProfileOut( __METHOD__ );
36 return $bad;
37 }
38
39 $cacheable = ( $blacklist === null );
40 if( $cacheable && $badImageCache !== null ) {
41 $badImages = $badImageCache;
42 } else { // cache miss
43 if ( $blacklist === null ) {
44 $blacklist = wfMsgForContentNoTrans( 'bad_image_list' ); // site list
45 }
46 # Build the list now
47 $badImages = array();
48 $lines = explode( "\n", $blacklist );
49 foreach( $lines as $line ) {
50 # List items only
51 if ( substr( $line, 0, 1 ) !== '*' ) {
52 continue;
53 }
54
55 # Find all links
56 $m = array();
57 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
58 continue;
59 }
60
61 $exceptions = array();
62 $imageDBkey = false;
63 foreach ( $m[1] as $i => $titleText ) {
64 $title = Title::newFromText( $titleText );
65 if ( !is_null( $title ) ) {
66 if ( $i == 0 ) {
67 $imageDBkey = $title->getDBkey();
68 } else {
69 $exceptions[$title->getPrefixedDBkey()] = true;
70 }
71 }
72 }
73
74 if ( $imageDBkey !== false ) {
75 $badImages[$imageDBkey] = $exceptions;
76 }
77 }
78 if ( $cacheable ) {
79 $badImageCache = $badImages;
80 }
81 }
82
83 $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
84 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
85 wfProfileOut( __METHOD__ );
86 return $bad;
87 }