r90132 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90131‎ | r90132 | r90133 >
Date:18:30, 15 June 2011
Author:nimishg
Status:deferred (Comments)
Tags:
Comment:
adding code for skeleton API
Modified paths:
  • /trunk/wikistats/MetricsReporting/MetricsReporting.php (added) (history)
  • /trunk/wikistats/MetricsReporting/api (added) (history)
  • /trunk/wikistats/MetricsReporting/api/ApiAnalytics.php (added) (history)
  • /trunk/wikistats/MetricsReporting/api/ApiAnalyticsMetric.php (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics/comscore_reach_percentage.php (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics/comscore_unique_visotors.php (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics/dump_active_editors_100.php (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics/dump_active_editors_5.php (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics/dump_article_count.php (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics/dump_binary_count.php (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics/dump_edits.php (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics/dump_new_registered_editors.php (added) (history)
  • /trunk/wikistats/MetricsReporting/metrics/squid_page_views.php (added) (history)

Diff [purge]

Index: trunk/wikistats/MetricsReporting/MetricsReporting.php
@@ -0,0 +1,91 @@
 2+<?php
 3+
 4+$wgeAnalyticsFieldNames = array(
 5+ 'select_regions' => "REGION",
 6+ 'select_countries' => "COUNTRY",
 7+ 'select_web_projects' => "WEBPROJ",
 8+ 'select_wikis' => "WIKI",
 9+ 'select_editors' => "EDITOR",
 10+ 'select_edits' => "EDITS",
 11+ 'select_platform' => "MOBILE",
 12+);
 13+
 14+
 15+$wgeAnalyticsValidParams = array(
 16+
 17+"select_regions" => array(
 18+ "AS" => "Asia Pacific",
 19+ "C" => "China",
 20+ "EU" => "Europe",
 21+ "I" => "India",
 22+ "LA" => "Latin-America",
 23+ "MA" => "Middle-East/Africa",
 24+ "NA" => "North-America",
 25+ "US" => "United States",
 26+ "W" => "World",
 27+),
 28+//"select_countries" => array(),
 29+//"select_web_properties"=> array(),
 30+"select_projects"=> array(
 31+ "wb" => "Wikibooks",
 32+ "wk" => "Wiktionary",
 33+ "wn" => "Wikinews",
 34+ "wp" => "Wikipedia",
 35+ "wq" => "Wikiquote",
 36+ "ws" => "Wikisource",
 37+ "wv" => "Wikiversity",
 38+ "co" => "Commons",
 39+ "wx" => "Other projects",
 40+),
 41+//"select_wikis"=> array(),
 42+"select_editors"=> array(
 43+ "A" => "Anonymous",
 44+ "R" => "Registered User",
 45+ "B" => "Bot",
 46+),
 47+"select_edits"=> array(
 48+ "M" => "Manual",
 49+ "B" => "Bot",
 50+),
 51+"select_platform"=> array(
 52+ "M" => "Moblie",
 53+ "N" => "Non-Mobile",
 54+),
 55+
 56+);
 57+
 58+
 59+$wgeAnalyticsMetricsList = array();
 60+$metricsdir = $dir."/metrics";
 61+$dh = opendir($metricsdir);
 62+while( ($file = readdir($dh)) !== false){
 63+ if(filetype($dir.$file) == "file" ){
 64+ $file_path_parts = pathinfo($dir.$file);
 65+ if($file_path_parts['extension'] == 'php'){
 66+ $wgAutoloadClasses["ApiAnalyticsMetric{$file_path_parts['filename']}"] = $dir.$file;
 67+ include_once($dir.$file);
 68+ $wgeAnalyticsMetricsList[] = $file_path_parts['filename'];
 69+ }
 70+
 71+ }
 72+
 73+}
 74+
 75+
 76+function wfAnalyticsMetricConnection() {
 77+ global $wgeAnalyticsMetricDBserver, $wgeAnalyticsMetricDBname;
 78+ global $wgeAnalyticsMetricDBuser, $wgeAnalyticsMetricDBpassword;
 79+
 80+ static $db;
 81+
 82+ if ( !$db ) {
 83+ $db = new DatabaseMysql(
 84+ $wgeAnalyticsMetricDBserver,
 85+ $wgeAnalyticsMetricDBuser,
 86+ $wgeAnalyticsMetricDBpassword,
 87+ $wgeAnalyticsMetricDBname );
 88+ $db->query( "SET names utf8" );
 89+ }
 90+
 91+ return $db;
 92+}
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/MetricsReporting.php
___________________________________________________________________
Added: svn:eol-style
193 + native
Index: trunk/wikistats/MetricsReporting/metrics/comscore_reach_percentage.php
@@ -0,0 +1,16 @@
 2+<?php
 3+
 4+class ApiAnalyticsMetriccomscore_reach_percentage extends ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = true;
 8+
 9+ protected $name = "comscore_reach_percentage";
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array("select_regions", "select_countries");
 13+ }
 14+
 15+}
 16+
 17+//Any additions go here
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/metrics/comscore_reach_percentage.php
___________________________________________________________________
Added: svn:eol-style
118 + native
Index: trunk/wikistats/MetricsReporting/metrics/dump_active_editors_100.php
@@ -0,0 +1,16 @@
 2+<?php
 3+
 4+class ApiAnalyticsMetricdump_active_editors_100 extends ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = true;
 8+
 9+ protected $name = "dump_active_editors_100";
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array("select_projects", "select_wikis");
 13+ }
 14+
 15+}
 16+
 17+//Any additions go here
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/metrics/dump_active_editors_100.php
___________________________________________________________________
Added: svn:eol-style
118 + native
Index: trunk/wikistats/MetricsReporting/metrics/dump_active_editors_5.php
@@ -0,0 +1,16 @@
 2+<?php
 3+
 4+class ApiAnalyticsMetricdump_active_editors_5 extends ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = true;
 8+
 9+ protected $name = "dump_active_editors_5";
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array("select_projects", "select_wikis");
 13+ }
 14+
 15+}
 16+
 17+//Any additions go here
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/metrics/dump_active_editors_5.php
___________________________________________________________________
Added: svn:eol-style
118 + native
Index: trunk/wikistats/MetricsReporting/metrics/dump_edits.php
@@ -0,0 +1,16 @@
 2+<?php
 3+
 4+class ApiAnalyticsMetricdump_edits extends ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = true;
 8+
 9+ protected $name = "dump_active_edits";
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array("select_projects", "select_wikis");
 13+ }
 14+
 15+}
 16+
 17+//Any additions go here
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/metrics/dump_edits.php
___________________________________________________________________
Added: svn:eol-style
118 + native
Index: trunk/wikistats/MetricsReporting/metrics/squid_page_views.php
@@ -0,0 +1,17 @@
 2+<?php
 3+
 4+class ApiAnalyticsMetricsquid_page_views extends ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = true;
 8+
 9+ protected $name = "squid_page_views";
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array("select_regions", "select_countries", "select_web_properties", "select_projects", "select_wikis", "select_platform"
 13+ );
 14+ }
 15+
 16+}
 17+
 18+//Any additions go here
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/metrics/squid_page_views.php
___________________________________________________________________
Added: svn:eol-style
119 + native
Index: trunk/wikistats/MetricsReporting/metrics/dump_article_count.php
@@ -0,0 +1,16 @@
 2+<?php
 3+
 4+class ApiAnalyticsMetricdump_article_count extends ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = true;
 8+
 9+ protected $name = "dump_article_count";
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array("select_projects", "select_wikis");
 13+ }
 14+
 15+}
 16+
 17+//Any additions go here
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/metrics/dump_article_count.php
___________________________________________________________________
Added: svn:eol-style
118 + native
Index: trunk/wikistats/MetricsReporting/metrics/dump_binary_count.php
@@ -0,0 +1,16 @@
 2+<?php
 3+
 4+class ApiAnalyticsMetricdump_binary_count extends ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = true;
 8+
 9+ protected $name = "dump_binary_count";
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array("select_projects", "select_wikis");
 13+ }
 14+
 15+}
 16+
 17+//Any additions go here
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/metrics/dump_binary_count.php
___________________________________________________________________
Added: svn:eol-style
118 + native
Index: trunk/wikistats/MetricsReporting/metrics/dump_new_registered_editors.php
@@ -0,0 +1,16 @@
 2+<?php
 3+
 4+class ApiAnalyticsMetricdump_new_registered_editors extends ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = true;
 8+
 9+ protected $name = "dump_new_registered_editors";
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array("select_projects", "select_wikis");
 13+ }
 14+
 15+}
 16+
 17+//Any additions go here
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/metrics/dump_new_registered_editors.php
___________________________________________________________________
Added: svn:eol-style
118 + native
Index: trunk/wikistats/MetricsReporting/metrics/comscore_unique_visotors.php
@@ -0,0 +1,16 @@
 2+<?php
 3+
 4+class ApiAnalyticsMetriccomscore_unique_visitors extends ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = true;
 8+
 9+ protected $name = "comscore_unique_visitors";
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array("select_regions", "select_countries");
 13+ }
 14+
 15+}
 16+
 17+//Any additions go here
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/metrics/comscore_unique_visotors.php
___________________________________________________________________
Added: svn:eol-style
118 + native
Index: trunk/wikistats/MetricsReporting/api/ApiAnalytics.php
@@ -0,0 +1,116 @@
 2+<?php
 3+
 4+if ( !defined( 'MEDIAWIKI' ) ) {
 5+ // Eclipse helper - will be ignored in production
 6+ require_once( 'ApiBase.php' );
 7+}
 8+
 9+
 10+class ApiAnalytics extends ApiBase {
 11+
 12+ public function execute() {
 13+
 14+ global $wgeAnalyticsMetricsList;
 15+ $params = $this->extractRequestParams();
 16+ $result = $this->getResult();
 17+
 18+ foreach($wgeAnalyticsMetricsList as $metricName){
 19+ if($metricName == $params["metric"]){
 20+ $metricClassName = "ApiAnalyticsMetric$metricName";
 21+ $metric = new $metricClassName;
 22+ $filterParams = $metric->getAllowedFilterParams();
 23+ $queries = array();
 24+ foreach($filterParams as $filterParam){
 25+ if(isset( $params["$filterParam"])){
 26+ $queries= array_merge($queries, $this->collectQueryFilters($filterParam, $params["$filterParam"]));
 27+ }
 28+ }
 29+
 30+ $range = " DATE = {$params["months"]}"; //TODO: query
 31+
 32+ if(strpos($params["months"],";") !== FALSE){
 33+ $rangeVals = explode($params["months"], ";");
 34+ $range = " DATE >= $rangeVals[0] AND DATE <= $rangeVals[1]";
 35+ }
 36+
 37+ foreach($queries as &$query){
 38+ $query = $query . " AND " . $range;
 39+
 40+ if($metric->canBeNormalized){
 41+ $result->addValue("","normalized","false");
 42+ //TODO: modify queries for normalization
 43+ if(isset($params["normalized"]) && (strpos($params["normalized"], "true") !== FALSE)){
 44+ //is normalized
 45+ $result->addValue("","normalized","true");
 46+ }
 47+ }
 48+
 49+ }
 50+
 51+ $language = $params["report_language"];
 52+
 53+ $dbr = wfAnalyticsMetricConnection();
 54+ //TODO: build return object from queries
 55+
 56+
 57+
 58+ }
 59+ }
 60+ }
 61+
 62+
 63+ //override with vars for individual metrics
 64+ protected function collectQueryFilters($field, $fieldVal){
 65+ global $wgeAnalyticsFieldNames;
 66+
 67+ //get rid of any potential whitespace
 68+ $param = preg_replace('/\s\s+/', '', $fieldVal);
 69+ $returnQueries = array();
 70+
 71+ if(!isset ($wgeAnalyticsFieldNames["$field"]) ){
 72+ return $returnQueries;
 73+ }
 74+
 75+ $fieldName = $wgeAnalyticsFieldNames["$field"];
 76+
 77+ $sub_queries = explode(",", $param);
 78+ foreach($sub_queries as $sub_query){
 79+ $topval = 0;
 80+ if(strpos($sub_query,"top:") !== FALSE){
 81+ $topval = (int) substr($sub_query, strpos($sub_query,"top:") + 4);
 82+ }
 83+
 84+ if(strpos($sub_query,"+") !== FALSE ){
 85+ $and_params = explode("+", $sub_query);
 86+ foreach($and_params as $and_param){
 87+ $this->validate_atomic_param($and_param, $field);
 88+ }
 89+ $returnQueries[] = "$filedName = $sub_query "; //TODO: This is not what the query means
 90+ }
 91+ else{
 92+ if(!$topval){
 93+ //TODO:check for {project}:{lang} (ie wp:en) style here
 94+
 95+ $this->validate_atomic_param($sub_query, $field);
 96+ $returnQueries[] = "$fieldName = $sub_query ";
 97+ }
 98+ else{
 99+ $returnQueries[] = "$fieldName < $topval"; //TODO: This is not what this query means
 100+ }
 101+ }
 102+ }
 103+ return $returnQueries;
 104+ }
 105+
 106+ protected function validate_atomic_param($param, $field){
 107+ global $wgeAnalyticsValidParams;
 108+ if(isset($wgeAnalyticsValidParams["$field"]) && count($wgeAnalyticsValidParams["$field"]) > 0){
 109+ if(count( array_keys($wgeAnalyticsValidParams["$field"], $param) ) > 0){
 110+ return true;
 111+ } else {
 112+ return false;
 113+ }
 114+ }
 115+ return true;
 116+ }
 117+}
\ No newline at end of file
Property changes on: trunk/wikistats/MetricsReporting/api/ApiAnalytics.php
___________________________________________________________________
Added: svn:eol-style
1118 + native
Index: trunk/wikistats/MetricsReporting/api/ApiAnalyticsMetric.php
@@ -0,0 +1,18 @@
 2+<?php
 3+
 4+abstract class ApiAnalyticsMetric{
 5+
 6+
 7+ protected $canBeNormalized = false;
 8+
 9+ protected $name = null;
 10+
 11+ protected function getAllowedFilterParams(){
 12+ return array();
 13+ }
 14+
 15+
 16+
 17+}
 18+
 19+
Property changes on: trunk/wikistats/MetricsReporting/api/ApiAnalyticsMetric.php
___________________________________________________________________
Added: svn:eol-style
120 + native

Comments

#Comment by Reedy (talk | contribs)   19:06, 15 June 2011

You shouldn't need to build SQL queries manually.... ie the

$query = $query . " AND " . $range;
#Comment by Reedy (talk | contribs)   22:40, 15 June 2011

Can you stylize this too please?

#Comment by Reedy (talk | contribs)   22:44, 15 June 2011

Also, you don't look to have an MW entrypoint/loader etc file...

#Comment by 😂 (talk | contribs)   22:57, 15 June 2011

Look at MetricsReporting.php. Although it's not in the usual style. The following is particularly scary:

$metricsdir = $dir."/metrics";
$dh = opendir($metricsdir);
while( ($file = readdir($dh)) !== false){
	if(filetype($dir.$file) == "file" ){
		$file_path_parts = pathinfo($dir.$file);
		if($file_path_parts['extension'] == 'php'){
			$wgAutoloadClasses["ApiAnalyticsMetric{$file_path_parts['filename']}"] = $dir.$file;
			include_once($dir.$file);
			$wgeAnalyticsMetricsList[] = $file_path_parts['filename'];
		}
	
	}

}
#Comment by Nimish Gautam (talk | contribs)   00:16, 16 June 2011

I was trying to figure out an easy way to 'drop in' new metrics with minimal effort on the part of the metrics developer (some of which may be very nontechnical)...any suggestions most definitely welcome

#Comment by 😂 (talk | contribs)   00:23, 16 June 2011

You could put them in an array, and then load $wgAutoloadClasses from that. Something like

// Add new metrics classes here
$myMetrics = ( 'foo', 'bar', 'baz' );

// Initialize classes from metric files
foreach( $myMetrics as $metric ) {
	// Generate classname and filename from $metric. You'll need to enforce some naming convention
	$wgAutoloadClasses[$className] = $fileName;
}

You'll note there's no need for an include or require. Autoloader does all that magic for you once you've given it the classname => path associations.

Status & tagging log