summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/resources/ext.srf.api.query.js
blob: 17a4fdbb95d28982927998263db118d0ef50b722 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
 * SRF JavaScript for the api/query
 *
 * @since 1.9
 * @release 0.1
 *
 * @file
 * @ingroup SRF
 *
 * @licence GNU GPL v2 or later
 * @author mwjames
 */
( function( $, mw, srf ) {
 'use strict';

	/**
	 * Private methods and objects used within the class
	 *
	 * @since  1.9
	 */
	var results = new srf.api.results();

	/**
	 * Public API namespace declaration
	 *
	 * @since 1.9
	 * @type Object
	 */
	srf.api = srf.api || {};

	/**
	 * Base constructor for objects representing a api.query instance
	 *
	 * @since 1.9
	 */
	srf.api.query = function() {};

	/**
	 * Public methods to access information via the SMWAPI
	 *
	 * @since 1.9
	 * @type Object
	 */
	srf.api.query.prototype = {

		conditions: {

			/**
			 * Builds a conditional statement
			 *
			 * For example
			 * operators ( ::, ::>, ::< etc.)
			 *
			 * @since 1.9
			 * @type Object
			 *
			 * @return array
			 */
			build: function ( property, value, operator ){
				return '[[' + property + ( operator || '::' ) + value + ']]';
			}
		},

		printouts: {

			/**
			 * Normalize printouts in order to get access via key reference
			 *
			 * e.g. |?Has location=location	will be transformed into an
			 * array ["Has location", "location"]
			 *
			 * @since 1.9
			 * @type Object
			 *
			 * @return array
			 */
			toList: function( printouts ){
				var list = [];
				var identifier = new RegExp( '[\\?&]' + '(.*?)' + '[=]' );

				$.each( printouts, function( value, text ) {
					// Split the text and find anything that is between ? and = otherwise
					// split the string just after ?
					var match = text.split( identifier );
					if( match !== null ){
						if ( match[0] === '' ){
							// match ["", "Has lcoation", "location"]
							list.push( [ match[1], match[2]] );
						}else{
							list.push( match[0].split( '?' )[1] );
						}
					}
				} );
				return list;
			},

			/**
			 * Find printout matches
			 *
			 * @since 1.9
			 * @type Object
			 */
			search: {

				/**
				 * Find printout that matches a specific identifier
				 *
				 * e.g. |?Has location=location
				 *
				 * search.identifier( [...], 'location' ) will result in "Has location"
				 *
				 * @since 1.9
				 * @type Object
				 * @type Object
				 *
				 * @return array
				 */
				identifier: function( printouts, identifier ){
					var property = '';
					var regexS = '[\\?&]' + '(.*?)' + '=' + identifier;
					var regex = new RegExp(regexS);

					$.each( printouts , function( key, value ) {
						if( value.match( regex ) !== null ){
							property = value.match( regex )[1];
						}
					} );
					return property;
				},

				/**
				 * Returns properties for a specific type where properties
				 * aren't marked with an identifier ( |?property=indentifier)
				 *
				 * SMWQUERY printouts, SMWAPI printrequests, ["_str","_txt"]
				 *
				 * For example
				 * type( printouts, printrequests, ["_str"] )
				 * result in ["Has location", "..."] that matches the type _str
				 *
				 * Filter all printout properties that are of type [...] and check against
				 * the printout list to indentify which of these printouts do not
				 * carry an additional identifier because those are not eligible
				 * to beused as filter properties
				 *
				 * @since 1.9
				 * @type Object
				 * @type Object
				 * @type Object
				 *
				 * @return array
				 */
				type: function( printouts, printrequests, dataTypes ){
					// Cache printouts, printrequests
					var	po = srf.api.query.prototype.printouts.toList( printouts ),
						pr = srf.api.results.prototype.printrequests();
						pr.toArray( printrequests );

					// Normalize printouts to an amendable structure
					function normalize(){
						var matches = [];
						// Match printouts against the list of available printrequests
						$.each( po, function( index, property ) {
							if ( typeof property === 'object' ) {
								if ( $.inArray( pr.getTypeId( property[1] ), dataTypes ) > -1 ){
									matches.push( property[0] );
								}
							} else {
								if ( $.inArray( pr.getTypeId( property ), dataTypes ) > -1 ){
									matches.push( property );
								}
							}
						} );
						return matches;
					}

					// Find those properties that are without an identifier
					function withoutIdentifier( list ){
						var matches = [];
						$.each( list, function( index, property ) {
							if ( $.inArray( property, po ) > -1 ) {
								matches.push( property );
							}
						} );
						return matches;
					}

					var record = withoutIdentifier( normalize() );
					return record.length > 0 ? srf.api.util.prototype.array.unique( record ) : '';
				}
			}
		},

		/**
		 * Returns a concatenated query string
		 *
		 * @since 1.9
		 * @type Object
		 *
		 * @return string
		 */
		toString: function( options ){

			var printouts = '';
			if ( options.printouts ){
				$.each( options.printouts , function( key, value ) {
					printouts = printouts + '|' + value;
				} );
			}

			var parameters = '';
			$.each( options.parameters , function( key, value ) {
				parameters = parameters + '|' + key + '=' + value;
			} );

			var conditions = '';
			if ( typeof options.conditions === 'object' ) {
				$.each( options.conditions , function( key, value ) {
					conditions = conditions + value;
				} );
			} else {
				conditions = options.conditions;
			}

			return  conditions + printouts + parameters;
		},

		/**
		 * Return results from the SMWAPI interface as callback
		 *
		 * @since 1.9
		 * @type Object
		 *
		 * @return array
		 */
		fetch: function( query, callback, log ){

			// Log data is only visible while in debug mode( &debug=true )
			if ( log ){
				var startDate = new Date();
				srf.log( 'Query: ' + query );
			}

			$.ajax( {
				url: mw.util.wikiScript( 'api' ),
				dataType: 'json',
				data: {
					'action': 'ask',
					'format': 'json',
					'query' : query
					}
			} )
			.done( function ( data ) {
				if ( log ){
					srf.log( 'Hash: ' + data.query.meta.hash );
					srf.log( 'Fetched: ' + ( new Date().getTime() - startDate.getTime() ) + ' ms ' + '( ' + data.query.meta.count + ' object )' );
				}

				// Return data to the callback
				if ( typeof callback === 'function' ) {
					callback.call( this, true, data );
				}
				return;
			} )
			.fail( function ( error ) {
				if ( typeof callback === 'function' ) {
					callback.call( this, false, error );
				}
				return;
			} );
		}
	};

} )( jQuery, mediaWiki, semanticFormats );