Extension:ImageFilter

MediaWiki extensions manual
ImageFilter
Release status: beta
Implementation Link markup
Description Filters images, filtering can be disabled by registered users (fixed for 1.38)
Author(s) (Ppehrsontalk)
MediaWiki since 1.13; works on 1.38
License GPL
Download No link
Example http://rationalwiki.org/wiki/Help:Images#Image_filtering

What can this extension do? edit

This extension can prevent specific images from being rendered inline, similarly to MediaWiki: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.

Originally written for http://rationalwiki.org/ by User:Nx, now the responsibility of User:David Gerard.

Usage edit

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

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

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

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

Code edit

ImageFilter.i18n.php edit

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

ImageFilter.php edit

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
	exit;
}

$wgExtensionCredits['other'][] = array(
	'name' => 'NSFW-billedfilter',
	'author' => '[https://www.spademanns.dk/Bruger:CooperDK]',
	'description' => 'NSFW-billedfilter',
	'url' => ''
);

$wgImageFilterIP = dirname( __FILE__ );
$wgExtensionMessagesFiles['ImageFilter'] = "$wgImageFilterIP/ImageFilter.i18n.php";

$wgHooks['PageRenderingHash'][] = 'ImageFilterHash';
$wgHooks['ImageBeforeProduceHTML'][] = 'ImageFilterProduceHTML';
$wgHooks['GetPreferences'][] = 'ImageFilterPreferences';

use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;

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*/
	$revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
	$revision = $revisionLookup->getRevisionByTitle( $title );
	
  	if ( !$revision ) return true;
			//$audience = $revision->hasOption( 'show-private' ) ?
			//$RevisionRecord::RAW :
			//$RevisionRecord::FOR_PUBLIC;

  	$text = $revision->getContent( SLOTRECORD::MAIN, FOR_PUBLIC)->serialize();
  	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;
	}
}