MediaWiki-Bot-Plugin-Admin

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


  unprotect($page, $reason)
    Unprotects a page. You can also set parameters for protect() such that
    the page is unprotected.

        my @obsolete_protections = ('Main Page', 'Project:Community Portal', 'Template:Tlx');
        foreach my $page (@obsolete_protections) {
            $bot->unprotect($page, 'Removing old obsolete page protection');
        }

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

  transwiki_import($options_hashref)
    Do a *transwiki* import of a page specified in the hashref.

    *   prefix must be a valid interwiki on the wiki you're importing to. It
        specifies where to import from.

    *   page is the title to import from the remote wiki, including
        namespace

README.mkdn  view on Meta::CPAN

## unprotect($page, $reason)

Unprotects a page. You can also set parameters for protect() such that 
the page is unprotected.

    my @obsolete_protections = ('Main Page', 'Project:Community Portal', 'Template:Tlx');
    foreach my $page (@obsolete_protections) {
        $bot->unprotect($page, 'Removing old obsolete page protection');
    }

## protect($page, $reason, $editlvl, $movelvl, $time, $cascade)

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

## transwiki_import($options_hashref)

Do a _transwiki_ import of a page specified in the hashref.

- prefix must be a valid interwiki on the wiki you're importing to. It
specifies where to import from.
- page is the title to import from the remote wiki, including namespace
- ns is the namespace _number_ to import _to_. For example, some wikis
have a "Transwiki" namespace to import into where cleanup happens before

lib/MediaWiki/Bot/Plugin/Admin.pm  view on Meta::CPAN

}


sub protect {
    my $self    = shift;
    my $page    = shift;
    my $reason  = shift;
    my $editlvl = defined($_[0]) ? shift : 'sysop';
    my $movelvl = defined($_[0]) ? shift : 'sysop';
    my $time    = shift || 'infinite';
    my $cascade = shift;

    $editlvl = 'all' if $editlvl eq '';
    $movelvl = 'all' if $movelvl eq '';

    if ($cascade and ($editlvl ne 'sysop' or $movelvl ne 'sysop')) {
        carp "Can't set cascading unless both editlvl and movelvl are sysop." if $self->{debug};
    }
    my $res = $self->{api}->api({
        action  => 'query',
        titles  => $page,
        prop    => 'info|revisions',
        intoken => 'protect'
    });

    my $data = [ %{ $res->{query}->{pages} } ]->[1];
    my $edittoken = $data->{protecttoken};

    $res = $self->{api}->api({
        action      => 'protect',
        title       => $page,
        token       => $edittoken,
        reason      => $reason,
        protections => "edit=$editlvl|move=$movelvl",
        expiry      => $time,
        cascade     => $cascade,
    });
    return $self->_handle_api_error() unless $res;

    return $res;
}

sub transwiki_import {
    my $self = shift;
    my $prefix      = $_[0]->{prefix} || 'w';
    my $page        = $_[0]->{page};

lib/MediaWiki/Bot/Plugin/Admin.pm  view on Meta::CPAN

=head2 unprotect($page, $reason)

Unprotects a page. You can also set parameters for protect() such that 
the page is unprotected.

    my @obsolete_protections = ('Main Page', 'Project:Community Portal', 'Template:Tlx');
    foreach my $page (@obsolete_protections) {
        $bot->unprotect($page, 'Removing old obsolete page protection');
    }

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

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

=head2 transwiki_import($options_hashref)

Do a I<transwiki> import of a page specified in the hashref.

=over 4

=item *
prefix must be a valid interwiki on the wiki you're importing to. It
specifies where to import from.

t/02_protect.t  view on Meta::CPAN

my $host     = $ENV{PWPAdminHost};
my $path     = $ENV{PWPAdminPath};
plan $username && $password && $host
    ? (tests => 6)
    : (skip_all => 'test wiki and admin login required');

my $t = __FILE__;
my $summary = "MediaWiki::Bot::Plugin::Admin tests ($t)";

my $page         = "User:$username/02 protect.t";
my $cascade_page = "User:$username/02 protect.t (2)";

# Create bot objects to play with
my $admin = MediaWiki::Bot->new({
    # debug   => 2,
    agent   => $summary,
    host    => $host,
    ($path ? (path => $path) : ()),
    login_data => { username => $username, password => $password },
});
my $anon = MediaWiki::Bot->new({
    # debug   => 2,
    agent   => $summary,
    host    => $host,
    ($path ? (path => $path) : ()),
});

subtest q/Ensure test pages aren't protected/ => sub {
    plan tests => 2;
    foreach my $title ($page, $cascade_page) {
        if ( defined $anon->get_text($title) ) { # page exists, make sure it is unprotected
            $admin->unprotect($title, $summary);
        }
        else { # page doesn't exist; create it
            $admin->edit({ page => $title, text => $summary });
        }
        my $protected = $admin->get_protection($title);
        is($protected, undef, "[[$title]] isn't protected");
    }
};

t/02_protect.t  view on Meta::CPAN

    is($text, $rand, 'Successfully edited page');
};

subtest q/Protect the page/ => sub {
    plan tests => 3;
    $admin->protect($page, $summary, 'sysop', '', 'infinite');
    my $cmp_protection = [
          {
            'expiry' => 'infinity',
            'level' => 'sysop',
            'cascade' => '',
            'type' => 'edit'
          }
        ];
    my $protection = $admin->get_protection($page);
    is_deeply($protection, $cmp_protection, 'Protection applied correctly');

    {   # Fail to edit it anonymously
        my $rand = rand();
        $anon->edit({ page => $page, text => $rand, summary => $summary });
        my $text = $admin->get_text($page);

t/02_protect.t  view on Meta::CPAN

        my $rand = rand();
        $admin->edit({ page => $page, text => $rand, summary => $summary });
        my $text = $admin->get_text($page);
        is($text, $rand, "Should be able to edit [[$page]] with sysop account");
    }
};

subtest q/Cascade-protect a test page/ => sub {
    plan tests => 4;
    $admin->protect(
        $cascade_page,
        $summary,
        'sysop', 'sysop', undef, 1 # edit, move, expiry, cascading
    );
    my $cmp_protection = [
          {
            'expiry' => 'infinity',
            'level' => 'sysop',
            'cascade' => '',
            'type' => 'edit'
          },
          {
            'expiry' => 'infinity',
            'level' => 'sysop',
            'type' => 'move'
          }
        ];
    my $protection = $admin->get_protection($cascade_page);
    is_deeply($protection, $cmp_protection, "[[$cascade_page]] protected properly");

    {   # Transclude it into another
        my $rand = rand();
        $admin->edit({
            page    => $page,
            text    => $rand . "{{$cascade_page}}",
            summary => $summary,
        });
        my $text = $admin->get_text($page);
        like($text, qr/\Q{{$cascade_page}}\E/, 'Set cascading');
    }

    {   # Fail to edit a page transcluding a cascade-protected page anonymously
        my $rand = rand();
        $anon->edit({ page => $page, text => $rand, summary => $summary });
        my $text = $admin->get_text($page);
        isnt($text, $rand, q{Shouldn't be able to edit anon after setting cascading});
        is($text, $text,   q{Should be the same as before});
    }
};

subtest q/Remove protection and edit anonymously/ => sub {
    plan tests => 3;
    {
        $admin->unprotect($page, $summary);
        my $protection = $admin->get_protection($page);
        is($protection, undef, "[[$page]] no longer protected");
    }

    {
        $admin->unprotect($cascade_page, $summary);
        my $protection = $admin->get_protection($cascade_page);
        is($protection, undef, "[[$cascade_page]] no longer protected");
    }

    my $rand = rand();
    $anon->edit({ page => $page, text => $rand, summary => $summary });
    $admin->purge_page($page);
    my $text = $admin->get_text($page);
    is($text, $rand, 'Should be able to edit anon');
};

subtest q/Cleanup/ => sub {
    plan tests => 2;
    {
        $admin->unprotect($page, $summary);
        my $protection = $admin->get_protection($page);
        is($protection, undef, "[[$page]] no longer protected");
    }

    {
        $admin->unprotect($cascade_page, $summary);
        my $protection = $admin->get_protection($cascade_page);
        is($protection, undef, "[[$cascade_page]] no longer protected");
    }
};



( run in 0.547 second using v1.01-cache-2.11-cpan-49f99fa48dc )