WikipediaStore.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. define("dojox/data/WikipediaStore", ["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/declare", "dojo/io/script",
  2. "dojo/io-query", "dojox/rpc/Service", "dojox/data/ServiceStore"],
  3. function(kernel, lang, declare, scriptIO, ioQuery, Service, ServiceStore) {
  4. kernel.experimental("dojox.data.WikipediaStore");
  5. /*===== var ServiceStore = dojox.data.ServiceStore; =====*/
  6. return declare("dojox.data.WikipediaStore", ServiceStore, {
  7. // summary:
  8. // Initializer for the Wikipedia data store interface.
  9. // description:
  10. // The WikipediaStore is a data store interface to Wikipedia, using the
  11. // Wikipedia SMD spec from dojox.rpc. It currently is useful only for
  12. // finding articles that contain some particular text or grabbing single
  13. // articles by full name; no wildcards or other filtering are supported.
  14. // example:
  15. // | var store = new dojox.data.WikipediaStore();
  16. // | store.fetch({
  17. // | query: {title:"Dojo Toolkit"},
  18. // | onItem: function(item){
  19. // | dojo.byId("somediv").innerHTML = item.text["*"];
  20. // | }
  21. // | });
  22. constructor: function(options){
  23. if(options && options.service){
  24. this.service = options.service;
  25. }else{
  26. var svc = new Service(require.toUrl("dojox/rpc/SMDLibrary/wikipedia.smd"));
  27. this.service = svc.query;
  28. }
  29. this.idAttribute = this.labelAttribute = "title";
  30. },
  31. fetch: function(/* object */ request){
  32. // summary:
  33. // Fetch a page or some partially-loaded search results from
  34. // Wikipedia. Note that there isn't a way to sort data coming
  35. // in from the API, so we just ignore the *sort* parameter.
  36. // example:
  37. // Loading a page:
  38. // | store.fetch({
  39. // | query: {title:"Dojo Toolkit"},
  40. // | // define your handlers here
  41. // | });
  42. // example:
  43. // Searching for pages containing "dojo":
  44. // | store.fetch({
  45. // | query: {
  46. // | action: "query",
  47. // | text: "dojo"
  48. // | },
  49. // | // define your handlers here
  50. // | });
  51. // example:
  52. // Searching for the next 50 pages containing "dojo":
  53. // | store.fetch({
  54. // | query: {
  55. // | action: "query",
  56. // | text: "dojo",
  57. // | start: 10,
  58. // | count: 50 // max 500; will be capped if necessary
  59. // | },
  60. // | // define your handlers here
  61. // | });
  62. var rq = lang.mixin({}, request.query);
  63. if(rq && (!rq.action || rq.action === "parse")){
  64. // default to a single page fetch
  65. rq.action = "parse";
  66. rq.page = rq.title;
  67. delete rq.title;
  68. }else if(rq.action === "query"){
  69. // perform a full text search on page content
  70. rq.list = "search";
  71. rq.srwhat = "text";
  72. rq.srsearch = rq.text;
  73. if(request.start){
  74. rq.sroffset = request.start-1;
  75. }
  76. if(request.count){
  77. rq.srlimit = request.count >= 500 ? 500 : request.count;
  78. }
  79. delete rq.text;
  80. }
  81. request.query = rq;
  82. return this.inherited(arguments);
  83. },
  84. _processResults: function(results, def){
  85. if(results.parse){
  86. // loading a complete page
  87. results.parse.title = ioQuery.queryToObject(def.ioArgs.url.split("?")[1]).page;
  88. results = [results.parse];
  89. }else if(results.query && results.query.search){
  90. // loading some search results; all we have here is page titles,
  91. // so we mark our items as incomplete
  92. results = results.query.search;
  93. var _thisStore = this;
  94. for(var i in results){
  95. results[i]._loadObject = function(callback){
  96. _thisStore.fetch({
  97. query: { action:"parse", title:this.title },
  98. onItem: callback
  99. });
  100. delete this._loadObject;
  101. }
  102. }
  103. }
  104. return this.inherited(arguments);
  105. }
  106. });
  107. });