Extension:ImageFilter

Category:Extensions which host their code in-wiki Category:Extensions without an imageCategory:Extensions without a compatibility policyCategory:Extensions with unknown license
MediaWiki extensions manual
ImageFilter
Release status: betaCategory:Beta status extensions
Implementation Link markup Category:Link markup extensions
Description Filters images, filtering can be disabled by registered users (fixed for 1.38)
Author(s) (Ppehrsontalk)
MediaWiki since 1.13; works on 1.38Category:Extensions with manual MediaWiki version
License GPL
Download No link
Example http://rationalwiki.org/wiki/Help:Images#Image_filtering
Category:All extensionsCategory:Extensions not in ExtensionJson

What can this extension do?

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

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

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

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

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

Code

ImageFilter.i18n.php

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

ImageFilter.php

<?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;
	}
}
Category:Image extensions
Category:All extensions Category:Beta status extensions Category:Extensions not in ExtensionJson Category:Extensions which host their code in-wiki Category:Extensions with manual MediaWiki version Category:Extensions with unknown license Category:Extensions without a compatibility policy Category:Extensions without an image Category:GetPreferences extensions Category:ImageBeforeProduceHTML extensions Category:Image extensions Category:Link markup extensions Category:PageRenderingHash extensions