User:Gryllida/stats

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

This page contains graphs for monthly publish statistics at Wikinews; there is a substantial drop in 2009, when the reviewing system was introduced.

2005+[edit]

Wikinews Stats all 2012-10.png

2006+[edit]

Wikinews Stats 2006 2012-10.png

2010+[edit]

Wikinews Stats 2010 2012-10.png

2012[edit]

Wikinews Stats 2012 01-10.png

Source[edit]

use strict;
use warnings;
use Data::Dumper;
use Chart::Lines;
use MediaWiki::API;

sub get_data{
  my $years = shift;
  my $fileName = shift;
  my @months=(1,2,3,4,5,6,7,8,9,10,11,12);
  my @xs;
  my @ys;

  my $mw = MediaWiki::API->new();
  $mw->{config}->{api_url} = 'http://en.wikinews.org/w/api.php';

  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  $year += 1900;

  for my $y (@{$years}){
    for my $m (@months){
      if ($y == $year and $m > $mon) {
        next;
      }
      my $startTime = sprintf "$y:%02d:01 00:00:00", $m;
      my $endTime;
      if ($m == 12){
        my $y1 = $y + 1;
        $endTime = "$y1:01:01 00:00:00";
      } else {
        my $m1 = $m + 1;
        $endTime = sprintf "$y:%02d:01 00:00:00", $m1;
      }
     
      print "from $startTime to $endTime\n";
      my $articles = $mw->list ( {
        action => 'query',
        list => 'categorymembers',
        cmtitle => 'Category:Published',
        cmnamespace => 0,
        cmlimit => 5000,
        cmsort => 'timestamp',
        cmdir => 'desc',
        cmstart => $endTime,
        cmend => $startTime
       } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
      
      my $count = scalar @{$articles};
     
      # Add the values to @x, @y.
      push @xs, 100*$y+$m;
      push @ys, $count;
    }
  }

  # Create chart object.
  my $chart = new Chart::Lines(800, 400);

  # Add data.
  $chart->add_dataset(@xs);
  $chart->add_dataset(@ys);

  # Set properties.
  my %property;
  $property{'integer_ticks_only'} = 'true';
  $property{'x_label'} = 'Time';
  $property{'skip_int_ticks'} = 50;
  $property{'legend_labels'} = [sprintf "Monthly Publish Statistics in %d-%d", $years->[0], $years->[-1] ];
  $property{'skip_y_ticks'} = 50;
  $property{'skip_x_ticks'} = 6;
  $property{'grid_lines'} = 'true';
  $property{'brush_size'} = 1;
  $property{'legend'} = 'bottom';
  $property{'min_val'} = 0;
  $chart->set(%property);

  # Set colors.
  my $n = 150;
  $chart->set('colors' => {'x_grid_lines' => [$n,$n,$n],
                           'y_grid_lines' => [$n,$n,$n]});

  # Plot to file.
  $chart->png($fileName);
}


my @years=(2005,2006,2007,2008,2009,2010,2011,2012);
get_data(\@years,'all.png');

@years=(2006,2007,2008,2009,2010,2011,2012);
get_data(\@years,'2006.png');

@years=(2010,2011,2012);
get_data(\@years,'2010.png');

@years=(2012);
get_data(\@years,'2012.png');