http://www.mediawiki.org/wiki/Special:Code/MediaWiki/72938
Revision: 72938
Author: awjrichards
Date: 2010-09-13 22:49:17 +0000 (Mon, 13 Sep 2010)
Log Message:
-----------
Added an extension to display a URL from an array of URLs using a weighted
random selection algorightm
Added Paths:
-----------
trunk/extensions/VariablePage/
trunk/extensions/VariablePage/VariablePage.body.php
trunk/extensions/VariablePage/VariablePage.i18n.php
trunk/extensions/VariablePage/VariablePage.php
Added: trunk/extensions/VariablePage/VariablePage.body.php
===================================================================
--- trunk/extensions/VariablePage/VariablePage.body.php
(rev 0)
+++ trunk/extensions/VariablePage/VariablePage.body.php 2010-09-13 22:49:17 UTC
(rev 72938)
@@ -0,0 +1,55 @@
+<?php
+
+class SpecialVariablePage extends UnlistedSpecialPage {
+
+ public function __construct() {
+ parent::__construct( 'VariablePage' );
+ }
+
+ public function execute() {
+ global $wgOut, $wgRequest;
+ global $wgVariablePagePossibilities;
+
+ $lang = ( preg_match( '/^[A-Za-z-]+$/', $wgRequest->getVal(
'lang' ) ) ) ? $wgRequest->getVal( 'lang' ) : 'en' ;
+ $utm_source = $wgRequest->getVal( 'utm_source' );
+ $utm_medium = $wgRequest->getVal( 'utm_medium' );
+ $utm_campaign = $wgRequest->getVal( 'utm_campaign' );
+ $referrer = $wgRequest->getHeader( 'referrer' );
+
+ $tracking = '?' . wfArrayToCGI( array(
+ 'utm_source' => "$utm_source",
+ 'utm_medium' => "$utm_medium",
+ 'utm_campaign' => "$utm_campaign",
+ 'referrer' => "$referrer"
+ ));
+
+ $url = $this->determinePage( $wgVariablePagePossibilities );
+ $wgOut->redirect( $url . '/' . $lang . $tracking );
+ }
+
+ /**
+ * Determine the URL to use based on its configured probability
+ *
+ * This is a basic weighted random selection algorithm borrowed from:
+ * http://20bits.com/articles/random-weighted-elements-in-php/
+ *
+ * @param array $page_possibilities
+ * @return string $url
+ */
+ public function determinePage( $page_possibilities ) {
+ /**
+ * Determine a random number to measure probability again
+ *
+ * We use a # larger than 100 to increase 'randomness'
+ */
+ $random_number = mt_rand( 0, 10000 );
+ $offset = 0;
+
+ foreach ( $page_possibilities as $url => $probability ) {
+ $offset += $probability * 100;
+ if ( $random_number <= $offset ) {
+ return $url;
+ }
+ }
+ }
+}
Added: trunk/extensions/VariablePage/VariablePage.i18n.php
===================================================================
--- trunk/extensions/VariablePage/VariablePage.i18n.php
(rev 0)
+++ trunk/extensions/VariablePage/VariablePage.i18n.php 2010-09-13 22:49:17 UTC
(rev 72938)
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Internationalization for VariablePlage extension
+ *
+ * @file
+ * @ingroup Extensions
+ */
+
+$messages = array();
+
+/**
+ * English
+ * @author Arthur Richards
+ */
+$messages[ 'en' ] = array(
+ 'variablepage-desc' => 'Lightweight variable page redirection',
+);
Added: trunk/extensions/VariablePage/VariablePage.php
===================================================================
--- trunk/extensions/VariablePage/VariablePage.php
(rev 0)
+++ trunk/extensions/VariablePage/VariablePage.php 2010-09-13 22:49:17 UTC
(rev 72938)
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Lightweight variable page redirection
+ */
+
+//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/VariablePage/VariablePage.php" );
+EOT;
+ exit( 1 );
+}
+
+$wgExtensionCredits[ 'VariablePage' ][] = array(
+ 'path' => __FILE__,
+ 'name' => 'VariablePage',
+ 'version' => '0.1',
+ 'author' => 'Arthur Richards',
+ 'descriptionmsg' => 'variablepage-desc',
+);
+
+/**
+ * An array of pages and the probability of a user being redirected to it.
+ *
+ * The key in the array is the full URL path, the value is an integer
representing
+ * a percentage (0-100) probability of a user being redirected to that page.
+ *
+ * The following will redirect a user to http://foo.com/bar 90% of the time:
+ * $wgVariablePagePossibilities = array(
+ * 'http://foo.com/bar' => 90,
+ * );
+ */
+$wgVariablePagePossibilities = array(
+ 'http://wikimediafoundation.org/wiki/Support_Wikipedia' => 100
+);
+
+$dir = dirname( __FILE__ ) . '/';
+
+$wgAutoloadClasses[ 'SpecialVariablePage' ] = $dir . 'VariablePage.body.php';
+$wgExtensionMessagesFiles[ 'VariablePage' ] = $dir . 'VariablePage.i18n.php';
+$wgSpecialPages[ 'VariablePage' ] = 'SpecialVariablePage';
+$wgSpecialPageGroups[ 'VariablePage' ] = 'contribution';
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs