From: Aaron Schulz Date: Thu, 27 Oct 2011 00:23:23 +0000 (+0000) Subject: * Allow passing in a blacklist into wfIsBadImage() X-Git-Tag: 1.31.0-rc.0~26893 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=e8dfaccd3bc16592815f78145956303f4882e730;p=lhc%2Fweb%2Fwiklou.git * Allow passing in a blacklist into wfIsBadImage() * Added wfIsBadImage() unit tests --- diff --git a/includes/ImageFunctions.php b/includes/ImageFunctions.php index 471465d08e..4b90e24aac 100644 --- a/includes/ImageFunctions.php +++ b/includes/ImageFunctions.php @@ -16,10 +16,11 @@ * * @param $name string the image name to check * @param $contextTitle Title|bool the page on which the image occurs, if known + * @param $blacklist string wikitext of a file blacklist * @return bool */ -function wfIsBadImage( $name, $contextTitle = false ) { - static $badImages = null; +function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) { + static $badImageCache = null; // based on bad_image_list msg wfProfileIn( __METHOD__ ); # Handle redirects @@ -34,11 +35,17 @@ function wfIsBadImage( $name, $contextTitle = false ) { wfProfileOut( __METHOD__ ); return $bad; } - - if( $badImages === null ) { + + $cacheable = ( $blacklist === null ); + if( $cacheable && $badImageCache !== null ) { + $badImages = $badImageCache; + } else { // cache miss + if ( $blacklist === null ) { + $blacklist = wfMsgForContentNoTrans( 'bad_image_list' ); // site list + } # Build the list now $badImages = array(); - $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) ); + $lines = explode( "\n", $blacklist ); foreach( $lines as $line ) { # List items only if ( substr( $line, 0, 1 ) !== '*' ) { @@ -68,6 +75,9 @@ function wfIsBadImage( $name, $contextTitle = false ) { $badImages[$imageDBkey] = $exceptions; } } + if ( $cacheable ) { + $badImageCache = $badImages; + } } $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false; diff --git a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php index b2b8448601..25581de031 100644 --- a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php +++ b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php @@ -935,6 +935,28 @@ class GlobalTest extends MediaWikiTestCase { ); } + /** + * @dataProvider provideWfIsBadImageList + */ + function testWfIsBadImage( $name, $title, $blacklist, $expected, $desc ) { + $this->assertEquals( $expected, wfIsBadImage( $name, $title, $blacklist ), $desc ); + } + + function provideWfIsBadImageList() { + $blacklist = '* [[File:Bad.jpg]] except [[Nasty page]]'; + return array( + array( 'Bad.jpg', false, $blacklist, true, + 'Called on a bad image' ), + array( 'Bad.jpg', Title::makeTitle( NS_MAIN, 'A page' ), $blacklist, true, + 'Called on a bad image' ), + array( 'NotBad.jpg', false, $blacklist, false, + 'Called on a non-bad image' ), + array( 'Bad.jpg', Title::makeTitle( NS_MAIN, 'Nasty page' ), $blacklist, false, + 'Called on a bad image but is on a whitelisted page' ), + array( 'File:Bad.jpg', false, $blacklist, false, + 'Called on a bad image with File:' ), + ); + } /* TODO: many more! */ }