ระบบ Chat สำหรับ MediaWiki
-
Upload
onisuka-trongdee -
Category
Education
-
view
501 -
download
6
description
Transcript of ระบบ Chat สำหรับ MediaWiki
<?php
/**
* MediaWiki WikiChat extension
* See: http://www.mediawiki.org/wiki/Extension:Chat for installation
* Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
* Author: http://www.wikichat.org/User:Firebreather
*/
#set to the directory phpfreechat is installed in
define('WC_PFC_DIRECTORY', 'phpfreechat');
# set to false to deny access to anonymous users
define('WC_ALLOW_ANONYMOUS_USERS', true);
# message shown when denying anonymous users. Change if you want a different message. WikiText ok..
define('WC_DENY_ACCESS_MESSAGE', 'You must [[Special:Userlogin|login]] to be allowed into this chatroom');
# when true there will be a chat room per article. Set to false to have a single site-wide chat room.
define('WC_ROOM_PER_ARTICLE', true);
if (!defined('MEDIAWIKI')) die ('Not an entry point');
$wgPFC_Directory = WC_PFC_DIRECTORY;
require_once "$IP/$wgPFC_Directory/src/phpfreechat.class.php";
require_once "$IP/includes/DatabaseFunctions.php";
$wgRoomPerArticle = WC_ROOM_PER_ARTICLE;
$wgAllowAnonUsers = WC_ALLOW_ANONYMOUS_USERS;
$wgDenyAccessMessage = WC_DENY_ACCESS_MESSAGE;
/* Extension variables */
$wgExtensionFunctions[] = 'wfSetupWikiChat';
$wgExtensionCredits['other'][] = array(
'name' => 'WikiChat',
'version' => '0.3.8, 2010-09-03',
'author' => '[http://www.wikichat.org/User:Firebreather User:Firebreather]',
'url' => 'http://www.wikichat.org/',
'description' => 'Adds a tab to each article that switches to a chatroom',
);
class WikiChat {
// Constructor
public function WikiChat() {
global $wgHooks;
# Add all our needed hooks
$wgHooks['UnknownAction'][] = $this;
$wgHooks['SkinTemplateTabs'][] = $this;
}
// main entry point
public function onUnknownAction($action, $article) {
global $wgOut, $wgCachePages, $wgTitle, $wgDBprefix, $wgDenyAccessMessage;
$wgCachePages = false;
if($action == 'chat') {
$wgOut->setPageTitle(str_replace('_', ' ', $wgTitle->getDBKey()));
if($this->isAnonDenied()) {
$wgOut->addWikiText($wgDenyAccessMessage);
return false;
}
$params = $this->configurePFC_Params();
$tblname = $wgDBprefix."chathistory";
$this->createChatHistoryDBTable($tblname);
$this->configurePFC_DB_Params($tblname, $params);
$this->configureNick($params);
$this->showChat($params);
return false;
}
else {
return true;
}
}
private function isAnonDenied() {
global $wgUser, $wgAllowAnonUsers;
return !$wgAllowAnonUsers && $wgUser->isAnon();
}
private function configureNick(&$params) {
global $wgUser, $wgOut, $wgAllowAnonUsers, $wgDenyAccessMessage, $wgTitle;
$nick = "";
if($wgUser->isAnon()) {
}
else {
$nick = $wgUser->getName();
}
$params["nick"] = $nick; // setup the initial nickname
return $nick;
}
private function configurePFC_Params() {
global $wgTitle, $wgRoomPerArticle, $wgPFC_Directory;
global $wgOut;
$params["title"] = "WikiChat";
// apply room per page or site-wide room configuration option
if($wgRoomPerArticle) {
//replace slashes in the page db key to ensure chat works for subpages
$params["serverid"] = str_replace('/', '##', $wgTitle->getDBKey());
}
else {
$params["serverid"] = md5(__FILE__);
}
$params["data_public_url"] = "$wgPFC_Directory/data/public";
$params["data_public_path"] = "$wgPFC_Directory/data/public";
$params["server_script_url"] = $_SERVER['REQUEST_URI'];
$params["openlinknewwindow"] = true;
$params["channels"] = array(str_replace('_', ' ', $wgTitle->getDBKey()));
$params["frozen_nick"] = true; // do not allow to change the nickname
$params["shownotice"] = 0; // 0 = nothing, 1 = just nickname changes, 2 = connect/quit, 3 = nick + connect
$params["max_nick_len"] = 30; // nickname length could not be longer than 10 caracteres
$params["max_text_len"] = 300; // a message cannot be longer than 50 caracteres
$params["max_channels"] = 3; // limit the number of joined channels tab to 3
$params["max_privmsg"] = 1; // limit the number of private message tab to 1
$params["refresh_delay"] = 2000; // chat refresh speed is 2 secondes (2000ms)
$params["max_msg"] = 30; // max message in the history is 15 (message seen when reloading the chat)
$params["height"] = "230px"; // height of chat area is 230px
$params["debug"] = false; // activate debug console
$params["timeout"] = 600000; // msecs until user is disconnected
return $params;
}
private function createChatHistoryDBTable($tblname) {
$dbr =& wfGetDB( DB_SLAVE );
$sql = "show tables like '$tblname'";
$res = $dbr->query ( $sql ) ;
$num_rows = $dbr->numRows($res) + 0;
$dbr->freeResult( $res );
//create the chathistory table if it doesn't exist
if($num_rows == 0) {
$sql =
"CREATE TABLE IF NOT EXISTS `$tblname` (
`server` varchar(32) NOT NULL default '',
`group` varchar(64) NOT NULL default '',
`subgroup` varchar(64) NOT NULL default '',
`leaf` varchar(64) NOT NULL default '',
`leafvalue` text NOT NULL,
`timestamp` int(11) NOT NULL default 0,
PRIMARY KEY (`server`,`group`,`subgroup`,`leaf`),
INDEX (`server`,`group`,`subgroup`,`timestamp`)
) ENGINE=InnoDB;";
$ret = wfQuery($sql, DB_WRITE, "");
}
}
private function configurePFC_DB_Params($tblname, &$params) {
global $wgDBserver, $wgDBname, $wgDBprefix, $wgDBuser, $wgDBpassword;
$params["container_type"] = "mysql";
$params["container_cfg_mysql_host"] = $wgDBserver;
$params["container_cfg_mysql_database"] = $wgDBname;
$params["container_cfg_mysql_port"] = 3306;
$params["container_cfg_mysql_table"] = $tblname;
$params["container_cfg_mysql_username"] = $wgDBuser;
$params["container_cfg_mysql_password"] = $wgDBpassword;
}
private function showChat($params) {
global $wgOut;
$pfc = new phpFreeChat($params);
$wgOut->addHTML($pfc->printChat(true));
$wgOut->addHTML('<br><br><p>Type /help for a list of all commands</p>');
$wgOut->addWikiText("[http://www.wikichat.org/index.php/WikiChat_help More help on WikiChat.org]");
}
public function onSkinTemplateTabs( &$skin, &$content_actions ) {
global $wgRequest;
$action = $wgRequest->getText( 'action' );
$content_actions['chat'] = array(
'class' => ($action == 'chat') ? 'selected' : false,
'text' => "chat",
'href' => $skin->mTitle->getLocalURL( 'action=chat' )
);
return true;
}
# Needed in some versions to prevent Special:Version from breaking
public function __toString() { return 'WikiChat'; }
} /* class WikiChat */
/* Global function */
# Called from $wgExtensionFunctions array when initialising extensions
function wfSetupWikiChat() {
global $wgWikiChat;
$wgWikiChat = new WikiChat();
}
?>