User:Austin J. Che/Extensions/WikiChat

From OpenWetWare

Jump to: navigation, search

This extension enables chat on the wiki using phpfreechat. A special page is added at Special:Chat. Also, all pages have an extra chat tab added that automatically takes people into a room of the given name.

Setup

A global object $wgWikiChat is defined. Variables that can be set:

  • $wgWikiChat->phpfreechat: relative location of the phpfreechat.class.php file. This will be included by php so should be relative to the php search path. Default is "phpfreechat/src/phpfreechat.class.php".
  • $wgWikiChat->defaultrooms: array of default rooms to have all users enter. The default is array("Lounge").
  • $wgWikiChat->params: array of params for phpfreechat (read the manual for phpfreechat). Default parameters are set for most parameters from wiki parameters. For example, the chat nick name for a user is the same as their user name on the wiki. Two parameters that probably need to be set or checked at least are the "theme_default_url" and the "data_public_url" which should be urls that point to the right places in the phpfreechat source directories.

To change the help text that appears under the chat window, edit MediaWiki:Wikichat-text.

Source

<?php
 
/**
 * For using phpFreeChat with MediaWiki
 * Some code based on http://www.phpfreechat.net/forum/viewtopic.php?id=656
 * - creates a global $wgWikiChat object. look at variables that can be set in class below
 * - Makes a Special:Chat page
 * - Adds a chat tab on every page
 
 * light up the chat tab when someone is in the chat room on the page
 * Allow entering [[foo]] to link to page foo on wiki, can do this with a proxy
 * instead of the chat tab going to special:tab create a new action so that other tabs stay there for the page
 * can use pfcinfo and getOnlineNick() to get online status
 * add commands to maintain a friend list, can store via usermeta data
   similar to  http://www.phpfreechat.net/forum/viewtopic.php?id=1232
 * - Also allow <chat> on a page
 * - Linker.php override userToolLinks() so that extra 'chat' item for each line on recent changes
 *  put chat history on discussion page
 */
 
$wgExtensionFunctions[] = "wfExtensionWikiChat";
$wgExtensionCredits['specialpage'][] = array(
    'name' => 'Chat',
    'version' => '2007/03/21',
    'author' => 'Austin Che',
    'url' => 'http://openwetware.org/wiki/User:Austin_J._Che/Extensions/WikiChat',
    'description' => 'Enables chatting on pages using www.phpfreechat.net',
);
 
class WikiChat // not extending SpecialPage to avoid unnecessarily loading SpecialPage.php
{
    var $phpfreechat;           // path to phpfreechat.class.php
    var $params;                // phpfreechat params
    var $defaultrooms;          // array of default rooms, default array("Lounge")
 
    function WikiChat()
    {
        global $wgSitename, $wgScript, $wgUploadDirectory, $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword;
        $this->phpfreechat = "phpfreechat/src/phpfreechat.class.php";
        $this->defaultrooms = array("Lounge");
        $this->params = array("serverid" => $wgSitename,
                              "title" => "Wiki Chat",
 
                              "frozen_nick" => true, // don't allow changing nickname
                              "max_nick_len" => 128,
 
                              // these are set later when the chat is started
                              //"nick", "channels", "privmsg"
                              "isadmin" => false, // ** maybe make admin if sysop on wiki or use $wgUser->isAllowed("..."),
 
                              // use same database parameters as wiki
                              "container_type" => "mysql",
                              "container_cfg_mysql_host"     => $wgDBserver,        // default value is "localhost"
                              //"container_cfg_mysql_port"     => 3306,   // default value is 3306 // MediaWiki has no port specification for mysql, $wgDbport is only used for postgres
                              "container_cfg_mysql_database" => $wgDBname,
                              "container_cfg_mysql_table"    => "phpfreechat",             // default value is "phpfreechat"
                              "container_cfg_mysql_username" => $wgDBuser,
                              "container_cfg_mysql_password" => $wgDBpassword,
                              "container_cfg_mysql_fieldtype_subgroup" => 'varchar(128)', //fix for bug in phpfreechat mysql container
 
                              // paths
                              "server_script_url" => "${wgScript}/Special:Chat",
                              "data_private_path" => "${wgUploadDirectory}/freechat",
 
                              // these may need to be overwritten
                              "theme_url" => "/freechat/themes",
                              "theme_default_url" => "/freechat/themes",
                              "data_public_url" => "/freechat/data/public",
 
                              // general prefs
                              "quit_on_closedwindow" => true,
                              "skip_proxies" => array("censor"), // no censoring of words
                              "shownotice" => 0, // show: 0 = nothing, 1 = just nickname changes, 2 = join/quit, 3 = 1+2
                              "showsmileys" => false,
                              "nickmarker" => true,
                              "btn_sh_smileys" => true,
                              "max_msg" => 100,
                              "openlinknewwindow"         => true,
                              );
    }
 
    function addChatTab(&$skin_template, &$content_actions)
    {
        global $wgRequest, $wgTitle, $wgUser;
 
        if ($wgUser->isAnon())
            return true;
 
        $curaction = $wgRequest->getText('action');
        $i = 0;
        foreach ($content_actions as $key => $tab)
        {
            if ($key == "talk")
            {
                // add chat tab after talk tab
                $title = Title::makeTitle(NS_SPECIAL, "Chat/" . $wgTitle->getSubjectPage()->getPrefixedDBkey());
                $chat["chat"] = array('class' => $curaction == "chat" ? 'selected' : false,
                                      'text' => wfMsg("chat"),
                                      'href' => $title->getLocalUrl(),
                                      );
                array_splice($content_actions, $i, 0, $chat);
                break;
            }
            $i++;
        }
 
        return true;
    }
 
    function chat($room)
    {
        global $wgOut, $wgUser, $wgCachePages;
 
        $wgCachePages = false;
        $wgOut->setHTMLTitle(wfMsg('chat'));
        $wgOut->setPageTitle(wfMsg('chat'));
 
        if ($wgUser->isAnon())
        {
            $wgOut->addHTML(wfMsg("wikichat-anon"));
            return true;
        }
 
        require_once $this->phpfreechat;
 
        $this->params["nick"] = $wgUser->getName();
        $this->params["channels"] = $this->defaultrooms;
        if ($room)
        {
            $title = Title::newFromText($room);
            if ($title && $title->getNamespace() == NS_USER)
                $this->params["privmsg"] = $title->getText();
            else
                $this->params["channels"] = array_merge($this->defaultrooms, array($room));
        }
 
        $chat = new phpFreeChat($this->params);
        $wgOut->addHTML($chat->printChat(true));
        $wgOut->addHTML(wfMsg('wikichat-text'));
    }
}
 
function wfExtensionWikiChat()
{
    global $wgMessageCache, $wgSpecialPages, $wgHooks, $wgWikiChat;
    $wgMessageCache->addMessages(array('chat' => 'Chat', 'wikichat-text' => "Type /help to list chat commands.", 
                                       'wikichat-anon' => 'You must log in to chat.'));
    $wgSpecialPages["Chat"] = array('SpecialPage', 'Chat');
    //    $wgHooks['SkinTemplateTabs'][] = array(&$wgWikiChat, "addChatTab");
}
 
function wfSpecialChat($par)
{
    // entry point for special page
    global $wgWikiChat;
    return $wgWikiChat->chat($par);
}
 
$wgWikiChat = new WikiChat();
?>
Personal tools