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.

My First Book….

Today I received the cover page of my first book “MediaWiki Administrators’ Tutorial Guide” from Packt Publishing. I knew it is going to publish at the end of February or early march and was expecting to see the cover of the book beforehand. Adding to my surprise I was invited by Nikhil to discuss the cover page of my book. I am an amateur photographer and I was willing to contribute to the cover image of the book. Akshara (my technical editor) and Nikhil, both remembered the word ‘photographer’ from me and they wanted to have my thoughts on the cover image. I sent them one of my taken pictures and was not sure it will make any impact on the book cover. Next morning I woke up from bed and read the mail from Nikhil that my picture had been chosen for my book. It was really a surprising moment for me as I relish my biggest success as a photographer. I hope others like the picture too as well as the book. Now I am counting the days when I will received the email from Packt with the confirmation that the book has been published.