Extension:UsageBulkStats

From MediaWiki.org

Jump to: navigation, search

           

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
Usage Bulk Stats

Release status: experimental

Implementation  User activity
Description Builds a mass statistics table. Extension was build to produce a SPSS rapport.
Author(s)  user:Rinkel
Last Version  091023
MediaWiki  Is tested on 1.14 and 1.15
License No license specified
Download see below

check usage (experimental)

Contents

[edit] Goal

For a cooperated wiki was a detailled rapport needed. I created this simple extension for recording a big amount of information which is used to created a rapport in SPSS

[edit] Installation

Copy the code to file in extensions/UsageBulkStats/UsageBulkStats.php and include it in localsettings.php with

# 	This extension builds mass statics database. 
#	link:	http://www.mediawiki.org/wiki/Extension:UsageBulkStats	
	require_once( "$IP/extensions/UsageBulkStats/UsageBulkStats.php" );

if the database table doesnt exist the script will created an table.

[edit] Thanks

ceo /a/ mmg5 /./ com for the browser detection function. http://us3.php.net/manual/en/function.get-browser.php#89387

[edit] Help

If used a lots of "hacks" to build this extension. I dont know the correct implementation of the wiki code. i people can help me with correcting those mistakes. Please :D

[edit] Code - UsageBulkStats.php

<?php
/**
 * UsageBulkStats extension - Records a lots of data in a database table. Original produced for producing statistics in SPSS
 *
 * 
 * For more info see 
 * http://mediawiki.org/wiki/Extension:UsageBulkStats
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @author Rienk Jan Schurer, www.rienkjanschurer.nl, rjs@gmx.li
 * @copyright © 2009 Rienk Jan Schurer
 * @license GNU General Public License 2.0 or later
 */
 
if( !defined( 'MEDIAWIKI' ) ) {
	echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
	// die( 1 );
}
 
 
$wgExtensionCredits['other'][] = array( 
	'name' => 'UsageBulkStats', 
	'author' => 'Rienk Jan Schurer', 
	'url' => 'http://mediawiki.org/wiki/Extension:UsageBulkStats',
	'version' => '091023',
	'description' => 'Stats extension',
);
 
 
// captain Hook
$wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'SaveUsageBulkStats';
 
 
// main function
function SaveUsageBulkStats (){
 
	// Globals are "ranzig" . But this is a time of crisis . 
	// Globals are evil. - http://www.mediawiki.org/wiki/Manual:Global_object_variables	
 
	global $wgRequest,$wgTitle,$wgUser,$wgDBuser,$wgDBpassword, $wgDBname,$wgDBserver, $wgDBprefix;
 
	// say hi to the database
	$link = mysql_connect($wgDBserver, $wgDBuser, $wgDBpassword)
	or die('Could not connect: ' . mysql_error());
	mysql_select_db($wgDBname) or die('Could not select database');
 
 
 
	// count the total amount of pages in the wiki 
	// How can i use // {{NUMBEROFPAGES}} ?
	$total_page	= 	mysql_query("SELECT COUNT(*) as total FROM ".$wgDBprefix."page") or die('Query failed: ' . mysql_error());
	$total_page =	mysql_result($total_page,0);
 
 
	// count the total amount of media in the wiki 
	// how can i use // {{NUMBEROFFILES}} ?
	$total_media	= 	mysql_query("SELECT COUNT(*) as total FROM ".$wgDBprefix."image") or die('Query failed: ' . mysql_error());
	$total_media 	=  	mysql_result($total_media,0);
 
 
	// Count the total size of all the uploaded media in bytes.
	$total_space	= 	mysql_query("SELECT SUM(img_size) FROM ".$wgDBprefix."image") or die('Query failed: ' . mysql_error());
	$total_space 	=  	mysql_result($total_space,0);
 
 
	// Get the information about the used browser, Browser version and which computer platform is used
	$browser = _browser();	// get the platform, browser and version
 
 
	// The time in YYYYMMDDHHmmss format. 
	// How can i use the {{CURRENTTIMESTAMP}} ??
	$visit_time = 	date('YmdHis');
 
 
	// User id en user name
	$user_id 	= 	$wgUser->mId;
	$user_name	= 	$wgUser->mName;
 
 
	// name of the page and which action
	$page		=	$wgTitle;
 
 
	// which action
	$action = "empty"; 
	if(isset($wgRequest->data['action'])){
		$action	=	$wgRequest->data['action'];
	}
 
 
	// the ip-number
	$ip			=	$_SERVER['REMOTE_ADDR'];
 
 
	// referer 
	$referer = "";
	if(isset($_SERVER['HTTP_REFERER'])){
		$referer	=	$_SERVER['HTTP_REFERER'];
	}
 
 
	$table = $wgDBprefix."usage_bulk_stats";
 
	$sql = "	CREATE TABLE IF NOT EXISTS  ".$table." 
					(
						`id` int(10) NOT NULL auto_increment,
						`user_id` int(10) NOT NULL,
						`user_name` varchar(255) NOT NULL,
						`visit_time` varchar(14) NOT NULL,
						`page` varchar(255) NOT NULL,
						`ip` varchar(255) NOT NULL,
						`action` varchar(255) NOT NULL,
						`referer` varchar(255) NOT NULL,
						`platform` varchar(255) NOT NULL,
						`browser` varchar(255) NOT NULL,
						`version` varchar(255) NOT NULL,
						`total_page` int(10) NOT NULL,
						`total_space` int(15) NOT NULL,
						`total_media` int(10) NOT NULL,
						PRIMARY KEY  (`id`)
					) 
					ENGINE=MyISAM 
					AUTO_INCREMENT=1 
					DEFAULT 
					CHARSET=latin1 
					AUTO_INCREMENT=1
			";
 
	// executed the sql query
	$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
 
	// build the mysql query	
	$sql = ("	INSERT INTO
						".$table."
						(
							user_id,
							user_name,
							visit_time,
							page,
							ip,
							action,
							referer,
							platform,
							browser,
							version,
							total_page,
							total_media,
							total_space	
						)
					VALUES
						(
							'".$user_id."',
							'".$user_name."',
							'".$visit_time."',
							'".$page."',
							'".$ip."',
							'".$action."',
							'".$referer."',
							'".$browser['platform']."',
							'".$browser['browser']."',
							'".$browser['version']."',
							'".$total_page."',
							'".$total_media."',
							'".$total_space."'
						)
			");
 
 
	// executed the mysql query
	$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
 
	return true;
}
 
// http://us3.php.net/manual/en/function.get-browser.php#89387
 
function w($a = '')
{
    if (empty($a)) return array();
 
    return explode(' ', $a);
}
 
function _browser($a_browser = false, $a_version = false, $name = false)
{
    $browser_list = 'msie firefox konqueror safari netscape navigator opera mosaic lynx amaya omniweb chrome avant camino flock seamonkey aol mozilla gecko';
    $user_browser = strtolower($_SERVER['HTTP_USER_AGENT']);
    $this_version = $this_browser = '';
 
    $browser_limit = strlen($user_browser);
    foreach (w($browser_list) as $row)
    {
        $row = ($a_browser !== false) ? $a_browser : $row;
        $n = stristr($user_browser, $row);
        if (!$n || !empty($this_browser)) continue;
 
        $this_browser = $row;
        $j = strpos($user_browser, $row) + strlen($row) + 1;
        for (; $j <= $browser_limit; $j++)
        {
            $s = trim(substr($user_browser, $j, 1));
            $this_version .= $s;
 
            if ($s === '') break;
        }
    }
 
    if ($a_browser !== false)
    {
        $ret = false;
        if (strtolower($a_browser) == $this_browser)
        {
            $ret = true;
 
            if ($a_version !== false && !empty($this_version))
            {
                $a_sign = explode(' ', $a_version);
                if (version_compare($this_version, $a_sign[1], $a_sign[0]) === false)
                {
                    $ret = false;
                }
            }
        }
 
        return $ret;
    }
 
    //
    $this_platform = '';
    if (strpos($user_browser, 'linux'))
    {
        $this_platform = 'linux';
    }
    elseif (strpos($user_browser, 'macintosh') || strpos($user_browser, 'mac platform x'))
    {
        $this_platform = 'mac';
    }
    else if (strpos($user_browser, 'windows') || strpos($user_browser, 'win32'))
    {
        $this_platform = 'windows';
    }
 
    if ($name !== false)
    {
        return $this_browser . ' ' . $this_version;
    }
 
    return array(
        "browser"      => $this_browser,
        "version"      => $this_version,
        "platform"     => $this_platform,
        "useragent"    => $user_browser
    );
}