Page MenuHomePhabricator

How to use pywikibot to get data from SMW ask API?
Open, Needs TriagePublic

Description

The SemanticMediaWiki extension has it's own Mediawiki API extensions see

https://www.semantic-mediawiki.org/wiki/Help:API
I'd like to use pywikbot to use these API functions and created the following pyunit test:

api=APIGenerator("ask",query="[[Topic name::Fixme]]",site=site)
           for item in api:
               print (item)

When i try this i get:

WARNING: API warning (main): Unrecognized parameter: 'continue'

Event Timeline

Restricted Application added subscribers: pywikibot-bugs-list, Aklapper. · View Herald TranscriptMay 29 2020, 12:46 PM
Seppl2013 updated the task description. (Show Details)May 29 2020, 12:47 PM
Xqt added a subscriber: Xqt.EditedMay 29 2020, 2:00 PM

I see. SMW does not have the MW api continue system but an offset parameter which must be given to the query string delimited by "|" i guess. The returned Offset is not response['continue']['continue'] but there is query-continue-offset key insid the response dict. Look like we need either a meta parameter for SMW api or a separate Class to handle this.

@Xqt - thanks for looking into this. I am currently adding this to https://github.com/WolfgangFahl/py-3rdparty-mediawiki/issues/1 with https://github.com/WolfgangFahl/py-3rdparty-mediawiki/blob/master/wikibot/smw.py where I am happily using a standard Request for the time being to simplify things. It seems that APIGenerator adds caching handling and some legacy stuff as well as continuation - for the time being i'd focus more on the core functionality of unpacking the printrequest structures delivered and being able to use cut&paste for ask queries. The implementation will be mostly along the lines of https://github.com/BITPlan/com.bitplan.simplegraph/blob/master/simplegraph-smw/src/main/java/com/bitplan/simplegraph/smw/SmwSystem.java where this was already succesfully implemented in Java.

Seppl2013 added a comment.EditedJun 5 2020, 7:29 AM

The https://pypi.org/project/py-3rdparty-mediawiki/ SMW support now works quite nicely to handle the SMW API. This task can IMHO be closed.

Dvorapa added a subscriber: Dvorapa.Jun 5 2020, 8:51 AM

Feel free to share how you resolved the issue

Seppl2013 added a comment.EditedJun 5 2020, 1:13 PM

https://github.com/WolfgangFahl/py-3rdparty-mediawiki/blob/master/wikibot/smw.py#L137

from pywikibot.data.api import Request

...
def submit(self, parameters):
        """ submit the request with the given parameters
        Args:
            parameters(list): the parameters to use for the SMW API request
        Returns:
            dict: the submit result"""
        request=Request(site=self.site,parameters=parameters)
        return request.submit()    

def rawquery(self,ask):
        """ send a query see https://www.semantic-mediawiki.org/wiki/Help:Inline_queries#Parser_function_.23ask
        Args:
            ask(str): the SMW ASK query as it would be used in MediaWiki markup"""
        # 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

from there it was all "downhill" since the deserialization code is similar to the Java solution i already had done and this is Semantic Media Wiki specific.