Extension:Whos online

From MediaWiki.org

Jump to: navigation, search
Zeichen 206.svg WARNING: the code or configuration described here poses a major security risk.

Problem: Possible injection of raw SQL if username contains apostrophe
Solution: use $dbr->addQuotes whereever necessary
Signed: Bryan 20:54, 21 August 2008 (UTC)

       

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
Whos online

Release status: unknown

Implementation  Tag, User activity
Description 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.
Author(s)  Shannon McNaught
License No license specified
Download see below
Example  http://www.wikiindex.org/

check usage (experimental)

Quick little extension that shows the number of guests and the number of registered users online.

Contents

[edit] Maintainer

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

[edit] Homepage

ChekMate Technical Focus Group

[edit] 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.

[edit] 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:

  • The most up to date version of the original extension can be found at chekmate.org.
  • Wikia made some changes to this since the original version caused database problems. You can find those changes at svn.wikimedia.org.

[edit] Special Thanks

  • Special thanks to Xypron for adding the table creation functionality within the PHP script.

[edit] Example

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

The above example in Wiki text: <b>Users Online</b>: <whosonline></whosonline>

[edit] Installation

[edit] 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';

[edit] 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 <whosonline> tags
  $wgParser->setHook( "whosonline", "renderWhosOnline" );
}
 
#parser hook callback function
function renderWhosOnline( $input ) {
  global $wgUser, $wgDBprefix,$wgVersion,$wgOut;
  global $wgOutputEncoding;
 
  // ###### 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 = $dbr->addQuotes($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);
}

[edit] 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");

[edit] Other sites using this extension

[edit] See also