User:SubstWelcomeBot/1.0

维基百科,自由的百科全书
#!/usr/bin/perl
# SubstWelcome.pl - {{welcome}} to {{subst:welcome}} bot
# By WikiPedia:User:Jnlin
# Portions largely taken or based on upload.pl by WikiPedia:User:Eloquence 
#  and mwpush.pl by WikiPedia:User:KeithTyler
# Modified from STcatBot2.0.pl - Simplified and Traditional CATegorization roBOT

# Disclaimer: No warranty ganranteed. Use at your own risk. 

# call requirements
use Getopt::Std;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use HTTP::Cookies;
#use warnings;

my $username="SubstWelcomeBot";      #input your username here, only English names are tested.
my $password="****";  #input your password here
my $WIKI_PATH="zh.wikipedia.org";
my $WIKI_PAGE;
my $MAX_COUNT=100;	# Max subst done at each run

### Login to wiki

# Set up connection data
my $browser=LWP::UserAgent->new();
my @ns_headers = (
 'User-Agent' => 'SubstWelcomeBot by Jnlin',  #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) { #cannot login
        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 
rfget.debug.out....\n\n";
        open(DEBUG,">rfget.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;
}

my $URL;
my $pagename;
my $count = 0;
my $string1 = "以下頁面連結到這裡";
my $string2 = "(包含)";
my $string3 = "User talk";
my $tmp;
my $flag = 0;
my $content1;
my $content2;
my $editToken;
my $editTime;
my $filestartstr;
my $fileendstr;
my $filestart;
my $fileend;

#go to Whatlinkshere/Template:Welcome and get the contents
$URL="http://".$WIKI_PATH."/w/index.php?title=Special:Whatlinkshere/Template:Welcome&limit=500&from=0";
$response=$browser->get($URL, @ns_headers);
@content=split(/\n/, $response->as_string);

foreach $tmp (@content) # process
{
	if($count >= $MAX_COUNT) # max process count
	{
		last;
	}
	# find to string1
	if($flag == 0 && $tmp =~ /$string1/)
	{
		$flag=1;
	}
	
	if($flag == 0)
	{
		next;
	}

	# if the template is included and is a user talk page
	if($tmp =~ /$string2/ && $tmp =~ /$string3/)
	{
		#go to the target page
		$tmp =~ /<a href=\"\/wiki\/([^\"]*)\"/;
		$pagename = $1;
		$URL="http://".$WIKI_PATH."/w/index.php?title=".$pagename."&action=edit";
		$response=$browser->get($URL, @ns_headers);
		$content1=$response->as_string;
		# Get EditToken
        ($editToken) = ( $content1 =~ m/value\=\"([0-9a-f\\]*)\" name\=\"wpEditToken\"/ );
        ($editTime) = ( $content1 =~ m/value\=\"([0-9a-f]*)\" name\=\"wpEdittime\"/ );
        
		$filestartstr="<textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" id=\"wpTextbox1\" rows='25'";
        $fileendstr="</textarea>";
        $filestart= index($content1, $filestartstr);
        $filestart+=92;
        $fileend= index($content1, $fileendstr);
        $content2=substr($content1, $filestart, $fileend-$filestart);
        
        #substitute
        $content2 =~ s/{{welcome}}/{{subst:welcome}}/gi;

		$response=$browser->post("http://".$WIKI_PATH."/w/index.php?title=".$pagename."&action=submit",
			@ns_headers,
			Content_Type=>'form-data',Content=>
				[ wpTextbox1 => $content2,
				wpSummary => "[[User:$username|$username]]: {{welcome}} ->{{subst:welcome}}",
				wpSave => "Save page",
				wpSection => "",
				wpEdittime => $editTime,
				wpEditToken => $editToken,
				wpMinoredit => "1",
				]
			);
		sleep 1;
		$count++;
	}
}

#go to Whatlinkshere/Template:Welcomeip and get the contents
$flag = 0;
$URL="http://".$WIKI_PATH."/w/index.php?title=Special:Whatlinkshere/Template:Welcomeip&limit=500&from=0";
$response=$browser->get($URL, @ns_headers);
@content=split(/\n/, $response->as_string);

foreach $tmp (@content) # process
{
	if($count >= $MAX_COUNT) # max process count
	{
		last;
	}
	# find to string1
	if($flag == 0 && $tmp =~ /$string1/)
	{
		$flag=1;
	}
	
	if($flag == 0)
	{
		next;
	}

	# if the template is included and is a user talk page
	if($tmp =~ /$string2/ && $tmp =~ /$string3/)
	{
		#go to the target page
		$tmp =~ /<a href=\"\/wiki\/([^\"]*)\"/;
		$pagename = $1;
		$URL="http://".$WIKI_PATH."/w/index.php?title=".$pagename."&action=edit";
		$response=$browser->get($URL, @ns_headers);
		$content1=$response->as_string;
		# Get EditToken
        ($editToken) = ( $content1 =~ m/value\=\"([0-9a-f\\]*)\" name\=\"wpEditToken\"/ );
        ($editTime) = ( $content1 =~ m/value\=\"([0-9a-f]*)\" name\=\"wpEdittime\"/ );
        
		$filestartstr="<textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" id=\"wpTextbox1\" rows='25'";
        $fileendstr="</textarea>";
        $filestart= index($content1, $filestartstr);
        $filestart+=92;
        $fileend= index($content1, $fileendstr);
        $content2=substr($content1, $filestart, $fileend-$filestart);
        
        #substitute
        $content2 =~ s/{{welcomeip}}/{{subst:welcomeip}}/gi;

		$response=$browser->post("http://".$WIKI_PATH."/w/index.php?title=".$pagename."&action=submit",
			@ns_headers,
			Content_Type=>'form-data',Content=>
				[ wpTextbox1 => $content2,
				wpSummary => "[[User:$username|$username]]: {{welcomeip}} ->{{subst:welcomeip}}",
				wpSave => "Save page",
				wpSection => "",
				wpEdittime => $editTime,
				wpEditToken => $editToken,
				wpMinoredit => "1",
				]
			);
		sleep 1;
		$count++;
	}
}