Index: trunk/extensions/VariablePage/VariablePage.body.php |
— | — | @@ -4,12 +4,17 @@ |
5 | 5 | |
6 | 6 | public function __construct() { |
7 | 7 | parent::__construct( 'VariablePage' ); |
| 8 | + |
| 9 | + // make sure configuration will result in something sane |
| 10 | + if ( !$this->sanityCheck() ) { |
| 11 | + throw new MWException( 'VariablePage configuation not sane! Either $wgVariablePageDefault must be set or probabilites in $wgVariablePagePossibilities must add up to 100.' ); |
| 12 | + } |
8 | 13 | } |
9 | 14 | |
10 | 15 | public function execute() { |
11 | 16 | global $wgOut, $wgRequest; |
12 | 17 | global $wgVariablePagePossibilities, $wgVariablePageUtmMedium; |
13 | | - |
| 18 | + |
14 | 19 | $lang = ( preg_match( '/^[A-Za-z-]+$/', $wgRequest->getVal( 'lang' ) ) ) ? $wgRequest->getVal( 'lang' ) : 'en' ; |
15 | 20 | $utm_source = $wgRequest->getVal( 'utm_source' ); |
16 | 21 | $utm_medium = ( strlen($wgVariablePageUtmMedium )) ? $wgVariablePageUtmMedium : $wgRequest->getVal( 'utm_medium' ); |
— | — | @@ -37,6 +42,8 @@ |
38 | 43 | * @return string $url |
39 | 44 | */ |
40 | 45 | public function determinePage( $page_possibilities ) { |
| 46 | + global $wgVariablePageDefault; |
| 47 | + |
41 | 48 | /** |
42 | 49 | * Determine a random number to measure probability again |
43 | 50 | * |
— | — | @@ -51,5 +58,31 @@ |
52 | 59 | return $url; |
53 | 60 | } |
54 | 61 | } |
| 62 | + |
| 63 | + // if all else fails, return the default |
| 64 | + return $wgVariablePageDefault; |
55 | 65 | } |
| 66 | + |
| 67 | + /** |
| 68 | + * Check configuartion sanity |
| 69 | + * |
| 70 | + * Probabilities defined in $wgVariablePagePossibilities MUST add |
| 71 | + * up to 100 -or- $wgVariablePageDefault MUST be set. |
| 72 | + * |
| 73 | + * @return bool |
| 74 | + */ |
| 75 | + public function sanityCheck() { |
| 76 | + global $wgVariablePagePossibilities, $wgVariablePageDefault; |
| 77 | + |
| 78 | + $total_probability = 0; |
| 79 | + foreach ( $wgVariablePagePossibilities as $url => $probability ) { |
| 80 | + $total_probability += $probability; |
| 81 | + } |
| 82 | + |
| 83 | + if ( $total_probability != 100 && !strlen( $wgVariablePageDefault )) { |
| 84 | + return FALSE; |
| 85 | + } else { |
| 86 | + return TRUE; |
| 87 | + } |
| 88 | + } |
56 | 89 | } |
Index: trunk/extensions/VariablePage/VariablePage.php |
— | — | @@ -26,6 +26,9 @@ |
27 | 27 | * The key in the array is the full URL path, the value is an integer representing |
28 | 28 | * a percentage (0-100) probability of a user being redirected to that page. |
29 | 29 | * |
| 30 | + * The percentages here MUST add up to 100 -or- a value must be set for |
| 31 | + * $wgVariablePageDefault |
| 32 | + * |
30 | 33 | * The following will redirect a user to http://foo.com/bar 90% of the time: |
31 | 34 | * $wgVariablePagePossibilities = array( |
32 | 35 | * 'http://foo.com/bar' => 90, |
— | — | @@ -42,6 +45,14 @@ |
43 | 46 | */ |
44 | 47 | $wgVariablePageUtmMedium = ''; |
45 | 48 | |
| 49 | +/** |
| 50 | + * The default URL to send a user to in the event that one of the URLs in |
| 51 | + * $wgVariablePagePossibilities not selected. |
| 52 | + * |
| 53 | + * Either this must be set or the probabilities in $wgVariablePagePossibiliites |
| 54 | + * must add up to 100. |
| 55 | + */ |
| 56 | +$wgVariablePageDefault = ''; |
46 | 57 | |
47 | 58 | $dir = dirname( __FILE__ ) . '/'; |
48 | 59 | |