Difference between revisions of "Extension:CurrentUsers"

From Organic Design wiki
(still not quite right)
(list bots as well (don't think its working tho))
Line 4: Line 4:
 
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]{{Category:Extensions created with Template:Extension}}
 
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]{{Category:Extensions created with Template:Extension}}
 
# - Started: 2007-07-25
 
# - Started: 2007-07-25
 
+
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
+
define('CURRENTUSERS_VERSION','1.0.1, 2007-07-26');
+
define('CURRENTUSERS_VERSION','1.0.2, 2007-07-26');
  
 
$wgCurrentUsersMagic          = 'currentusers';
 
$wgCurrentUsersMagic          = 'currentusers';
 
$wgCurrentUsersTemplate        = 'CurrentUsers';
 
$wgCurrentUsersTemplate        = 'CurrentUsers';
 
$wgCurrentUsersTimeout        = 60;
 
$wgCurrentUsersTimeout        = 60;
 
+
 
$wgExtensionFunctions[]        = 'wfSetupCurrentUsers';
 
$wgExtensionFunctions[]        = 'wfSetupCurrentUsers';
 
$wgHooks['LanguageGetMagic'][] = 'wfCurrentUsersLanguageGetMagic';
 
$wgHooks['LanguageGetMagic'][] = 'wfCurrentUsersLanguageGetMagic';
 
+
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
 
'name'        => 'CurrentUsers',
 
'name'        => 'CurrentUsers',
 
'author'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
 
'author'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
'description' => 'Allows a list of currently active users to be embedded into a page using a template',
+
'description' => 'An example extension made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension]',
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:CurrentUsers',
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:CurrentUsers',
 
'version'    => CURRENTUSERS_VERSION
 
'version'    => CURRENTUSERS_VERSION
 
);
 
);
 
+
 
# Called from $wgExtensionFunctions array when initialising extensions
 
# Called from $wgExtensionFunctions array when initialising extensions
 
function wfSetupCurrentUsers() {
 
function wfSetupCurrentUsers() {
 
global $wgUser,$wgParser,$wgCurrentUsers,$wgCurrentUsersTimeout,$wgCurrentUsersMagic;
 
global $wgUser,$wgParser,$wgCurrentUsers,$wgCurrentUsersTimeout,$wgCurrentUsersMagic;
 
$wgParser->setFunctionHook($wgCurrentUsersMagic,'wfCurrentUsersMagic');
 
$wgParser->setFunctionHook($wgCurrentUsersMagic,'wfCurrentUsersMagic');
 +
if (strtolower($_REQUEST['title']) == 'robots.txt') $bot = 'bot';
 
$file = dirname(__FILE__).'/CurrentUsers.txt';
 
$file = dirname(__FILE__).'/CurrentUsers.txt';
 
$data = file($file);
 
$data = file($file);
Line 36: Line 37:
 
$wgCurrentUsers = array("$h:$m:$user");
 
$wgCurrentUsers = array("$h:$m:$user");
 
foreach ($data as $item) {
 
foreach ($data as $item) {
list($h,$m,$u) = split(':',trim($item));
+
list($h,$m,$u,$b) = split(':',trim($item));
 
$age = $now-$h*60-$m;
 
$age = $now-$h*60-$m;
                if ($age < 0) $age += 1440;
+
if ($age < 0) $age += 1440;
if ($u != '' && $u != $user && $age < $wgCurrentUsersTimeout) $wgCurrentUsers[] = "$h:$m:$u";
+
if ($u == $user && $b == 'bot') $bot = $b;
 +
if ($u != '' && $u != $user && $age < $wgCurrentUsersTimeout) $wgCurrentUsers[] = "$h:$m:$u:$b";
 
}
 
}
 +
$wgCurrentUsers[0] .= ":$bot";
 
file_put_contents($file,join("\n",$wgCurrentUsers));
 
file_put_contents($file,join("\n",$wgCurrentUsers));
 
}
 
}
 
+
 
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
 
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
 
function wfCurrentUsersLanguageGetMagic(&$magicWords,$langCode = 0) {
 
function wfCurrentUsersLanguageGetMagic(&$magicWords,$langCode = 0) {
Line 56: Line 59:
 
$users = '';
 
$users = '';
 
$guests = 0;
 
$guests = 0;
 +
$bots = 0;
 
foreach ($wgCurrentUsers as $item) {
 
foreach ($wgCurrentUsers as $item) {
list($h,$m,$u) = split(':',$item);
+
list($h,$m,$u,$b) = split(':',$item);
if (ereg("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$",$u)) $guests++;
+
if (User::isIP($u)) $b ? $bots++ : $guests++;
 
else $users .= "{"."{"."$wgCurrentUsersTemplate|$h:$m|$u|}"."}\n";
 
else $users .= "{"."{"."$wgCurrentUsersTemplate|$h:$m|$u|}"."}\n";
 
}
 
}
 
if ($guests) $users .= "{"."{"."$wgCurrentUsersTemplate|Guests||$guests}"."}\n";
 
if ($guests) $users .= "{"."{"."$wgCurrentUsersTemplate|Guests||$guests}"."}\n";
 +
if ($bots)  $users .= "{"."{"."$wgCurrentUsersTemplate|Robots||$bots}"."}\n";
 
return $users;
 
return $users;
 
}
 
}
 
?>
 
?>

Revision as of 02:39, 26 July 2007

<?php

  1. Extension:CurrentUsers
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:Php

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:NadCategory:Extensions created with Template:Extension
  3. - Started: 2007-07-25

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('CURRENTUSERS_VERSION','1.0.2, 2007-07-26');

$wgCurrentUsersMagic = 'currentusers'; $wgCurrentUsersTemplate = 'CurrentUsers'; $wgCurrentUsersTimeout = 60;

$wgExtensionFunctions[] = 'wfSetupCurrentUsers'; $wgHooks['LanguageGetMagic'][] = 'wfCurrentUsersLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'CurrentUsers', 'author' => 'User:Nad', 'description' => 'An example extension made with Template:Extension', 'url' => 'http://www.mediawiki.org/wiki/Extension:CurrentUsers', 'version' => CURRENTUSERS_VERSION );

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupCurrentUsers() { global $wgUser,$wgParser,$wgCurrentUsers,$wgCurrentUsersTimeout,$wgCurrentUsersMagic; $wgParser->setFunctionHook($wgCurrentUsersMagic,'wfCurrentUsersMagic'); if (strtolower($_REQUEST['title']) == 'robots.txt') $bot = 'bot'; $file = dirname(__FILE__).'/CurrentUsers.txt'; $data = file($file); $h = strftime('%H'); $m = strftime('%M'); $now = $h*60+$m; $user = $wgUser->getUserPage()->getText(); $wgCurrentUsers = array("$h:$m:$user"); foreach ($data as $item) { list($h,$m,$u,$b) = split(':',trim($item)); $age = $now-$h*60-$m; if ($age < 0) $age += 1440; if ($u == $user && $b == 'bot') $bot = $b; if ($u != && $u != $user && $age < $wgCurrentUsersTimeout) $wgCurrentUsers[] = "$h:$m:$u:$b"; } $wgCurrentUsers[0] .= ":$bot"; file_put_contents($file,join("\n",$wgCurrentUsers)); }

  1. Needed in MediaWiki >1.8.0 for magic word hooks to work properly

function wfCurrentUsersLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgCurrentUsersMagic; $magicWords[$wgCurrentUsersMagic] = array(0,$wgCurrentUsersMagic); return true; }

function wfCurrentUsersMagic(&$parser) { global $wgCurrentUsers,$wgCurrentUsersTemplate; $parser->disableCache(); $users = ; $guests = 0; $bots = 0; foreach ($wgCurrentUsers as $item) { list($h,$m,$u,$b) = split(':',$item); if (User::isIP($u)) $b ? $bots++ : $guests++; else $users .= "{"."{"."$wgCurrentUsersTemplate|$h:$m|$u|}"."}\n"; } if ($guests) $users .= "{"."{"."$wgCurrentUsersTemplate|Guests||$guests}"."}\n"; if ($bots) $users .= "{"."{"."$wgCurrentUsersTemplate|Robots||$bots}"."}\n"; return $users; } ?>