Perlwikipedia

 view release on metacpan or  search on metacpan

lib/Perlwikipedia.pm  view on Meta::CPAN

	my $page	= shift;
	my $id	= shift;
	my $summary = shift;
	my $image	= $page;
	$image=~s/\s/_/g;
	$image=~s/\%20/_/g;
	$image=~s/Image://gi;
	my $res	 = $self->_get( $page, 'delete', "&oldimage=$id%21$image" );
	unless ($res) { return; }
	my $options = {
		   fields	=> {
				wpReason  => $summary,
			},
		};
	$res = $self->{mech}->submit_form( %{$options});
#use Data::Dumper;print Dumper($res);
#print $res->decoded_content."\n";
	return $res;
}

=item block($user, $length, $summary, $anononly, $autoblock, $blockaccountcreation, $blockemail)

Blocks the user with the specified options.  All options optional except $user and $length. Last four are true/false. Defaults to empty summary, all options disabled.

=cut

sub block {
	my $self	= shift;
	my $user	= shift;
	my $length  = shift;
	my $summary = shift;
	my $anononly= shift;
	my $autoblock=shift;
	my $blockac = shift;
	my $blockemail=shift;
	my $res	 = $self->_get( "Special:Blockip/$user" );
	unless ($res) { return; }

	$res = $self->{api}->api( {
		action=>'query',
		titles=>'Main_Page',
		prop=>'info|revisions',
		intoken=>'block' } );
	my ($id, $data)=%{$res->{query}->{pages}};
	my $edittoken=$data->{blocktoken};
	my $hash = {
		action=>'block',
		user=>$user,
		token=>$edittoken,
		expiry=>$length,
		reason=>$summary };
	$hash->{anononly}=$anononly if ($anononly);
	$hash->{autoblock}=$autoblock if ($autoblock);
	$hash->{nocreate}=$blockac if ($blockac);
	$hash->{noemail}=$blockemail if ($blockemail);
	$res = $self->{api}->api( $hash );

	return $res;
}

=item protect($page, $reason, $editlvl, $movelvl, $time, $cascade)

Protects (or unprotects) the page. $editlvl and $movelvl may be '', 'autoconfirmed', or 'sysop'. $cascade is true/false.

=cut

sub protect {
	my $self	= shift;
	my $page	= shift;
	my $reason	= shift;
	my $editlvl	= shift || 'all';
	my $movelvl 	= shift || 'all';
	my $time	= shift || 'infinite';
	my $cascade	= shift;
	
	if ($cascade and ($editlvl ne 'sysop' or $movelvl ne 'sysop')) {
		carp "Can't set cascading unless both editlvl and movelvl are sysop."
	}
	my $res = $self->{api}->api( {
		action=>'query',
		titles=>$page,
		prop=>'info|revisions',
		intoken=>'protect' } );
#use Data::Dumper;print STDERR Dumper($res);
	my ($id, $data)=%{$res->{query}->{pages}};
	my $edittoken=$data->{protecttoken};
	my $hash={	action=>'protect',
		title=>$page,
		token=>$edittoken,
		reason=>$reason,
		protections=>"edit=$editlvl|move=$movelvl",
		expiry=>$time };
	$hash->{'cascade'}=$cascade if ($cascade);
	$res = $self->{api}->api( $hash );

	return $res;
}

=item get_pages_in_namespace($namespace_id,$page_limit)

Returns an array containing the names of all pages in the specified namespace. The $namespace_id must be a number, not a namespace name. Setting $page_limit is optional. If $page_limit is over 500, it will be rounded up to the next multiple of 500.

=cut

sub get_pages_in_namespace {
	my $self = shift;
	my $namespace = shift;
	my $page_limit = shift || 500;
	my $apilimit=500;
	if ($self->{highlimits}) {
		$apilimit=5000;
	}

 	my @return;
	my $max;

	if ($page_limit<=$apilimit) {
		$max=1;
	} else {
		$max=($page_limit-1)/$apilimit+1;
		$page_limit=$apilimit;
	}

	my $res = $self->{api}->list( {
		action=>'query',
		list=>'allpages',
		apnamespace=>$namespace,
		aplimit=>$page_limit },
		{ max=>$max } );

	foreach (@{$res}) {
		push @return, $_->{title};
	}
	return @return;
}

=item count_contributions($user)

Uses the API to count $user's contributions.

=cut

sub count_contributions {
	my $self=shift;
	my $username=shift;
	$username=~s/User://i; #strip namespace
	my $res = $self->{api}->list( {
		action=>'query',
		list=>'users',
		ususers=>$username,
		usprop=>'editcount' },
		{ max=>1 } );
	my $return = ${$res}[0]->{'editcount'};



( run in 0.696 second using v1.01-cache-2.11-cpan-9581c071862 )