3 * Global functions related to images
9 * Determine if an image exists on the 'bad image list'.
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.
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
22 function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
23 static $badImageCache = null; // based on bad_image_list msg
24 wfProfileIn( __METHOD__
);
27 $redirectTitle = RepoGroup
::singleton()->checkRedirect( Title
::makeTitle( NS_FILE
, $name ) );
28 if( $redirectTitle ) {
29 $name = $redirectTitle->getDbKey();
32 # Run the extension hook
34 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
35 wfProfileOut( __METHOD__
);
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
48 $lines = explode( "\n", $blacklist );
49 foreach( $lines as $line ) {
51 if ( substr( $line, 0, 1 ) !== '*' ) {
57 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
61 $exceptions = array();
63 foreach ( $m[1] as $i => $titleText ) {
64 $title = Title
::newFromText( $titleText );
65 if ( !is_null( $title ) ) {
67 $imageDBkey = $title->getDBkey();
69 $exceptions[$title->getPrefixedDBkey()] = true;
74 if ( $imageDBkey !== false ) {
75 $badImages[$imageDBkey] = $exceptions;
79 $badImageCache = $badImages;
83 $contextKey = $contextTitle ?
$contextTitle->getPrefixedDBkey() : false;
84 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
85 wfProfileOut( __METHOD__
);