开发者

Upload files to a mediawiki via PHP

I'm currently using Extension:Push to copy articles from one MediaWiki (1.16) installation (which sits in local intranet and is not accessible from the outside) to another one. However, Push can only copy files by having the remote MediaWiki download the file, which isn't possible in this case (the intranet wiki isn't accessible from the outside for a reason).

So, I need some way to bind to the Upload API of MediaWiki directly. My current attempt is the following:

$header = 'Content-Type: multipart/form-data; boundary='.$multipart_boundary.'\r\n'.
          'Cookie: '.$cookies;
$file_contents = file_get_contents ($imagePage->getDisplayedFile()->getFullUrl());
$content = "--".$multipart_boundary.'\r\n'.
           'Content-Disposition: form-data; name="file"; filename="'.basename($imagePage->getDisplayedFile()->getFullUrl()).'"\r\n'.
           'Content-Type: application/octet-stream\r\n\r\n'.
           $file_contents.'\r\n'.
           '--'.$multipart_boundary.'\r\n'.
           'Content-Disposition: form-data; name="action"\r\n\r\n'.
           'upload\r\n'.
           '--'.$multipart_boundary.'\r\n'.
           'Content-Disposition: form-data; name="format"\r\n\r\n'.
           'json\r\n'.
           '--'.$mu开发者_如何学Pythonltipart_boundary.'\r\n'.
           'Content-Disposition: form-data; name="ignorewarnings"\r\n\r\n'.
           '1\r\n'.
           '--'.$multipart_boundary.'\r\n'.
           'Content-Disposition: form-data; name="token"\r\n\r\n'.
           $token.'\r\n';

$context = stream_context_create (array (
  'http' => array (
    'method' => 'POST',
    'header' => $header,
    'content' => $content,
    ),  
  )); 

$response = file_get_contents ($target, false, $context);

I don't get any error message (neither from local nor remote Apache), but the file isn't appearing, either. Anyone got an idea what's wrong, has a working solution or can at least point me to the involved mediawiki functions so I can put some debug logging in them?


I haven't tried this yet, but if you're creating pages on the remote Wiki (whose URL you know) then it looks as if the way to do it is described in http://www.mediawiki.org/wiki/API:Edit which says that you need to get an edit token and then post with the token, your page contents, etc. You get XML-formatted messages back to track success or failure.


Well, the original author updated the extension. That solved the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜