r48222 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48221‎ | r48222 | r48223 >
Date:13:13, 9 March 2009
Author:demon
Status:reverted
Tags:
Comment:
* Document pretty much everything so far
* Clean up some code formatting
* Put some proper debug code into SearchPage
Modified paths:
  • /trunk/android-client/README (modified) (history)
  • /trunk/android-client/android-client.apk (modified) (history)
  • /trunk/android-client/src/org/mediawiki/android/HttpRequest.java (modified) (history)
  • /trunk/android-client/src/org/mediawiki/android/WikiApi.java (modified) (history)
  • /trunk/android-client/src/org/mediawiki/android/WikiPage.java (modified) (history)
  • /trunk/android-client/src/org/mediawiki/android/client/ConfigurationPage.java (modified) (history)
  • /trunk/android-client/src/org/mediawiki/android/client/MainPage.java (modified) (history)
  • /trunk/android-client/src/org/mediawiki/android/client/SearchPage.java (modified) (history)
  • /trunk/android-client/src/org/mediawiki/android/client/ViewPage.java (modified) (history)

Diff [purge]

Index: trunk/android-client/android-client.apk
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: trunk/android-client/src/org/mediawiki/android/WikiPage.java
@@ -1,5 +1,6 @@
22 package org.mediawiki.android;
33
 4+// Imports
45 import java.util.ArrayList;
56 import java.util.Hashtable;
67 import org.json.JSONArray;
@@ -7,12 +8,15 @@
89 import org.json.JSONObject;
910 import org.mediawiki.android.WikiApi;
1011
 12+// Class to handle the concept of a page in a Mediawiki install
1113 public class WikiPage {
1214
 15+ // Have we loaded the page
1316 private boolean __loaded = false;
 17+
 18+ // Our private instance of the WikiApi
1419 private WikiApi __api;
1520
16 -
1721 // Aspects of the page
1822 protected int __revId;
1923 protected String __pageName;
@@ -24,6 +28,10 @@
2529 protected ArrayList<String> __templates;
2630 protected Hashtable<String,String> __interwikiLinks;
2731
 32+ /**
 33+ * Get the API
 34+ * @return WikiApi
 35+ */
2836 public WikiApi getApi() {
2937 return this.__api;
3038 }
@@ -85,7 +93,8 @@
8694 }
8795
8896 /**
89 - * Load a new page
 97+ * Load a new page from the current API
 98+ * @param String page Page title to load
9099 */
91100 public void loadPage( String page ) {
92101 if ( this.__pageName == page ) {
@@ -98,13 +107,17 @@
99108
100109 /**
101110 * Have we loaded the data on this page?
 111+ * @return boolean
102112 */
103113 protected boolean isLoaded() {
104114 return this.__loaded;
105115 }
106116
107117 /**
108 - *
 118+ * Load the page data. Functions needing data should call this,
 119+ * as it handles (potentially) reading data from cache instead
 120+ * of making remote requests.
 121+ * @return boolean
109122 */
110123 protected boolean loadData() {
111124 if ( this.isLoaded() ) {
@@ -119,11 +132,16 @@
120133 return this.__loaded;
121134 }
122135
 136+ /**
 137+ * Couldn't find it in the cache, so make some remote calls to get
 138+ * the information
 139+ * @return boolean
 140+ * @throws JSONException
 141+ */
123142 protected boolean loadDataFromRemote() throws JSONException {
124143 WikiApi api = this.getApi();
125144 api.newAction( "parse" );
126145 api.addParams( "prop", "revid|text|categories|externallinks|images|templates|links" );
127 - // api.addParams( "prop", "langlinks" );
128146 api.addParams( "page", this.__pageName );
129147 if ( api.doRequest() ) {
130148 JSONObject json = api.getJson().getJSONObject( "parse" );
Index: trunk/android-client/src/org/mediawiki/android/HttpRequest.java
@@ -1,5 +1,4 @@
22 package org.mediawiki.android;
3 -
43 // Imports
54 import java.io.IOException;
65 import java.net.SocketException;
@@ -14,14 +13,19 @@
1514 import org.apache.http.impl.client.DefaultHttpClient;
1615
1716 /**
18 - * Class definition. Pretty simple use to get a raw HTTP GET/POST
19 - * No caching, just a raw request.
 17+ * Pretty simple use to get a raw HTTP GET/POST. No caching, just a raw request
 18+ * Should remind MW devs of Http::get() and Http::post().
2019 */
2120 public class HttpRequest {
2221
2322 // Keep a single client
2423 private static HttpClient __client;
2524
 25+ /**
 26+ * Http GET request
 27+ * @param String url URL to request
 28+ * @return String
 29+ */
2630 public static String get( String url ) {
2731 URI uri = HttpRequest.makeUri( url );
2832 if ( uri == null ) {
@@ -39,7 +43,12 @@
4044 }
4145 return responseBody;
4246 }
43 -
 47+
 48+ /**
 49+ * Http POST request
 50+ * @param String url URL to request
 51+ * @return String
 52+ */
4453 public static String post( String url ) {
4554 URI uri = HttpRequest.makeUri( url );
4655 if ( uri == null ) {
@@ -61,6 +70,10 @@
6271 return responseBody;
6372 }
6473
 74+ /**
 75+ * Get the static instance of the HTTP client we keep around
 76+ * @return HttpClient
 77+ */
6578 private static HttpClient getClient() {
6679 if ( HttpRequest.__client == null ) {
6780 HttpRequest.__client = new DefaultHttpClient();
@@ -68,6 +81,11 @@
6982 return HttpRequest.__client;
7083 }
7184
 85+ /**
 86+ * Friendly wrapper around making a URI object
 87+ * @param String uri String URL to make into a URI object
 88+ * @return URI
 89+ */
7290 private static URI makeUri( String uri ) {
7391 try {
7492 return new URI( uri );
Index: trunk/android-client/src/org/mediawiki/android/WikiApi.java
@@ -1,27 +1,42 @@
22 package org.mediawiki.android;
33
 4+// Imports
45 import org.json.JSONException;
56 import org.json.JSONObject;
67 import java.net.URLEncoder;
78
 9+// Represents a single MW API somewhere in the universe
810 public class WikiApi {
911
 12+ // Always always always request JSON.
1013 public static final String FORMAT = "json";
 14+
 15+ // Info about the wiki
1116 private String __name;
1217 private String __urlBase;
 18+
 19+ // About our query
1320 private String __lastJsonResult = null;
1421 private String __action = "query"; // Default to action=query
1522 private String __params = "";
1623 private String __fullRequestUrl;
1724 private boolean __status = false;
1825 private boolean __post = false; // set to true to be a posted request
 26+
 27+ // We use a singleton() method, here's the instance
1928 private static WikiApi __instance = null;
2029
 30+ /**
 31+ * Protect the constructor
 32+ * @param apiName
 33+ * @param apiUrl
 34+ */
2135 protected WikiApi( String apiName, String apiUrl ) {
2236 this.__name = apiName;
2337 this.__urlBase = apiUrl;
2438 }
2539
 40+ /** Get singleton */
2641 public static WikiApi getSingleton() {
2742 if ( WikiApi.__instance == null ) {
2843 WikiApi.__instance = new WikiApi( "enwiki", "http://en.wikipedia.org/w/api.php" );
@@ -29,23 +44,42 @@
3045 return WikiApi.__instance;
3146 }
3247
 48+ /** Destroy singleton */
 49+ public void destroySingleton() {
 50+ WikiApi.__instance = null;
 51+ }
 52+
 53+ /** Set a new action and void the parameters */
3354 public void newAction( String action ) {
3455 this.__action = action;
3556 this.__params = "";
3657 }
3758
 59+ /** Get the name of this wiki */
3860 public String getName() {
3961 return this.__name;
4062 }
4163
 64+ /**
 65+ * Add some params to the request
 66+ * @param String key Name of the param (prop, etc)
 67+ * @param String value Value to set
 68+ */
4269 public void addParams( String key, String value ) {
4370 this.__params += "&" + key + "=" + URLEncoder.encode(value);
4471 }
4572
 73+ /** Should this be a post? */
4674 public void setPost( boolean p ) {
4775 this.__post = p;
4876 }
4977
 78+ /**
 79+ * Do the actual request. Note that we do some sanity checking
 80+ * here, so we don't make the same request two times in a row.
 81+ * @TODO Might want a way to allow that
 82+ * @return boolean
 83+ */
5084 public boolean doRequest() {
5185 if ( this.__fullRequestUrl == this.getFullRequestUrl() ) {
5286 return this.__status;
@@ -66,15 +100,27 @@
67101 return this.__status;
68102 }
69103
 104+ /**
 105+ * Construct the full query URL
 106+ * @return String
 107+ */
70108 public String getFullRequestUrl() {
71109 return this.__urlBase + "?action=" + this.__action +
72110 this.__params + "&format=" + WikiApi.FORMAT;
73111 }
74112
 113+ /**
 114+ * Return the last JSON results raw
 115+ * @return String
 116+ */
75117 public String getRawResult() {
76118 return this.__lastJsonResult;
77119 }
78120
 121+ /**
 122+ * Get the last JSON results in a pretty wrapper
 123+ * @return JSONObject
 124+ */
79125 public JSONObject getJson() {
80126 try {
81127 return new JSONObject( this.__lastJsonResult );
Index: trunk/android-client/src/org/mediawiki/android/client/MainPage.java
@@ -11,8 +11,10 @@
1212 import android.widget.EditText;
1313 import android.widget.Toast;
1414
 15+// Entry point into MW client
1516 public class MainPage extends Activity {
1617
 18+ // Menu constants. We keep HOME public so other places can use it
1719 public static final int HOME = 1;
1820 private static final int ABOUT = 2;
1921 private static final int CONFIG = 3;
@@ -34,8 +36,7 @@
3537 startActivity( i );
3638 }
3739 };
38 -
39 -
 40+
4041 /** Called when the activity is first created. */
4142 @Override
4243 public void onCreate(Bundle savedInstanceState) {
@@ -81,8 +82,7 @@
8283 }
8384
8485 /**
85 - * Simple helper function for making the "About" dialogue. Used
86 - * on every page, so we'll just keep it here.
 86+ * Simple helper function for making the "About" dialogue.
8787 */
8888 private void constructAboutDialogue() {
8989 String msg = getString(R.string.app_name) + "\nVersion " +
Index: trunk/android-client/src/org/mediawiki/android/client/SearchPage.java
@@ -1,15 +1,16 @@
22 package org.mediawiki.android.client;
33
 4+// Imports
45 import android.app.ListActivity;
56 import android.content.Intent;
67 import android.os.Bundle;
 8+import android.util.Log;
79 import android.view.Menu;
810 import android.view.MenuItem;
911 import android.view.View;
1012 import android.widget.ArrayAdapter;
1113 import android.widget.ListView;
1214 import android.widget.Toast;
13 -
1415 import java.util.ArrayList;
1516 import java.util.List;
1617 import org.json.JSONArray;
@@ -17,8 +18,14 @@
1819 import org.json.JSONObject;
1920 import org.mediawiki.android.WikiApi;
2021
 22+// Class definition for a search results page
2123 public class SearchPage extends ListActivity {
2224
 25+ /**
 26+ * Overriding the parent method. When the list item is clicked (in this
 27+ * case, a search term), load up the ViewPage and let it show us the page
 28+ * we've selected.
 29+ */
2330 @Override
2431 protected void onListItemClick(ListView l, View v, int position, long id) {
2532 String title = (String)l.getItemAtPosition( position );
@@ -35,11 +42,15 @@
3643 try {
3744 this.fillData( getIntent().getExtras().getString( "searchTerm" ) );
3845 } catch (JSONException e) {
39 - Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG );
40 - t.show();
 46+ Log.e( "JSON Error in Search", e.getMessage() );
4147 }
4248 }
4349
 50+ /**
 51+ * Search for a term and fill ListActivity with the results
 52+ * @param String searchTerm The term to look for in the API
 53+ * @throws JSONException
 54+ */
4455 private void fillData( String searchTerm ) throws JSONException {
4556 List<String> items = new ArrayList<String>();
4657 WikiApi api = WikiApi.getSingleton();
@@ -63,7 +74,7 @@
6475 }
6576
6677 /**
67 - * Setup our menus at the bottom
 78+ * Setup our menus at the bottom. Only linking to HOME here
6879 */
6980 @Override
7081 public boolean onCreateOptionsMenu( Menu menu ) {
Index: trunk/android-client/src/org/mediawiki/android/client/ConfigurationPage.java
@@ -1,8 +1,10 @@
22 package org.mediawiki.android.client;
33
 4+// Imports
45 import android.os.Bundle;
56 import android.preference.PreferenceActivity;
67
 8+// Simple dummy activity for our preferences. Android manages 99% of this for us
79 public class ConfigurationPage extends PreferenceActivity {
810
911 /** Called when the activity is first created. */
Index: trunk/android-client/src/org/mediawiki/android/client/ViewPage.java
@@ -1,8 +1,8 @@
22 package org.mediawiki.android.client;
33
 4+// Imports
45 import org.mediawiki.android.WikiApi;
56 import org.mediawiki.android.WikiPage;
6 -
77 import android.app.Activity;
88 import android.content.Intent;
99 import android.os.Bundle;
@@ -11,6 +11,7 @@
1212 import android.view.MenuItem;
1313 import android.widget.TextView;
1414
 15+// Base class definition for viewing pages
1516 public class ViewPage extends Activity {
1617
1718 protected static final int PAGELINKS = 2;
@@ -41,6 +42,7 @@
4243 view.setText( Html.fromHtml(html) );
4344 }
4445
 46+ /** Accessor method for setting our page title. */
4547 private void makePageTitle() {
4648 this.setTitle( this.getTitle() + " - " + this.pageTitle );
4749 }
Index: trunk/android-client/README
@@ -15,14 +15,13 @@
1616 * Makes request to API url entered, Returns some default information on page
1717 name provided.
1818
19 -TODO:
 19+TODO (in no real order):
2020 1) i18n - check Google's progress in native Android. Possible workarounds?
21 - 2) Document everything
22 - 3) Make the cache work
23 - 4) Multiple wiki sources in prefs
24 - 5) User authentication
25 - 6) Make data available as a ContentProvider
26 - 7) Make page views actually attractive and usable (not just POC)
 21+ 2) Make the cache work
 22+ 3) Multiple wiki sources in prefs
 23+ 4) User authentication
 24+ 5) Make data available as a ContentProvider
 25+ 6) Make page views actually attractive and usable (not just POC)
2726
2827 --
2928 This program is free software; you can redistribute it and/or modify

Status & tagging log