contrib / mw-to-git / t / test-gitmw.plon commit Merge branch 'lf/setup-prefix-pathspec' (51ebd0f)
   1#!/usr/bin/perl -w -s
   2# Copyright (C) 2012
   3#     Charles Roussel <charles.roussel@ensimag.imag.fr>
   4#     Simon Cathebras <simon.cathebras@ensimag.imag.fr>
   5#     Julien Khayat <julien.khayat@ensimag.imag.fr>
   6#     Guillaume Sasdy <guillaume.sasdy@ensimag.imag.fr>
   7#     Simon Perrat <simon.perrat@ensimag.imag.fr>
   8# License: GPL v2 or later
   9
  10# Usage:
  11#       ./test-gitmw.pl <command> [argument]*
  12# Execute in terminal using the name of the function to call as first
  13# parameter, and the function's arguments as following parameters
  14#
  15# Example:
  16#     ./test-gitmw.pl "get_page" foo .
  17# will call <wiki_getpage> with arguments <foo> and <.>
  18#
  19# Available functions are:
  20#     "get_page"
  21#     "delete_page"
  22#     "edit_page"
  23#     "getallpagename"
  24
  25use MediaWiki::API;
  26use Getopt::Long;
  27use encoding 'utf8';
  28use DateTime::Format::ISO8601;
  29use open ':encoding(utf8)';
  30use constant SLASH_REPLACEMENT => "%2F";
  31
  32#Parsing of the config file
  33
  34my $configfile = "$ENV{'CURR_DIR'}/test.config";
  35my %config;
  36open my $CONFIG, "<",  $configfile or die "can't open $configfile: $!";
  37while (<$CONFIG>)
  38{
  39        chomp;
  40        s/#.*//;
  41        s/^\s+//;
  42        s/\s+$//;
  43        next unless length;
  44        my ($key, $value) = split (/\s*=\s*/,$_, 2);
  45        $config{$key} = $value;
  46        last if ($key eq 'LIGHTTPD' and $value eq 'false');
  47        last if ($key eq 'PORT');
  48}
  49close $CONFIG or die "can't close $configfile: $!";
  50
  51my $wiki_address = "http://$config{'SERVER_ADDR'}".":"."$config{'PORT'}";
  52my $wiki_url = "$wiki_address/$config{'WIKI_DIR_NAME'}/api.php";
  53my $wiki_admin = "$config{'WIKI_ADMIN'}";
  54my $wiki_admin_pass = "$config{'WIKI_PASSW'}";
  55my $mw = MediaWiki::API->new;
  56$mw->{config}->{api_url} = $wiki_url;
  57
  58
  59# wiki_login <name> <password>
  60#
  61# Logs the user with <name> and <password> in the global variable
  62# of the mediawiki $mw
  63sub wiki_login {
  64        $mw->login( { lgname => "$_[0]",lgpassword => "$_[1]" } )
  65        || die "getpage: login failed";
  66}
  67
  68# wiki_getpage <wiki_page> <dest_path>
  69#
  70# fetch a page <wiki_page> from the wiki referenced in the global variable
  71# $mw and copies its content in directory dest_path
  72sub wiki_getpage {
  73        my $pagename = $_[0];
  74        my $destdir = $_[1];
  75
  76        my $page = $mw->get_page( { title => $pagename } );
  77        if (!defined($page)) {
  78                die "getpage: wiki does not exist";
  79        }
  80
  81        my $content = $page->{'*'};
  82        if (!defined($content)) {
  83                die "getpage: page does not exist";
  84        }
  85
  86        $pagename=$page->{'title'};
  87        # Replace spaces by underscore in the page name
  88        $pagename =~ s/ /_/g;
  89        $pagename =~ s/\//%2F/g;
  90        open(my $file, ">$destdir/$pagename.mw");
  91        print $file "$content";
  92        close ($file);
  93
  94}
  95
  96# wiki_delete_page <page_name>
  97#
  98# delete the page with name <page_name> from the wiki referenced
  99# in the global variable $mw
 100sub wiki_delete_page {
 101        my $pagename = $_[0];
 102
 103        my $exist=$mw->get_page({title => $pagename});
 104
 105        if (defined($exist->{'*'})){
 106                $mw->edit({ action => 'delete',
 107                                title => $pagename})
 108                || die $mw->{error}->{code} . ": " . $mw->{error}->{details};
 109        } else {
 110                die "no page with such name found: $pagename\n";
 111        }
 112}
 113
 114# wiki_editpage <wiki_page> <wiki_content> <wiki_append> [-c=<category>] [-s=<summary>]
 115#
 116# Edit a page named <wiki_page> with content <wiki_content> on the wiki
 117# referenced with the global variable $mw
 118# If <wiki_append> == true : append <wiki_content> at the end of the actual
 119# content of the page <wiki_page>
 120# If <wik_page> doesn't exist, that page is created with the <wiki_content>
 121sub wiki_editpage {
 122        my $wiki_page = $_[0];
 123        my $wiki_content = $_[1];
 124        my $wiki_append = $_[2];
 125        my $summary = "";
 126        my ($summ, $cat) = ();
 127        GetOptions('s=s' => \$summ, 'c=s' => \$cat);
 128
 129        my $append = 0;
 130        if (defined($wiki_append) && $wiki_append eq 'true') {
 131                $append=1;
 132        }
 133
 134        my $previous_text ="";
 135
 136        if ($append) {
 137                my $ref = $mw->get_page( { title => $wiki_page } );
 138                $previous_text = $ref->{'*'};
 139        }
 140
 141        my $text = $wiki_content;
 142        if (defined($previous_text)) {
 143                $text="$previous_text$text";
 144        }
 145
 146        # Eventually, add this page to a category.
 147        if (defined($cat)) {
 148                my $category_name="[[Category:$cat]]";
 149                $text="$text\n $category_name";
 150        }
 151        if(defined($summ)){
 152                $summary=$summ;
 153        }
 154
 155        $mw->edit( { action => 'edit', title => $wiki_page, summary => $summary, text => "$text"} );
 156}
 157
 158# wiki_getallpagename [<category>]
 159#
 160# Fetch all pages of the wiki referenced by the global variable $mw
 161# and print the names of each one in the file all.txt with a new line
 162# ("\n") between these.
 163# If the argument <category> is defined, then this function get only the pages
 164# belonging to <category>.
 165sub wiki_getallpagename {
 166        # fetch the pages of the wiki
 167        if (defined($_[0])) {
 168                my $mw_pages = $mw->list ( { action => 'query',
 169                                list => 'categorymembers',
 170                                cmtitle => "Category:$_[0]",
 171                                cmnamespace => 0,
 172                                cmlimit => 500 },
 173                )
 174                || die $mw->{error}->{code}.": ".$mw->{error}->{details};
 175                open(my $file, ">all.txt");
 176                foreach my $page (@{$mw_pages}) {
 177                        print $file "$page->{title}\n";
 178                }
 179                close ($file);
 180
 181        } else {
 182                my $mw_pages = $mw->list({
 183                                action => 'query',
 184                                list => 'allpages',
 185                                aplimit => 500,
 186                        })
 187                || die $mw->{error}->{code}.": ".$mw->{error}->{details};
 188                open(my $file, ">all.txt");
 189                foreach my $page (@{$mw_pages}) {
 190                        print $file "$page->{title}\n";
 191                }
 192                close ($file);
 193        }
 194}
 195
 196sub wiki_upload_file {
 197        my $file_name = $_[0];
 198        my $resultat = $mw->edit ( {
 199                action => 'upload',
 200                filename => $file_name,
 201                comment => 'upload a file',
 202                file => [ $file_name ],
 203                ignorewarnings=>1,
 204        }, {
 205                skip_encoding => 1
 206        } ) || die $mw->{error}->{code} . ' : ' . $mw->{error}->{details};
 207}
 208
 209
 210
 211# Main part of this script: parse the command line arguments
 212# and select which function to execute
 213my $fct_to_call = shift;
 214
 215wiki_login($wiki_admin, $wiki_admin_pass);
 216
 217my %functions_to_call = qw(
 218        upload_file    wiki_upload_file
 219        get_page       wiki_getpage
 220        delete_page    wiki_delete_page
 221        edit_page      wiki_editpage
 222        getallpagename wiki_getallpagename
 223);
 224die "$0 ERROR: wrong argument" unless exists $functions_to_call{$fct_to_call};
 225&{$functions_to_call{$fct_to_call}}(@ARGV);