Extension talk:Emoticons
Why this Extension was created
editI 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...
...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.
Install instructions do not work
edit2020-09-24: If you follow the install instructions you will get an error! Please update them.
Does not appear to work in 1.10.1
editOn version 1.11 I got the following error: "Detected bug in an extension! Hook fnEmoticons failed to return a value; should return true to continue hook processing or false to abort." Mediawiki apparently now requires a return value of true for continue or false for cancel.
Adding "return true" between line 67 and line 68 (just before the last curly brace) seemed to fix the problem for me.
Can't get it to work
editIn most recent version it gives a 500 internal server error.
return true fix doesn't work
Return true; definitely needed in extension.
edit* MediaWiki: 1.11.0 * PHP: 5.2.0 (apache2handler) * MySQL: 5.0.45
under this setup, adding return true; before the last } made it work fine.
headers already sent by (output started at /.../extensions/emoticons.php:1)
editI used the code from the main page that included the "return true" statement at the end and I got this error.
- MediaWiki: 1.11.1
The main page also included this information before the code:
If you have MediaWiki 1.11 or higher, you must change the 2nd to last line on this file!
I have no idea what this means. I believe it is referring to the "return true" statement but I'm not sure.
Any ideas what is wrong?
Use the Mediawiki:Emoticons page for viewing purposes also
editIf you look at the parsed configuration page it doesn't look very suitable for using it as a description page for the authors to see which emoticons are available.
So for our wiki I replaced the line
$emoticonList = explode( "\n", $emoticonListArticle->mContent );
by the following
// first remove/change everything from the text that was only needed
// for formatting purposes to view the list in the wiki
$emSearch = array('<nowiki>','</nowiki>','<tr>','</tr>',
'<td>','<td align=center>','</td>','<!--','-->');
$emReplace = array();
$emoticonCleanedArticle = str_replace( $emSearch, $emReplace, $emoticonListArticle->mContent );
$emoticonList = explode( "\n", $emoticonCleanedArticle );
so that I can use some tags like nowiki or table to format the list so that it is viewable also.
The example
<table border=0>
<tr><th>Emoticon</th><th> </th><th>Gives you ...</th></tr>
<tr><td align=center>:)</td><td><!-- // --></td><td align=center>[[Bild:icon_smile.gif]]</td></tr>
<tr><td align=center>:(</td><td><!-- // --></td><td align=center>[[Bild:icon_sad.gif]]</td></tr>
</table>
would show (imagine a suitable icon image) something like this:
Emoticon | Gives you ... | |
---|---|---|
:) | Bild:icon_smile.gif | |
:( | Bild:icon_sad.gif |
and can still be parsed by the extension.
Depending on the tags you want to use in the definition lines you could add further strings to the emSearch array. Lines without the //
separator (i.e. the table lines in the example above) don't need to be removed as they are ignored already by the unchanged extension.
--Wolverine 16:43, 2 January 2008 (UTC)
UNIQ errors in MediaWiki 1.12.0
editThis extension causes UNIQ errors. As seen in the example below.
?UNIQ3cb085dc58727e57-nowiki-00000000-QINU?.
Someone might want to update it so that it can work in the latest version.
MediaWiki 1.12.0 Problems
editAny idea when they will be fixed so I can start using this great extension again?
- It works for my end. Try this: --74.130.36.17 23:37, 20 April 2008 (UTC)
<?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', '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 ); } } } } // this extension will work on MediaWiki 1.11.0 when you remove the "//" from the next line! return true; }
MediaWiki 1.12 fix
editCompletely rewrited and optimized original code. Now it works for MW 1.12:
<?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' );
}
$wgExtensionCredits['parserhook'][] = Array (
'name' => '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'][] = 'efEmoticons_ParserAfterStrip';
/**
* The callback function for replacing emoticons with image tags
*
* @param Parser $parser
* @param string $text
* @param StripState $strip_state
* @return bool
*/
function efEmoticons_ParserAfterStrip(&$parser, &$text, &$strip_state)
{
global $wgTitle;
/* @var $wgTitle Title */
if (in_array($wgTitle->getNamespace(), Array (NS_SPECIAL, NS_MEDIAWIKI))) {
// don't replace emotions on special pages & in system messages
return true;
}
// Get the list of emoticons from the "MediaWiki:Emoticons" article.
$title = Title::makeTitle(NS_MEDIAWIKI, 'Emoticons');
if ($title->getArticleID() == 0) {
// article not found
return true;
}
$article = new Article($title, 0);
$content = $article->getContent();
$emoticon_list = explode("\n", $content);
foreach($emoticon_list as $index => $emoticon) {
$emoticon = explode("//", str_replace(' ', '', $emoticon), 2);
if (count($emoticon) != 2) {
// skip badly formatted emotions
continue;
}
array_map('trim', $emoticon);
// and finally perform the replacement
$text = str_replace(' ' . $emoticon[0], ' ' . $emoticon[1], $text);
}
return true;
}
limiting use to talk pages?
editIt would seem that it should be possible to limit this so that it is active on talk pages only, and not busily mangling actual article text. I presume that the section:
function efEmoticons_ParserAfterStrip(&$parser, &$text, &$strip_state)
{
global $wgTitle;
/* @var $wgTitle Title */
if (in_array($wgTitle->getNamespace(), Array (NS_SPECIAL, NS_MEDIAWIKI))) {
// don't replace emotions on special pages & in system messages
return 1;
}
needs to be followed with the one added statement:
// replace on talk pages only
if (!$wgTitle->isTalkPage() )
return 1;
Does this look reasonable? --Carlb 15:59, 12 November 2008 (UTC)
Error
editWell i suppose for the people that need it will look for this anyway but just for good style
require_once( "extensions/emoticons.php" );
should be
require_once( "$IP/extensions/emoticons.php" );
-- Deo Favente
Image / Media Substitution - unwanted
editApparently, this extension will replace things like this too:
I tried to figure it out myself, but my limited php skills didn't help.
Any ideas?
--212.23.224.72 14:01, 31 March 2009 (UTC)
- Yes, this exact situation is discussed (together with a solution) on the extension page: Extension:Emoticons#Restrict replacement. :-) -Stelio 18:41, 1 April 2009 (UTC)
- I should READ these things ;-) Cheers!
- --Abonhote 06:58, 2 April 2009 (UTC)
Links
editHi everybody, I'm from the Oncyclopedia and we had installed this extension. It worked good.. A bit too good: Some links (like "Special:Statistics" for example) aren't usable now, because the extension recognizes the :S in the link and automatically replaces Statistics with [[Special tatistics|Statistics]] (But with the right image, of course). How do we fix this? Greets, Vsotvep 19:54, 2 May 2009 (UTC)
- Ow whatever, found it already. Thanks anyways :P 77.169.36.17 20:11, 2 May 2009 (UTC)
Not working with 1.13.4
editwww.wiki365.info I have just installed and I am running this extension on my mediawiki version 1.13.4, i am unable to get the replacements after creating the MediaWiki:Emoticons page.
Emoticons versus References
editWhen I install this on Oncyclopedia (yes, we again ;)), the references don't work anymore... Anyone who knows a solution? Karoma 09:40, 9 May 2009 (UTC)
Error in code
editI see in this edit you changed if( $ini == 0 )
to if( $ini == false )
. This will not give you the desired results either. strpos() will return 0 (which evaluates as false) if the location of the needle is at the first position (0) in the haystack. Due to PHP's weak typing, the if() will return true when strpos() returns false OR 0. What you should change it to is if( $ini === false )
so PHP checks that the result is boolean false. ^demon 18:23, 17 July 2009 (UTC)
- OK, I will try this! Techjar 20:02, 17 July 2009 (UTC)
- Actually, the
==
operator will give me the desired result. Because it doesn't HAVE to befalse
, it only has to be equivalent tofalse
. (Such as0
or""
.)
Notice the code$string = " ".$string;
, this makes it so it's always greater than0
if the value of$start
is found in$string
!
- Actually, the
EmoticonsLite
editFor those of you with slow servers, I have made EmoticonsLite!! The "lite" version has the <nowiki>
tag parser removed, because this feature slows down MediaWiki when loading articles full of <nowiki>
tags. ENJOY!!!!!
MediaWiki:Emoticons
editIf I edit the MediaWiki:Emoticons-Article the Extension do'nt work.
MediaWiki:EmoticonsLite
editIt's not clear where to download all of the icons/images required for EmoticonsLite.
Updated for MW 1.19
editSince this was requested on the Support Desk, I've adapted it for 1.19+. I'll try getting this into git soon. — ☠MarkAHershberger☢(talk)☣ 03:55, 23 July 2013 (UTC)
<?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' => 'Emoticons',
'type' => 'hook',
'author' => 'Alex Wollangk, Techjar, [[mw:User:MarkAHershberger|Mark A. Hershberger]]',
'version' => '1.2.3',
'update' => '2013-07-22',
'url' => 'http://www.mediawiki.org/wiki/Extension:Emoticons',
'description' => 'Enable forum-style emoticon (smiley) replacement within MediaWiki.',
);
$wgHooks['ParserAfterStrip'][] = 'fnEmoticons';
// fnStringBetween function
function fnStringBetween( $string, $start, $end ) {
$string = " ".$string;
if( strlen( $string ) > strlen( $start ) ) {
$ini = strpos( $string, $start );
if( $ini === false ) {
return false;
} else {
$ini = $ini + strlen( $start );
$len = strpos( $string, $end, $ini ) - $ini;
return substr( $string, $ini, $len );
}
} else {
return false;
}
}
// The callback function for replacing emoticons with image tags
function fnEmoticons( &$parser, &$text, &$strip_state ) {
global $wgRequest;
$action = $wgRequest->getVal( "action", "" );
// 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_ireplace( " ".$currEmoticon[ 0 ], $currEmoticon[ 1 ], $text );
// <nowiki> tag parser
$a = 0;
$text2 = array();
// loop through the <nowiki> tags
while( ( $nowiki_text = fnStringBetween( $text, "<nowiki>", "</nowiki>" ) ) && $a < 10 ) {
// replace the <nowiki> with a 'placeholder' tag
$text = str_ireplace( "<nowiki>".$nowiki_text."</nowiki>", "[nowiki".$a."]", $text );
// replace the images with text
$text2[ $a ] = str_ireplace( $currEmoticon[ 1 ], " ".$currEmoticon[ 0 ], $nowiki_text);
$a++;
}
$a--;
// now do the <nowiki> replacement, and loop through the $text2 array()
while( $a > -1 ) {
// reinsert the <nowiki> tag
$text = str_ireplace( "[nowiki".$a."]", "<nowiki>".$text2[ $a ]."</nowiki>", $text );
$a--;
}
}
}
}
}
// Always a good practice to let the other hooks have their turn ... whenever it make sense.
return true;
}
Modification to make it work with WikiForum extension
editTo make it work with the WikiForum extension, find the lines:
&& strpos( $parser->getTitle()->getPrefixedText(), 'Special:' ) === false && $parser->getTitle()->getNamespace() !== 8
And replace them with the following:
&& ( !$parser->getTitle()->isSpecialPage() || $parser->getTitle()->isSpecial( 'WikiForum' ) )
Thanks to Vulpix from the #mediawiki IRC channel, for helping me to make this work. --BradLeeBH (talk) 09:54, 6 September 2015 (UTC)