Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.php |
— | — | @@ -39,6 +39,8 @@ |
40 | 40 | $wgAutoloadClasses[$className] = $cwd . $classFilename . '.php'; |
41 | 41 | } |
42 | 42 | |
| 43 | +$wgEnableZeroRatedMobileAccessTesting = false; |
| 44 | + |
43 | 45 | $wgExtZeroRatedMobileAccess = new ExtZeroRatedMobileAccess(); |
44 | 46 | |
45 | 47 | $wgHooks['BeforePageDisplay'][] = array( &$wgExtZeroRatedMobileAccess, 'beforePageDisplayHTML' ); |
\ No newline at end of file |
Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | class ExtZeroRatedMobileAccess { |
5 | | - const VERSION = '0.0.6'; |
| 5 | + const VERSION = '0.0.7'; |
6 | 6 | |
7 | 7 | public static $renderZeroRatedLandingPage; |
8 | 8 | public static $renderZeroRatedBanner; |
— | — | @@ -24,189 +24,192 @@ |
25 | 25 | * @return bool |
26 | 26 | */ |
27 | 27 | public function beforePageDisplayHTML( &$out, &$text ) { |
28 | | - global $wgRequest; |
| 28 | + global $wgRequest, $wgConf, $wgDBname, $wgEnableZeroRatedMobileAccessTesting; |
29 | 29 | wfProfileIn( __METHOD__ ); |
30 | | - |
31 | | - $xDevice = isset( $_SERVER['HTTP_X_DEVICE'] ) ? $_SERVER['HTTP_X_DEVICE'] : ''; |
32 | | - self::$useFormat = $wgRequest->getText( 'useformat' ); |
33 | | - |
34 | | - if ( self::$useFormat !== 'mobile' && self::$useFormat !== 'mobile-wap' && |
35 | | - !$xDevice ) { |
36 | | - wfProfileOut( __METHOD__ ); |
37 | | - return true; |
38 | | - } |
39 | 30 | |
40 | | - $output = Html::openElement( 'div', |
41 | | - array( 'id' => 'zero-landing-page' ) ); |
| 31 | + list( $site, $lang ) = $wgConf->siteFromDB( $wgDBname ); |
| 32 | + if ( $site == 'wikipedia' || $wgEnableZeroRatedMobileAccessTesting ) { |
42 | 33 | |
43 | | - self::$renderZeroRatedLandingPage = $wgRequest->getFuzzyBool( 'renderZeroRatedLandingPage' ); |
44 | | - self::$renderZeroRatedBanner = $wgRequest->getFuzzyBool( 'renderZeroRatedBanner' ); |
45 | | - self::$renderZeroRatedRedirect = $wgRequest->getFuzzyBool( 'renderZeroRatedRedirect' ); |
46 | | - self::$forceClickToViewImages = $wgRequest->getFuzzyBool( 'forceClickToViewImages' ); |
47 | | - self::$acceptBilling = $wgRequest->getVal( 'acceptbilling' ); |
48 | | - self::$title = $out->getTitle(); |
| 34 | + $xDevice = isset( $_SERVER['HTTP_X_DEVICE'] ) ? $_SERVER['HTTP_X_DEVICE'] : ''; |
| 35 | + self::$useFormat = $wgRequest->getText( 'useformat' ); |
| 36 | + |
| 37 | + if ( self::$useFormat !== 'mobile' && self::$useFormat !== 'mobile-wap' && |
| 38 | + !$xDevice ) { |
| 39 | + wfProfileOut( __METHOD__ ); |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + $output = Html::openElement( 'div', |
| 44 | + array( 'id' => 'zero-landing-page' ) ); |
| 45 | + |
| 46 | + self::$renderZeroRatedLandingPage = $wgRequest->getFuzzyBool( 'renderZeroRatedLandingPage' ); |
| 47 | + self::$renderZeroRatedBanner = $wgRequest->getFuzzyBool( 'renderZeroRatedBanner' ); |
| 48 | + self::$renderZeroRatedRedirect = $wgRequest->getFuzzyBool( 'renderZeroRatedRedirect' ); |
| 49 | + self::$forceClickToViewImages = $wgRequest->getFuzzyBool( 'forceClickToViewImages' ); |
| 50 | + self::$acceptBilling = $wgRequest->getVal( 'acceptbilling' ); |
| 51 | + self::$title = $out->getTitle(); |
49 | 52 | |
50 | | - $carrier = $wgRequest->getHeader( 'HTTP_X_CARRIER' ); |
51 | | - if ( $carrier !== '(null)' && $carrier ) { |
52 | | - self::$renderZeroRatedBanner = true; |
53 | | - } |
| 53 | + $carrier = $wgRequest->getHeader( 'HTTP_X_CARRIER' ); |
| 54 | + if ( $carrier !== '(null)' && $carrier ) { |
| 55 | + self::$renderZeroRatedBanner = true; |
| 56 | + } |
54 | 57 | |
55 | | - if ( self::$title->getNamespace() == NS_FILE ) { |
56 | | - self::$isFilePage = true; |
57 | | - } |
| 58 | + if ( self::$title->getNamespace() == NS_FILE ) { |
| 59 | + self::$isFilePage = true; |
| 60 | + } |
58 | 61 | |
59 | | - if ( self::$acceptBilling === 'no' ) { |
60 | | - $targetUrl = $wgRequest->getVal( 'returnto' ); |
61 | | - $out->redirect( $targetUrl, '301' ); |
62 | | - $out->output(); |
63 | | - } |
64 | | - |
65 | | - if ( self::$acceptBilling === 'yes' ) { |
66 | | - $targetUrl = $wgRequest->getVal( 'returnto' ); |
67 | | - if ( $targetUrl ) { |
| 62 | + if ( self::$acceptBilling === 'no' ) { |
| 63 | + $targetUrl = $wgRequest->getVal( 'returnto' ); |
68 | 64 | $out->redirect( $targetUrl, '301' ); |
69 | 65 | $out->output(); |
70 | 66 | } |
71 | | - } |
72 | 67 | |
73 | | - if ( self::$isFilePage && self::$acceptBilling !== 'yes' ) { |
74 | | - $acceptBillingYes = Html::rawElement( 'a', |
75 | | - array('href' => $wgRequest->appendQuery( 'renderZeroRatedBanner=true&acceptbilling=yes' ) ), |
76 | | - wfMsg( 'zero-rated-mobile-access-banner-text-data-charges-yes' ) ); |
77 | | - $referrer = $wgRequest->getHeader( 'referer' ); |
78 | | - $acceptBillingNo = Html::rawElement( 'a', |
79 | | - array('href' => $wgRequest->appendQuery( 'acceptbilling=no&returnto=' . urlencode( $referrer ) ) ), |
80 | | - wfMsg( 'zero-rated-mobile-access-banner-text-data-charges-no' ) ); |
81 | | - $bannerText = Html::rawElement( 'h3', |
82 | | - array( 'id' => 'zero-rated-banner-text' ), |
83 | | - wfMsg( 'zero-rated-mobile-access-banner-text-data-charges', $acceptBillingYes, $acceptBillingNo ) ); |
84 | | - $banner = Html::rawElement( 'div', |
85 | | - array( 'style' => 'display:none;', |
86 | | - 'id' => 'zero-rated-banner-red' ), |
87 | | - $bannerText |
88 | | - ); |
89 | | - $output .= $banner; |
90 | | - $out->clearHTML(); |
91 | | - $out->setPageTitle( null ); |
92 | | - } elseif ( self::$renderZeroRatedRedirect === true ) { |
93 | | - $returnto = $wgRequest->getVal( 'returnto' ); |
94 | | - $acceptBillingYes = Html::rawElement( 'a', |
95 | | - array('href' => $wgRequest->appendQuery( 'renderZeroRatedBanner=true&acceptbilling=yes&returnto=' . urlencode( $returnto ) ) ), |
96 | | - wfMsg( 'zero-rated-mobile-access-banner-text-data-charges-yes' ) ); |
97 | | - $referrer = $wgRequest->getHeader( 'referer' ); |
98 | | - $acceptBillingNo = Html::rawElement( 'a', |
99 | | - array('href' => $wgRequest->appendQuery( 'acceptbilling=no&returnto=' . urlencode( $referrer ) ) ), |
100 | | - wfMsg( 'zero-rated-mobile-access-banner-text-data-charges-no' ) ); |
101 | | - $bannerText = Html::rawElement( 'h3', |
102 | | - array( 'id' => 'zero-rated-banner-text' ), |
103 | | - wfMsg( 'zero-rated-mobile-access-banner-text-data-charges', $acceptBillingYes, $acceptBillingNo ) ); |
104 | | - $banner = Html::rawElement( 'div', |
105 | | - array( 'style' => 'display:none;', |
106 | | - 'id' => 'zero-rated-banner-red' ), |
107 | | - $bannerText |
108 | | - ); |
109 | | - $output .= $banner; |
110 | | - $out->clearHTML(); |
111 | | - $out->setPageTitle( null ); |
112 | | - } elseif ( self::$renderZeroRatedBanner === true ) { |
113 | | - // a2enmod headers >>> .htaccess >>> RequestHeader set HTTP_CARRIER Verizon |
114 | | - self::$carrier = $this->lookupCarrier( $carrier ); |
115 | | - $html = $out->getHTML(); |
116 | | - $parsedHtml = $this->parseLinksForZeroQueryString( $html ); |
117 | | - $out->clearHTML(); |
118 | | - $out->addHTML( $parsedHtml ); |
119 | | - $carrierLink = ( isset( self::$carrier['link'] ) ) ? self::$carrier['link'] : ''; |
120 | | - $bannerText = Html::rawElement( 'h3', |
121 | | - array( 'id' => 'zero-rated-banner-text' ), |
122 | | - wfMsg( 'zero-rated-mobile-access-banner-text', $carrierLink ) ); |
123 | | - $banner = Html::rawElement( 'div', |
124 | | - array( 'style' => 'display:none;', |
125 | | - 'id' => 'zero-rated-banner' ), |
126 | | - $bannerText |
127 | | - ); |
128 | | - $output .= $banner; |
129 | | - } |
130 | | - if ( self::$renderZeroRatedLandingPage === true ) { |
131 | | - $out->clearHTML(); |
132 | | - $out->setPageTitle( null ); |
133 | | - $output .= wfMsg( 'zero-rated-mobile-access-desc' ); |
134 | | - $languageNames = Language::getLanguageNames(); |
135 | | - $country = $wgRequest->getVal( 'country' ); |
136 | | - $ip = $wgRequest->getVal( 'ip', wfGetIP() ); |
137 | | - // Temporary hack to allow for testing on localhost |
138 | | - $countryIps = array( |
139 | | - 'GERMANY' => '80.237.226.75', |
140 | | - 'MEXICO' => '187.184.240.247', |
141 | | - 'THAILAND' => '180.180.150.104', |
142 | | - 'FRANCE' => '90.6.70.28', |
143 | | - ); |
144 | | - $ip = ( strpos( $ip, '192.168.' ) === 0 ) ? $countryIps['THAILAND'] : $ip; |
145 | | - if ( IP::isValid( $ip ) ) { |
146 | | - // If no country was passed, try to do GeoIP lookup |
147 | | - // Requires php5-geoip package |
148 | | - if ( !$country && function_exists( 'geoip_country_code_by_name' ) ) { |
149 | | - $country = geoip_country_code_by_name( $ip ); |
| 68 | + if ( self::$acceptBilling === 'yes' ) { |
| 69 | + $targetUrl = $wgRequest->getVal( 'returnto' ); |
| 70 | + if ( $targetUrl ) { |
| 71 | + $out->redirect( $targetUrl, '301' ); |
| 72 | + $out->output(); |
150 | 73 | } |
151 | | - self::addDebugOutput( $country ); |
152 | 74 | } |
153 | | - $languageOptions = self::createLanguageOptionsFromWikiText(); |
154 | | - //self::$displayDebugOutput = true; |
155 | | - $languagesForCountry = ( isset( $languageOptions[self::getFullCountryNameFromCode( $country )] ) ) ? |
156 | | - $languageOptions[self::getFullCountryNameFromCode( $country )] : null; |
157 | | - //self::addDebugOutput( $languageOptions ); |
158 | | - self::addDebugOutput( self::getFullCountryNameFromCode( $country ) ); |
159 | | - self::addDebugOutput( $languagesForCountry ); |
160 | | - self::writeDebugOutput(); |
161 | 75 | |
162 | | - if ( is_array( $languagesForCountry ) ) { |
163 | | - $sizeOfLanguagesForCountry = sizeof( $languagesForCountry ); |
164 | | - for ( $i = 0; $i < $sizeOfLanguagesForCountry; $i++ ) { |
165 | | - $languageName = $languageNames[$languagesForCountry[$i]['language']]; |
166 | | - $languageCode = $languagesForCountry[$i]['language']; |
167 | | - $output .= Html::element( 'hr' ); |
168 | | - $output .= Html::element( 'h3', |
169 | | - array( 'id' => 'lang_' . $languageCode ), |
170 | | - $languageName |
171 | | - ); |
172 | | - if ( $i == 0 ) { |
173 | | - $output .= self::getSearchFormHtml( $languageCode ); |
174 | | - } else { |
175 | | - $languageUrl = sprintf( self::$formatMobileUrl, $languageCode ); |
176 | | - $output .= Html::element( 'a', |
177 | | - array( 'id' => 'lang_' . $languageCode, |
178 | | - 'href' => $languageUrl ), |
179 | | - wfMessage( 'zero-rated-mobile-access-home-page-selection', |
180 | | - $languageName )->inLanguage( $languageCode ) |
| 76 | + if ( self::$isFilePage && self::$acceptBilling !== 'yes' ) { |
| 77 | + $acceptBillingYes = Html::rawElement( 'a', |
| 78 | + array('href' => $wgRequest->appendQuery( 'renderZeroRatedBanner=true&acceptbilling=yes' ) ), |
| 79 | + wfMsg( 'zero-rated-mobile-access-banner-text-data-charges-yes' ) ); |
| 80 | + $referrer = $wgRequest->getHeader( 'referer' ); |
| 81 | + $acceptBillingNo = Html::rawElement( 'a', |
| 82 | + array('href' => $wgRequest->appendQuery( 'acceptbilling=no&returnto=' . urlencode( $referrer ) ) ), |
| 83 | + wfMsg( 'zero-rated-mobile-access-banner-text-data-charges-no' ) ); |
| 84 | + $bannerText = Html::rawElement( 'h3', |
| 85 | + array( 'id' => 'zero-rated-banner-text' ), |
| 86 | + wfMsg( 'zero-rated-mobile-access-banner-text-data-charges', $acceptBillingYes, $acceptBillingNo ) ); |
| 87 | + $banner = Html::rawElement( 'div', |
| 88 | + array( 'style' => 'display:none;', |
| 89 | + 'id' => 'zero-rated-banner-red' ), |
| 90 | + $bannerText |
| 91 | + ); |
| 92 | + $output .= $banner; |
| 93 | + $out->clearHTML(); |
| 94 | + $out->setPageTitle( null ); |
| 95 | + } elseif ( self::$renderZeroRatedRedirect === true ) { |
| 96 | + $returnto = $wgRequest->getVal( 'returnto' ); |
| 97 | + $acceptBillingYes = Html::rawElement( 'a', |
| 98 | + array('href' => $wgRequest->appendQuery( 'renderZeroRatedBanner=true&acceptbilling=yes&returnto=' . urlencode( $returnto ) ) ), |
| 99 | + wfMsg( 'zero-rated-mobile-access-banner-text-data-charges-yes' ) ); |
| 100 | + $referrer = $wgRequest->getHeader( 'referer' ); |
| 101 | + $acceptBillingNo = Html::rawElement( 'a', |
| 102 | + array('href' => $wgRequest->appendQuery( 'acceptbilling=no&returnto=' . urlencode( $referrer ) ) ), |
| 103 | + wfMsg( 'zero-rated-mobile-access-banner-text-data-charges-no' ) ); |
| 104 | + $bannerText = Html::rawElement( 'h3', |
| 105 | + array( 'id' => 'zero-rated-banner-text' ), |
| 106 | + wfMsg( 'zero-rated-mobile-access-banner-text-data-charges', $acceptBillingYes, $acceptBillingNo ) ); |
| 107 | + $banner = Html::rawElement( 'div', |
| 108 | + array( 'style' => 'display:none;', |
| 109 | + 'id' => 'zero-rated-banner-red' ), |
| 110 | + $bannerText |
| 111 | + ); |
| 112 | + $output .= $banner; |
| 113 | + $out->clearHTML(); |
| 114 | + $out->setPageTitle( null ); |
| 115 | + } elseif ( self::$renderZeroRatedBanner === true ) { |
| 116 | + // a2enmod headers >>> .htaccess >>> RequestHeader set HTTP_CARRIER Verizon |
| 117 | + self::$carrier = $this->lookupCarrier( $carrier ); |
| 118 | + $html = $out->getHTML(); |
| 119 | + $parsedHtml = $this->parseLinksForZeroQueryString( $html ); |
| 120 | + $out->clearHTML(); |
| 121 | + $out->addHTML( $parsedHtml ); |
| 122 | + $carrierLink = ( isset( self::$carrier['link'] ) ) ? self::$carrier['link'] : ''; |
| 123 | + $bannerText = Html::rawElement( 'h3', |
| 124 | + array( 'id' => 'zero-rated-banner-text' ), |
| 125 | + wfMsg( 'zero-rated-mobile-access-banner-text', $carrierLink ) ); |
| 126 | + $banner = Html::rawElement( 'div', |
| 127 | + array( 'style' => 'display:none;', |
| 128 | + 'id' => 'zero-rated-banner' ), |
| 129 | + $bannerText |
| 130 | + ); |
| 131 | + $output .= $banner; |
| 132 | + } |
| 133 | + if ( self::$renderZeroRatedLandingPage === true ) { |
| 134 | + $out->clearHTML(); |
| 135 | + $out->setPageTitle( null ); |
| 136 | + $output .= wfMsg( 'zero-rated-mobile-access-desc' ); |
| 137 | + $languageNames = Language::getLanguageNames(); |
| 138 | + $country = $wgRequest->getVal( 'country' ); |
| 139 | + $ip = $wgRequest->getVal( 'ip', wfGetIP() ); |
| 140 | + // Temporary hack to allow for testing on localhost |
| 141 | + $countryIps = array( |
| 142 | + 'GERMANY' => '80.237.226.75', |
| 143 | + 'MEXICO' => '187.184.240.247', |
| 144 | + 'THAILAND' => '180.180.150.104', |
| 145 | + 'FRANCE' => '90.6.70.28', |
| 146 | + ); |
| 147 | + $ip = ( strpos( $ip, '192.168.' ) === 0 ) ? $countryIps['THAILAND'] : $ip; |
| 148 | + if ( IP::isValid( $ip ) ) { |
| 149 | + // If no country was passed, try to do GeoIP lookup |
| 150 | + // Requires php5-geoip package |
| 151 | + if ( !$country && function_exists( 'geoip_country_code_by_name' ) ) { |
| 152 | + $country = geoip_country_code_by_name( $ip ); |
| 153 | + } |
| 154 | + self::addDebugOutput( $country ); |
| 155 | + } |
| 156 | + $languageOptions = self::createLanguageOptionsFromWikiText(); |
| 157 | + //self::$displayDebugOutput = true; |
| 158 | + $languagesForCountry = ( isset( $languageOptions[self::getFullCountryNameFromCode( $country )] ) ) ? |
| 159 | + $languageOptions[self::getFullCountryNameFromCode( $country )] : null; |
| 160 | + //self::addDebugOutput( $languageOptions ); |
| 161 | + self::addDebugOutput( self::getFullCountryNameFromCode( $country ) ); |
| 162 | + self::addDebugOutput( $languagesForCountry ); |
| 163 | + self::writeDebugOutput(); |
| 164 | + |
| 165 | + if ( is_array( $languagesForCountry ) ) { |
| 166 | + $sizeOfLanguagesForCountry = sizeof( $languagesForCountry ); |
| 167 | + for ( $i = 0; $i < $sizeOfLanguagesForCountry; $i++ ) { |
| 168 | + $languageName = $languageNames[$languagesForCountry[$i]['language']]; |
| 169 | + $languageCode = $languagesForCountry[$i]['language']; |
| 170 | + $output .= Html::element( 'hr' ); |
| 171 | + $output .= Html::element( 'h3', |
| 172 | + array( 'id' => 'lang_' . $languageCode ), |
| 173 | + $languageName |
181 | 174 | ); |
182 | | - $output .= Html::element( 'br' ); |
| 175 | + if ( $i == 0 ) { |
| 176 | + $output .= self::getSearchFormHtml( $languageCode ); |
| 177 | + } else { |
| 178 | + $languageUrl = sprintf( self::$formatMobileUrl, $languageCode ); |
| 179 | + $output .= Html::element( 'a', |
| 180 | + array( 'id' => 'lang_' . $languageCode, |
| 181 | + 'href' => $languageUrl ), |
| 182 | + wfMessage( 'zero-rated-mobile-access-home-page-selection', |
| 183 | + $languageName )->inLanguage( $languageCode ) |
| 184 | + ); |
| 185 | + $output .= Html::element( 'br' ); |
| 186 | + } |
183 | 187 | } |
184 | 188 | } |
185 | | - } |
186 | | - $output .= Html::element( 'hr' ); |
187 | | - $output .= wfMsg( 'zero-rated-mobile-access-home-page-selection-text' ); |
188 | | - $output .= Html::openElement( 'select', |
189 | | - array( 'id' => 'languageselection', |
190 | | - 'onchange' => 'javascript:window.location = this.options[this.selectedIndex].value;', |
191 | | - ) |
192 | | - ); |
193 | | - $output .= Html::element( 'option', |
194 | | - array( 'value' => '' ), |
195 | | - wfMsg( 'zero-rated-mobile-access-language-selection' ) |
196 | | - ); |
197 | | - foreach ( $languageNames as $languageCode => $languageName ) { |
| 189 | + $output .= Html::element( 'hr' ); |
| 190 | + $output .= wfMsg( 'zero-rated-mobile-access-home-page-selection-text' ); |
| 191 | + $output .= Html::openElement( 'select', |
| 192 | + array( 'id' => 'languageselection', |
| 193 | + 'onchange' => 'javascript:window.location = this.options[this.selectedIndex].value;', |
| 194 | + ) |
| 195 | + ); |
198 | 196 | $output .= Html::element( 'option', |
199 | | - array( 'value' => sprintf( self::$formatMobileUrl, $languageCode ) ), |
200 | | - $languageName |
| 197 | + array( 'value' => '' ), |
| 198 | + wfMsg( 'zero-rated-mobile-access-language-selection' ) |
201 | 199 | ); |
| 200 | + foreach ( $languageNames as $languageCode => $languageName ) { |
| 201 | + $output .= Html::element( 'option', |
| 202 | + array( 'value' => sprintf( self::$formatMobileUrl, $languageCode ) ), |
| 203 | + $languageName |
| 204 | + ); |
| 205 | + } |
| 206 | + $output .= Html::closeElement( 'select' ); |
202 | 207 | } |
203 | | - $output .= Html::closeElement( 'select' ); |
204 | | - } |
205 | 208 | |
206 | | - $output .= Html::closeElement( 'div' ); |
207 | | - if ( $output != '<div id="zero-landing-page"></div>' ) { |
208 | | - $out->addHTML( $output ); |
| 209 | + $output .= Html::closeElement( 'div' ); |
| 210 | + if ( $output != '<div id="zero-landing-page"></div>' ) { |
| 211 | + $out->addHTML( $output ); |
| 212 | + } |
209 | 213 | } |
210 | | - |
211 | 214 | wfProfileOut( __METHOD__ ); |
212 | 215 | return true; |
213 | 216 | } |