MW: Whos Online Extension

From ChekMate Security Group

Contents

Maintainer

Shannon McNaught (smcnaught)

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: <b>Users Online</b>: <whosonline></whosonline>

Installation

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
# 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 ) {
  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);
  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 .= "[[User:".$row->username."|".$row->username."]] ";
  }

  $dbr->freeResult( $res );

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

#  return $output;
return $wgOut->parse($output);
}

?>


Add to your LocalSettings.php file

Add the following to the end of LocalSettings.php:

require_once("extensions/WhosOnline.php");