r101334 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101333‎ | r101334 | r101335 >
Date:01:56, 31 October 2011
Author:awjrichards
Status:ok
Tags:
Comment:
Changes for LandingCheck 2.0 which adds the ability to have multiple servers running LandingCheck where one might be configured to handle only requests for priority countries and another configured to only handle requests for non-priority countries. When configured correctly, LandingCheck will forward the request to the appropriate server or otherwise handle the request locally. This will allow us to keep WMF-specific fundraising landing pages on donate.wikimedia.org but continue using foundationwiki for chapter landing pages for chapters running their own campaigns
Modified paths:
  • /trunk/extensions/LandingCheck/LandingCheck.php (modified) (history)
  • /trunk/extensions/LandingCheck/SpecialLandingCheck.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LandingCheck/LandingCheck.php
@@ -13,16 +13,12 @@
1414 $wgExtensionCredits['specialpage'][] = array(
1515 'path' => __FILE__,
1616 'name' => 'LandingCheck',
17 - 'version' => '1.2',
 17+ 'version' => '2.0',
1818 'url' => 'http://www.mediawiki.org/wiki/Extension:LandingCheck',
19 - 'author' => 'Ryan Kaldari',
 19+ 'author' => array( 'Ryan Kaldari', 'Arthur Richards' ),
2020 'descriptionmsg' => 'landingcheck-desc',
2121 );
2222
23 -// If there are any countries for which the country page should be the fallback rather than a
24 -// language page, add its country code to this array.
25 -$wgPriorityCountries = array();
26 -
2723 $dir = dirname( __FILE__ ) . '/';
2824
2925 $wgAutoloadClasses['SpecialLandingCheck'] = $dir . 'SpecialLandingCheck.php';
@@ -30,3 +26,25 @@
3127 $wgExtensionAliasesFiles['LandingCheck'] = $dir . 'LandingCheck.alias.php';
3228 $wgSpecialPages['LandingCheck'] = 'SpecialLandingCheck';
3329 $wgSpecialPageGroups['LandingCheck'] = 'contribution';
 30+
 31+// If there are any countries for which the country page should be the fallback rather than a
 32+// language page, add its country code to this array.
 33+$wgPriorityCountries = array();
 34+
 35+/**
 36+ * It is possible to configure a separate server running LandingCheck to handle
 37+ * requests for priority countries and another for non-priority countries. By
 38+ * default, an instance of LandingCheck will handle both unless the following
 39+ * variables are configured.
 40+ *
 41+ * The URLs contained in these variables should be the full URL to the location
 42+ * of LandingCheck - the query string will be appended. For example:
 43+ * $wgLandingCheckPriorityURLBase = '//wikimediafoundation.org/wiki/Special:LandingCheck';
 44+ *
 45+ * LandingCheck will compare the host portion of these URLs to what is
 46+ * configured for $wgServer. If the hosts match, the LandingCheck will just
 47+ * handle the request locally. If these are not set, LandingCheck will default to
 48+ * handling the request locally.
 49+ */
 50+$wgLandingCheckPriorityURLBase = null;
 51+$wgLandingCheckNormalURLBase = null;
\ No newline at end of file
Index: trunk/extensions/LandingCheck/SpecialLandingCheck.php
@@ -10,6 +10,7 @@
1111 * it looks for the English version. If any of those exist, it then redirects the user.
1212 */
1313 class SpecialLandingCheck extends SpecialPage {
 14+ protected $localServerType = null;
1415
1516 public function __construct() {
1617 // Register special page
@@ -17,7 +18,7 @@
1819 }
1920
2021 public function execute( $sub ) {
21 - global $wgOut, $wgRequest, $wgPriorityCountries;
 22+ global $wgRequest, $wgPriorityCountries;
2223
2324 // Pull in query string parameters
2425 $language = $wgRequest->getVal( 'language', 'en' );
@@ -35,6 +36,90 @@
3637 $country = 'US'; // Default
3738 }
3839
 40+ // determine if we are fulfilling a request for a priority country
 41+ $priority = in_array( $country, $wgPriorityCountries );
 42+
 43+ // handle the actual redirect
 44+ $this->routeRedirect( $country, $language, $priority );
 45+ }
 46+
 47+ /**
 48+ * Determine whether this server is configured as the priority or normal server
 49+ *
 50+ * If this is neither the priority nor normal server, assumes 'local' - meaning
 51+ * this server should be handling the request.
 52+ */
 53+ public function determineLocalServerType() {
 54+ global $wgServer, $wgLandingCheckPriorityURLBase, $wgLandingCheckNormalURLBase;
 55+
 56+ $localServerDetails = wfParseUrl( $wgServer );
 57+ $priorityServerDetails = wfParseUrl( $wgLandingCheckPriorityURLBase );
 58+ $normalServerDetails = wfParseUrl( $wgLandingCheckNormalURLBase );
 59+
 60+ if ( $localServerDetails[ 'host' ] == $priorityServerDetails[ 'host' ] ) {
 61+ return 'priority';
 62+ } elseif( $localServerDetails[ 'host' ] == $normalServerDetails[ 'host' ] ) {
 63+ return 'normal';
 64+ } else {
 65+ return 'local';
 66+ }
 67+ }
 68+
 69+ /**
 70+ * Route the request to the appropriate redirect method
 71+ * @param string $country
 72+ * @param string $language
 73+ * @param bool $priority Whether or not we handle this request on behalf of a priority country
 74+ */
 75+ public function routeRedirect( $country, $language, $priority ) {
 76+ $localServerType = $this->getLocalServerType();
 77+
 78+ if ( $localServerType == 'local' ) {
 79+ $this->localRedirect( $country, $language, $priority );
 80+
 81+ }elseif ( $priority && $localServerType == 'priority' ) {
 82+ $this->localRedirect( $country, $language, $priority );
 83+
 84+ }elseif ( !$priority && $localServerType == 'normal' ) {
 85+ $this->localRedirect( $country, $language, $priority );
 86+
 87+ } else {
 88+ $this->externalRedirect( $priority );
 89+
 90+ }
 91+ }
 92+
 93+ /**
 94+ * Handle an external redirect
 95+ *
 96+ * The external redirect should point to another instance of LandinCheck
 97+ * which will ultimately handle the request.
 98+ * @param bool $priority
 99+ */
 100+ public function externalRedirect( $priority ) {
 101+ global $wgRequest, $wgOut, $wgLandingCheckPriorityURLBase, $wgLandingCheckNormalURLBase;
 102+
 103+ if ( $priority ) {
 104+ $urlBase = $wgLandingCheckPriorityURLBase;
 105+
 106+ } else {
 107+ $urlBase = $wgLandingCheckNormalURLBase;
 108+
 109+ }
 110+
 111+ $query = $wgRequest->getValues();
 112+ unset( $query[ 'title' ] );
 113+
 114+ $url = wfAppendQuery( $urlBase, $query );
 115+ $wgOut->redirect( $url );
 116+ }
 117+
 118+ /**
 119+ * Handle local redirect
 120+ * @param bool $priority Whether or not we handle this request on behalf of a priority country
 121+ */
 122+ public function localRedirect( $country, $language, $priority=false ) {
 123+ global $wgOut, $wgRequest;
39124 $landingPage = $wgRequest->getVal( 'landing_page', 'Donate' );
40125
41126 // Construct new query string for tracking
@@ -47,7 +132,7 @@
48133 'referrer' => $wgRequest->getHeader( 'referer' )
49134 ) );
50135
51 - if ( in_array( $country, $wgPriorityCountries ) ) {
 136+ if ( $priority ) {
52137 // Build array of landing pages to check for
53138 $targetTexts = array(
54139 $landingPage . '/' . $country . '/' . $language,
@@ -75,6 +160,27 @@
76161 return;
77162 }
78163 }
79 -
80164 }
 165+
 166+ /**
 167+ * Setter for $this->localServerType
 168+ * @param string $type
 169+ */
 170+ public function setLocalServerType( $type = null ) {
 171+ if ( !$type ) {
 172+ $this->localServerType = $this->determineLocalServerType();
 173+ } else {
 174+ $this->localServerType = $type;
 175+ }
 176+ }
 177+
 178+ /**
 179+ * Getter for $this->localServerType
 180+ */
 181+ public function getLocalServerType() {
 182+ if ( !$this->localServerType ) {
 183+ $this->setLocalServerType();
 184+ }
 185+ return $this->localServerType;
 186+ }
81187 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r101418follow-up to r101334, fixing minor issueskaldari22:19, 31 October 2011
r101515MFT r88028, r96108, r96184, r96248, r96366, r96473r96692, r96961, r100773, r1...awjrichards19:25, 1 November 2011

Status & tagging log