Index: trunk/extensions/ZeroRatedMobileAccess/ZeroRatedMobileAccess.php |
— | — | @@ -17,12 +17,12 @@ |
18 | 18 | |
19 | 19 | // Extension credits that will show up on Special:Version |
20 | 20 | $wgExtensionCredits['other'][] = array( |
21 | | - 'path' => __FILE__, |
22 | | - 'name' => 'ZeroRatedMobileAccess', |
23 | | - 'version' => ExtZeroRatedMobileAccess::VERSION, |
24 | | - 'author' => '[http://www.mediawiki.org/wiki/User:Preilly Preilly]', |
25 | | - 'descriptionmsg' => 'zero-rated-mobile-access-desc', |
26 | | - 'url' => 'https://www.mediawiki.org/wiki/Extension:ZeroRatedMobileAccess', |
| 21 | + 'path' => __FILE__, |
| 22 | + 'name' => 'ZeroRatedMobileAccess', |
| 23 | + 'version' => ExtZeroRatedMobileAccess::VERSION, |
| 24 | + 'author' => '[http://www.mediawiki.org/wiki/User:Preilly Preilly]', |
| 25 | + 'descriptionmsg' => 'zero-rated-mobile-access-desc', |
| 26 | + 'url' => 'https://www.mediawiki.org/wiki/Extension:ZeroRatedMobileAccess', |
27 | 27 | ); |
28 | 28 | |
29 | 29 | $cwd = dirname( __FILE__ ) . DIRECTORY_SEPARATOR; |
— | — | @@ -43,9 +43,11 @@ |
44 | 44 | $wgHooks['BeforePageDisplay'][] = array( &$wgExtZeroRatedMobileAccess, 'beforePageDisplayHTML' ); |
45 | 45 | |
46 | 46 | class ExtZeroRatedMobileAccess { |
47 | | - const VERSION = '0.0.2'; |
| 47 | + const VERSION = '0.0.3'; |
48 | 48 | |
49 | 49 | public static $renderZeroRatedLandingPage; |
| 50 | + private static $debugOutput = array(); |
| 51 | + private static $displayDebugOutput = false; |
50 | 52 | |
51 | 53 | /** |
52 | 54 | * @param $out OutputPage |
— | — | @@ -58,9 +60,334 @@ |
59 | 61 | self::$renderZeroRatedLandingPage = $wgRequest->getInt( 'renderZeroRatedLandingPage' ); |
60 | 62 | if ( self::$renderZeroRatedLandingPage === 1 ) { |
61 | 63 | echo wfMsg( 'zero-rated-mobile-access-desc' ); |
62 | | - exit(); |
| 64 | + $country = $wgRequest->getVal( 'country' ); |
| 65 | + $ip = ( $wgRequest->getVal( 'ip') ) ? $wgRequest->getVal( 'ip' ) : wfGetIP(); |
| 66 | + // Temporary hack to allow for testing on localhost |
| 67 | + $countryIps = array( |
| 68 | + 'GERMANY' => '80.237.226.75', |
| 69 | + 'MEXICO' => '187.184.240.247', |
| 70 | + 'THAILAND' => '180.180.150.104', |
| 71 | + 'FRANCE' => '90.6.70.28', |
| 72 | + ); |
| 73 | + $ip = ( strpos( $ip, '192.168.' ) === 0 ) ? $countryIps['FRANCE'] : $ip; |
| 74 | + if ( IP::isValid( $ip ) ) { |
| 75 | + // If no country was passed, try to do GeoIP lookup |
| 76 | + // Requires php5-geoip package |
| 77 | + if ( !$country && function_exists( 'geoip_country_code_by_name' ) ) { |
| 78 | + $country = geoip_country_code_by_name( $ip ); |
| 79 | + } |
| 80 | + self::addDebugOutput( $country ); |
| 81 | + } |
| 82 | + $languageOptions = self::createLanguageOptionsFromWikiText(); |
| 83 | + //self::$displayDebugOutput = true; |
| 84 | + //self::addDebugOutput( $languageOptions ); |
| 85 | + self::addDebugOutput( self::getFullCountryNameFromCode( $country ) ); |
| 86 | + self::addDebugOutput( $languageOptions[self::getFullCountryNameFromCode( $country )] ); |
| 87 | + self::writeDebugOutput(); |
63 | 88 | } |
64 | 89 | wfProfileOut( __METHOD__ ); |
| 90 | + exit(); |
65 | 91 | return true; |
66 | 92 | } |
| 93 | + |
| 94 | + private static function addDebugOutput( $object ) { |
| 95 | + wfProfileIn( __METHOD__ ); |
| 96 | + self::$debugOutput[] = $object; |
| 97 | + wfProfileOut( __METHOD__ ); |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + private static function writeDebugOutput() { |
| 102 | + wfProfileIn( __METHOD__ ); |
| 103 | + if ( self::$debugOutput && self::$displayDebugOutput === true ) { |
| 104 | + echo "<pre>"; |
| 105 | + foreach( self::$debugOutput as $debugOutput ) { |
| 106 | + var_dump( $debugOutput ); |
| 107 | + } |
| 108 | + echo "</pre>"; |
| 109 | + } |
| 110 | + wfProfileOut( __METHOD__ ); |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + private static function createLanguageOptionsFromWikiText() { |
| 115 | + wfProfileIn( __METHOD__ ); |
| 116 | + $languageOptions = array(); |
| 117 | + $title = Title::newFromText( "Language_options", NS_MEDIAWIKI ); |
| 118 | + // Use the revision directly to prevent other hooks to be called |
| 119 | + $rev = Revision::newFromTitle( $title ); |
| 120 | + if ( $rev ) { |
| 121 | + $lines = explode( "\n", $rev->getRawText() ); |
| 122 | + } |
| 123 | + if ( $lines && count( $lines ) > 0 ) { |
| 124 | + $sizeOfLines = sizeof( $lines ); |
| 125 | + for ( $i = 0; $i < $sizeOfLines; $i++ ) { |
| 126 | + $line = $lines[$i]; |
| 127 | + if ( strpos( $line, '*' ) === 0 && strpos( $line, '**' ) !== 0 && $i >= 0 ) { |
| 128 | + $countryName = strtoupper( str_replace( '* ', '', $line ) ); |
| 129 | + $languageOptions[$countryName] = ''; |
| 130 | + } elseif ( strpos( $line, '**' ) === 0 && $i > 0 ) { |
| 131 | + $lineParts = explode('#', $line); |
| 132 | + $language = ( isset( $lineParts[0] ) ) ? |
| 133 | + trim( str_replace( '** ', '', $lineParts[0] ) ) : |
| 134 | + trim( str_replace( '** ', '', $line ) ) ; |
| 135 | + if ( $language !== 'portal' && $language !== 'other' ) { |
| 136 | + $languageOptions[$countryName][] = ( isset( $lineParts[1] ) ) ? |
| 137 | + array( 'language' => $language, |
| 138 | + 'percentage' => intval( str_replace( '%', '', trim( $lineParts[1] ) ) ) ) : |
| 139 | + $language; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + wfProfileOut( __METHOD__ ); |
| 145 | + return $languageOptions; |
| 146 | + } |
| 147 | + |
| 148 | + private static function getFullCountryNameFromCode( $code ) { |
| 149 | + wfProfileIn( __METHOD__ ); |
| 150 | + $countries = array( |
| 151 | + 'AF' => 'AFGHANISTAN', |
| 152 | + 'AL' => 'ALBANIA', |
| 153 | + 'DZ' => 'ALGERIA', |
| 154 | + 'AS' => 'AMERICAN SAMOA', |
| 155 | + 'AD' => 'ANDORRA', |
| 156 | + 'AO' => 'ANGOLA', |
| 157 | + 'AI' => 'ANGUILLA', |
| 158 | + 'AQ' => 'ANTARCTICA', |
| 159 | + 'AG' => 'ANTIGUA AND BARBUDA', |
| 160 | + 'AR' => 'ARGENTINA', |
| 161 | + 'AM' => 'ARMENIA', |
| 162 | + 'AW' => 'ARUBA', |
| 163 | + 'AU' => 'AUSTRALIA', |
| 164 | + 'AT' => 'AUSTRIA', |
| 165 | + 'AZ' => 'AZERBAIJAN', |
| 166 | + 'BS' => 'BAHAMAS', |
| 167 | + 'BH' => 'BAHRAIN', |
| 168 | + 'BD' => 'BANGLADESH', |
| 169 | + 'BB' => 'BARBADOS', |
| 170 | + 'BY' => 'BELARUS', |
| 171 | + 'BE' => 'BELGIUM', |
| 172 | + 'BZ' => 'BELIZE', |
| 173 | + 'BJ' => 'BENIN', |
| 174 | + 'BM' => 'BERMUDA', |
| 175 | + 'BT' => 'BHUTAN', |
| 176 | + 'BO' => 'BOLIVIA', |
| 177 | + 'BA' => 'BOSNIA AND HERZEGOVINA', |
| 178 | + 'BW' => 'BOTSWANA', |
| 179 | + 'BV' => 'BOUVET ISLAND', |
| 180 | + 'BR' => 'BRAZIL', |
| 181 | + 'IO' => 'BRITISH INDIAN OCEAN TERRITORY', |
| 182 | + 'BN' => 'BRUNEI DARUSSALAM', |
| 183 | + 'BG' => 'BULGARIA', |
| 184 | + 'BF' => 'BURKINA FASO', |
| 185 | + 'BI' => 'BURUNDI', |
| 186 | + 'KH' => 'CAMBODIA', |
| 187 | + 'CM' => 'CAMEROON', |
| 188 | + 'CA' => 'CANADA', |
| 189 | + 'CV' => 'CAPE VERDE', |
| 190 | + 'KY' => 'CAYMAN ISLANDS', |
| 191 | + 'CF' => 'CENTRAL AFRICAN REPUBLIC', |
| 192 | + 'TD' => 'CHAD', |
| 193 | + 'CL' => 'CHILE', |
| 194 | + 'CN' => 'CHINA', |
| 195 | + 'CX' => 'CHRISTMAS ISLAND', |
| 196 | + 'CC' => 'COCOS (KEELING) ISLANDS', |
| 197 | + 'CO' => 'COLOMBIA', |
| 198 | + 'KM' => 'COMOROS', |
| 199 | + 'CG' => 'CONGO', |
| 200 | + 'CD' => 'CONGO, THE DEMOCRATIC REPUBLIC OF THE', |
| 201 | + 'CK' => 'COOK ISLANDS', |
| 202 | + 'CR' => 'COSTA RICA', |
| 203 | + 'CI' => 'COTE D IVOIRE', |
| 204 | + 'HR' => 'CROATIA', |
| 205 | + 'CU' => 'CUBA', |
| 206 | + 'CY' => 'CYPRUS', |
| 207 | + 'CZ' => 'CZECH REPUBLIC', |
| 208 | + 'DK' => 'DENMARK', |
| 209 | + 'DJ' => 'DJIBOUTI', |
| 210 | + 'DM' => 'DOMINICA', |
| 211 | + 'DO' => 'DOMINICAN REPUBLIC', |
| 212 | + 'TP' => 'EAST TIMOR', |
| 213 | + 'EC' => 'ECUADOR', |
| 214 | + 'EG' => 'EGYPT', |
| 215 | + 'SV' => 'EL SALVADOR', |
| 216 | + 'GQ' => 'EQUATORIAL GUINEA', |
| 217 | + 'ER' => 'ERITREA', |
| 218 | + 'EE' => 'ESTONIA', |
| 219 | + 'ET' => 'ETHIOPIA', |
| 220 | + 'FK' => 'FALKLAND ISLANDS (MALVINAS)', |
| 221 | + 'FO' => 'FAROE ISLANDS', |
| 222 | + 'FJ' => 'FIJI', |
| 223 | + 'FI' => 'FINLAND', |
| 224 | + 'FR' => 'FRANCE', |
| 225 | + 'GF' => 'FRENCH GUIANA', |
| 226 | + 'PF' => 'FRENCH POLYNESIA', |
| 227 | + 'TF' => 'FRENCH SOUTHERN TERRITORIES', |
| 228 | + 'GA' => 'GABON', |
| 229 | + 'GM' => 'GAMBIA', |
| 230 | + 'GE' => 'GEORGIA', |
| 231 | + 'DE' => 'GERMANY', |
| 232 | + 'GH' => 'GHANA', |
| 233 | + 'GI' => 'GIBRALTAR', |
| 234 | + 'GR' => 'GREECE', |
| 235 | + 'GL' => 'GREENLAND', |
| 236 | + 'GD' => 'GRENADA', |
| 237 | + 'GP' => 'GUADELOUPE', |
| 238 | + 'GU' => 'GUAM', |
| 239 | + 'GT' => 'GUATEMALA', |
| 240 | + 'GN' => 'GUINEA', |
| 241 | + 'GW' => 'GUINEA-BISSAU', |
| 242 | + 'GY' => 'GUYANA', |
| 243 | + 'HT' => 'HAITI', |
| 244 | + 'HM' => 'HEARD ISLAND AND MCDONALD ISLANDS', |
| 245 | + 'VA' => 'HOLY SEE (VATICAN CITY STATE)', |
| 246 | + 'HN' => 'HONDURAS', |
| 247 | + 'HK' => 'HONG KONG', |
| 248 | + 'HU' => 'HUNGARY', |
| 249 | + 'IS' => 'ICELAND', |
| 250 | + 'IN' => 'INDIA', |
| 251 | + 'ID' => 'INDONESIA', |
| 252 | + 'IR' => 'IRAN, ISLAMIC REPUBLIC OF', |
| 253 | + 'IQ' => 'IRAQ', |
| 254 | + 'IE' => 'IRELAND', |
| 255 | + 'IL' => 'ISRAEL', |
| 256 | + 'IT' => 'ITALY', |
| 257 | + 'JM' => 'JAMAICA', |
| 258 | + 'JP' => 'JAPAN', |
| 259 | + 'JO' => 'JORDAN', |
| 260 | + 'KZ' => 'KAZAKSTAN', |
| 261 | + 'KE' => 'KENYA', |
| 262 | + 'KI' => 'KIRIBATI', |
| 263 | + 'KP' => 'KOREA DEMOCRATIC PEOPLES REPUBLIC OF', |
| 264 | + 'KR' => 'KOREA REPUBLIC OF', |
| 265 | + 'KW' => 'KUWAIT', |
| 266 | + 'KG' => 'KYRGYZSTAN', |
| 267 | + 'LA' => 'LAO PEOPLES DEMOCRATIC REPUBLIC', |
| 268 | + 'LV' => 'LATVIA', |
| 269 | + 'LB' => 'LEBANON', |
| 270 | + 'LS' => 'LESOTHO', |
| 271 | + 'LR' => 'LIBERIA', |
| 272 | + 'LY' => 'LIBYAN ARAB JAMAHIRIYA', |
| 273 | + 'LI' => 'LIECHTENSTEIN', |
| 274 | + 'LT' => 'LITHUANIA', |
| 275 | + 'LU' => 'LUXEMBOURG', |
| 276 | + 'MO' => 'MACAU', |
| 277 | + 'MK' => 'MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF', |
| 278 | + 'MG' => 'MADAGASCAR', |
| 279 | + 'MW' => 'MALAWI', |
| 280 | + 'MY' => 'MALAYSIA', |
| 281 | + 'MV' => 'MALDIVES', |
| 282 | + 'ML' => 'MALI', |
| 283 | + 'MT' => 'MALTA', |
| 284 | + 'MH' => 'MARSHALL ISLANDS', |
| 285 | + 'MQ' => 'MARTINIQUE', |
| 286 | + 'MR' => 'MAURITANIA', |
| 287 | + 'MU' => 'MAURITIUS', |
| 288 | + 'YT' => 'MAYOTTE', |
| 289 | + 'MX' => 'MEXICO', |
| 290 | + 'FM' => 'MICRONESIA, FEDERATED STATES OF', |
| 291 | + 'MD' => 'MOLDOVA, REPUBLIC OF', |
| 292 | + 'MC' => 'MONACO', |
| 293 | + 'MN' => 'MONGOLIA', |
| 294 | + 'MS' => 'MONTSERRAT', |
| 295 | + 'MA' => 'MOROCCO', |
| 296 | + 'MZ' => 'MOZAMBIQUE', |
| 297 | + 'MM' => 'MYANMAR', |
| 298 | + 'NA' => 'NAMIBIA', |
| 299 | + 'NR' => 'NAURU', |
| 300 | + 'NP' => 'NEPAL', |
| 301 | + 'NL' => 'NETHERLANDS', |
| 302 | + 'AN' => 'NETHERLANDS ANTILLES', |
| 303 | + 'NC' => 'NEW CALEDONIA', |
| 304 | + 'NZ' => 'NEW ZEALAND', |
| 305 | + 'NI' => 'NICARAGUA', |
| 306 | + 'NE' => 'NIGER', |
| 307 | + 'NG' => 'NIGERIA', |
| 308 | + 'NU' => 'NIUE', |
| 309 | + 'NF' => 'NORFOLK ISLAND', |
| 310 | + 'MP' => 'NORTHERN MARIANA ISLANDS', |
| 311 | + 'NO' => 'NORWAY', |
| 312 | + 'OM' => 'OMAN', |
| 313 | + 'PK' => 'PAKISTAN', |
| 314 | + 'PW' => 'PALAU', |
| 315 | + 'PS' => 'PALESTINIAN TERRITORY, OCCUPIED', |
| 316 | + 'PA' => 'PANAMA', |
| 317 | + 'PG' => 'PAPUA NEW GUINEA', |
| 318 | + 'PY' => 'PARAGUAY', |
| 319 | + 'PE' => 'PERU', |
| 320 | + 'PH' => 'PHILIPPINES', |
| 321 | + 'PN' => 'PITCAIRN', |
| 322 | + 'PL' => 'POLAND', |
| 323 | + 'PT' => 'PORTUGAL', |
| 324 | + 'PR' => 'PUERTO RICO', |
| 325 | + 'QA' => 'QATAR', |
| 326 | + 'RE' => 'REUNION', |
| 327 | + 'RO' => 'ROMANIA', |
| 328 | + 'RU' => 'RUSSIAN FEDERATION', |
| 329 | + 'RW' => 'RWANDA', |
| 330 | + 'SH' => 'SAINT HELENA', |
| 331 | + 'KN' => 'SAINT KITTS AND NEVIS', |
| 332 | + 'LC' => 'SAINT LUCIA', |
| 333 | + 'PM' => 'SAINT PIERRE AND MIQUELON', |
| 334 | + 'VC' => 'SAINT VINCENT AND THE GRENADINES', |
| 335 | + 'WS' => 'SAMOA', |
| 336 | + 'SM' => 'SAN MARINO', |
| 337 | + 'ST' => 'SAO TOME AND PRINCIPE', |
| 338 | + 'SA' => 'SAUDI ARABIA', |
| 339 | + 'SN' => 'SENEGAL', |
| 340 | + 'SC' => 'SEYCHELLES', |
| 341 | + 'SL' => 'SIERRA LEONE', |
| 342 | + 'SG' => 'SINGAPORE', |
| 343 | + 'SK' => 'SLOVAKIA', |
| 344 | + 'SI' => 'SLOVENIA', |
| 345 | + 'SB' => 'SOLOMON ISLANDS', |
| 346 | + 'SO' => 'SOMALIA', |
| 347 | + 'ZA' => 'SOUTH AFRICA', |
| 348 | + 'GS' => 'SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS', |
| 349 | + 'ES' => 'SPAIN', |
| 350 | + 'LK' => 'SRI LANKA', |
| 351 | + 'SD' => 'SUDAN', |
| 352 | + 'SR' => 'SURINAME', |
| 353 | + 'SJ' => 'SVALBARD AND JAN MAYEN', |
| 354 | + 'SZ' => 'SWAZILAND', |
| 355 | + 'SE' => 'SWEDEN', |
| 356 | + 'CH' => 'SWITZERLAND', |
| 357 | + 'SY' => 'SYRIAN ARAB REPUBLIC', |
| 358 | + 'TW' => 'TAIWAN, PROVINCE OF CHINA', |
| 359 | + 'TJ' => 'TAJIKISTAN', |
| 360 | + 'TZ' => 'TANZANIA, UNITED REPUBLIC OF', |
| 361 | + 'TH' => 'THAILAND', |
| 362 | + 'TG' => 'TOGO', |
| 363 | + 'TK' => 'TOKELAU', |
| 364 | + 'TO' => 'TONGA', |
| 365 | + 'TT' => 'TRINIDAD AND TOBAGO', |
| 366 | + 'TN' => 'TUNISIA', |
| 367 | + 'TR' => 'TURKEY', |
| 368 | + 'TM' => 'TURKMENISTAN', |
| 369 | + 'TC' => 'TURKS AND CAICOS ISLANDS', |
| 370 | + 'TV' => 'TUVALU', |
| 371 | + 'UG' => 'UGANDA', |
| 372 | + 'UA' => 'UKRAINE', |
| 373 | + 'AE' => 'UNITED ARAB EMIRATES', |
| 374 | + 'GB' => 'UNITED KINGDOM', |
| 375 | + 'US' => 'UNITED STATES', |
| 376 | + 'UM' => 'UNITED STATES MINOR OUTLYING ISLANDS', |
| 377 | + 'UY' => 'URUGUAY', |
| 378 | + 'UZ' => 'UZBEKISTAN', |
| 379 | + 'VU' => 'VANUATU', |
| 380 | + 'VE' => 'VENEZUELA', |
| 381 | + 'VN' => 'VIET NAM', |
| 382 | + 'VG' => 'VIRGIN ISLANDS, BRITISH', |
| 383 | + 'VI' => 'VIRGIN ISLANDS, U.S.', |
| 384 | + 'WF' => 'WALLIS AND FUTUNA', |
| 385 | + 'EH' => 'WESTERN SAHARA', |
| 386 | + 'YE' => 'YEMEN', |
| 387 | + 'YU' => 'YUGOSLAVIA', |
| 388 | + 'ZM' => 'ZAMBIA', |
| 389 | + 'ZW' => 'ZIMBABWE', |
| 390 | + ); |
| 391 | + wfProfileOut( __METHOD__ ); |
| 392 | + return ( isset( $countries[strtoupper( $code )] ) ) ? $countries[strtoupper( $code )] : null; |
| 393 | + } |
67 | 394 | } |
\ No newline at end of file |