WikipediaStore.js 3.7 KB

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