tosfos has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/231979

Change subject: Initial commit
......................................................................

Initial commit

Change-Id: I442412a40b41b29cf19f00d564aa942df7602540
---
A GoogleAnalyticsMetrics.hooks.php
A GoogleAnalyticsMetrics.i18n.magic.php
A GoogleAnalyticsMetrics.php
A i18n/en.json
A i18n/qqq.json
5 files changed, 177 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GoogleAnalyticsMetrics 
refs/changes/79/231979/1

diff --git a/GoogleAnalyticsMetrics.hooks.php b/GoogleAnalyticsMetrics.hooks.php
new file mode 100644
index 0000000..982a34f
--- /dev/null
+++ b/GoogleAnalyticsMetrics.hooks.php
@@ -0,0 +1,121 @@
+<?php
+
+class GoogleAnalyticsMetricsHooks {
+
+       /**
+        * Sets up the parser function
+        *
+        * @param Parser $parser
+        */
+       public static function onParserFirstCallInit( Parser &$parser ) {
+               $parser->setFunctionHook( 'googleanalyticsmetrics',
+                       'GoogleAnalyticsMetricsHooks::googleAnalyticsMetrics' );
+       }
+
+       /**
+        * Handles the googleanalyticsmetrics parser function
+        *
+        * @global string|array $wgGoogleAnalyticsMetricsAllowed
+        * @param Parser $parser Unused
+        * @param string $metric
+        * @param string $startDate
+        * @param string $endDate
+        * @return string
+        */
+       public static function googleAnalyticsMetrics( Parser &$parser, 
$metric, $startDate = null,
+               $endDate = null ) {
+               global $wgGoogleAnalyticsMetricsAllowed;
+
+               // Setting the defaults above would not allow an empty start 
parameter
+               if ( !$startDate ) {
+                       $startDate = '2005-01-01';
+               }
+               if ( !$endDate ) {
+                       $endDate = 'today';
+               }
+               if ( $wgGoogleAnalyticsMetricsAllowed !== '*' && !in_array( 
$metric,
+                               $wgGoogleAnalyticsMetricsAllowed ) ) {
+                       return self::getWrappedError( 'The requested metric is 
forbidden.' );
+               }
+
+               return self::getMetric( $metric, $startDate, $endDate );
+       }
+
+       /**
+        * Gets the Analytics metric with the dates provided
+        *
+        * @global string $wgGoogleAnalyticsMetricsViewID
+        * @param string $metric The name of the Analyitcs metric, without the 
"ga:" prefix
+        * @param string $startDate Must be a valid date recognized by the 
Google API
+        * @param string $endDate Must be a valid date recognized by the Google 
API
+        * @return string
+        */
+       public static function getMetric( $metric, $startDate, $endDate ) {
+               global $wgGoogleAnalyticsMetricsViewID;
+               $service = self::getService();
+               try {
+                       $response = $service->data_ga->get(
+                               'ga:' . $wgGoogleAnalyticsMetricsViewID, 
$startDate, $endDate, 'ga:' . $metric
+                       );
+               } catch ( Exception $e ) {
+                       MWExceptionHandler::logException( $e );
+                       return self::getWrappedError( 'Error!' );
+               }
+
+               $rows = $response->getRows();
+               return $rows[0][0];
+       }
+
+       /**
+        * Returns the Analytics service, ready for use
+        *
+        * @global string $wgGoogleAnalyticsMetricsEmail
+        * @global string $wgGoogleAnalyticsMetricsPath
+        * @global WebRequest $wgRequest
+        * @return \Google_Service_Analytics
+        */
+       private static function getService() {
+               //This entire function is copied from 
GoogleAnalyticsTopPages::getData()
+               global $wgGoogleAnalyticsMetricsEmail, 
$wgGoogleAnalyticsMetricsPath, $wgRequest;
+
+               // create a new Google_Client object
+               $client = new Google_Client();
+               // set app name
+               $client->setApplicationName( 'GoogleAnalyticsMetrics' );
+
+               $request = $wgRequest;
+               // check, if the client is already authenticated
+               if ( $request->getSessionData( 'service_token' ) !== null ) {
+                       $client->setAccessToken( $request->getSessionData( 
'service_token' ) );
+               }
+
+               // load the certificate key file
+               $key = file_get_contents( $wgGoogleAnalyticsMetricsPath );
+               // create the service account credentials
+               $cred = new Google_Auth_AssertionCredentials(
+                       $wgGoogleAnalyticsMetricsEmail, array( 
'https://www.googleapis.com/auth/analytics.readonly' ),
+                       $key
+               );
+               // set the credentials
+               $client->setAssertionCredentials( $cred );
+               if ( $client->getAuth()->isAccessTokenExpired() ) {
+                       // authenticate the service account
+                       $client->getAuth()->refreshTokenWithAssertion( $cred );
+               }
+               // set the service_token to the session for future requests
+               $request->setSessionData( 'service_token', 
$client->getAccessToken() );
+
+               // Create the needed Google Analytics service object
+               return new Google_Service_Analytics( $client );
+       }
+
+       /**
+        * Convenience function that returns text wrapped in an error class
+        *
+        * @param string $text
+        * @return string HTML
+        */
+       private static function getWrappedError( $text ) {
+               return Html::element( 'span', array( 'class' => 'error' ), 
$text );
+       }
+}
diff --git a/GoogleAnalyticsMetrics.i18n.magic.php 
b/GoogleAnalyticsMetrics.i18n.magic.php
new file mode 100644
index 0000000..8859973
--- /dev/null
+++ b/GoogleAnalyticsMetrics.i18n.magic.php
@@ -0,0 +1,14 @@
+<?php
+/**
+
+ * @file
+ * @author Ike Hecht
+ */
+$magicWords = array();
+
+/** English
+ * @author Ike Hecht
+ */
+$magicWords['en'] = array(
+       'googleanalyticsmetrics' => array( 0, 'googleanalyticsmetrics' ),
+);
diff --git a/GoogleAnalyticsMetrics.php b/GoogleAnalyticsMetrics.php
new file mode 100644
index 0000000..841b28d
--- /dev/null
+++ b/GoogleAnalyticsMetrics.php
@@ -0,0 +1,26 @@
+<?php
+if ( !defined( 'MEDIAWIKI' ) ) {
+       die( 'This file is a MediaWiki extension, not a valid entry point.' );
+}
+
+$wgExtensionCredits['parserhook'][] = array(
+       'path' => __FILE__,
+       'name' => 'GoogleAnalyticsMetrics',
+       'author' => 'Ike Hecht',
+       'url' => 
'https://www.mediawiki.org/wiki/Extension:GoogleAnalyticsMetrics',
+       'descriptionmsg' => 'google-analytics-metrics-desc',
+       'version' => '0.1.0 beta',
+       'license-name' => 'GPL-2.0+'
+);
+
+$wgMessageDirs['GoogleAnalyticsMetrics'] = __DIR__ . '/i18n';
+$wgExtensionMessagesFiles['GoogleAnalyticsMetricsHooksMagic'] = __DIR__ .
+       '/GoogleAnalyticsMetrics.i18n.magic.php';
+
+$wgAutoloadClasses['GoogleAnalyticsMetricsHooks'] = __DIR__ . 
'/GoogleAnalyticsMetrics.hooks.php';
+
+$wgHooks['ParserFirstCallInit'][] = 
'GoogleAnalyticsMetricsHooks::onParserFirstCallInit';
+
+$wgGoogleAnalyticsMetricsAllowed = '*';
+$wgGoogleAnalyticsMetricsEmail = null;
+$wgGoogleAnalyticsMetricsPath = null;
diff --git a/i18n/en.json b/i18n/en.json
new file mode 100644
index 0000000..7e44d12
--- /dev/null
+++ b/i18n/en.json
@@ -0,0 +1,8 @@
+{
+    "@metadata": {
+               "authors": [
+                       "Ike Hecht"
+               ]
+    },
+    "google-analytics-metrics-desc": "Gets metrics from Google Analytics"
+}
\ No newline at end of file
diff --git a/i18n/qqq.json b/i18n/qqq.json
new file mode 100644
index 0000000..b447efd
--- /dev/null
+++ b/i18n/qqq.json
@@ -0,0 +1,8 @@
+{
+       "@metadata": {
+               "authors": [
+                       "Ike Hecht"
+               ]
+       },
+       "google-analytics-metrics-desc": 
"{{desc|name=GoogleAnalyticsMetrics|url=https://www.mediawiki.org/wiki/Extension:GoogleAnalyticsMetrics}}";
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/231979
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I442412a40b41b29cf19f00d564aa942df7602540
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GoogleAnalyticsMetrics
Gerrit-Branch: master
Gerrit-Owner: tosfos <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to