Adding working proof of concept for quick geo ip redirection
authorTomasz Finc <tomasz@users.mediawiki.org>
Fri, 6 Nov 2009 14:13:31 +0000 (14:13 +0000)
committerTomasz Finc <tomasz@users.mediawiki.org>
Fri, 6 Nov 2009 14:13:31 +0000 (14:13 +0000)
extensions/GeoLite/GeoLite.i18n.php [new file with mode: 0644]
extensions/GeoLite/GeoLite.php [new file with mode: 0644]
extensions/GeoLite/GeoLite_body.php [new file with mode: 0644]

diff --git a/extensions/GeoLite/GeoLite.i18n.php b/extensions/GeoLite/GeoLite.i18n.php
new file mode 100644 (file)
index 0000000..635892a
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Internationalisation for GeoLite extension
+ *
+ * @file
+ * @ingroup Extensions
+ */
+
+$messages = array();
+
+/** English
+ * @author Tomasz Finc
+ */
+$messages['en'] = array(
+       'geolite-desc' => 'Lightweight GeoIp Redirection',
+        'geolite' => 'GeoLite',
+); 
diff --git a/extensions/GeoLite/GeoLite.php b/extensions/GeoLite/GeoLite.php
new file mode 100644 (file)
index 0000000..a36649a
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+# Alert the user that this is not a valid entry point to MediaWiki if they try to access the special pages file directly.
+if ( !defined( 'MEDIAWIKI' ) ) {
+        echo <<<EOT
+To install my extension, put the following line in LocalSettings.php:
+require_once( "\$IP/extensions/ContributionReporting/ContributionReporting.php" );
+EOT;
+        exit( 1 );
+}
+
+$wgLandingPageBase = 'http://wikimediafoundation.org/wiki/Support_Wikipedia';
+
+$wgKnownLandingPages = array( 'US' => 'en',
+                             'DE' => '',
+                             'PL' => ''
+                      ); # Which Chapters actually have landing pages
+
+$wgExtensionCredits['specialpage'][] = array(
+        'path' => __FILE__,
+        'name' => 'GeoLite',
+        'url' => 'http://www.mediawiki.org/wiki/Extension:GeoLite',
+        'author' => array( 'Tomasz Finc' ),
+        'descriptionmsg' => 'geolite-desc',
+);
+
+$dir = dirname( __FILE__ ) . '/';
+
+$wgAutoloadClasses['SpecialGeoLite'] = $dir . 'GeoLite_body.php';
+$wgExtensionMessagesFiles['GeoLite'] = $dir . 'GeoLite.i18n.php';
+$wgSpecialPages['GeoLite'] = 'SpecialGeoLite';
+$wgSpecialPageGroups['GeoLite'] = 'contribution';
+
+?>
diff --git a/extensions/GeoLite/GeoLite_body.php b/extensions/GeoLite/GeoLite_body.php
new file mode 100644 (file)
index 0000000..d28ff39
--- /dev/null
@@ -0,0 +1,46 @@
+<?php 
+/**
+ * Special Page for GeoLite
+ *
+ * @file
+ * @ingroup Extensions
+ */
+
+// Special page GeoLite
+
+class SpecialGeoLite extends SpecialPage {
+
+        /* Functions */
+
+        public function __construct() {
+                // Initialize special page
+                parent::__construct( 'GeoLite' );
+        }
+
+       public function execute( $sub ) {
+               global $wgOut, $wgRequest, $wgLandingPageBase, $wgKnownLandingPages;
+
+               $lang = ( $wgRequest->getVal( 'lang' ) ) ? $wgRequest->getVal( 'lang' ) : 'en' ;
+               $utm_source = $wgRequest->getVal( 'utm_source' );
+               $utm_medium = $wgRequest->getVal( 'utm_medium' );
+               $utm_campaign = $wgRequest->getVal( 'utm_camapaign' );
+
+               $tracking = '?' . "utm_source=$utm_source" . "&utm_medium=$utm_medium" . "&utm_campaign=$utm_campaign";
+               
+               if ( $wgRequest->getVal( 'ip') ) {
+                  $ip = $wgRequest->getVal( 'ip' ); 
+               } else { 
+                  $ip = $_SERVER[ 'REMOTE_ADDR' ];
+               }
+               
+               if ( IP::isValid( $ip ) ) {
+                  $country = geoip_country_code_by_name( $ip );
+               
+                   if ( is_string ( $country ) && array_key_exists( $country, $wgKnownLandingPages ) ) {
+                         $wgOut->redirect( $wgLandingPageBase . "/" . $wgKnownLandingPages[ $country ] . $tracking );
+                  }
+               }
+               $wgOut->redirect( $wgLandingPageBase . "/" . $lang . $tracking );
+       }
+
+}