Jump to content

Extension:Emoticons: Difference between revisions

From mediawiki.org
Content deleted Content added
New page: {{Extension |name = Emoticons Extension |status = Stable |type = hook |author = Alex Wollangk ([mailto:alex@wollangk.com alex@wollangk.com]) |version ...
 
Line 11: Line 11:
==Introduction==
==Introduction==


I was tasked with setting up a Wiki for a small business. They were currently using [http://www.phpbb.com/ phpBB], but for training info and guidelines a forum was just too cumbersome. I knew about [[MediaWiki]] and since I was asked to set something "like [http://www.wikipedia.org/ Wikipedia]" up, it quickly came to mind. The one thing I worried about was that the smileys people were used to in the forums would not be available in the Wiki.
I was tasked with setting up a Wiki for a small business. They were currently using [http://www.phpbb.com/ phpBB], but for training info and guidelines a forum was just too cumbersome. I knew about [[MediaWiki]] and since I was asked to set something "like [http://www.wikipedia.org/ Wikipedia]", it quickly came to mind. The one thing I worried about was that the smileys people were used to in the forums would not be available in the Wiki.


I started by getting MediaWiki installed since the lack of smileys was bothersome, but unlikely to prevent the Wiki from being useful. Next I put in the [http://meta.wikimedia.org/wiki/RandomText RandomText] extension for a random "quote" on the main page.
I started by getting MediaWiki installed since the lack of smileys was bothersome, but unlikely to prevent the Wiki from being useful. Next I put in the [http://meta.wikimedia.org/wiki/RandomText RandomText] extension for a random "quote" on the main page.

Revision as of 06:00, 21 February 2007

MediaWiki extensions manual
Emoticons Extension
Release status: stable
Implementation Hook
Description Enable forum-style emoticon (smiley) replacement within MediaWiki.
Author(s) Alex Wollangk (alex@wollangk.com)
Latest version 1.0 (2-20-2007)
MediaWiki
License No license specified
Download No link

Introduction

I was tasked with setting up a Wiki for a small business. They were currently using phpBB, but for training info and guidelines a forum was just too cumbersome. I knew about MediaWiki and since I was asked to set up something "like Wikipedia", it quickly came to mind. The one thing I worried about was that the smileys people were used to in the forums would not be available in the Wiki.

I started by getting MediaWiki installed since the lack of smileys was bothersome, but unlikely to prevent the Wiki from being useful. Next I put in the RandomText extension for a random "quote" on the main page.

I then searched for an emoticon extension for as long as my attention span would allow. (I think managed to search for a whole fifteen minutes: A new record for me.) After that I gave up and decided to write my own.

Installation

1. Create the file:

extensions/emoticons.php

<?php

# Emoticon MediaWiki Extension
# Created by Alex Wollangk (alex@wollangk.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' => 'Emoticons',
	'status' => 'Stable',
	'type' => 'hook',
	'author' => 'Alex Wollangk (alex@wollangk.com)',
	'version' => '1.0',
	'update' => '2-20-2007',
	'description' => 'Enable forum-style emoticon (smiley) replacement within MediaWiki.',
);

$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  , lop it off
					if( substr( $currEmoticon[ 0 ], 0, 6 ) == ' ' )
					{
						$currEmoticon[ 0 ] = trim( substr( $currEmoticon[ 0 ], 6 ) );
					}
					// trim the replacement value
					$currEmoticon[ 1 ] = trim( $currEmoticon[ 1 ] );
					// and finally perform the replacement
					$text = str_replace( $currEmoticon[ 0 ], $currEmoticon[ 1 ], $text );
				}
			}
		}
	}
}

?>

2. Add the following to the end of: (right above the "?>" at the end)

LocalSettings.php

require_once( "extensions/Emoticons.php" );

3. Create the page "MediaWiki:Emoticons"

4. Add 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.

Sample MediaWiki:Emoticons

Here is the page I ended up creating:

 :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]]

After saving this file I viewed it and all the images showed up as links I could use to upload the appropriate image.

Notice

This is the first extension I have attempted with MediaWiki. Any stylistic variation from standard MediaWiki extension coding practices is just the way things go. If it really bugs you, fix it. Anyone who wants to use, modify or mutilate this code beyond recognition is free to do so, just don't blame it on me. If this extension causes your server to burst into flames and run screaming through the data center, I cannot be held responsible. I check my email quite regularly, so if anyone has any questions feel free to drop me a line, but since I have two jobs, a family and more pets than I care to admit I can't promise I'll be able to respond in short order.