Index: branches/wmf/1.16wmf4/extensions/VariablePage/VariablePage.body.php |
— | — | @@ -0,0 +1,90 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class SpecialVariablePage extends UnlistedSpecialPage { |
| 5 | + |
| 6 | + public function __construct() { |
| 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 | + } |
| 13 | + } |
| 14 | + |
| 15 | + public function execute() { |
| 16 | + global $wgOut, $wgRequest; |
| 17 | + global $wgVariablePagePossibilities, $wgVariablePageUtmMedium; |
| 18 | + |
| 19 | + $lang = ( preg_match( '/^[A-Za-z-]+$/', $wgRequest->getVal( 'lang' ) ) ) ? $wgRequest->getVal( 'lang' ) : 'en' ; |
| 20 | + $utm_source = $wgRequest->getVal( 'utm_source' ); |
| 21 | + $utm_medium = ( strlen($wgVariablePageUtmMedium )) ? $wgVariablePageUtmMedium : $wgRequest->getVal( 'utm_medium' ); |
| 22 | + $utm_campaign = $wgRequest->getVal( 'utm_campaign' ); |
| 23 | + $referrer = $wgRequest->getHeader( 'referrer' ); |
| 24 | + |
| 25 | + $query = array(); |
| 26 | + if ( strlen( $lang ) ) $query[ 'language' ] = $lang; |
| 27 | + if ( strlen( $utm_source )) $query[ 'utm_source' ] = $utm_source; |
| 28 | + if ( strlen( $utm_medium )) $query[ 'utm_medium' ] = $utm_medium; |
| 29 | + if ( strlen( $utm_campaign )) $query[ 'utm_campaign' ] = $utm_campaign; |
| 30 | + if ( strlen( $referrer )) $query[ 'referrer' ] = $referrer; |
| 31 | + |
| 32 | + $tracking = '?' . wfArrayToCGI( $query ); |
| 33 | + |
| 34 | + $url = $this->determinePage( $wgVariablePagePossibilities ); |
| 35 | + $wgOut->redirect( $url . $tracking ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Determine the URL to use based on its configured probability |
| 40 | + * |
| 41 | + * This is a basic weighted random selection algorithm borrowed from: |
| 42 | + * http://20bits.com/articles/random-weighted-elements-in-php/ |
| 43 | + * |
| 44 | + * @param array $page_possibilities |
| 45 | + * @return string $url |
| 46 | + */ |
| 47 | + public function determinePage( $page_possibilities ) { |
| 48 | + global $wgVariablePageDefault; |
| 49 | + |
| 50 | + /** |
| 51 | + * Determine a random number to measure probability again |
| 52 | + * |
| 53 | + * We use a # larger than 100 to increase 'randomness' |
| 54 | + */ |
| 55 | + $random_number = mt_rand( 0, 100*100 ); |
| 56 | + $offset = 0; |
| 57 | + |
| 58 | + foreach ( $page_possibilities as $url => $probability ) { |
| 59 | + $offset += $probability * 100; |
| 60 | + if ( $random_number <= $offset ) { |
| 61 | + return $url; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // if all else fails, return the default |
| 66 | + return $wgVariablePageDefault; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Check configuartion sanity |
| 71 | + * |
| 72 | + * Probabilities defined in $wgVariablePagePossibilities MUST add |
| 73 | + * up to 100 -or- $wgVariablePageDefault MUST be set. |
| 74 | + * |
| 75 | + * @return bool |
| 76 | + */ |
| 77 | + public function sanityCheck() { |
| 78 | + global $wgVariablePagePossibilities, $wgVariablePageDefault; |
| 79 | + |
| 80 | + $total_probability = 0; |
| 81 | + foreach ( $wgVariablePagePossibilities as $url => $probability ) { |
| 82 | + $total_probability += $probability; |
| 83 | + } |
| 84 | + |
| 85 | + if ( $total_probability != 100 && !strlen( $wgVariablePageDefault )) { |
| 86 | + return FALSE; |
| 87 | + } else { |
| 88 | + return TRUE; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
Property changes on: branches/wmf/1.16wmf4/extensions/VariablePage/VariablePage.body.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 92 | + native |
Index: branches/wmf/1.16wmf4/extensions/VariablePage/VariablePage.i18n.php |
— | — | @@ -0,0 +1,95 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Internationalization for VariablePage extension |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + */ |
| 9 | + |
| 10 | +$messages = array(); |
| 11 | + |
| 12 | +/** |
| 13 | + * English |
| 14 | + * @author Arthur Richards |
| 15 | + */ |
| 16 | +$messages['en'] = array( |
| 17 | + 'variablepage-desc' => 'Lightweight variable page redirection', |
| 18 | +); |
| 19 | + |
| 20 | +/** Bosnian (Bosanski) |
| 21 | + * @author CERminator |
| 22 | + */ |
| 23 | +$messages['bs'] = array( |
| 24 | + 'variablepage-desc' => 'Lahko varijabilno preusmjeravanje stranice', |
| 25 | +); |
| 26 | + |
| 27 | +/** German (Deutsch) |
| 28 | + * @author Kghbln |
| 29 | + */ |
| 30 | +$messages['de'] = array( |
| 31 | + 'variablepage-desc' => 'Ermöglicht ressourcenschonende variable Weiterleitungen von Wikiseiten', |
| 32 | +); |
| 33 | + |
| 34 | +/** Galician (Galego) |
| 35 | + * @author Toliño |
| 36 | + */ |
| 37 | +$messages['gl'] = array( |
| 38 | + 'variablepage-desc' => 'Redirección lixeira de páxina variable', |
| 39 | +); |
| 40 | + |
| 41 | +/** Interlingua (Interlingua) |
| 42 | + * @author McDutchie |
| 43 | + */ |
| 44 | +$messages['ia'] = array( |
| 45 | + 'variablepage-desc' => 'Redirection variabile de paginas sin consumption excessive de ressources', |
| 46 | +); |
| 47 | + |
| 48 | +/** Luxembourgish (Lëtzebuergesch) |
| 49 | + * @author Robby |
| 50 | + */ |
| 51 | +$messages['lb'] = array( |
| 52 | + 'variablepage-desc' => 'Erméiglecht eng ressourcschounend variabel Viruleedung vu Säiten', |
| 53 | +); |
| 54 | + |
| 55 | +/** Macedonian (Македонски) |
| 56 | + * @author Bjankuloski06 |
| 57 | + */ |
| 58 | +$messages['mk'] = array( |
| 59 | + 'variablepage-desc' => 'Леко променливо пренасочување на страници', |
| 60 | +); |
| 61 | + |
| 62 | +/** Dutch (Nederlands) |
| 63 | + * @author Siebrand |
| 64 | + */ |
| 65 | +$messages['nl'] = array( |
| 66 | + 'variablepage-desc' => "Lichtgewicht variabele doorverwijzing voor pagina's", |
| 67 | +); |
| 68 | + |
| 69 | +/** Piedmontese (Piemontèis) |
| 70 | + * @author Dragonòt |
| 71 | + */ |
| 72 | +$messages['pms'] = array( |
| 73 | + 'variablepage-desc' => 'Rediression ëd pàgina variàbila ëd peis linger', |
| 74 | +); |
| 75 | + |
| 76 | +/** Portuguese (Português) |
| 77 | + * @author Hamilton Abreu |
| 78 | + */ |
| 79 | +$messages['pt'] = array( |
| 80 | + 'variablepage-desc' => 'Redireccionamento ligeiro de páginas variáveis', |
| 81 | +); |
| 82 | + |
| 83 | +/** Brazilian Portuguese (Português do Brasil) |
| 84 | + * @author Giro720 |
| 85 | + */ |
| 86 | +$messages['pt-br'] = array( |
| 87 | + 'variablepage-desc' => 'Redirecionamento ligeiro de páginas variáveis', |
| 88 | +); |
| 89 | + |
| 90 | +/** Russian (Русский) |
| 91 | + * @author Александр Сигачёв |
| 92 | + */ |
| 93 | +$messages['ru'] = array( |
| 94 | + 'variablepage-desc' => 'Простое перенаправление на различные страницы', |
| 95 | +); |
| 96 | + |
Property changes on: branches/wmf/1.16wmf4/extensions/VariablePage/VariablePage.i18n.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 97 | + native |
Index: branches/wmf/1.16wmf4/extensions/VariablePage/VariablePage.php |
— | — | @@ -0,0 +1,62 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Lightweight variable page redirection |
| 5 | + */ |
| 6 | + |
| 7 | +//Alert the user that this is not a valid entry point to MediaWiki if they try to access the setup file directly. |
| 8 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 9 | + echo <<<EOT |
| 10 | +To install this extension, put the following line in LocalSettings.php: |
| 11 | +require_once( "\$IP/extensions/VariablePage/VariablePage.php" ); |
| 12 | +EOT; |
| 13 | + exit( 1 ); |
| 14 | +} |
| 15 | + |
| 16 | +$wgExtensionCredits[ 'VariablePage' ][] = array( |
| 17 | + 'path' => __FILE__, |
| 18 | + 'name' => 'VariablePage', |
| 19 | + 'version' => '0.1', |
| 20 | + 'author' => 'Arthur Richards', |
| 21 | + 'descriptionmsg' => 'variablepage-desc', |
| 22 | +); |
| 23 | + |
| 24 | +/** |
| 25 | + * An array of pages and the probability of a user being redirected to each page. |
| 26 | + * |
| 27 | + * The key in the array is the full URL path, the value is an integer representing |
| 28 | + * a percentage (0-100) probability of a user being redirected to that page. |
| 29 | + * |
| 30 | + * The percentages here MUST add up to 100 -or- a value must be set for |
| 31 | + * $wgVariablePageDefault |
| 32 | + * |
| 33 | + * The following will redirect a user to http://foo.com/bar 90% of the time: |
| 34 | + * $wgVariablePagePossibilities = array( |
| 35 | + * 'http://foo.com/bar' => 90, |
| 36 | + * ); |
| 37 | + */ |
| 38 | +$wgVariablePagePossibilities = array( |
| 39 | + 'http://wikimediafoundation.org/wiki/Support_Wikipedia' => 100 |
| 40 | +); |
| 41 | + |
| 42 | +/** |
| 43 | + * You may set a custom utm_medium to be used for pages reached via VariablePage |
| 44 | + * |
| 45 | + * This can be set to whatever string you wish to use for utm_medium |
| 46 | + */ |
| 47 | +$wgVariablePageUtmMedium = ''; |
| 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 = ''; |
| 57 | + |
| 58 | +$dir = dirname( __FILE__ ) . '/'; |
| 59 | + |
| 60 | +$wgAutoloadClasses[ 'SpecialVariablePage' ] = $dir . 'VariablePage.body.php'; |
| 61 | +$wgExtensionMessagesFiles[ 'VariablePage' ] = $dir . 'VariablePage.i18n.php'; |
| 62 | +$wgSpecialPages[ 'VariablePage' ] = 'SpecialVariablePage'; |
| 63 | +$wgSpecialPageGroups[ 'VariablePage' ] = 'contribution'; |
Property changes on: branches/wmf/1.16wmf4/extensions/VariablePage/VariablePage.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 64 | + native |
Property changes on: branches/wmf/1.16wmf4/extensions/VariablePage |
___________________________________________________________________ |
Added: svn:mergeinfo |
2 | 65 | Merged /trunk/extensions/VariablePage:r72938-73122 |
3 | 66 | Merged /trunk/extensions/VariablePage/VariablePage:r72939-72940 |
4 | 67 | Merged /trunk/phase3/extensions/VariablePage:r63545-63546,63549,63643,63764,63897-63901,64113,64509,65387,65391,65555,65590,65650,65816 |
5 | 68 | Merged /branches/wmf-deployment/extensions/VariablePage:r60970 |