Open main menu

Extension:UsageBulkStats

MediaWiki extensions manual
OOjs UI icon advanced.svg
Usage Bulk Stats
Release status: beta
Implementation User activity
Description Builds a mass statistics table. Extension was build to produce a SPSS rapport.
Author(s) user:Rinkel
Latest version 100415
MediaWiki Is tested on 1.14 , 1.15 and 1.16
License No license specified
Download see below
Added rights
GNU
Translate the UsageBulkStats extension if it is available at translatewiki.net
Check usage and version matrix.

Contents

GoalEdit

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

InstallationEdit

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.

ThanksEdit

Guillermo Azurdia for the browser detection function. Extended browser detection.

HelpEdit

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

changelogEdit

  • 091211 version. All the database requests are now handled with the mediawiki code. + upgrade of the browser code
  • 100415 Made compatible with mediawiki 1.16

Code - UsageBulkStats.phpEdit

<?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' => '100415',
	'description' => 'Stats extension',
);


// captain Hook
//$wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'SaveUsageBulkStats';


$wgHooks['BeforePageDisplay'][] = '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, $wgDBprefix;

	
	#master database handle
	$dbw =& wfGetDB( DB_MASTER );
	#slave database handle
	$dbr =& wfGetDB( DB_SLAVE );
	
	$dbw->query("	CREATE TABLE IF NOT EXISTS ".$wgDBprefix."usage_bulk_stats
				(
					`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
		"
		);
	


	// count the total amount of pages in the wiki 
	// How can i use // {{NUMBEROFPAGES}} ?	
	$total_page = $dbr->fetchObject( $dbr->query("SELECT COUNT(*) as total FROM ".$wgDBprefix."page") );
	
	// count the total amount of media in the wiki 
	// how can i use // {{NUMBEROFFILES}} ?
	$total_media = $dbr->fetchObject( $dbr->query("SELECT COUNT(*) as total FROM ".$wgDBprefix."image") );
		
	// Count the total size of all the uploaded media in bytes.
	$total_space = $dbr->fetchObject( $dbr->query("SELECT SUM(img_size) as total FROM ".$wgDBprefix."image") );
	
	// Get the information about the used browser, Browser version and which computer platform is used
	$browser = _browser();	// get the platform, browser and version
	
	// which action
	$action = $wgRequest->getVal('action');
	
	if ($action == "")
		$action = "empty"; 
	
	
	
	
	// referer 
	$referer = "";
	if(isset($_SERVER['HTTP_REFERER']))
		$referer	=	$_SERVER['HTTP_REFERER'];
	
	
	$dbw->insert( 'usage_bulk_stats',
	                array(
	                         'user_id' 		=> $wgUser->mId,
	                         'user_name'    => $wgUser->mName,
	                         'visit_time'  	=> date('YmdHis') ,
	                         'page' 		=> $wgTitle ,
	                         'ip'  			=> $_SERVER['REMOTE_ADDR'] ,
	                         'action' 		=> $action ,
	                         'referer'  	=> $referer ,
	                         'platform' 	=> $browser['platform'] ,
	                         'browser'  	=> $browser['browser'] ,
	                         'version'  	=> $browser['version'] ,
	                         'total_page'  	=> $total_page->total ,
	                         'total_media'  => $total_media->total ,
	                         'total_space'  => $total_space->total 
	                         )
	              ,  "",
	                'IGNORE' );	
	
	
	return true;
}



/*
$Id: v 1.4 2009/12/08 11:22:00 $

<XBD, Extended Browser Detection.>
Copyright (C) <2009>  <Guillermo Azurdia, www.nopticon.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function _print($name, $ret)
{
	echo $name . '<br />';
	
	if (!f($ret))
	{
		$ret = 'false';
	}
	elseif ($ret == '1')
	{
		$ret = 'true';
	}
	
	echo '<blockquote><pre>';
	print_r($ret);
	echo '</pre></blockquote>';
	
	return;
}

function v_server($a)
{
	return (isset($_SERVER[$a])) ? $_SERVER[$a] : '';
}

function f($s)
{
	return !empty($s);
}

function w($a = '', $d = false)
{
	if (!f($a) || !is_string($a)) return array();
	
	$e = explode(' ', $a);
	if ($d !== false)
	{
		foreach ($e as $i => $v)
		{
			$e[$v] = $d;
			unset($e[$i]);
		}
	}
	
	return $e;
}

/*
If you want to add more browsers, do it but be careful... order matters.
*/
function _browser($a_browser = false, $a_version = false, $name = false, $d_name = false, $ret_ary = false)
{
	$browser_list  = 'nokia motorola samsung sonyericsson blackberry iphone htc ';
	$browser_list .= 'flock firefox konqueror lobo msie netscape navigator mosaic netsurf lynx amaya omniweb ';
	$browser_list .= 'googlebot googlebot-image feedfetcher-google gigabot msnbot thunderbird fennec minimo ';
	$browser_list .= 'minefield chrome wget cheshire safari avant camino seamonkey aol bloglines ';
	$browser_list .= 'wii playstation netfront opera mozilla gecko ubuntu';
	
	$browser_type = array(
		'mobile' => 'nokia motorola samsung sonyericsson blackberry iphone fennec minimo htc',
		'console' => 'wii playstation',
		'bot' => 'googlebot googlebot-image feedfetcher-google gigabot msnbot bloglines'
	);
	
	$platforms = array(
		'linux' => w('linux'),
		'mac' => array('macintosh', 'mac platform x', 'mac os x'),
		'windows' => w('windows win32')
	);
	
	$user_browser = strtolower(v_server('HTTP_USER_AGENT'));
	
	$this_version = $this_browser = $this_platform = '';
	
	if ($a_browser == '*') {
		$a_browser = $a_version = $name = false;
		$d_name = true;
	}
	
	if ($a_browser === false && $a_version === false && $name === false && $d_name !== false)
	{
		return $user_browser;
	}
	
	foreach (w('user_browser a_browser a_version name d_name') as $row)
	{
		$vrow = $$row;
		if (is_string($vrow)) {
			$$row = strtolower($vrow);
		}
	}
	
	$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 || f($this_browser)) continue;
		
		$this_browser = $row;
		$j = strpos($user_browser, $row) + strlen($row);
		$j2 = substr($user_browser, $j, 1);
		if (preg_match('#[\/\_\-\ ]#', $j2)) {
			$j += 1;
		}
		
		for (; $j <= $browser_limit; $j++)
		{
			$s = trim(substr($user_browser, $j, 1));
			if (!preg_match('/[\w\.\-]/', $s)) break;
			
			$this_version .= $s;
		}
	}
	
	if ($a_browser !== false && ($d_name === false || $name === true) && $ret_ary === false)
	{
		$ret = false;
		if (strtolower($a_browser) == $this_browser)
		{
			$ret = true;
			if ($a_version !== false)
			{
				if (f($this_version))
				{
					$a_sign = explode(' ', $a_version);
					if (version_compare($this_version, $a_sign[1], $a_sign[0]) === false) {
						$ret = false;
						$vf = true;
					}
				}
				else
				{
					$ret = false;
				}
			}
		}
		
		if ($name !== true)
		{
			return $ret;
		}
	}
	
	foreach ($platforms as $os => $match)
	{
		foreach ($match as $os_name)
		{
			if (strpos($user_browser, $os_name) !== false)
			{
				$this_platform = $os;
				break 2;
			}
		}
	}
	
	$this_type = '';
	if (f($this_browser))
	{
		foreach ($browser_type as $type => $browsers)
		{
			foreach (w($browsers) as $row)
			{
				if (strpos($this_browser, $row) !== false)
				{
					$this_type = $type;
					break 2;
				}
			}
		}
		
		if (!$this_type) $this_type = 'desktop';
	}
	
	if ($name !== false && $ret_ary === false)
	{
		if ($a_browser !== false && $a_version !== false && $ret === false)
		{
			return false;
		}
		
		$s_browser = '';
		$s_data = array($this_type, $this_platform, $this_browser, $this_version);
		foreach ($s_data as $row)
		{
			if (f($row)) $s_browser .= (($s_browser != '') ? ' ' : '') . $row;
		}
		
		return $s_browser;
	}
	
	return array(
		'browser' => $this_browser,
		'version' => $this_version,
		'platform' => $this_platform,
		'type' => $this_type,
		'useragent' => $user_browser
	);
}