Topic on Project:Support desk

Create new page by Perl via MediaWiki::API

7
JanTappenbeck (talkcontribs)

I want to create simple pages in a company-wiki by Perl. In reposetory i found the modul MediaWiki::API - but not a simple example to create a page.

can someone help to me ?

MarkAHershberger (talkcontribs)

This is really a question for the maintainer of the CPAN module (Jools Wills), but there is an example in the documentation that may help. Note I have not tested this.

use MediaWiki::API;

my $mw = MediaWiki::API->new();
$mw->{config}->{api_url} = 'YOUR-API-URL';

# log in to the wiki
$mw->login( { lgname => 'username', lgpassword => 'password' } )
    || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};

my $pagename = "Project:Sandbox";
my $ref = $mw->get_page( { title => $pagename } );
$mw->edit( {
      action => 'edit',
      title => $pagename,
      text => "Additional text"
} ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};

If you try that, does it work? If not, what problems do you run into?

JanTappenbeck (talkcontribs)

thanks and first i try to login - but get following message:

2: Failed to decode JSON returned by http://ma22-wiki-001/eblwiki/api.php
Decoding Error:
malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "\x{feff}{"login":{"r...") at C:/strawberry/perl/site/lib/MediaWiki/API.pm line 400.

in other websites the reason will be declare by wrong api.php-url - but when i call the url in browser i get the dokumentation-side like http://www.exotica.org.uk/mediawiki/api.php but with xml source at the beginning and and like:

<?xml version="1.0"?>
<api>
  <error code="help" info="" xml:space="preserve">
i use 1.23.5 of mediawiki.
MarkAHershberger (talkcontribs)

I created an account on the server and successfully used the script to post to the wiki from Linux. I happen to have a version of Strawberry Perl on a Windows desktop, so I'll try that. But while I do that you might try upgrading your perl installation.

Ciencia Al Poder (talkcontribs)

Maybe it complains from the \x{feff} character? It's the Byte order mark that usually start unicode streams... looks like that perl script doesn't support that?

Note that on the browser you see the documentation because the perl script calls the api passing parameters probably in a HTTP POST request, while your browser is using HTTP GET

MarkAHershberger (talkcontribs)

Worked for me from windows, but not using Strawberry Perl -- I didn't try Strawberry Perl -- so I'm pretty sure this is a problem of your version of perl.

Reply to "Create new page by Perl via MediaWiki::API"