r108565 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108564‎ | r108565 | r108566 >
Date:23:42, 10 January 2012
Author:preilly
Status:resolved (Comments)
Tags:
Comment:
enable extension only if testing or on wikipedia
Modified paths:
  • /trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php (modified) (history)
  • /trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.php
@@ -39,6 +39,8 @@
4040 $wgAutoloadClasses[$className] = $cwd . $classFilename . '.php';
4141 }
4242
 43+$wgEnableZeroRatedMobileAccessTesting = false;
 44+
4345 $wgExtZeroRatedMobileAccess = new ExtZeroRatedMobileAccess();
4446
4547 $wgHooks['BeforePageDisplay'][] = array( &$wgExtZeroRatedMobileAccess, 'beforePageDisplayHTML' );
\ No newline at end of file
Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.body.php
@@ -1,7 +1,7 @@
22 <?php
33
44 class ExtZeroRatedMobileAccess {
5 - const VERSION = '0.0.6';
 5+ const VERSION = '0.0.7';
66
77 public static $renderZeroRatedLandingPage;
88 public static $renderZeroRatedBanner;
@@ -24,189 +24,192 @@
2525 * @return bool
2626 */
2727 public function beforePageDisplayHTML( &$out, &$text ) {
28 - global $wgRequest;
 28+ global $wgRequest, $wgConf, $wgDBname, $wgEnableZeroRatedMobileAccessTesting;
2929 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 - }
3930
40 - $output = Html::openElement( 'div',
41 - array( 'id' => 'zero-landing-page' ) );
 31+ list( $site, $lang ) = $wgConf->siteFromDB( $wgDBname );
 32+ if ( $site == 'wikipedia' || $wgEnableZeroRatedMobileAccessTesting ) {
4233
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();
4952
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+ }
5457
55 - if ( self::$title->getNamespace() == NS_FILE ) {
56 - self::$isFilePage = true;
57 - }
 58+ if ( self::$title->getNamespace() == NS_FILE ) {
 59+ self::$isFilePage = true;
 60+ }
5861
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' );
6864 $out->redirect( $targetUrl, '301' );
6965 $out->output();
7066 }
71 - }
7267
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();
15073 }
151 - self::addDebugOutput( $country );
15274 }
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();
16175
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
181174 );
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+ }
183187 }
184188 }
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+ );
198196 $output .= Html::element( 'option',
199 - array( 'value' => sprintf( self::$formatMobileUrl, $languageCode ) ),
200 - $languageName
 197+ array( 'value' => '' ),
 198+ wfMsg( 'zero-rated-mobile-access-language-selection' )
201199 );
 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' );
202207 }
203 - $output .= Html::closeElement( 'select' );
204 - }
205208
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+ }
209213 }
210 -
211214 wfProfileOut( __METHOD__ );
212215 return true;
213216 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r108697fix for r108565 c29376preilly23:40, 11 January 2012
r108698fix for r108565 c29376 better variable namespreilly23:44, 11 January 2012

Comments

#Comment by 😂 (talk | contribs)   20:12, 11 January 2012

Don't use $wgDBname directly, use getDBname() on a DB object. Otherwise ok.

Status & tagging log