Extension:Whos online

Maintainer
Shannon McNaught (smcnaught) - I am also available on irc.chekmate.org #MediaWiki

Homepage
ChekMate Technical Focus Group

License
Copyright (C) 2005 Shannon McNaught

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Introduction
A quick and dirty implementation of WhosOnline for MediaWiki. This script was created for a user within the #MediaWiki IRC channel. This version displays the number of Guests and Registered users online.

The timeperiod is presently set to 3600 seconds (1 hour), thus it will show how many unique users have accessed the site within the last hour.

Change $timeperiod to a more appropriate time frame if desired.

Note: Most up to date version of this extension can be found at: http://www.chekmate.org/wiki/index.php/MW:_Whos_Online_Extension

Special Thanks

 * Special thanks to Xypron for adding the table creation functionality within the php script.

Example
Users Online: Guests: 1 Registered: 1 (Smcnaught)

The above example in Wiki text: &lt;b&gt;Users Online&lt;/b&gt;: &lt;whosonline&gt;&lt;/whosonline&gt;

Allowing wikiuser to have CREATE access
Here is how you can configure your wikiuser to have CREATE access to your wikidb: [user@server extensions]$ mysql -u root -p wikidb Enter password: mysql> REVOKE ALL PRIVILEGES ON *. * FROM 'wikiuser'@'localhost'; mysql> REVOKE GRANT OPTION ON *. * FROM 'wikiuser'@ 'localhost'; mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE ON *. * TO 'wikiuser'@ 'localhost';

Save the following in your ./extensions/ directory
<?php
 * 1) WhosOnline Mediawiki extension
 * 2) by Shannon McNaught 22.06.2006
 * 3) http://www.chekmate.org/wiki/index.php/Projects
 * 1) http://www.chekmate.org/wiki/index.php/Projects


 * 1) Installation:
 * 2)  * Add new table to your wikidb.
 * 3)  * put this file (WhosOnline.php) into the extension directory of your mediawiki installation
 * 4)  * add the following to the end of LocalSettings.php: include("extensions/WhosOnline.php");
 * 5) Example:
 * 1) Example:

$wgExtensionFunctions[] = "wfWhosOnlineExtension";
 * 1) install extension hook

function wfWhosOnlineExtension { global $wgParser;
 * 1) extension hook callback function

#install parser hook for tags $wgParser->setHook( "whosonline", "renderWhosOnline" ); }

function renderWhosOnline( $input ) { global $wgUser, $wgDBprefix,$wgVersion,$wgOut; global $wgOutputEncoding;
 * 1) parser hook callback function

// ###### INVALIDATE CACHE ###### global $wgTitle; $Userlist = ""; $ts = mktime; $now = gmdate("YmdHis", $ts + 120); $ns = $wgTitle->getNamespace; $ti = wfStrencode($wgTitle->getDBkey); $version = preg_replace("/^([1-9]).([1-9]).*/", "\\1\\2", $wgVersion); if ($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 = $wgDBprefix."online";

$sql = "DELETE from $tblname 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 $wgDBprefix"."online (userid,username,timestamp) VALUES ('$userid','$username','$now')"; $output = $sql; wfQuery($sql, DB_WRITE, ""); $sql = "select * from $wgDBprefix"."online where userid = 0"; $dbr =& wfGetDB( DB_SLAVE );

$res = $dbr->query ( $sql ) ; $guests = $dbr->numRows($res) + 0; $sql = "select username from $wgDBprefix"."online where userid != 0"; $res = $dbr->query ( $sql ) ; $registered = $dbr->numRows($res) + 0; while( $row = $dbr->fetchObject( $res ) ){ $Userlist .= "".$row->username." "; }

$dbr->freeResult( $res );

$output = "Guests: $guests Registered: $registered ($Userlist)";

return $wgOut->parse($output); }
 * 1)  return $output;

?>

Add to your LocalSettings.php file
Add the following to the end of LocalSettings.php: require_once("extensions/WhosOnline.php"); and if using 1.9.x : require_once ("includes/DatabaseFunctions.php");

Other sites using this extension

 * ChekMate Technical Focus Group