Index: trunk/extensions/LandingCheck/LandingCheck.php |
— | — | @@ -13,16 +13,12 @@ |
14 | 14 | $wgExtensionCredits['specialpage'][] = array( |
15 | 15 | 'path' => __FILE__, |
16 | 16 | 'name' => 'LandingCheck', |
17 | | - 'version' => '1.2', |
| 17 | + 'version' => '2.0', |
18 | 18 | 'url' => 'http://www.mediawiki.org/wiki/Extension:LandingCheck', |
19 | | - 'author' => 'Ryan Kaldari', |
| 19 | + 'author' => array( 'Ryan Kaldari', 'Arthur Richards' ), |
20 | 20 | 'descriptionmsg' => 'landingcheck-desc', |
21 | 21 | ); |
22 | 22 | |
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 | | - |
27 | 23 | $dir = dirname( __FILE__ ) . '/'; |
28 | 24 | |
29 | 25 | $wgAutoloadClasses['SpecialLandingCheck'] = $dir . 'SpecialLandingCheck.php'; |
— | — | @@ -30,3 +26,25 @@ |
31 | 27 | $wgExtensionAliasesFiles['LandingCheck'] = $dir . 'LandingCheck.alias.php'; |
32 | 28 | $wgSpecialPages['LandingCheck'] = 'SpecialLandingCheck'; |
33 | 29 | $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 @@ |
11 | 11 | * it looks for the English version. If any of those exist, it then redirects the user. |
12 | 12 | */ |
13 | 13 | class SpecialLandingCheck extends SpecialPage { |
| 14 | + protected $localServerType = null; |
14 | 15 | |
15 | 16 | public function __construct() { |
16 | 17 | // Register special page |
— | — | @@ -17,7 +18,7 @@ |
18 | 19 | } |
19 | 20 | |
20 | 21 | public function execute( $sub ) { |
21 | | - global $wgOut, $wgRequest, $wgPriorityCountries; |
| 22 | + global $wgRequest, $wgPriorityCountries; |
22 | 23 | |
23 | 24 | // Pull in query string parameters |
24 | 25 | $language = $wgRequest->getVal( 'language', 'en' ); |
— | — | @@ -35,6 +36,90 @@ |
36 | 37 | $country = 'US'; // Default |
37 | 38 | } |
38 | 39 | |
| 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; |
39 | 124 | $landingPage = $wgRequest->getVal( 'landing_page', 'Donate' ); |
40 | 125 | |
41 | 126 | // Construct new query string for tracking |
— | — | @@ -47,7 +132,7 @@ |
48 | 133 | 'referrer' => $wgRequest->getHeader( 'referer' ) |
49 | 134 | ) ); |
50 | 135 | |
51 | | - if ( in_array( $country, $wgPriorityCountries ) ) { |
| 136 | + if ( $priority ) { |
52 | 137 | // Build array of landing pages to check for |
53 | 138 | $targetTexts = array( |
54 | 139 | $landingPage . '/' . $country . '/' . $language, |
— | — | @@ -75,6 +160,27 @@ |
76 | 161 | return; |
77 | 162 | } |
78 | 163 | } |
79 | | - |
80 | 164 | } |
| 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 | + } |
81 | 187 | } |