~clinton-collins/familyproject/trunk

« back to all changes in this revision

Viewing changes to ZendFramework/externals/dojo/dojox/data/WikipediaStore.js

  • Committer: Clinton Collins
  • Date: 2009-06-26 19:54:58 UTC
  • Revision ID: clinton.collins@gmail.com-20090626195458-5ebba0qcvo15xlpy
Initial Import

Show diffs side-by-side

added added

removed removed

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