Hi, i upload the files to the mediawiki using the API. i use the code bellow: use strict; use MediaWiki::API; use DateTime::Format::ISO8601; #use encoding 'utf8'; binmode STDERR, ":utf8"; use URI::Escape; use warnings; use MediaWiki::API;
my $url="http://localhost/mediawiki"; my $wiki_login="nguyenki"; my $wiki_passwd="linh"; my $wiki_domain=""; my $mediawiki; mw_connect_maybe(); # configure the special upload location. $mediawiki->{config}->{upload_url} = "$url/index.php/Special:Upload"; * my @extensionFiles = ("png","gif","jpg","jpeg","doc","xls","mpp","pdf","ppt","tiff","bmp","docx", "xlsx", "pptx","ps","odt","ods","odp","odg","txt");* my %hashFiles = map { $_ => 1} @extensionFiles; #Test with a file .png *my $name="somefile.png"; * if(exists($hashFiles{substr($name,-3)}) || exists($hashFiles{substr($name,-4)})) {
# upload a file to MediaWiki open (my $toi, $name) or die "can't open UTF-8 encoded filename: $!"; binmode $toi; my ($buffer, $data); while (my $n=read($toi, $buffer, 65536) ) { $data .= $buffer; print STDERR "$n bytes read\n"; } close($toi);
$mediawiki->upload( { title => $name, summary => 'This is the summary to go on the Image:file.jpg page', data => $data } ) || die $mediawiki->{error}->{code} . ': ' . $mediawiki->{error}->{details};
} else { print "FILE TYPE NOT SUPPORTED\n";
}
sub mw_connect_maybe {
if ($mediawiki) { return; } $mediawiki = MediaWiki::API->new; $mediawiki->{config}->{api_url} = "$url/api.php";
if ($wiki_login) { if (!$mediawiki->login({ lgname => $wiki_login, lgpassword => $wiki_passwd, lgdomain => $wiki_domain, })) { print STDERR "Failed to log in mediawiki user "$wiki_login" on $url\n"; print STDERR "(error " . $mediawiki->{error}->{code} . ': ' . $mediawiki->{error}->{details} . ")\n"; exit 1; } else { print STDERR "Logged in with user "$wiki_login".\n"; } } }
To test the type of file supported, i have to use this variable: * my @extensionFiles = ("png","gif","jpg","jpeg","doc","xls","mpp","pdf","ppt","tiff","bmp","docx", "xlsx", "pptx","ps","odt","ods","odp","odg","txt");
*if the file name *$name* is not in the array, I'll generate a message error. : *print "FILE TYPE NOT SUPPORTED\n"; *My question is if the user change the type of file supported in the * LocalSettings.php* , this programme is not true any more. How can i get the array *$wgFileExtensions* from the *LocalSettings.php*?
Thank. * *
On Thu, May 31, 2012 at 11:10:30AM +0200, nguyenkim thuat wrote:
*My question is if the user change the type of file supported in the * LocalSettings.php* , this programme is not true any more. How can i get the array *$wgFileExtensions* from the *LocalSettings.php*?
Use meta=siteinfo&siprop=fileextensions. For example, https://en.wikipedia.org/w/api.php?action=query&meta=siteinfo&siprop... gives you enwiki's list.
Yeah, but, how can i do it, i think of doing some thing like:
*sub get_file_extensions { mw_connect_maybe(); my $query = { action => 'query', meta => 'siteinfo', siprop => 'fileextensions' }; my $result = $mediawiki->api($query);
##### how to get the array of file extensions here?
} * Best regards.
On Thu, May 31, 2012 at 4:57 PM, Brad Jorsch <b-jorsch@alum.northwestern.edu
wrote:
On Thu, May 31, 2012 at 11:10:30AM +0200, nguyenkim thuat wrote:
*My question is if the user change the type of file supported in the * LocalSettings.php* , this programme is not true any more. How can i get the array *$wgFileExtensions* from the *LocalSettings.php*?
Use meta=siteinfo&siprop=fileextensions. For example,
https://en.wikipedia.org/w/api.php?action=query&meta=siteinfo&siprop... gives you enwiki's list.
Mediawiki-api mailing list Mediawiki-api@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
On Fri, Jun 01, 2012 at 11:36:35AM +0200, nguyenkim thuat wrote:
Yeah, but, how can i do it, i think of doing some thing like:
*sub get_file_extensions { mw_connect_maybe(); my $query = { action => 'query', meta => 'siteinfo', siprop => 'fileextensions' }; my $result = $mediawiki->api($query);
##### how to get the array of file extensions here?
}
The same way you parse any other API response? Possibly something like my @extensions = map $_->{'ext'}, @{$x->{query}{fileextensions}} will do it.
At worst, use Data::Dumper, print Dumper($result), and look at it to figure out what type of data structure it is.
*The same way you parse any other API response? Possibly something like my @extensions = map $_->{'ext'}, @{$x->{query}{fileextensions}} will do it.*
Thank you so much, it works now. The code is:
*sub get_file_extensions { mw_connect_maybe();
my $query = { action => 'query', meta => 'siteinfo', siprop => 'fileextensions' };
my $result = $mediawiki->api($query); my @file_extensions= map $_->{ext},@{$result->{query}->{fileextensions}};
return @file_extensions; } *
Best regards.
On Fri, Jun 1, 2012 at 6:31 PM, Brad Jorsch b-jorsch@alum.northwestern.eduwrote:
On Fri, Jun 01, 2012 at 11:36:35AM +0200, nguyenkim thuat wrote:
Yeah, but, how can i do it, i think of doing some thing like:
*sub get_file_extensions { mw_connect_maybe(); my $query = { action => 'query', meta => 'siteinfo', siprop => 'fileextensions' }; my $result = $mediawiki->api($query);
##### how to get the array of file extensions here?
}
The same way you parse any other API response? Possibly something like my @extensions = map $_->{'ext'}, @{$x->{query}{fileextensions}} will do it.
At worst, use Data::Dumper, print Dumper($result), and look at it to figure out what type of data structure it is.
Mediawiki-api mailing list Mediawiki-api@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
mediawiki-api@lists.wikimedia.org