Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e6aca3
commit 2268a77
Showing
4 changed files
with
167 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
''' | ||
Created on 25.05.2020 | ||
@author: wf | ||
''' | ||
import unittest | ||
import getpass | ||
from wikibot.wikibot import WikiBot | ||
from wikibot.smw import SMW | ||
|
||
class TestSMW(unittest.TestCase): | ||
""" test access to SemanticMediaWiki API""" | ||
debug=True | ||
testask1="""{{#ask: [[Concept:Semantic MediaWiki Cons 2012]] | ||
|?Has_Wikidata_item_ID = WikiDataId | ||
|?Has planned finish = finish | ||
|?Has planned start = start | ||
|?Has_location = location | ||
| format=table }}""" | ||
|
||
def testFixAsk(self): | ||
smw=SMW() | ||
fixedAsk=smw.fixAsk(TestSMW.testask1) | ||
expected="""[[Concept:Semantic_MediaWiki_Cons_2012]]|?Has_Wikidata_item_ID=WikiDataId|?Has_planned_finish=finish|?Has_planned_start=start|?Has_location=location|format=table""" | ||
if TestSMW.debug: | ||
print(fixedAsk) | ||
self.assertEqual(expected,fixedAsk) | ||
|
||
def testSMWInfo(self): | ||
if not getpass.getuser()=="travis": | ||
''' | ||
needs: | ||
.mediawiki-japi/smw.ini as: | ||
#Mediawiki JAPI credentials for semantic-mediawiki | ||
#manually entered 2020-05-39 | ||
url=https://www.semantic-mediawiki.org | ||
scriptPath=/w | ||
wikiId=smw | ||
version=MediaWiki 1.31.7 | ||
''' | ||
wikibot=WikiBot.ofWikiId("smw") | ||
smw=SMW(wikibot.site) | ||
result=smw.info() | ||
if (TestSMW.debug): | ||
print (result) | ||
self.assertTrue('info' in result) | ||
info=result['info'] | ||
expectedlist=['propcount','usedpropcount','declaredpropcount'] | ||
for expected in expectedlist: | ||
self.assertTrue(expected in info) | ||
|
||
|
||
def testSMWAskRaw(self): | ||
if not getpass.getuser()=="travis": | ||
wikibot=WikiBot.ofWikiId("smw") | ||
smw=SMW(wikibot.site) | ||
result=smw.rawquery(TestSMW.testask1) | ||
if TestSMW.debug: | ||
print (result) | ||
self.assertTrue('query' in result) | ||
query=result['query'] | ||
self.assertTrue('printrequests' in query) | ||
|
||
if __name__ == "__main__": | ||
#import sys;sys.argv = ['', 'Test.testSMWApi'] | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
''' | ||
Created on 29.05.2020 | ||
@author: wf | ||
''' | ||
from pywikibot.data.api import Request | ||
import re | ||
|
||
class SMW(object): | ||
''' | ||
Semantic MediaWiki Access e.g. for ask API | ||
see | ||
* https://www.semantic-mediawiki.org/wiki/Help:API | ||
* https://www.semantic-mediawiki.org/wiki/Serialization_(JSON) | ||
adapted from Java SimpleGraph Module | ||
https://github.com/BITPlan/com.bitplan.simplegraph/blob/master/simplegraph-smw/src/main/java/com/bitplan/simplegraph/smw/SmwSystem.java | ||
''' | ||
|
||
def __init__(self, site=None): | ||
''' | ||
Constructor | ||
''' | ||
self.site=site | ||
|
||
def submit(self, parameters): | ||
request=Request(site=self.site,parameters=parameters) | ||
return request.submit() | ||
|
||
def info(self): | ||
parameters={"action": "smwinfo"} | ||
return self.submit(parameters) | ||
|
||
def rawquery(self,ask): | ||
""" send a query """ | ||
# allow usage of original Wiki ask content - strip all non needed parts | ||
fixedAsk=self.fixAsk(ask) | ||
# set parameters for request | ||
parameters={"action": "ask","query":fixedAsk} | ||
result=self.submit(parameters) | ||
return result | ||
|
||
def query(self,ask): | ||
return self.rawquery(ask) | ||
|
||
def fixAsk(self,ask): | ||
""" fix an ask String to be usable for the API | ||
@param ask | ||
- a "normal" ask query | ||
@return - the fixed asked query | ||
""" | ||
# ^\\s*\\{\\{ | ||
# remove {{ with surrounding white space at beginning | ||
fixedAsk = re.sub(r"^\s*\{\{", "",ask) | ||
# remove #ask: | ||
fixedAsk = re.sub(r"#ask:", "",fixedAsk) | ||
# remove }} with surrounding white space at end | ||
fixedAsk = re.sub(r"\}\}\s*$", "",fixedAsk) | ||
# split by lines (with side effect to remove newlines) | ||
parts = fixedAsk.split(r"\n") | ||
fixedAsk = "" | ||
for part in parts: | ||
# remove whitespace around part | ||
part = part.strip(); | ||
# remove whitespace around pipe sign | ||
part = re.sub(r"\s*\|\s*", "|",part); | ||
# remove whitespace around assignment = | ||
part = re.sub(r"\s*=\s*", "=",part); | ||
# remove whitespace in query parts | ||
part = re.sub(r"\]\s*\[", "][",part); | ||
# replace blanks with _ | ||
part = re.sub(" ", "_",part); | ||
fixedAsk = fixedAsk+ part; | ||
return fixedAsk | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters