Extension:Emoticons

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
 'Emoticons',	'status' => 'stable',	'type' => 'hook',	'author' => 'Alex Wollangk (alex@wollangk.com)',	'version' => '1.0',	'update' => '2-20-2007',       'url' => 'http://www.mediawiki.org/wiki/Extension:Emoticons',	'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// :grin:// :)// :-)// :smile:// :(// :-(// :sad:// :o// :-o// :eek:// :shock:// :?// :-?// :???:// 8)// 8-)// :cool:// :lol:// :x// :-x// :mad:// :P// :-P// :razz:// :oops:// :cry:// :evil:// :twisted:// :roll:// :wink:// ;)// ;-)// :!:// :?:// :idea:// :arrow:// :|// :-|// :neutral:// :mrgreen://

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 extension does not seem to work for included pages. Is this 'generic' or specific maybe caused by some extension we use. Any help?

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.

About the Code
(If you don't care about how this extension was written, ignore the following section.)

When writing this extension the biggest challenge I had was accessing the MediaWiki:Emoticons article. I wanted to use the existing framework rather than use SQL to help insure that it would continue to work even if the database structure is updated. A fair amount of searching did not come up with many hints on how to do this, however. At one point I tried to access the article through the global $mediaWiki object, but that object has not been instantiated when LocalSettings.php is included and so extensions do not have access to it. I finally made the decision to manually instantiate an "Article" which requires instantiating a "Title" and use that "Article" to load the appropriate page.

A few other notes:


 * I started out referring to the MediaWiki namespace using the NS_MEDIAWIKI constant, but saw a reference that said that this constant would not work in non-english Wikis and so used the integer '8' instead.
 * I disabled emoticon replacement in most actions other than just viewing the page to keep things dynamic. If the replacement is done when editing a page the substitution will happen to the text in the textarea so that the replaced text will end up in the database.  At that point if that emoticon is updated, since the original text was replaced, that page will not reflect the new emoticon.
 * I saw a suggestion made to a different programmer that instead of accessing the "m" fields that they use the associated getter and setter function. In several cases I am accessing the "m" fields as well, but this is only because I could not find a related getter or setter in the code documentation.
 * Since the values are trimmed before replacements are made, if you want to replace text ending with a forward slash ("/") or replace something with text beginning with a forward slash, all you need to do is put a space between the double forward slashes used as a separator and the forward slash in the search or replacement text. If you want to replace text ending with two double slashes replace something with text beginning with two double slashes, you need to come up with a different separator.  Once you have figured out what you want to use, make sure you are using it in the MediaWiki:Emoticons file and replace the "//" in extensions/emoticons.php (in: " $currEmoticon = explode( "//", $emoticon, 2 ); ") with that separator as well.
 * This extension may be useful if you want to do other replacements as well, but be careful as it will replace any instance of the search text before other wiki formatting gets a chance to run even in areas of text where wiki formatting is turned off. For instance, if you set up ":D" to be replaced with a smiley face and somewhere in the wiki someone wants to indent a line beginning with "D" like this ":Details of this transaction..." it will be rendered as a big smiley followed by "etails of this transaction..."