Wikipedia.js
Go to the documentation of this file.
1 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
2 Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
3 Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
4 
5 const Cc = Components.classes;
6 const Ci = Components.interfaces;
7 const Cr = Components.results;
8 var JSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
9 
10 const DESCRIPTION = "mashTape Provider: Wikipedia Artist Info Provider";
11 const CID = "{a7780910-8c10-11dd-ad8b-0800200c9a66}";
12 const CONTRACTID = "@songbirdnest.com/mashTape/provider/info/WikipediaLocal;1";
13 
17 
18 // XPCOM constructor for our Artist Info mashTape provider
19 function ArtistInfo() {
20  this.wrappedJSObject = this;
21  Components.utils.import("resource://mashtape/mtUtils.jsm");
22 
23  if (typeof(language) == "undefined") {
24  // Get the user's locale
25  var prefBranch = Cc["@mozilla.org/preferences-service;1"]
26  .getService(Ci.nsIPrefService).getBranch("general.");
27  var locale = prefBranch.getCharPref("useragent.locale");
28  language = locale.split(/-/)[0];
29  mtUtils.log("Wikipedia", "language set to " + language);
30  }
31  if (typeof(homepage) == "undefined") {
32  // Initialise to English for now
33  homepage = "Homepage";
34  wikipedia = "Wikipedia";
35 
36  if (language == "en")
37  return;
38  var gtReq = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
39  .createInstance(Ci.nsIXMLHttpRequest);
40  var url = "http://ajax.googleapis.com/ajax/services/language/translate"
41  + "?v=1.0&q=Wikipedia%7CHomepage&langpair=en%7C" + language;
42  mtUtils.log("Wikipedia", "translate URL: " + url);
43  gtReq.open("GET", url, true);
44  gtReq.onreadystatechange = function(ev) {
45  if (this.readyState != 4)
46  return;
47  if (this.status != 200)
48  return;
49  var results = JSON.decode(this.responseText);
50  if (results.responseStatus == 200) {
51  var text = results.responseData.translatedText.split("|");
52  wikipedia = text[0].replace(/^[\s]*/, '').replace(/[\s]*$/, '');
53  homepage = text[1].replace(/^[\s]*/, '').replace(/[\s]*$/, '');
54  mtUtils.log("Wikipedia", "Homepage: " + homepage);
55  mtUtils.log("Wikipedia", "Wikipedia: " + wikipedia);
56  }
57  }
58  gtReq.send(null);
59  }
60 }
61 
62 ArtistInfo.prototype.constructor = ArtistInfo;
63 ArtistInfo.prototype = {
65  classID: Components.ID(CID),
67  QueryInterface: XPCOMUtils.generateQI([Ci.sbIMashTapeInfoProvider,
68  Ci.sbIMashTapeProvider]),
69 
70  providerName: "Wikipedia",
71  providerType: "info",
72  numSections: 4,
73  providerIconBio : "chrome://mashtape/content/tabs/wikipedia.png",
74  providerIconDiscography : "chrome://mashtape/content/tabs/wikipedia.png",
75  providerIconMembers : "chrome://mashtape/content/tabs/wikipedia.png",
76  providerIconTags : "chrome://mashtape/content/tabs/wikipedia.png",
77  providerIconLinks : "chrome://mashtape/content/tabs/wikipedia.png",
78 
79  query: function(artist, callback) {
80  // Initiate the request
81  var dbReq = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
82  .createInstance(Ci.nsIXMLHttpRequest);
83  /*
84  var url = "http://dbpedia.org:8890/sparql?" +
85  "default-graph-uri=http%3A%2F%2Fdbpedia.org" +
86  "&query=DESCRIBE+%3Chttp://dbpedia.org/resource/" +
87  artist.replace(/ /g, "_") + "%3E&output=xml";
88  */
89  var url = "http://dbpedia.org/data/" +
90  artist.replace(/ /g, "_") + ".rdf";
91  mtUtils.log("Wikipedia", "DBpedia URL:" + url);
92  var prefBranch = Cc["@mozilla.org/preferences-service;1"]
93  .getService(Ci.nsIPrefService).getBranch("extensions.mashTape.");
94  var autolocalise = prefBranch.getBoolPref("info.autolocalise");
95  if (!autolocalise)
96  language = "en";
97  dbReq.open("GET", url, true);
98  dbReq.onreadystatechange = function(ev) {
99  return function(updateFn) {
100  if (dbReq.readyState != 4)
101  return;
102  if (dbReq.status != 200)
103  return;
104  var xmlText = dbReq.responseText.replace(
105  /<\?xml version="1.0" encoding="utf-8" \?>/,
106  "");
107  var x = new XML(xmlText);
108 
109  // Set our E4X namespaces
110  var dbNs = new Namespace("http://dbpedia.org/property/");
111  var foafNs = new Namespace("http://xmlns.com/foaf/0.1/");
112  var rdfNs = new Namespace(
113  "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
114  var xmlNs = new Namespace(
115  "http://www.w3.org/XML/1998/namespace");
116 
117  // Links
118  var links = new Object;
119  var wikiUrl;
120  links.provider = "Wikipedia";
121  links.links = new Array();
122  if (x..foafNs::page.length() >= 1) {
123  wikiUrl = x..foafNs::page[0].@rdfNs::resource.toString();
124  wikiUrl = wikiUrl.replace("en.wikipedia.org",
125  language+".wikipedia.org");
126  links.links.push({
127  name: wikipedia,
128  url: wikiUrl
129  });
130  }
131  links.url = wikiUrl;
132 
133  /* bug 18865 - commenting this out for now since the schema changed
134  var wikiUrl = null;
135  var enUrl = null;
136  for each (var resource in x..rdfNs::Description) {
137  var child = resource.child(0);
138  if (child.localName() == ("wikipage-" + language)) {
139  wikiUrl = decodeURI(child.@rdfNs::resource);
140  links.links.push({
141  name: wikipedia,
142  url: wikiUrl
143  });
144  } else if (child.localName() == ("page")) {
145  enUrl = decodeURI(child.@rdfNs::resource);
146  links.links.push({
147  name: wikipedia,
148  url: enUrl
149  });
150  }
151  }
152  if (wikiUrl == null)
153  wikiUrl = enUrl;
154  links.url = wikiUrl;
155  */
156 
157  var bio = new Object;
158  var bioText = null;
159  // Artist bio
160  var englishBio = null;
161  for each (var bio in x..dbNs::abstract) {
162  var thisLang = bio.@xmlNs::lang.toString();
163  mtUtils.log("Wikipedia", "Language scan: " + thisLang);
164  if (thisLang == language)
165  bioText = bio.toString();
166  if (thisLang == "en")
167  englishBio = bio.toString();
168  }
169  if (bioText == null)
170  bioText = englishBio;
171  if (bioText == null)
172  updateFn.wrappedJSObject.update(CONTRACTID, null, "bio");
173  else {
174  bio.provider = "Wikipedia";
175  bio.bioText = bioText;
176  bio.bioUrl = wikiUrl;
177  bio.bioEditUrl = wikiUrl;
178  updateFn.wrappedJSObject.update(CONTRACTID, bio, "bio");
179  }
180 
181  // Members
182  var members = new Object;
183  members.provider = "Wikipedia";
184  members.url = wikiUrl;
185  members.members = new Array();
186  if (x..dbNs::currentMembers.length() == 1 &&
187  x..dbNs::currentMembers.@rdfNs::resource.toString() == "")
188  {
189  var memberStr = x..dbNs::currentMembers.toString();
190  members.members = memberStr.split(/\n/);
191  } else {
192  for each (var memberName in x..dbNs::currentMembers) {
193  var name = memberName.@rdfNs::resource.toString();
194  var uri = name.split(/\//);
195  name = unescape(uri[uri.length-1].replace(/_/g, " "));
196  members.members.push(name);
197  }
198  }
199 
200 
201  var image = x..dbNs::img.@rdfNs::resource.toString();
202  if (image == "")
203  updateFn.wrappedJSObject.update(CONTRACTID, null, "photo");
204  else {
205  updateFn.wrappedJSObject.update(CONTRACTID, image, "photo");
206  }
207 
208  if (members.members.length > 0)
209  updateFn.wrappedJSObject.update(CONTRACTID, members,
210  "members");
211  else
212  updateFn.wrappedJSObject.update(CONTRACTID, null,
213  "members");
214 
215  if (links.links.length > 0)
216  updateFn.wrappedJSObject.update(CONTRACTID, links, "links");
217  else
218  updateFn.wrappedJSObject.update(CONTRACTID, null, "links");
219  }(callback);
220  }
221  dbReq.send(null);
222  },
223 }
224 
225 var components = [ArtistInfo];
226 function NSGetModule(compMgr, fileSpec) {
227  return XPCOMUtils.generateModule([ArtistInfo]);
228 }
229 
const Cc
Definition: Wikipedia.js:5
var _
const CONTRACTID
Definition: Wikipedia.js:12
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
sbOSDControlService prototype QueryInterface
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
version(170)
var homepage
Definition: Wikipedia.js:15
function NSGetModule(compMgr, fileSpec)
Definition: Wikipedia.js:226
function ArtistInfo()
Definition: Wikipedia.js:19
var getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('Songbird SBProperties artist
Definition: tuner2.js:40
var wikipedia
Definition: Wikipedia.js:16
const Cr
Definition: Wikipedia.js:7
grep callback
const Ci
Definition: Wikipedia.js:6
const CID
Definition: Wikipedia.js:11
return null
Definition: FeedWriter.js:1143
var uri
Definition: FeedWriter.js:1135
function url(spec)
var language
Definition: Wikipedia.js:14
sbDeviceFirmwareAutoCheckForUpdate prototype classID
const DESCRIPTION
Definition: Wikipedia.js:10
var JSON
Definition: Wikipedia.js:8