MediaWiki Extension – WhosOnline

This extension is the popular “Whos Online extension” of ChekMate Security Group(http://www.chekmate.org/). This extension is modified version in order to make it work with MediaWiki version 1.8.2 and above. In order to work it with previous versions earlier than 1.8.2 , please visit their site to download the code.

URL: http://www.chekmate.org/wiki/index.php/MW:_Whos_Online_Extension

here are the steps that you have to perform to make this extension work.

1. Add one new table in your database, named online. If database uses a prefix, then add the prefix before the table name. Here is the SQL code for creating the table:

CREATE TABLE `online` (

`userid` int(5) NOT NULL default ‘0’,

`username` varchar(255) NOT NULL default ”,

`timestamp` varchar(255) NOT NULL default ”

) TYPE=MyISAM;

2. Download the code below and Save the following in your ./extensions/ directory

<?php
# WhosOnline Mediawiki extension
#
# by Shannon McNaught 22.06.2006
# http://www.chekmate.org/wiki/index.php/Projects

# Installation:
#  * Add new table to your wikidb.
#  * 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");
#
# Example:
#    <whosonline></whosonline>
#

#install extension hook
$wgExtensionFunctions[] = "wfWhosOnlineExtension";

#extension hook callback function
function wfWhosOnlineExtension() {
  global $wgParser;

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

#parser hook callback function
function renderWhosOnline( $input, $arge, &$parser ) {
  global $wgUser, $wgDBprefix,$wgVersion,$wgOut;
  global $wgOutputEncoding;

  $dbr =& wfGetDB( DB_SLAVE );

  // ###### INVALIDATE CACHE ######
  global $wgTitle;
  $ts = mktime();
  $now = gmdate("YmdHis", $ts + 120);
  $ns = $wgTitle->getNamespace();
  #$ti = wfStrencode($wgTitle->getDBkey());
  $ti = $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, "");
  $dbr->query( $sql );

  $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, "");
    $ret = $dbr->query( $sql );
  }

  $sql = "INSERT INTO $wgDBprefix"."online (userid,username,timestamp) VALUES ('$userid','$username','$now')";
  $output = $sql;
  #wfQuery($sql, DB_WRITE, "");
  $dbr->query( $sql );
  $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;
  $Userlist ="";
  while( $row = $dbr->fetchObject( $res ) ){
    $Userlist .= " [[User:".$row->username."|".$row->username."]] ";
  }
  $dbr->freeResult( $res );

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

#return $output;
#return $wgOut->parse($output,false);
$localParser = new Parser();
$parserOutput =  $localParser->parse( $output, $parser->mTitle, $parser->mOptions, false );
return $parserOutput->getText();
}

?>

3. Add the following to the end of LocalSettings.php:

require_once("extensions/WhosOnline.php");

4. Add the following code in your wiki page:

<b>Users Online</b>: <whosonline></whosonline>

 

Now save the page and view it in browser. You are done.

Advertisements

4 thoughts on “MediaWiki Extension – WhosOnline

  1. Marcel de Ruiter says:

    Hi Mizanur,

    Sounds like an interesting extension, but I am puzzled a bit at what it offers exactly. Does it show the logged-in users (would be a bit strange since some people never log off)? Or is it something else, like users being active the last x minutes?

    Thanks in advance for your answer.

    Best regards,
    Marcel

  2. Jakob says:

    Hi!

    I really wanna make this extension work on my wiki.. I get this error when I try to create the table though (I’m not so handy at MySQL):

    #1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘0′,

    `username` varchar(255) NOT NULL default â€

    Can you help me?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s