Extension:ImageFilter

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
ImageFilter

Release status: beta

Implementation Link markup
Description Filters images, filtering can be disabled by registered users
Author(s) (Nxtalk)
MediaWiki 1.13; tested on 1.14, 1.15 and 1.16
License GPL
Download No link
Example http://rationalwiki.com/wiki/Help:Images#Image_filtering
Hooks used
UserToggles

PageRenderingHash
ImageBeforeProduceHTML
ArticleSaveComplete
GetPreferences

Translate the ImageFilter extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

What can this extension do?[edit | edit source]

This extension can prevent specific images from being rendered inline, similarly to MediaWiki:Bad image list and Extension:Bad Image List. There is no option to allow the image on certain pages, but registered users can disable the filtering altogether.

The extension does not hide images in galleries and search results.

Usage[edit | edit source]

Put __NSFW__ on the description page of an image to filter it. Because the extension checks the source of the page only, this can't be in a template, but it can be inside a comment block so it doesn't appear on the rendered page.

Registered users can disable filtering using the checkbox in the Appearance/Files (Misc in MediaWiki <= 1.15) section of Preferences.

Download instructions[edit | edit source]

Please cut and paste the code found below and place it in $IP/extensions/ImageFilter/ImageFilter.php and $IP/extensions/ImageFilter/ImageFilter.i18n.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation[edit | edit source]

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/ImageFilter/ImageFilter.php");

Code[edit | edit source]

ImageFilter.i18n.php[edit | edit source]

<?php
$messages = array();
$messages['en'] = array(
  'tog-displayfiltered'            => 'Display filtered images',
);

ImageFilter.php[edit | edit source]

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
	exit;
}
 
$wgExtensionCredits['other'][] = array(
	'name' => 'ImageFilter',
	'author' => '[http://mediawiki.org/wiki/User:Nx Nx]',
	'description' => 'Image filter',
	'url' => 'http://www.mediawiki.org/wiki/Extension:ImageFilter'
);
 
$wgImageFilterIP = dirname( __FILE__ );
$wgExtensionMessagesFiles['ImageFilter'] = "$wgImageFilterIP/ImageFilter.i18n.php";
 
//Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, as per r35980
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
	$wgHooks['ParserFirstCallInit'][] = 'ImageFilterInit';
} else { // Otherwise do things the old fashioned way
	$wgExtensionFunctions[] = 'ImageFilterInit';
}
 
function ImageFilterInit()
{
  //Load messages so that color-coding works.
  wfLoadExtensionMessages( 'ImageFilter' );
  return true;
}
 
$wgHooks['UserToggles'][] = 'ImageFilterToggle';
$wgHooks['PageRenderingHash'][] = 'ImageFilterHash';
$wgHooks['ImageBeforeProduceHTML'][] = 'ImageFilterProduceHTML';
$wgHooks['ArticleSaveComplete'][] = 'ImageFilterUpdateCache';
$wgHooks['GetPreferences'][] = 'ImageFilterPreferences';
 
function ImageFilterToggle( &$extraToggles )
{
	$extraToggles[] = 'displayfiltered';
	return true;
}
 
function ImageFilterPreferences( $user, &$preferences )
{
	$preferences['displayfiltered'] = array(
		'type' => 'toggle',
		'label-message' => 'tog-displayfiltered',
		'section' => 'rendering/files',
	);
 
	return true;
}
 
function ImageFilterHash( $hash ) 
{
	global $wgUser;
	$hash .= '!' . ( $wgUser->getOption( 'displayfiltered' ) ? '1' : '' );
	return true;
}
 
function ImageFilterProduceHTML( &$skin, &$title, &$file, &$frameParams, &$handlerParams, &$time, &$res ) 
{
	global $wgUser;
	if ($wgUser->getOption( 'displayfiltered' )) return true;
	/*getDescriptionText parses the text ( and it screws up the parser), so we have to do it manually*/
	$revision = Revision::newFromTitle( $title );
  if ( !$revision ) return true;
  $text = $revision->getText();
  if ( !$text ) return true;
	if ( strpos($text,'__NSFW__') === FALSE ) {
		return true;
	} else {
		if ($frameParams['title'] !== '') {
			$res = $skin->link($title,$frameParams['title']);
		} else {
			$res = $skin->link($title);
		}
		return false;
	}
}
 
function ImageFilterUpdateCache( &$article, &$user, $text, $summary, $minoredit, $watchthis, $sectionanchor, &$flags, $revision, &$status )
{
		$title = $article->getTitle();
		//no change recorded
		if ($revision == null) return true;
		if ( $title->getNamespace() == NS_FILE ) {
			$prevrevision = $revision->getPrevious();
			if ($prevrevision == null) {
				$prevflagged = false;
			} else {
				$prevflagged = strpos($prevrevision->getRawText(),'__NSFW__') !== FALSE;
			}
			$curflagged = strpos($revision->getRawText(),'__NSFW__') !== FALSE;
			if ($prevflagged XOR $curflagged) {
				# Invalidate cache for all pages using this file
				$update = new HTMLCacheUpdate( $title, 'imagelinks' );
				$update->doUpdate();
				# Invalidate cache for all pages that redirects on this page
				$redirs = $title->getRedirectsHere();
				foreach( $redirs as $redir ) {
					$update = new HTMLCacheUpdate( $redir, 'imagelinks' );
					$update->doUpdate();
				}
			}
		}
		return true;
}

See also[edit | edit source]