跳轉到內容

Perlwikibot/3.0 版本釋出

來自華夏公益教科書
重要說明
此版本包含重大更改。這些發行說明將指導您完成更新呼叫程式碼的過程。

3.0 版本標誌著一次重大轉變。首先,所有方法都儘可能地使用 API。這意味著在某些情況下返回格式已更改。同樣,一些方法呼叫也已更改。其中一些仍然接受向後相容的形式;這將在適用時予以說明。

重大更改

[編輯 | 編輯原始碼]

所有方法

[編輯 | 編輯原始碼]
概述
幾乎所有方法現在在失敗時返回 undef
基本原理
將錯誤與返回的資料分開很重要。如果您需要錯誤程式碼或錯誤詳細資訊,它們分別在 $bot->{'error'}->{'code'}$bot->{'error'}->{'details'} 中可用。
影響
任何未忽略返回值的方法呼叫現在都必須檢查 undef

示例:get_text()get_pages()

[編輯 | 編輯原始碼]
概述
這些方法現在為不存在的頁面返回 undef,而不是錯誤程式碼。
基本原理
如上所述
影響
您處理錯誤程式碼的任何特殊情況現在都必須查詢 undef。涉及存在頁面的呼叫不受影響。
新呼叫
my $text = $bot->get_text('Non-existent page');
print $text if defined($text);

my @pages = ('Page 1', 'Page 2', 'Page 3');
my $thing = $bot->get_pages(\@pages);
foreach my $page (keys %$thing) {
    my $text = $thing->{$page};
    print "$text\n" if defined($text);
}
舊呼叫
my $text = $bot->get_text('Non-existent page');
print $text unless ($text eq "2");

my @pages = ('Page 1', 'Page 2', 'Page 3');
my $thing = $bot->get_pages(\@pages);
foreach my $page (keys %$thing) {
    my $text = $thing->{$page};
    print "$text\n" unless ($text eq "2");
}


概述
此方法現在在成功時返回 true;失敗時返回 false。
基本原理
在 Perl 中,非零為 true,表示成功。即使 Perl 中的系統呼叫包裝也遵循此約定。
影響
login() 的所有呼叫都需要更新檢查返回值的部分。
新呼叫
$bot->login({
    username => "Mike's bot account",
    password => $password,
}) or die "Couldn't log in";
舊呼叫
my $failure = $bot->login("Mike's bot account", $password);
die "Couldn't log in" if $failure;


linksearch()

[編輯 | 編輯原始碼]
概述
此方法現在使用鍵 'url' 和 'title'
基本原理
這減少了在程式設計師希望使用回撥鉤子進行增量處理的情況下出現的混淆。沒有此更改,訪問資料將使用兩組不同的鍵完成。
影響
任何呼叫都需要更新鍵名。
新呼叫
my $options = { max => 10, }; # I only want some results
my @links = $bot->linksearch("slashdot.org", 1, undef, $options);
foreach my $hash (@links) {
    my $url = $hash->{'url'};
    my $page = $hash->{'title'};
    print "$page: $url\n";
}

# Use a callback:
my $options = { hook => \&mysub, }; # I want to do incremental processing
$bot->linksearch("slashdot.org", 1, undef, $options) or die;
sub mysub {
    my ($res) = @_;
    foreach my $hashref (@$res) {
        my $url   = $hashref->{'url'};
        my $title = $hashref->{'title'};
        print "$title: $url\n";
    }
}
舊呼叫
my @links = $bot->linksearch("slashdot.org", 1) or die;
foreach my $hashref (@links) {
    my $link = $hashref->{'link'};
    my $page = $hashref->{'page'};
    print "$page: $link\n";
}


[編輯 | 編輯原始碼]
概述
此方法現在使用鍵 'title' 和 'redirect'。它還僅返回連結(包括重定向)。list_transclusions() 處理轉包。
基本原理
這減少了在程式設計師希望使用回撥鉤子進行增量處理的情況下出現的混淆。沒有此更改,訪問資料將使用兩組不同的鍵完成。
影響
對該方法的任何呼叫都需要評估是否需要用 list_transclusions() 的呼叫替換或補充。此外,鍵名必須更新。
新呼叫
my @links = $bot->what_links_here("Meta:Sandbox", undef, 1, {hook=>\&mysub});
sub mysub{
    my ($res) = @_;
    foreach my $hash (@$res) {
        my $title = $hash->{'title'};
        my $is_redir = $hash->{'redirect'};
        print "Redirect: $title\n" if $is_redir;
        print "Page: $title\n" unless $is_redir;
    }
}
舊呼叫
# dunno


其他更改

[編輯 | 編輯原始碼]

...

華夏公益教科書