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