Skip to content
This repository

modify semantic_query for SMW Core (v1.7) Ask API #36

Merged
merged 1 commit into from almost 2 years ago

2 participants

mtyeh411 Jani Patokallio
mtyeh411

As of Semantic Mediawiki 1.7 semantic queries can be run using the MediaWiki API. I updated the 'semantic_query' method to take advantage of this, falling back on the previous implementation of 'semantic_query' for unsupported Semantic MediaWiki versions.

Documentation link

Jani Patokallio jpatokal merged commit 7a1f8d4 into from June 27, 2012
Jani Patokallio jpatokal closed this June 27, 2012
Jani Patokallio
Owner

Brilliant, thank you!

mtyeh411

no problem. great work with this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 27, 2012
modify semantic_query for SMW Core (v1.7) Ask API 7a0b868
This page is out of date. Refresh to see the latest.
19  lib/media_wiki/gateway.rb
@@ -557,10 +557,21 @@ def extensions
557 557
     #
558 558
     # Returns result as an HTML string
559 559
     def semantic_query(query, params = [])
560  
-      params << "format=list"
561  
-      form_data = { 'action' => 'parse', 'prop' => 'text', 'text' => "{{#ask:#{query}|#{params.join('|')}}}" }
562  
-      xml, dummy = make_api_request(form_data)
563  
-      return xml.elements["parse/text"].text
  560
+			if extensions.include? 'Semantic MediaWiki'
  561
+				smw_version = extensions['Semantic MediaWiki'].to_f
  562
+				if smw_version >= 1.7
  563
+					form_data = { 'action' => 'ask', 'query' => "#{query}|#{params.join('|')}"}	
  564
+					xml, dummy = make_api_request(form_data)
  565
+					return xml
  566
+				else
  567
+					params << "format=list"
  568
+					form_data = { 'action' => 'parse', 'prop' => 'text', 'text' => "{{#ask:#{query}|#{params.join('|')}}}" }
  569
+					xml, dummy = make_api_request(form_data)
  570
+					return xml.elements["parse/text"].text
  571
+				end
  572
+			else
  573
+				raise MediaWiki::Exception.new "Semantic MediaWiki extension not installed."
  574
+			end
564 575
     end
565 576
 
566 577
     # Set groups for a user
17  script/semantic_query.rb
... ...
@@ -0,0 +1,17 @@
  1
+#!/usr/bin/env ruby
  2
+#
  3
+# Sample script for querying Semantic MediaWiki data
  4
+#
  5
+
  6
+require './lib/media_wiki'
  7
+
  8
+mw = MediaWiki::Gateway.new(ARGV[0])
  9
+
  10
+params = []
  11
+i = 2
  12
+until i == ARGV.length
  13
+	params << ARGV[i]
  14
+	i += 1
  15
+end
  16
+
  17
+puts mw.semantic_query(ARGV[1], params)
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.