通过 PHP 将文件上传到 mediawiki
我目前正在使用 Extension:Push 从一个 MediaWiki (1.16) 安装中复制文章 (位于本地 Intranet 中,无法从外部访问)到另一个 Intranet。但是,Push 只能通过让远程 MediaWiki 下载文件来复制文件,在这种情况下这是不可能的(由于某种原因无法从外部访问 Intranet wiki)。
所以,我需要一些方法来直接绑定到 MediaWiki 的上传 API。我当前的尝试如下:
$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'.
'--'.$multipart_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);
我没有收到任何错误消息(无论是来自本地还是远程 Apache),但该文件也没有出现。任何人都知道出了什么问题,有一个可行的解决方案,或者至少可以向我指出所涉及的 mediawiki 功能,以便我可以在其中添加一些调试日志记录?
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'.
'--'.$multipart_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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有尝试过这个,但是如果您在远程 Wiki(您知道其 URL)上创建页面,那么看起来好像 http://www.mediawiki.org/wiki/API:Edit 表示您需要获取编辑令牌,然后使用该令牌进行发布、您的页面内容等。您将收到 XML 格式的消息以跟踪成功或失败。
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.