Skip to content

Commit

Permalink
adds initial steps for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed May 29, 2020
1 parent 4e6aca3 commit 2268a77
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 13 deletions.
66 changes: 66 additions & 0 deletions tests/test_SMWApi.py
@@ -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()
4 changes: 4 additions & 0 deletions tests/test_wikibot.py
Expand Up @@ -37,6 +37,10 @@ def testCrypt(self):
self.assertEquals(secret,secret1)
pw=c.decrypt(secret)
self.assertEquals(expected,pw)

def testWikiBotNoLogin(self):
wikibot=WikiBot.ofWikiId("smw")


def testWikiBot(self):
'''
Expand Down
76 changes: 76 additions & 0 deletions wikibot/smw.py
@@ -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


34 changes: 21 additions & 13 deletions wikibot/wikibot.py
Expand Up @@ -75,21 +75,26 @@ def __init__(self,iniFile):
else:
raise Exception("wikiId missing for %s" % iniFile)
self.family=self.wikiId.replace("-","").replace("_","")
self.user=config['user']
self.url=config['url'].replace("\\:",":")
if not self.url:
raise Exception("url is missing for %s" % iniFile)

self.email=config['email']
self.salt=config['salt']
self.cypher=config['cypher']
self.secret=config['secret']

self.scriptPath=config['scriptPath']
self.version=config['version']
o=urlparse(self.url)
self.scheme=o.scheme
self.netloc=o.netloc
self.scriptPath=o.path+self.scriptPath

# if a user is configured a login will be tried
if 'user' in config:
self.user=config['user']
self.email=config['email']
self.salt=config['salt']
self.cypher=config['cypher']
self.secret=config['secret']
else:
self.user=None
self.checkFamily()

def getPassword(self):
Expand All @@ -115,7 +120,7 @@ def scriptpath(self, code):
return '%s'
def isPublic(self):
return False
return %s
def version(self, code):
return "%s" # The MediaWiki version used. Very important in most cases. (contrary to documentation)
Expand All @@ -124,17 +129,20 @@ def protocol(self, code):
return '%s'
'''
mw_version=self.version.lower().replace("mediawiki ","")
code=template % (self.family,self.netloc,self.scriptPath,mw_version,self.scheme)
ispublic='False' if self.user is not None else 'True'
code=template % (self.family,self.netloc,self.scriptPath,ispublic,mw_version,self.scheme)
with open(famfile,"w") as py_file:
py_file.write(code)
config2.register_family_file(self.family, famfile)
config2.usernames[self.family]['en'] = self.user
if self.user:
config2.usernames[self.family]['en'] = self.user
#config2.authenticate[self.netloc] = (self.user,self.getPassword())
self.site=pywikibot.Site('en',self.family)
# needs patch as outlined in https://phabricator.wikimedia.org/T248471
#self.site.login(password=self.getPassword())
lm = LoginManager(password=self.getPassword(), site=self.site, user=self.user)
lm.login()
if self.user:
# needs patch as outlined in https://phabricator.wikimedia.org/T248471
#self.site.login(password=self.getPassword())
lm = LoginManager(password=self.getPassword(), site=self.site, user=self.user)
lm.login()

def getPage(self,pageTitle):
''' get the page with the given title'''
Expand Down

0 comments on commit 2268a77

Please sign in to comment.