Difference between revisions of "Extension:CurrentUsers"

From Organic Design wiki
(Bug with MW1.12.0, need to expand $user template)
m (Nad moved page Extension:CurrentUsers.php to Extension:CurrentUsers without leaving a redirect)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<?php
+
{{svn|extensions|MediaWiki/CurrentUsers/CurrentUsers.php}}
# *** If you're running MediaWiki 1.12.0 or Higher, please download the link below instead. ***
+
[[Category:Extensions|CurrentUsers]]
# - http://www.organicdesign.co.nz/Extension:CurrentUsers.php/1.12
 
 
 
# Extension:CurrentUsers{{Category:Extensions|CurrentUsers}}{{php}}
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]{{Category:Extensions created with Template:Extension}}
 
# - 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'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
 
'description' => 'An example extension made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension]',
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:CurrentUsers',
 
'version'    => CURRENTUSERS_VERSION
 
);
 
 
# 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';
 
else $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");
 
        $bot = '';
 
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));
 
}
 
 
# 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  $parser->preprocess($users,$wgTitle,$parser->mOptions);
 
}
 

Latest revision as of 10:23, 30 April 2015

Info.svg This code is in our Git repository here.

Note: If there is no information in this page about this code and it's a MediaWiki extension, there may be something at mediawiki.org.