DemographicsExtension
From PeacockWiki
Contents |
[edit]
Details
- Version 0.0.1
- Date 15 Febuary 2007
- Tested on MediaWiki 1.9.2, PHP 5.1.6 (apache2handler)
[edit]
Features
- Adds tracking of age and gender of participants in the wiki, and reports on edits made by demographic groups.
- Adds fields to the create new user page.
- Adds these felds in user options
- Records the user options in edit summaries.
- Adds a special page that reports statistics
[edit]
Installation
[edit]
Extension
Copy and paste the source code into a file named 'UserStat.php', and place it in the 'extensions/' directory of your mediawiki installation.
Insert the following line into the end of 'LocalSettings.php' (before the '?>')
require("extensions/UserStat.php");
[edit]
File
Add the following line to includes/templates/Userlogin.php
<?php echo $this->data['stats']; ?>
The line should be placed under
<?php } ?>
following
<?php if( $this->data['userealname'] ) { ?>
In Mediawiki 1.9.2 this is line 177.
[edit]
Verification
To check to see if it is installed properly, visit your Version page, eg Special:Version.
You should see the following items:
[edit]
Releases
[edit]
Todo
- Determine exact requirements for data collection and reports.
[edit]
RoadMap
[edit]
0.1 No Date
- First release
[edit]
History
[edit]
0.0.1 - Date 15 Febuary 2007
- Initial prototype.
[edit]
Source Code
Authoritative version of source code available at DemographicsExtension/Source_Code.
<?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; } ?>