views:

58

answers:

2

I'm trying to upload an image (just a random picture for now) to my MediaWiki site, but I keep getting this error:

"Unrecognized value for parameter 'action': upload"

Here's what I did (site url and password changed):


Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import wikitools
>>> import poster
>>> wiki = wikitools.wiki.Wiki("mywikiurl/api.php")
>>> wiki.login(username="admin", password="mypassword")
True
>>> screenshotPage = wikitools.wikifile.File(wiki=wiki, title="screenshot")
>>> screenshotPage.upload(fileobj=open("/Users/jeff/Pictures/02273_magensbay_1280x800.jpg", "r"), ignorewarnings=True)
Traceback (most recent call last):
  File "", line 1, in 
  File "/Library/Python/2.6/site-packages/wikitools/wikifile.py", line 228, in upload
    res = req.query()
  File "/Library/Python/2.6/site-packages/wikitools/api.py", line 143, in query
    raise APIError(data['error']['code'], data['error']['info'])
wikitools.api.APIError: (u'unknown_action', u"Unrecognized value for parameter 'action': upload")
>>> 

From what I could find on google, the current MediaWiki doesn't support uploading files. But that's ridiculous... there must be some way, right?

I'm not married to the wikitools package—any way of doing it is appreciated.

EDIT: I set $wgEnableUploads = true in my LocalSettings.php, and I can upload files manually, just not through python.

EDIT: I think wikitools gets an edit token automatically. I've attached the upload method. Before it does the API request it calls self.getToken('edit'), which should take care of it I think? I'll play around with it a little though to see if manually adding that in fixes things.

    def upload(self, fileobj=None, comment='', url=None, ignorewarnings=False, watch=False):
        """Upload a file, requires the "poster" module

        fileobj - A file object opened for reading
        comment - The log comment, used as the inital page content if the file 
        doesn't already exist on the wiki
        url - A URL to upload the file from, if allowed on the wiki
        ignorewarnings - Ignore warnings about duplicate files, etc.
        watch - Add the page to your watchlist

        """
        if not api.canupload and fileobj:
            raise UploadError("The poster module is required for file uploading")
        if not fileobj and not url:
            raise UploadError("Must give either a file object or a URL")
        if fileobj and url:
            raise UploadError("Cannot give a file and a URL")
        params = {'action':'upload',
            'comment':comment,
            'filename':self.unprefixedtitle,
            'token':self.getToken('edit') # There's no specific "upload" token
        }
        if url:
            params['url'] = url
        else:
            params['file'] = fileobj
        if ignorewarnings:
            params['ignorewarnings'] = ''
        if watch:
            params['watch'] = ''
        req = api.APIRequest(self.site, params, write=True, multipart=bool(fileobj))
        res = req.query()
        if 'upload' in res and res['upload']['result'] == 'Success':
            self.wikitext = ''
            self.links = []
            self.templates = []
            self.exists = True
        return res

Also this is my first question so somebody let me know if you can't post other peoples' code or something. Thanks!

A: 

Maybe you have to "obtain a token" first?

To upload files, a token is required. This token is identical to the edit token and is the same regardless of target filename, but changes at every login. Unlike other tokens, it cannot be obtained directly, so one must obtain and use an edit token instead.

See here for details: http://www.mediawiki.org/wiki/API:Edit_-_Uploading_files

Adrian Archer
A: 

You need at least MediaWiki 1.16 (which is currently in begta) to be able to upload files via the API. Or you can try mwclient, which automatically falls back to uploading via Special:Upload if an older version of MediaWiki is used (with reduced functionality, such as no error handling etc.)

Bryan