WikiStats

From PeacockWiki

Jump to: navigation, search

WikiStats is some php code designed to keep stats on a Mediawiki site.

Currently it is a dirty hack to gather information from lyricwiki.org, but hopefully it will develop into a suite to generate detailed stats from a MediaWiki database.

Contents

Details

  • Date 11 July 2006

Features

  • Gathers and records data at a pre-defined interval
  • generates useful stats based on recorded historical data

Usage

To use these scripts, ensure they are installed correctly, and point your browser at the path at which the scripts are installed. eg

http://mywiki.com/stats/

You can also view daily stats by viewing

http://mywiki.com/stats/?daily=1

Installation

These scripts run on a standard apache/php server.

Requirements

  • Apache server running PHP
  • Cron daemon

Procedure

Copy files index.php update.php and .htaccess to a directory under htdocs on your apache server.

Set up a cron job to run update.php at a pre-defined interval. Ensure the command is run in the same as update.php. An example of a bash script to do this is:

cd $(dirname $0)
php update.php

For daily stats to work, the script must be run at 00:00 (midnight), with in the first minute of the day, from 00:00:00 to 00:00:59.

Source Code

update.php

<?php
  $statline="";
  function freaderror($errorno, $errmsg, $filename, $linenum, $vars)
  {
    global $statline;
    $statline='ERR:'.$errmsg;
  }
  set_error_handler('freaderror');
  $statspage=fopen('http://lyricwiki.org/Special:Statistics?action=raw', 'r');
  if($statspage!=false)
  $statline=fread($statspage, 1024);
  restore_error_handler();
  $logfile=fopen('log.txt', 'a');
  #date_default_timezone_set('UTC');
  fwrite($logfile, date('Y-m-d H:i:s')."\t".addslashes($statline));
?>

index.php

<html>
<head>
  <title>LyricWiki.org Stats</title>
  <meta http-equiv="Refresh" content="900;URL=">
</head>
<body>
<a href="http://wiki.peacocktech.com/wiki/WikiStats">Script Details</a>
<?php
  $logfile=fopen('log.txt', 'r');
  $logfile=trim(fread($logfile,  filesize('log.txt')));
  $logfilelines=split("\n", $logfile);
  $logfile=array();
  foreach($logfilelines as $id=>$data)
  {
    $data=split("\t", trim($data));
    $data2=array('date' => str_replace(' ', ' ', $data[0]));
    if(key_exists('daily', $_GET) AND (strpos($data2['date'], '24:00:')===FALSE) AND (strpos($data2['date'], '00:00:')===FALSE))
      continue;
    if(strpos($data[1], 'ERR:')!==FALSE)
    {
      $data2['error']=$data[1];
      $logfile[$id]=$data2;
      continue;
    }
    $data=split(';', $data[1]);
    foreach($data as $key=>$data)
    {
      $data=split('=', $data);
      $data2[$data[0]]=$data[1];
    }
    $logfile[$id]=$data2;
  }

  $diff=array('total'=>0, 'good'=>0, 'views'=>0, 'edits'=>0, 'users'=>0, 'admins'=>0, 'images'=>0);
  foreach($logfile as $id=>$data)
  {
    if(key_exists('error', $data))
      continue;
    $logfile[$id]['PercentAdmins']=number_format($data['admins']/$data['users']*100, 2).'%';
    $logfile[$id]['EditsPerView']=number_format($data['edits']/$data['views'], 4);
    $logfile[$id]['EditsPerPage']=number_format($data['edits']/$data['total'], 4);
    foreach($diff as $diffid=>$diffval)
    {
      $logfile[$id]['diff'.$diffid]=$data[$diffid]-$diffval;
      $diff[$diffid]=$logfile[$id][$diffid];
    }
  }
  #var_dump($logfile);

  $fields=array(
    'date',
    'total',
    'difftotal',
    'good',
    'diffgood',
    'views',
    'diffviews',
    'edits',
    'diffedits',
    'users',
    'diffusers',
    'admins',
    'diffadmins',
    'images',
    'diffimages',
    'PercentAdmins',
    'EditsPerView',
    'EditsPerPage'
  );
?>
<table border=1 style="font-size:12px;">
<?php
  function header_row()
  {
    global $fields;
    echo '<tr>';
    foreach($fields as $field)
    {
      echo '<td>'.$field.'</td>';
    }
    echo "</tr>\n";
  }

  $rowno=0;
  foreach($logfile as $data)
  {
    if(($rowno++ % 24)==0)
      header_row();
    echo "<tr>";
    foreach($fields as $field)
    {
      if(key_exists('error', $data))
      {
        if($field=='total')
          echo '<td colspan='.(count($fields)-1).'>'.$data['error'].'</td>';
        if($field=='date')
          echo '<td>'.$data[$field].'</td>';
      }
      else
        echo '<td>'.$data[$field].'</td>';
    }
    echo "</tr>\n";
  }
?>
</table>
</body>
</html>

.htaccess

<Files .htaccess>
  order deny,allow
  deny from all
</Files>
<Files update.php>
  order deny,allow
  deny from all
</Files>
<Files log.txt>
  order deny,allow
  deny from all
</Files>

Related Info

Personal tools