User:下一次登录/mwpush.pl
外观
这是一个自动向维基百科提交编辑内容的机器人(程序),用Perl脚本语言写成。
使用详细说明
[编辑]我的测试是在WinXP操作系统下进行的。步骤:
- 自http://www.activestate.com/Products/ActivePerl/下载ActivePERL,是一种PERL语言的开发环境。大概8M,安装。
- 自http://www.cygwin.com/下载Cygwin,可以在Windows下模拟Unix的脚本语言功能(Shell)。大概17M,安装。
- 把Perl/bin/中的内容拷贝至Cygwin/bin/中,Perl/lib/至Cygwin/lib/,其他Perl/下的目录拷贝至Cygwin/。
- 在Cygwin/目录下把下面的代码保存为“mwpush.pl”文件,将要提交的内容保存至“1.txt”文件。
- 运行cygwin.bat。
- 输入
mwpush.pl -l (你的用户名,最好是全英文) -p (登录密码) -w test.strore.xyz -t (目标条目名称) 1.txt
注意:可能覆盖以前的条目版本!!!一定要小心!!!
代码
[编辑]#!/usr/bin/perl
# mwpush.pl - Push page data to a Wikimedia server
# By WikiPedia:User:KeithTyler
# Portions largely taken or based on upload.pl by WikiPedia:User:Eloquence
# call requirements
use Getopt::Std;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use HTTP::Cookies;
#use warnings;
getopts('l:p:w:t:he');
if ($opt_h) { die "Usage: mwpush.pl -w <wiki host/path> -t <wiki page name> -l <wiki login> -p <wiki password> [-e] [files ...]\n"; }
if (!$opt_l) { die "Provide Wiki login with -l"; }
if (!$opt_p) { die "Provide Wiki password with -p"; }
if (!$opt_w) { die "Provide Wiki hostname and path with -w"; }
if (!$opt_t) { die "Provide Wiki page name with -t"; }
my $username=$opt_l;
my $password=$opt_p;
my $WIKI_PATH=$opt_w;
my $WIKI_PAGE=$opt_t;
my $onlyifempty=$opt_e;
### Login to wiki
# Set up connection data
my $browser=LWP::UserAgent->new();
my @ns_headers = (
'User-Agent' => 'MediaWiki Pusher 0.1 by KeithTyler', #Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20041107 Firefox/1.0',
'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
'Accept-Charset' => 'iso-8859-1,*,utf-8',
'Accept-Language' => 'en-US',
);
# Hold cookies
$browser->cookie_jar( {} );
# Make login request
$response=$browser->post("http://".$WIKI_PATH."/w/index.php?title=Special:Userlogin&action=submitlogin",
@ns_headers, Content=>[wpName=>$username,wpPassword=>$password,wpRemember=>"1",wpLoginAttempt=>"Log in"]);
# After logging in, we should be redirected to another page.
# If we aren't, something is wrong.
#
if($response->code!=302) {
print
"We weren't able to login. This could have the following causes:
* The username ($username) or password may be incorrect.
Solution: Re-run script with correct credentials.
* The MediaWiki software on the target host has been upgraded.
Solution: Go to http://commons.wikimedia.org/wiki/Commons:File_upload_service
and get a new version of the upload script.
* You are trying to hack this script for other wikis. The wiki you
are uploading to has cookie check disabled.
Solution: Try setting \$ignore_login_error to 1.
Regardless, we will now try to write the output from the server to
mwpush.debug.out....\n\n";
open(DEBUG,">mwpush.debug.out") or die "Could not write file.\n";
print DEBUG $response->as_string;
print
"This seems to have worked. Take a look at the file for further information or
send it to moeller AT scireview DOT de if you need help debugging the script.\n";
close(DEBUG);
exit 1;
}
### Get a wpEditToken
# We need to load our target page first in edit mode to capture a
# wpEditToken. Without this the submit page will not submit.
$response=$browser ->
get("http://".$WIKI_PATH."/w/index.php?title=".$WIKI_PAGE."&action=edit",
@ns_headers);
my $editToken;
my $content;
$content = $response->as_string;
# Get EditToken
($editToken) = ( $content =~ m/value\=\"([0-9a-f\\]*)\" name\=\"wpEditToken\"/ );
($editTime) = ( $content =~ m/value\=\"([0-9a-f]*)\" name\=\"wpEdittime\"/ );
# Determine page existence state
# If we find "selected new" (a CSS class set), it is a new (empty) page
# This is preferable to depending on message text (which is changeable)
if ($onlyifempty) {
if ( ! ($content =~ m/class=\"selected new\"/ )) {
print "Existing page is not empty. Aborting due to -e.\n";
exit 1;
}
}
### Collect input data into string
my $INPUT_DATA;
# Read data
while (<>) {
$INPUT_DATA.=$_;
}
### Post data to Wiki
$response=$browser ->
post("http://".$WIKI_PATH."/w/index.php?title=".$WIKI_PAGE."&action=submit",
@ns_headers,
Content_Type=>'form-data',Content=>
[ wpTextbox1 => $INPUT_DATA,
wpSummary => "Automated page entry using MWPush.pl",
wpSave => "Save page",
wpSection => "",
wpEdittime => $editTime,
wpEditToken => $editToken,
]);
### Evaluate response from Wiki
if($response->code!=302 && $response->code!=200) {
print "Upload failed! Will try again. Output was:\n";
print $response->as_string;
goto uploadfile;
} else {
print "Uploaded successfully.\n";
}
# Evaluate operation
print "Everything seems to be OK. Log will be written to mwpush.log.\n";
open(LOG,">mwpush.log") or die "Could not write file.\n";
print LOG $response->as_string;
参见
[编辑]- en:User:KeithTyler/mwpush.pl,程序是从那个页面拷贝过来的,由于版本的不同,做了一些修正。