File: MediaWiki.pm

package info (click to toggle)
libcatmandu-mediawiki-perl 0.021-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 224 kB
  • sloc: perl: 233; makefile: 2
file content (404 lines) | stat: -rw-r--r-- 11,780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package Catmandu::Importer::MediaWiki;
use Catmandu::Sane;
use MediaWiki::API;
use Catmandu::Util qw(:is :check array_includes);
use URI::Escape;
use Moo;

#only generators that generate pages, and that have generators
my $generators = [qw(
    alllinks
    allpages
    allredirects
    alltransclusions
    backlinks
    categorymembers
    embeddedin
    exturlusage
    imageusage
    iwbacklinks
    langbacklinks
    pageswithprop
    prefixsearch
    random
    recentchanges
    watchlist
    watchlistraw
)];
my $default_args = {
    prop => "revisions",
    rvprop => "ids|flags|timestamp|user|comment|size|content|sha1|tags|userid|parsedcomment",
    rvlimit => 'max',
    gaplimit => 100,
    gapfilterredir => "nonredirects"
};
my $ns_map = {
    allpages => "gapnamespace",
    alllinks => "galnamespace",
    allredirects => "garnamespace",
    alltransclusions => "gatnamespace",
    backlinks => "gblnamespace",
    categorymembers => "gcmnamespace",
    embeddedin => "geinamespace",
    exturlusage => "geunamespace",
    imageusage => "giunamespace",
    iwbacklinks => undef,
    langbacklinks => undef,
    pageswithprop => undef,
    prefixsearch => "gpsnamespace",
    random => "grnnamespace",
    recentchanges => "grcnamespace",
    watchlist => "gwlnamespace",
    watchlistraw => "gwrnamespace"
};

has url => (
    is => 'ro',
    isa => sub { check_string($_[0]); }
);
#cf. https://www.mediawiki.org/wiki/API:Login
has lgname => ( is => 'ro' );
has lgpassword => ( is => 'ro' );
#cf. http://www.mediawiki.org/wiki/API:Lists
#cf. http://www.mediawiki.org/wiki/API:Query#Generators
has generate => (
    is => 'ro',
    isa => sub {
        array_includes($generators,$_[0]) or die("invalid generator");
    },
    lazy => 1,
    default => sub { "allpages"; }
);
has args => (
    is => 'ro',
    isa => sub { check_hash_ref($_[0]); },
    lazy => 1,
    default => sub { $default_args },
    coerce => sub {
        my $l = $_[0];
        my $h = is_hash_ref($l) ? +{ %$default_args,%$l } : $default_args;
        for(keys %$h){
            delete $h->{$_} unless defined $h->{$_};
        }
        $h;
    }
);
has mediawiki => (
    is => 'ro',
    lazy => 1,
    builder => '_build_mw'
);

with 'Catmandu::Importer';

sub _build_mw {
    my $self = $_[0];
    my $mw = MediaWiki::API->new( { api_url => $self->url() }  );

    if(is_string($ENV{LWP_TRACE})){
        $mw->{ua}->add_handler("request_send",  sub { shift->dump; return });
        $mw->{ua}->add_handler("response_done", sub { shift->dump; return });
    }

    $mw;
}
sub _fail {
    my $err = $_[0];
    die( $err->{code}.': '.$err->{details} );
}

sub generator {
    my $self = $_[0];

    my $generator = $self->generate();
    my $args = $self->args();

    sub {
        state $mw = $self->mediawiki();
        state $pages = [];
        state $cont_args = {};
        state $logged_in = 0;
        state $namespaces = [];
        state $namespace_key = $ns_map->{ $generator };
        state $siteinfo = {};

        unless($logged_in){
            #only try to login when both arguments are set
            if(is_string($self->lgname) && is_string($self->lgpassword)){
                $mw->login({ lgname => $self->lgname, lgpassword => $self->lgpassword }) or _fail($mw->{error});
            }
            $logged_in = 1;
        }

        #store continue arguments per namespace
        if(scalar(@$namespaces) == 0){
            #namespaces supported: true
            if(defined $namespace_key){

                if(is_string($args->{ $namespace_key })){
                    push @$namespaces,split(/\|/o,$args->{ $namespace_key });
                }
                else{
                    my $r = $mw->api({ action => "query", meta => "siteinfo", "siprop" => "namespaces|general" }) or _fail($mw->{error});
                    return unless defined $r;
                    $siteinfo =  $r->{query};
                    push @$namespaces,sort keys(%{ $siteinfo->{namespaces} });
                }
                $cont_args->{$_} = { continue => '' } for @$namespaces;
            }
            #namespaces supported: false
            else{

                push @$namespaces,"_none_";
                $cont_args->{_none_} = { continue => '' };

            }
        }

        return unless scalar(@$namespaces);

        if(scalar(@$pages)==0){
            #results for namespace are depleted: goto next
            unless(defined ($cont_args->{ $namespaces->[0] })){
                shift @$namespaces;
            }

            return unless scalar(@$namespaces);

            #look until you found a decent namespace
            my $res;
            while(scalar(@$namespaces)){
                my $a = {
                    %$args,
                    %{$cont_args->{ $namespaces->[0] } },
                    action => "query",
                    indexpageids => 1,
                    generator => $generator,
                    format => "json"
                };
                if(defined $namespace_key){
                    $a->{ $namespace_key } = $namespaces->[0];
                }
                #will work with generator in the future
                delete $a->{rvlimit};
                $res = $mw->api($a);
                if(!$res){
                    if ( $mw->{error}->{details} =~  /gapunknown_gapnamespace/o || $mw->{error}->{details} =~ /API has returned an empty array reference/o ){
                        shift @$namespaces;
                        return unless scalar(@$namespaces);
                        next;
                    }
                    _fail($mw->{error});
                }else{
                    last;
                }
            }


            return unless defined $res;

            $cont_args->{ $namespaces->[0] } = $res->{'continue'};

            if(exists($res->{'query'}->{'pageids'})){

                for my $pageid(@{ $res->{'query'}->{'pageids'} }){
                    #'titles, pageids or a generator was used to supply multiple pages, but the limit, startid, endid, dirNewer, user, excludeuser, start and end parameters may only be used on a single page.'
                    #which means: cannot repeat pageids when asking for full history
                    my $page = $res->{'query'}->{'pages'}->{$pageid};

                    #add source url
                    my $title = $page->{title};
                    $title =~ s/\s/_/go;

                    {
                        my $articlepath = $siteinfo->{general}->{articlepath};
                        $articlepath =~ s/\$1/${title}/;

                        #server:  http://localhost:8000
                        my $server = $siteinfo->{general}->{server};
                        #servername: localhost:8000
                        my $servername = $siteinfo->{general}->{servername};

                        my $url;
                        if( is_string($server) ){

                            $url = $server.$articlepath;

                        }
                        else{

                            my $base = $siteinfo->{general}->{base};
                            my $protocol = $base =~ /^https/o ? "https" : "http";
                            $url = "${protocol}://".$siteinfo->{general}->{servername}.$articlepath;

                        }

                        $page->{_url} = $url;

                    }

                    if(is_string($args->{rvlimit})){

                        my $a = {
                            action => "query",
                            format => "json",
                            pageids => $pageid,
                            prop => "revisions",
                            rvprop => $args->{rvprop},
                            rvlimit => $args->{rvlimit}
                        };
                        my $res2 = $mw->api($a) or _fail($mw->{error});
                        $page->{revisions} = $res2->{'query'}->{'pages'}->{$pageid}->{revisions} if $res2->{'query'}->{'pages'}->{$pageid}->{revisions};

                        #add source url
                        for my $revision(@{ $page->{revisions} } ){

                            $revision->{_url} = $page->{_url}."?oldid=".$revision->{revid};

                        }

                    }

                    push @$pages,$page;
                }
            }
        }

        shift @$pages;
    };
}

=head1 NAME

Catmandu::Importer::MediaWiki - Catmandu importer that imports pages from mediawiki

=head1 DESCRIPTION

This importer uses the query api from mediawiki to get a list of pages
that match certain requirements.

It retrieves a list of pages and their content by using the generators
from mediawiki:

L<http://www.mediawiki.org/wiki/API:Query#Generators>

The default generator is 'allpages'.

The list could also be retrieved with the module 'list':

L<http://www.mediawiki.org/wiki/API:Lists>

But this module 'list' is very limited. It retrieves a list of pages
with a limited set of attributes (pageid, ns and title).

The module 'properties' on the other hand lets you add properties:

L<http://www.mediawiki.org/wiki/API:Properties>

But the selecting parameters (titles, pageids and revids) are too specific
to execute a query in one call. One should execute a list query, and then
use the pageids to feed them to the submodule 'properties'.

To execute a query, and add properties to the pages in one call can be
accomplished by use of generators.

L<http://www.mediawiki.org/wiki/API:Query#Generators>

These parameters are set automatically, and cannot be overwritten:

action = "query"
indexpageids = 1
generator = <generate>
format = "json"

Additional parameters can be set in the constructor argument 'args'.
Arguments for a generator origin from the list module with the same name,
but must be prepended with 'g'.

=head1 ARGUMENTS

=over 4

=item generate

type: string

explanation:    type of generator to use. For a complete list, see L<http://www.mediawiki.org/wiki/API:Lists>.
                because Catmandu::Iterable already defines 'generator', this parameter has been renamed
                to 'generate'.

default: 'allpages'.

=item args

type: hash

explanation: extra arguments. These arguments are merged with the defaults.

default:

    {
        prop => "revisions",
        rvprop => "ids|flags|timestamp|user|comment|size|content",
        gaplimit => 100,
        gapfilterredir => "nonredirects"
    }

which means:

    prop             add revisions to the list of page attributes
    rvprop           specific properties for the list of revisions
    gaplimit         limit for generator 'allpages' (every 'generator' has its own limit).
    gapfilterredir   filter out redirect pages

=item lgname

type: string

explanation:    login name. Only used when both lgname and lgpassword are set.

L<https://www.mediawiki.org/wiki/API:Login>

=item lgpassword

type: string

explanation:    login password. Only used when both lgname and lgpassword are set.

=back

=head1 SYNOPSIS

    use Catmandu::Sane;
    use Catmandu::Importer::MediaWiki;

    binmode STDOUT,":utf8";

    my $importer = Catmandu::Importer::MediaWiki->new(
        url => "http://en.wikipedia.org/w/api.php",
        generate => "allpages",
        args => {
            prop => "revisions",
            rvprop => "ids|flags|timestamp|user|comment|size|content",
            gaplimit => 100,
            gapprefix => "plato",
            gapfilterredir => "nonredirects"
        }
    );
    $importer->each(sub{
        my $r = shift;
        my $content = $r->{revisions}->[0]->{"*"};
        say $r->{title};
    });

=head1 AUTHORS

Nicolas Franck C<< <nicolas.franck at ugent.be> >>

=head1 SEE ALSO

L<Catmandu>, L<MediaWiki::API>

=cut

1;