Difference between revisions of "Extension:CurrentUsers"

From Organic Design wiki
(Extension listing most recent users)
 
(A pf for current users list)
Line 8: Line 8:
 
   
 
   
 
define('CURRENTUSERS_VERSION','1.0.0, 2007-07-25');
 
define('CURRENTUSERS_VERSION','1.0.0, 2007-07-25');
 +
 +
$wgCurrentUsersMagic          = 'currentusers';
 +
$wgCurrentUsersTemplate        = 'CurrentUsers';
 +
$wgCurrentUsersTimeout        = 30;
 
   
 
   
 
$wgExtensionFunctions[]        = 'wfSetupCurrentUsers';
 
$wgExtensionFunctions[]        = 'wfSetupCurrentUsers';
Line 22: Line 26:
 
# Called from $wgExtensionFunctions array when initialising extensions
 
# Called from $wgExtensionFunctions array when initialising extensions
 
function wfSetupCurrentUsers() {
 
function wfSetupCurrentUsers() {
global $wgUser,$wgParser,$wgCurrentUsers;
+
global $wgUser,$wgParser,$wgCurrentUsers,$wgCurrentUsersTimeout,$wgCurrentUsersMagic;
$wgParser->setFunctionHook('currentusers','wfCurrentUsersMagic');
+
$wgParser->setFunctionHook($wgCurrentUsersMagic,'wfCurrentUsersMagic');
 
$file = dirname(__FILE__).'/CurrentUsers.txt';
 
$file = dirname(__FILE__).'/CurrentUsers.txt';
 
$data = file($file);
 
$data = file($file);
$time = strftime('%H:%M');
+
$h = strftime('%H');
 +
$m = strftime('%M');
 +
$now = $h*60+$m;
 
$user = $wgUser->getUserPage()->getText();
 
$user = $wgUser->getUserPage()->getText();
$wgCurrentUsers = array("$time $user");
+
$wgCurrentUsers = array("$h:$m:$user");
$max = 50;
+
foreach ($data as $item) {
foreach ($data as $i) if ($max-->0 && ereg('^.+ (.+)$',$i,$m) && $m[1] != $user) $wgCurrentUsers[] = $i;
+
list($h,$m,$u) = split(':',trim($item));
file_put_contents($file,$wgCurrentUsers);
+
if ($u != '' && $u != $user && $h*60+$m-$now < $wgCurrentUsersTimeout) $wgCurrentUsers[] = "$h:$m:$u";
 +
}
 +
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) {
$magicWords['currentusers'] = array(0,'currentusers');
+
global $wgCurrentUsersMagic;
 +
$magicWords[$wgCurrentUsersMagic] = array(0,$wgCurrentUsersMagic);
 
return true;
 
return true;
 
}
 
}
  
function wfCurrentUsersMagic(&$parser,$count = 5) {
+
function wfCurrentUsersMagic(&$parser) {
global $wgCurrentUsers;
+
global $wgCurrentUsers,$wgCurrentUsersTemplate;
 +
$parser->disableCache();
 
$users = '';
 
$users = '';
for ($i = 0; $i < $count; $i++) if (isset($wgCurrentUsers[$i]))
+
$guests = 0;
$users .= '*'.$wgCurrentUsers[$i]."\n";
+
foreach ($wgCurrentUsers as $item) {
 +
list($h,$m,$u) = split(':',$item);
 +
if (ereg("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$",$u)) $guests++;
 +
else $users .= "{"."{"."$wgCurrentUsersTemplate|$h:$m|$u|}"."}\n";
 +
}
 +
if ($guests) $users .= "{"."{"."$wgCurrentUsersTemplate|Guests||$guests}"."}\n";
 
return $users;
 
return $users;
 
}
 
}
 
?>
 
?>

Revision as of 09:09, 25 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.0, 2007-07-25');

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

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

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

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupCurrentUsers() { global $wgUser,$wgParser,$wgCurrentUsers,$wgCurrentUsersTimeout,$wgCurrentUsersMagic; $wgParser->setFunctionHook($wgCurrentUsersMagic,'wfCurrentUsersMagic'); $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) = split(':',trim($item)); if ($u != && $u != $user && $h*60+$m-$now < $wgCurrentUsersTimeout) $wgCurrentUsers[] = "$h:$m:$u"; } 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; foreach ($wgCurrentUsers as $item) { list($h,$m,$u) = split(':',$item); if (ereg("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$",$u)) $guests++; else $users .= "{"."{"."$wgCurrentUsersTemplate|$h:$m|$u|}"."}\n"; } if ($guests) $users .= "{"."{"."$wgCurrentUsersTemplate|Guests||$guests}"."}\n"; return $users; } ?>