From PeacockWiki
<?php
################################################################################
# Setup
#
$wgExtensionFunctions[] = 'usSetup';
$wgHooks['UserCreateForm'][] = 'usCreateFormModify';
$wgHooks['AbortNewAccount'][] = 'usAbortUser';
$wgHooks['PreferencesUserInformationPanel'][]='usPrefs';
$wgHooks['ArticleSave'][] = 'usArticleSave';
$wgExtensionCredits['other'][] = array(
'name' => 'User Demographics Extension',
'version' => '0.0.1',
'author' => '[http://about.peacocktech.com/trevorp/ Trevor Ian Peacock]',
'url' => 'http://wiki.peacocktech.com/wiki/DemographicsExtension',
'description' => 'Facilitates recording and reporting of demographic information of contributors.'
);
function usSetup()
{
SpecialPage::addPage(new SpecialPage('Userstats', '', true, 'usSpecialStats', false));
}
################################################################################
# User Create form extension
#
# (also requires a one line addition to includes/templates/Userlogin.php)
#
function usCreateFormModify($template)
{ print "aaaaa";var_dump($template);
$html="</tr><tr><td align='right'>General Info:</td>\n";
$html.="<td>Age: ".usAgeInput($_POST['age'])."\n";
$html.="Gender: ".usGenderInput($_POST['gender'])."</td>\n";
$template->setref('stats', &$html );
}
function usAbortUser($user, $message)
{
$age=$_POST['age'];
if(!($age=='' || is_numeric($age)))
{
$message='Age must be entered correctly.';
return false; #abort
}
$g=$_POST['gender'];
if($g!='' && $g!='m' && $g!='f')
{
$message='Invalid Gender.';
return false; #abort
}
$user->setOption( 'age', $age );
$user->setOption( 'gender', $g );
return true;
}
################################################################################
# User Preferences Extension
#
function usPrefs($prefform, $userinfohtml)
{
global $wgUser;
if($_POST['wpSaveprefs']=='Save')
{
$age=$_POST['age'];
if(!($age=='' || is_numeric($age)))
$age=$wgUser->getOption('age');
$g=$_POST['gender'];
if($g!='' && $g!='m' && $g!='f')
$g=$wgUser->getOption('gender');
$wgUser->setOption( 'age', $age );
$wgUser->setOption( 'gender', $g );
$wgUser->saveSettings();
}
$userinfohtml.="<tr><td align='right'>Age:</td><td align='left'>".usAgeInput($wgUser->getOption('age'))."</td></tr>";
$userinfohtml.="<tr><td align='right'>Gender:</td><td align='left'>".usGenderInput($wgUser->getOption('gender'))."</td></tr>";
$userinfohtml.="<tr><td> </td><td> </td></tr>";
return true;
}
################################################################################
# Adds user stats to history
#
function usArticleSave(&$article, &$user, &$text, &$summary, $minor, $watch, $sectionanchor, &$flags)
{
$summary=$user->getOption('gender').$user->getOption('age').'|'.$summary;
return true;
}
################################################################################
# Adds Special page for reports
#
function usSpecialStats() {
require_once('includes/DatabaseFunctions.php');
global $wgOut;
$wgOut->setPageTitle("User Statistics");
$tables=usRunQuery("select user_id, user_options from user");
$users=array();
foreach($tables as $item)
{
$users[$item->user_id]=array();
$opts=split("\n", $item->user_options);
# array_reverse($opts);
# var_dump($opts);
foreach($opts as $opt)
{
$opt=split("=", $opt);
if($opt[0]=='gender')
if($opt[1])
$users[$item->user_id]['gender']=$opt[1];
if($opt[0]=='age')
if($opt[1])
$users[$item->user_id]['age']=$opt[1];
}
}
$tables=usRunQuery("SELECT * FROM testwiki.recentchanges");
$ages=array();
$genders=array();
foreach($tables as $item)
{
$ages[$users[$item->rc_user]['age']]+=1;
$genders[$users[$item->rc_user]['gender']]+=1;
}
$str="==Genders==\n{|border=1\n|-\n!Gender!!Count\n";
foreach($genders as $gender=>$count)
if($gender=="")
$str.="|-\n|unknown||$count\n";
else
$str.="|-\n|$gender||$count\n";
$wgOut->addWikiText($str."|}");
$str="==Ages==\n{|border=1\n|-\n!Age!!Count\n";
foreach($ages as $age=>$count)
if($age=="")
$str.="|-\n|unknown||$count\n";
else
$str.="|-\n|$age||$count\n";
$wgOut->addWikiText($str."|}");
}
################################################################################
# Function library
#
function usAgeInput($age)
{
return "<input type='text' name='age' size='3' maxlength='3' value='".$age."' />";
}
function usGenderInput($g)
{
return "<select name='gender'><option value=''></option>".
"<option value='m'".($g=='m'?" selected":"").">Male</option>".
"<option value='f'".($g=='f'?" selected":"").">Female</option></select>";
}
function usRunQuery($sql)
{
$dbr =& wfGetDB( DB_SLAVE );
$res=wfQuery($sql, DB_SLAVE, "");
$array=array();
while($item=$dbr->fetchObject( $res ))
$array[]=$item;
return $array;
}
?>