2010-07-05 15 views
2

我想要的圖像(只是現在隨機圖片)上傳到我的MediaWiki站點,但我不斷收到此錯誤:如何使用Python將文件上傳到MediaWiki?

"Unrecognized value for parameter 'action': upload"

這裏就是我所做的(網站網址和密碼更改):


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") 
>>> 

從我能找到谷歌,目前的MediaWiki不支持上傳文件。但是這太可笑了......必須有某種方式,對吧?

我沒有結婚wikitools包 - 任何方式讚賞。

編輯:我在我的LocalSettings.php中設置了$ wgEnableUploads = true,我可以手動上傳文件,而不是通過python。

編輯:我認爲wikitools會自動獲取編輯標記。我已附加上傳方法。在它發出API請求之前,它會調用self.getToken('edit'),我應該照顧它嗎?我會稍微討論一下,看看是否手動添加修復的東西。

 
    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 

而且這是我的第一個問題所以有人讓我知道,如果你不能發表其他人的代碼什麼的。謝謝!

+0

您是否在MediaWiki上啓用了文件上傳? http://www.mediawiki.org/wiki/Manual:Configuring_file_uploads – jfs 2010-07-06 00:11:40

回答

0

也許你必須先「獲取令牌」?

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.

看到這裏的細節:http://www.mediawiki.org/wiki/API:Edit_-_Uploading_files

3

你至少需要鏈接到MediaWiki 1.16(這是目前在begta)才能通過API來上傳文件。或者你可以嘗試mwclient,它會自動回到上傳通過特殊:上傳,如果使用較舊版本的MediaWiki(與減少功能,如沒有錯誤處理等)

0

我有類似的麻煩,我是得到一個

引發API錯誤(data ['error'] ['code'],data ['error'] ['info']) wikitools.api.APIError:(u'verification-error',u'此文件沒有通過文件驗證')

但是,我發現目標頁面需要與文件類型相同,並且您應該打開二進制文件:

testImage = wikitools.wikifile.File(wiki=site, title="Image:test_snapshot.jpg") 
testImage.upload(fileobj=open("somefile.jpg", "rb"), ignorewarnings=True) 

相關問題