summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/res/smw/api/ext.smw.api.js
blob: e87dd505c3124d0ee60c8056cd9fdff6cef36e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * This file is part of the Semantic MediaWiki Extension
 * @see https://semantic-mediawiki.org/
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * @file
 * @ignore
 *
 * @since 1.9
 * @ingroup SMW
 *
 * @licence GNU GPL v2+
 * @author mwjames
 */
( function( $, mw, smw ) {
	'use strict';

	/*global md5 */

	/**
	 * Constructor to create an object to interact with the Semantic
	 * MediaWiki Api
	 *
	 * @since 1.9
	 *
	 * @class
	 * @alias smw.api
	 * @constructor
	 */
	smw.Api = function() {};

	/* Public methods */

	smw.Api.prototype = {

		/**
		 * Convenience method to parse and map a JSON string
		 *
		 * Emulates partly $.parseJSON (jquery.js)
		 * (see https://web.archive.org/web/20170101125747/http://www.json.org/js.html)
		 * @since  1.9
		 *
		 * @param {string} data
		 *
		 * @return {object|null}
		 */
		parse: function( data ) {

			// Use smw.Api JSON custom parser to resolve raw data and add
			// type hinting
			var smwData = new smw.Data();

			if ( !data || typeof data !== 'string' ) {
				return null;
			}

			// Remove leading/trailing whitespace
			data = $.trim(data);

			// Attempt to parse using the native JSON parser first
			if ( window.JSON && window.JSON.parse ) {
				return JSON.parse( data, function ( key, value ) { return smwData.factory( key, value ); } );
			}

			// If the above fails, use jquery to do the rest
			return $.parseJSON( data );
		},

		/**
		 * Returns results from the SMWApi
		 *
		 * On the topic of converters (see http://bugs.jquery.com/ticket/9095)
		 *
		 * Example:
		 *         var smwApi = new smw.Api();
		 *         smwApi.fetch( query )
		 *           .done( function ( data ) { } )
		 *           .fail( function ( error ) { } );
		 *
		 * @since 1.9
		 *
		 * @param {string} queryString
		 * @param {boolean|number} useCache
		 *
		 * @return {jQuery.Promise}
		 */
		fetch: function( queryString, useCache ){
			var self = this,
				apiDeferred = $.Deferred();

			if ( !queryString || typeof queryString !== 'string' ) {
				throw new Error( 'Invalid query string: ' + queryString );
			}

			// Look for a cache object otherwise do an Ajax call
			if ( useCache ) {

				// Use a hash key to compare queries and use it as identifier for
				// stored resultObjects, each change in the queryString will result
				// in another hash key which will ensure only objects are stored
				// with this key can be reused
				var hash = md5( queryString );

				var resultObject = $.jStorage.get( hash );
				if ( resultObject !== null ) {
					var results = self.parse( resultObject );
					results.isCached = true;
					apiDeferred.resolve( results );
					return apiDeferred.promise();
				}
			}

			return $.ajax( {
				url: mw.util.wikiScript( 'api' ),
				dataType: 'json',
				data: {
					'action': 'ask',
					'format': 'json',
					'query' : queryString
					},
				converters: { 'text json': function ( data ) {
					// Store only the string as we want to return a typed object
					// If useCache is not a number use 15 min as default ttl
					if ( useCache ){
						$.jStorage.set( hash, data, {
							TTL: $.type( useCache ) === 'number' ? useCache : 900000
						} );
					}

					var results;

					try {
						results = self.parse( data );
					} catch ( e ) {
						console.log( e );
						throw e;
					}

					results.isCached = false;
					return results;
				} }
			} );
		}
	};

	//Alias
	smw.api = smw.Api;

} )( jQuery, mediaWiki, semanticMediaWiki );