WhosOnline Mediawiki extension version 2.0
From Zedomax Wiki
WhosOnline Mediawiki extension version 2.1
Here's an update to the original WhosOnline MediaWiki extension.
Code:
<?php
# WhosOnline Mediawiki extension version 2.1
#
# originally by Shannon McNaught 22.06.2006
# updated for MediaWiki 1.11.0 by Max Lee 17.11.2007
#
# - version 2.1 updated by Max Lee
# - added Who's Online Right Now... lol...
#
# - version 2.0 updated by Max Lee
# - added wgHook to automatically add Who's Online at the bottom of pages.
#
# updated version website: http://zedomax.com/wiki
# original website: http://www.chekmate.org/wiki/index.php/Projects
# Installation:
# * Add the follwing table to your wiki database through myPhpAdmin
# CREATE TABLE `online` (
# `userid` int(5) NOT NULL default '0',
# `username` varchar(255) NOT NULL default '',
# `timestamp` varchar(255) NOT NULL default ''
# ) TYPE=MyISAM;
# * put this file (WhosOnline.php) into the extension directory of your mediawiki installation
# * add the following to the end of LocalSettings.php: include("extensions/WhosOnline.php");
#
# Tested with MediaWiki 1.11.0
$wgHooks['BeforePageDisplay'][] = array("renderWhosOnline");
#parser hook callback function
function renderWhosOnline( &$out) {
global $wgUser, $wgDBprefix,$wgVersion,$wgOut;
global $wgOutputEncoding;
// ###### INVALIDATE CACHE ######
global $wgTitle;
$ts = mktime();
$now = gmdate("YmdHis", $ts + 120);
$ns = $wgTitle->getNamespace();
$ti = wfStrencode($wgTitle->getDBkey());
$version = preg_replace("/^([1-9]).([1-9]).*/", "\\1\\2", $wgVersion);
$sql = "UPDATE page SET page_touched='$now' WHERE page_namespace=$ns AND page_title='$ti'";
//yyif ($version>14) $sql = "UPDATE $wgDBprefix"."page SET page_touched='$now' WHERE page_namespace=$ns AND page_title='$ti'";
//else $sql = "UPDATE $wgDBprefix"."cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
wfQuery($sql, DB_WRITE, "");
$timeperiod = 3600; # number of seconds
$DefaultEncoding = "ISO-8859-1";
$DisableCache = true;
$ts = mktime();
$now = gmdate("YmdHis", $ts);
$old = gmdate("YmdHis", $ts-$timeperiod);
$userid = $wgUser->getID();
$username = $wgUser->getName();
$tblname = "online";
$sql = "DELETE from online WHERE username = '$username' OR timestamp < '$old' ";
$db =& wfGetDB( DB_WRITE );
if ( $db !== false ) {
$ret = $db->query( $sql, '', true );
}
else {
$ret = false;
}
if ( false === $ret ) {
$sql =
"CREATE TABLE $tblname (
`userid` int(5) NOT NULL default '0',
`username` varchar(255) NOT NULL default '',
`timestamp` varchar(255) NOT NULL default ''
) TYPE=MyISAM ";
$ret = wfQuery($sql, DB_WRITE, "");
}
$sql = "INSERT INTO online (userid,username,timestamp) VALUES ('$userid','$username','$now')";
$output = $sql;
wfQuery($sql, DB_WRITE, "");
$sql = "select * from online where userid = 0";
$dbr =& wfGetDB( DB_SLAVE );
$res = $dbr->query ( $sql ) ;
$guests = $dbr->numRows($res) + 0;
$sql = "select username from online where userid != 0";
$res = $dbr->query ( $sql ) ;
$registered = $dbr->numRows($res) + 0;
while( $row = $dbr->fetchObject( $res ) ){
$Userlist .= "[[User:".$row->username."|".$row->username."]] ";
}
$dbr->freeResult( $res );
$output = "<b>Who's Online Right Now:</b> Guests: $guests Registered: $registered ($Userlist)";
$output = $wgOut->parse($output);
$out->mBodytext.=$output;
return $out;
}
?>