r82628 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82627‎ | r82628 | r82629 >
Date:20:02, 22 February 2011
Author:hcatlin
Status:deferred
Tags:
Comment:
WikimediaMobile Redirect: Writing more complete test suite and writing a MUCH smaller version of the redirector so that we might be able to minimize it and embed it in every page
Modified paths:
  • /trunk/extensions/WikimediaMobile/MobileRedirect.js (modified) (history)
  • /trunk/extensions/WikimediaMobile/WikimediaMobile.php (modified) (history)
  • /trunk/extensions/WikimediaMobile/redirector_test.js (added) (history)
  • /trunk/extensions/WikimediaMobile/user_agent_test.js (deleted) (history)

Diff [purge]

Index: trunk/extensions/WikimediaMobile/user_agent_test.js
@@ -1,118 +0,0 @@
2 -/*
3 - User Agent Tests
4 -
5 - This is an incomplete test set. As you hit issues, please add strings that
6 - may be problematic.
7 -
8 - Written by Hampton Catlin
9 -*/
10 -
11 -var regex = /(iPhone|iPod|Android.*Mobile|webOS|NetFront|Opera Mini|SEMC-Browser|PlayStation Portable|Nintendo Wii|BlackBerry)/;
12 -
13 -var runTests = function() {
14 - // iPhone
15 - shouldRedirect("Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20");
16 - // Android on HTC Desire
17 - shouldRedirect("Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
18 - // Nintendo Wii
19 - shouldRedirect("Opera/9.30 (Nintendo Wii; U; ; 2047-7;pt-br)");
20 - // Netfront PDA
21 - shouldRedirect("Mozilla/5.0 (PDA; NF35WMPRO/1.0; like Gecko) NetFront/3.5");
22 - // Palm Pre
23 - shouldRedirect("Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0");
24 - // Safari on Mac OS X
25 - shouldIgnore("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4");
26 - // Chrome on OS X
27 - shouldIgnore("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10");
28 - // iPad
29 - shouldIgnore("Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10");
30 - shouldIgnore("Mozilla/5.0 (iPad; U; CPU OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C5101c Safari/6533.18.5");
31 - // iPhone 4
32 - shouldRedirect("Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.05 Mobile/8A293 Safari/6531.22.7");
33 - // Firefox
34 - shouldIgnore("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b9pre) Gecko/20101228 Firefox/4.0b9pre")
35 -
36 - // Android Nexus One Phone
37 - shouldRedirect("Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
38 - // Android Table
39 - shouldIgnore("Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; device Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Safari/533.1")
40 -
41 - // Fake test strings
42 -
43 - // Android, but only with Mobile
44 - shouldIgnore("Android");
45 - shouldRedirect("Mozilla/5.0 (Android; Mobile/5G77 Safari/525.20")
46 -};
47 -
48 -var doesMatch = function(user_agent) {
49 - return regex.test(user_agent);
50 -};
51 -
52 -var runTest = function(user_agent, should_be) {
53 - var result = doesMatch(user_agent);
54 - if(result == should_be) {
55 - print("OK");
56 - } else {
57 - print("FAIL: '" + user_agent + "'");
58 - }
59 -};
60 -
61 -var shouldRedirect = function(user_agent) {
62 - runTest(user_agent, true);
63 -};
64 -
65 -var shouldIgnore = function(user_agent) {
66 - runTest(user_agent, false);
67 -};
68 -
69 -runTests();
70 -/*
71 - User Agent Tests
72 -
73 - This is an incomplete test set. As you hit issues, please add strings that
74 - may be problematic.
75 -
76 - Written by Hampton Catlin
77 -*/
78 -
79 -var regex = /(Mobile.*Safari|webOS|NetFront|Opera Mini|SEMC-Browser|PlayStation Portable|Nintendo Wii|BlackBerry)/;
80 -
81 -var runTests = function() {
82 - // iPhone
83 - shouldRedirect("Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20");
84 - // Android on HTC Desire
85 - shouldRedirect("Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
86 - // Nintendo Wii
87 - shouldRedirect("Opera/9.30 (Nintendo Wii; U; ; 2047-7;pt-br)");
88 - // Netfront PDA
89 - shouldRedirect("Mozilla/5.0 (PDA; NF35WMPRO/1.0; like Gecko) NetFront/3.5");
90 - // Palm Pre
91 - shouldRedirect("Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0");
92 - // Safari on Mac OS X
93 - shouldIgnore("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4");
94 - // Chrome on OS X
95 - shouldIgnore("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10");
96 -}
97 -
98 -var doesMatch = function(user_agent) {
99 - return regex.test(user_agent);
100 -}
101 -
102 -var runTest = function(user_agent, should_be) {
103 - var result = doesMatch(user_agent);
104 - if(result == should_be) {
105 - print("OK")
106 - } else {
107 - print("FAIL: '" + user_agent + "'")
108 - }
109 -}
110 -
111 -var shouldRedirect = function(user_agent) {
112 - runTest(user_agent, true);
113 -}
114 -
115 -var shouldIgnore = function(user_agent) {
116 - runTest(user_agent, false);
117 -}
118 -
119 -runTests();
\ No newline at end of file
Index: trunk/extensions/WikimediaMobile/WikimediaMobile.php
@@ -19,7 +19,7 @@
2020 /**
2121 * Increment this when the JS file changes
2222 */
23 -$wgWikimediaMobileVersion = '2';
 23+$wgWikimediaMobileVersion = '3';
2424
2525 /**
2626 * The base URL of the mobile gateway
Index: trunk/extensions/WikimediaMobile/MobileRedirect.js
@@ -6,42 +6,20 @@
77 * You can turn off the redirect by setting the cookie "stopMobileRedirect=true"
88 */
99 if ( /(iPhone|iPod|Android.*Mobile|webOS|NetFront|Opera Mini|SEMC-Browser|PlayStation Portable|Nintendo Wii|BlackBerry)/
10 - .test( navigator.userAgent ) )
 10+ .test( navigator.userAgent ) )
1111 {
12 - (function () {
13 - function haveStopCookie() {
14 - return (document.cookie.indexOf("stopMobileRedirect=true") >= 0);
15 - }
 12+
 13+ if ( (document.cookie.indexOf("eRedirect=t") < 0) // Don't redirect if we have the stop cookie ... only testing a subportion of the cookie. Should be REALLY unique!
 14+ && (wgNamespaceNumber >= 0) // Don't redirect special pages
 15+ && (wgAction == "view")) // Don't redirect URLs that aren't simple page views
 16+ {
 17+ // If we've made it here, then we are going ahead with the redirect
 18+ var url = wgWikimediaMobileUrl;
 19+ // If we are NOT on the main page, then set the pageName!
 20+ if (wgPageName != wgMainPageTitle.replace(/ /g, '_')) {
 21+ url += '/' + encodeURI(wgPageName);
 22+ }
1623
17 - function getMobileUrl() {
18 - var mainPage = wgMainPageTitle.replace(/ /g, '_');
19 - var url = wgWikimediaMobileUrl + '/';
20 - if (wgPageName == mainPage) {
21 - url += '::Home'; // Special case
22 - } else {
23 - url += encodeURI(wgPageName);
24 - }
25 - url += '?wasRedirected=true';
26 - return url;
27 - }
28 -
29 - // Don't redirect if we have the stop cookie
30 - if (haveStopCookie()) return;
31 -
32 - // Don't redirect special pages
33 - if (wgNamespaceNumber < 0) return;
34 -
35 - // Don't redirect URLs that aren't simple page views
36 - if (document.location.search && document.location.search.length > 0) {
37 - var params = document.location.search.substr(1).split('&');
38 - for (var i = 0; i < params.length; i++) {
39 - var paramParts = params[i].split('=');
40 - if (paramParts.length && paramParts[0] != 'title') {
41 - return;
42 - }
43 - }
44 - }
45 -
46 - document.location = getMobileUrl();
47 - })();
 24+ document.location = url;
 25+ }
4826 }
Index: trunk/extensions/WikimediaMobile/redirector_test.js
@@ -0,0 +1,147 @@
 2+/*
 3+ WikimediaMobile Redirect Tests
 4+
 5+ This is a nearly complete test set. As you hit issues, please add UA strings and other combinations that
 6+ may be problematic.
 7+
 8+ Written by Hampton Catlin
 9+*/
 10+
 11+var navigator;
 12+var document;
 13+var wgAction;
 14+var wgNamespaceNumber;
 15+var wgMainPageTitle;
 16+var wgTitle;
 17+var wgPageTitle;
 18+var wgWikimediaMobileUrl;
 19+
 20+var runTests = function() {
 21+ shouldRedirect({});
 22+ testUserAgents();
 23+ testCookies();
 24+ testNamespaces();
 25+ testMainPages();
 26+}
 27+
 28+
 29+var runTest = function(options, shouldRedirect) {
 30+ wgAction = options.action || "view";
 31+ wgNamespaceNumber = options.namespace_number || 0;
 32+
 33+ wgMainPageTitle = options.main_page_title || "Wikipédia:Accueil principal"
 34+ wgTitle = options.title || "Tokyo Bay"
 35+ wgPageName = options.page_name || "Tokyo_Bay"
 36+ wgWikimediaMobileUrl = options.mobile_site_url || "http://en.m.wikipedia.org/wiki"
 37+
 38+ // Mock a browser
 39+ navigator = {};
 40+ document = {location: {search: "?title=" + wgPageName}};
 41+
 42+ // Set in browser variables
 43+ navigator.userAgent = options.user_agent || "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20";
 44+ document.cookie = options.cookie || "random=true"
 45+
 46+ load("MobileRedirect.js")
 47+ // Run against old redirect
 48+ //load("OriginalMobileRedirect.js");
 49+
 50+ if(((document.location != null) && (document.location.length > 0)) == shouldRedirect) {
 51+ if(options.should_redirect_to && (document.location != options.should_redirect_to)) {
 52+ print("Should have been redirect to " + options.should_redirect_to)
 53+ print("Instead was sent to " + document.location)
 54+ for(key in options)
 55+ {
 56+ print(" " + key + ": " + options[key]);
 57+ }
 58+ } else {
 59+ print("OK")
 60+ }
 61+ } else {
 62+ if(shouldRedirect) {
 63+ print("FAILED: SHOULD REDIRECT WITH:");
 64+ } else {
 65+ print("FAILED: SHOULD IGNORE WITH:");
 66+ }
 67+
 68+ for(key in options)
 69+ {
 70+ print(" " + key + ": " + options[key]);
 71+ }
 72+ }
 73+
 74+}
 75+
 76+var shouldRedirect = function(options) {
 77+ runTest(options, true)
 78+}
 79+
 80+var shouldIgnore = function(options) {
 81+ runTest(options, false)
 82+}
 83+
 84+/* ========== ACTUAL TEST CASES ================== */
 85+
 86+var testCookies = function() {
 87+ shouldRedirect({cookie: "StewardVoteEligible_r7=0"});
 88+ shouldIgnore({cookie: "stopMobileRedirect=true"})
 89+}
 90+
 91+var testNamespaces = function() {
 92+ shouldRedirect({namespace_number: 2})
 93+ shouldRedirect({namespace_number: 0})
 94+ shouldIgnore({namespace_number: -1})
 95+}
 96+
 97+var testMainPages = function() {
 98+ shouldRedirect({main_page_title: "", title: "", page_name: ""});
 99+
 100+ // EN
 101+ shouldRedirect({should_redirect_to: 'http://en.m.wikipedia.org/wiki', main_page_title: 'Main Page', title: 'Main Page', page_name: 'Main_Page'})
 102+ shouldRedirect({should_redirect_to: 'http://en.m.wikipedia.org/wiki/Main_(river)', main_page_title: 'Main Page', title: 'Main (river)', page_name: 'Main_(river)'});
 103+
 104+ // FR
 105+ shouldRedirect({should_redirect_to: 'http://fr.m.wikipedia.org/wiki', main_page_title: 'Wikipédia:Accueil principal', title: 'Accueil principal', page_name: 'Wikipédia:Accueil_principal', mobile_site_url: "http://fr.m.wikipedia.org/wiki"});
 106+ shouldRedirect({should_redirect_to: 'http://fr.m.wikipedia.org/wiki/Ralph_Waldo_Emerson', main_page_title: 'Wikipédia:Accueil principal', title: 'Ralph Waldo Emerson', page_name: 'Ralph_Waldo_Emerson', mobile_site_url: "http://fr.m.wikipedia.org/wiki"});
 107+
 108+ // DE
 109+ shouldRedirect({should_redirect_to: 'http://de.m.wikipedia.org/wiki', main_page_title: 'Wikipedia:Hauptseite', title: 'Hauptseite', page_name: 'Wikipedia:Hauptseite', mobile_site_url: "http://de.m.wikipedia.org/wiki"});
 110+ shouldRedirect({should_redirect_to: 'http://de.m.wikipedia.org/wiki/Ralph_Waldo_Emerson', main_page_title: 'Wikipedia:Hauptseite', title: 'Ralph Waldo Emerson', page_name: 'Ralph_Waldo_Emerson', mobile_site_url: "http://de.m.wikipedia.org/wiki"});
 111+}
 112+
 113+var testUserAgents = function() {
 114+ // iPhone
 115+ shouldRedirect({user_agent: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20"});
 116+ // Android on HTC Desire
 117+ shouldRedirect({user_agent: "Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17"});
 118+ // Nintendo Wii
 119+ shouldRedirect({user_agent: "Opera/9.30 (Nintendo Wii; U; ; 2047-7;pt-br)"});
 120+ // Netfront PDA
 121+ shouldRedirect({user_agent: "Mozilla/5.0 (PDA; NF35WMPRO/1.0; like Gecko) NetFront/3.5"});
 122+ // Palm Pre
 123+ shouldRedirect({user_agent: "Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0"});
 124+ // Safari on Mac OS X
 125+ shouldIgnore({user_agent: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4"});
 126+ // Chrome on OS X
 127+ shouldIgnore({user_agent: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10"});
 128+ // iPad
 129+ shouldIgnore({user_agent: "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10"});
 130+ shouldIgnore({user_agent: "Mozilla/5.0 (iPad; U; CPU OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C5101c Safari/6533.18.5"});
 131+ // iPhone 4
 132+ shouldRedirect({user_agent: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.05 Mobile/8A293 Safari/6531.22.7"});
 133+ // Firefox
 134+ shouldIgnore({user_agent: "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b9pre) Gecko/20101228 Firefox/4.0b9pre"});
 135+
 136+ // Android Nexus One Phone
 137+ shouldRedirect({user_agent: "Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"});
 138+ // Android Table
 139+ shouldIgnore({user_agent: "Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; device Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Safari/533.1"});
 140+
 141+ // Fake test strings
 142+
 143+ // Android, but only with Mobile
 144+ shouldIgnore({user_agent: "Android"});
 145+ shouldRedirect({user_agent: "Mozilla/5.0 (Android; Mobile/5G77 Safari/525.20"});
 146+}
 147+
 148+runTests();
\ No newline at end of file
Property changes on: trunk/extensions/WikimediaMobile/redirector_test.js
___________________________________________________________________
Added: svn:eol-style
1149 + native

Status & tagging log