Open main menu

Extension:EmoticonsLite

MediaWiki extensions manual
OOjs UI icon advanced.svg
EmoticonsLite
Release status: stable
Implementation Extended syntax
Description Enables a forum-style emoticon (smiley) replacement within a page
Author(s) Alex Wollangk (alex@wollangk.com), and Techjar (tecknojar@gmail.com)
Latest version 1.0 (2009-07-48)
Database changes No
License Public domain
Download See the code section
Translate the EmoticonsLite extension if it is available at translatewiki.net
Check usage and version matrix.

The EmoticonsLite extension makes forum-style emoticon (smiley) replacements within MediaWiki. See the list of emoticons below. Note that this extension is derived from the Emoticons extension.

InstallationEdit

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/EmoticonsLite/EmoticonsLite.php";
    
  • Create the page "MediaWiki:Emoticons" with one line to this page for each emoticon. Each line contains the text to be replaced followed by "//" followed by the text to replace it with. See this section for an extample.
  •   Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

CodeEdit

EmoticonsLite.php
<?php
 
# Emoticon MediaWiki Extension
# Created by Alex Wollangk (alex@wollangk.com), and Techjar (tecknojar@gmail.com)

if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This file is a MediaWiki extension, it is not a valid entry point.' );
}
 
global $wgHooks;
global $wgExtensionCredits;
 
$wgExtensionCredits['parserhook'][] = array(
	'name' => 'EmoticonsLite',
	'author' => 'Alex Wollangk, Techjar',
	'version' => '1.0',
	'update' => '2009-07-18',
	'url' => 'https://www.mediawiki.org/wiki/Extension:EmoticonsLite',
	'description' => 'Enables a forum-style emoticon (smiley) replacement within a page',
);
 
$wgHooks['ParserAfterStrip'][] = 'fnEmoticons';
 
// The callback function for replacing emoticons with image tags
function fnEmoticons( &$parser, &$text, &$strip_state ) {
	global $action; // Access the global "action" variable
	// Only do the replacement if the action is not edit or history
	if(
		$action !== 'edit'
		&& $action !== 'history'
		&& $action !== 'delete'
		&& $action !== 'watch'
		&& strpos( $parser->mTitle->mPrefixedText, 'Special:' ) === false
		&& $parser->mTitle->mNamespace !== 8
	)
	{
		// Get the list of emoticons from the "MediaWiki:Emoticons" article.
		$title = Title::makeTitle( 8, 'Emoticons' );
		$emoticonListArticle = new Article( $title );
		$emoticonListArticle->getContent();
 
		// If the content successfully loaded, do the replacement
		if( $emoticonListArticle->mContentLoaded )
		{
			$emoticonList = explode( "\n", $emoticonListArticle->mContent );
			foreach( $emoticonList as $index => $emoticon )
			{
				$currEmoticon = explode( "//", $emoticon, 2 );
				if( count($currEmoticon) == 2 )
				{
					// start by trimming the search value
					$currEmoticon[ 0 ] = trim( $currEmoticon[ 0 ] );
					// if the string begins with &nbsp;, lop it off
					if( substr( $currEmoticon[ 0 ], 0, 6 ) == '&nbsp;' )
					{
						$currEmoticon[ 0 ] = trim( substr( $currEmoticon[ 0 ], 6 ) );
					}
					// trim the replacement value
					$currEmoticon[ 1 ] = trim( $currEmoticon[ 1 ] );
					// and finally perform the replacement
					$text = str_ireplace( " ".$currEmoticon[ 0 ], $currEmoticon[ 1 ], $text );
				}
			}
		}
	}
	// Always a good practice to let the other hooks have their turn ... whenever it make sense.
	return true;
}

Sample MediaWiki:EmoticonsEdit

Here is a sample list of emoticons:

 :D//[[Image:VeryHappy.gif]]
 :-D//[[Image:VeryHappy.gif]]
 :grin://[[Image:VeryHappy.gif]]
 :)//[[Image:Smile.gif]]
 :-)//[[Image:Smile.gif]]
 :smile://[[Image:Smile.gif]]
 :(//[[Image:Sad.gif]]
 :-(//[[Image:Sad.gif]]
 :sad://[[Image:Sad.gif]]
 :o//[[Image:Surprised.gif]]
 :-o//[[Image:Surprised.gif]]
 :eek://[[Image:Surprised.gif]]
 :shock://[[Image:Shocked.gif]]
 :?//[[Image:Confused.gif]]
 :-?//[[Image:Confused.gif]]
 :???://[[Image:Confused.gif]]
 8)//[[Image:Cool.gif]]
 8-)//[[Image:Cool.gif]]
 :cool://[[Image:Cool.gif]]
 :lol://[[Image:Laughing.gif]]
 :x//[[Image:Mad.gif]]
 :-x//[[Image:Mad.gif]]
 :mad://[[Image:Mad.gif]]
 :P//[[Image:Razz.gif]]
 :-P//[[Image:Razz.gif]]
 :razz://[[Image:Razz.gif]]
 :oops://[[Image:Embarassed.gif]]
 :cry://[[Image:CryingorVerySad.gif]]
 :evil://[[Image:EvilorVeryMad.gif]]
 :twisted://[[Image:TwistedEvil.gif]]
 :roll://[[Image:RollingEyes.gif]]
 :wink://[[Image:Wink.gif]]
 ;)//[[Image:Wink.gif]]
 ;-)//[[Image:Wink.gif]]
 :!://[[Image:Exclamation.gif]]
 :?://[[Image:Question.gif]]
 :idea://[[Image:Idea.gif]]
 :arrow://[[Image:Arrow.gif]]
 :|//[[Image:Neutral.gif]]
 :-|//[[Image:Neutral.gif]]
 :neutral://[[Image:Neutral.gif]]
 :mrgreen://[[Image:Mr.Green.gif]]