Debian Codegraph @

lib/Catmandu/Importer/MediaWiki.pm

raw symbols SCIP
1 package Catmandu::Importer::MediaWikiscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/;
2 use Catmandu::Sanescip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Sane/;
3 use MediaWiki::APIscip-perl cpan Catmandu-MediaWiki 0.021 MediaWiki/API/;
4 use Catmandu::Utilscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Util/ qw(:is :check array_includes);
5 use URI::Escapescip-perl cpan Catmandu-MediaWiki 0.021 URI/Escape/;
6 use Mooscip-perl cpan Catmandu-MediaWiki 0.021 Moo/;
7
8 #only generators that generate pages, and that have generators
9 my $generators = [qw(
10 alllinks
11 allpages
12 allredirects
13 alltransclusions
14 backlinks
15 categorymembers
16 embeddedin
17 exturlusage
18 imageusage
19 iwbacklinks
20 langbacklinks
21 pageswithprop
22 prefixsearch
23 random
24 recentchanges
25 watchlist
26 watchlistraw
27 )];
28 my $default_args = {
29 prop => "revisions",
30 rvprop => "ids|flags|timestamp|user|comment|size|content|sha1|tags|userid|parsedcomment",
31 rvlimit => 'max',
32 gaplimit => 100,
33 gapfilterredir => "nonredirects"
34 };
35 my $ns_map = {
36 allpages => "gapnamespace",
37 alllinks => "galnamespace",
38 allredirects => "garnamespace",
39 alltransclusions => "gatnamespace",
40 backlinks => "gblnamespace",
41 categorymembers => "gcmnamespace",
42 embeddedin => "geinamespace",
43 exturlusage => "geunamespace",
44 imageusage => "giunamespace",
45 iwbacklinks => undef,
46 langbacklinks => undef,
47 pageswithprop => undef,
48 prefixsearch => "gpsnamespace",
49 random => "grnnamespace",
50 recentchanges => "grcnamespace",
51 watchlist => "gwlnamespace",
52 watchlistraw => "gwrnamespace"
53 };
54
55 hasscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/has(). url => (
56 is => 'ro',
57 isa => sub { check_stringscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/check_string().($_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/`@_`.[0]); }
58 );
59 #cf. https://www.mediawiki.org/wiki/API:Login
60 hasscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/has(). lgname => ( is => 'ro' );
61 hasscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/has(). lgpassword => ( is => 'ro' );
62 #cf. http://www.mediawiki.org/wiki/API:Lists
63 #cf. http://www.mediawiki.org/wiki/API:Query#Generators
64 hasscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/has(). generate => (
65 is => 'ro',
66 isa => sub {
67 array_includesscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/array_includes().($generators,$_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/`@_`.[0]) or diescip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/die().("invalid generator");
68 },
69 lazy => 1,
70 default => sub { "allpages"; }
71 );
72 hasscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/has(). args => (
73 is => 'ro',
74 isa => sub { check_hash_refscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/check_hash_ref().($_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/`@_`.[0]); },
75 lazy => 1,
76 default => sub { $default_args },
77 coerce => sub {
78 my $l = $_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/`@_`.[0];
79 my $h = is_hash_refscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/is_hash_ref().($l) ? +{ %$default_args,%$l } : $default_args;
80 for(keysscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/keys(). %$h){
81 deletescip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/delete(). $h->{$_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/$_.} unless definedscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/defined(). $h->{$_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/$_.};
82 }
83 $h;
84 }
85 );
86 hasscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/has(). mediawiki => (
87 is => 'ro',
88 lazy => 1,
89 builder => '_build_mw'
90 );
91
92 withscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/with(). 'Catmandu::Importer';
93
94 sub _build_mwscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/_build_mw(). {
95 my $self = $_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/`@_`.[0];
96 my $mw = MediaWiki::API->newscip-perl cpan Catmandu-MediaWiki 0.021 MediaWiki/API/new().( { api_url => $self->urlscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/url().() } );
97
98 if(is_stringscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/is_string().($ENVscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/`%ENV`.{LWP_TRACE})){
99 $mw->{ua}->add_handlerscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/add_handler().("request_send", sub { shift->dumpscip-perl cpan Catmandu-MediaWiki 0.021 shift/dump().; return });
100 $mw->{ua}->add_handlerscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/add_handler().("response_done", sub { shift->dumpscip-perl cpan Catmandu-MediaWiki 0.021 shift/dump().; return });
101 }
102
103 $mw;
104 }
105 sub _failscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/_fail(). {
106 my $err = $_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/`@_`.[0];
107 diescip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/die().( $err->{code}.': '.$err->{details} );
108 }
109
110 sub generatorscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/generator(). {
111 my $self = $_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/`@_`.[0];
112
113 my $generator = $self->generatescip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/generate().();
114 my $args = $self->argsscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/args().();
115
116 sub {
117 state $mw = $self->mediawikiscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/mediawiki().();
118 state $pages = [];
119 state $cont_args = {};
120 state $logged_in = 0;
121 state $namespaces = [];
122 state $namespace_key = $ns_map->{ $generator };
123 state $siteinfo = {};
124
125 unless($logged_in){
126 #only try to login when both arguments are set
127 if(is_stringscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/is_string().($self->lgnamescip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/lgname().) && is_stringscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/is_string().($self->lgpasswordscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/lgpassword().)){
128 $mw->loginscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/login().({ lgname => $self->lgnamescip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/lgname()., lgpassword => $self->lgpasswordscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/lgpassword(). }) or _failscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/_fail().($mw->{error});
129 }
130 $logged_in = 1;
131 }
132
133 #store continue arguments per namespace
134 if(scalarscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/scalar().(@$namespaces) == 0){
135 #namespaces supported: true
136 if(definedscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/defined(). $namespace_key){
137
138 if(is_stringscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/is_string().($args->{ $namespace_key })){
139 pushscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/push(). @$namespaces,splitscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/split().(/\|/o,$args->{ $namespace_key });
140 }
141 else{
142 my $r = $mw->apiscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/api().({ action => "query", meta => "siteinfo", "siprop" => "namespaces|general" }) or _failscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/_fail().($mw->{error});
143 return unless definedscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/defined(). $r;
144 $siteinfo = $r->{query};
145 pushscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/push(). @$namespaces,sortscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/sort(). keysscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/keys().(%{ $siteinfo->{namespaces} });
146 }
147 $cont_args->{$_scip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/$_.} = { continue => '' } for @$namespaces;
148 }
149 #namespaces supported: false
150 else{
151
152 pushscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/push(). @$namespaces,"_none_";
153 $cont_args->{_none_} = { continue => '' };
154
155 }
156 }
157
158 return unless scalarscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/scalar().(@$namespaces);
159
160 if(scalarscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/scalar().(@$pages)==0){
161 #results for namespace are depleted: goto next
162 unless(definedscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/defined(). ($cont_args->{ $namespaces->[0] })){
163 shiftscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/shift(). @$namespaces;
164 }
165
166 return unless scalarscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/scalar().(@$namespaces);
167
168 #look until you found a decent namespace
169 my $res;
170 while(scalarscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/scalar().(@$namespaces)){
171 my $a = {
172 %$args,
173 %{$cont_args->{ $namespaces->[0] } },
174 action => "query",
175 indexpageids => 1,
176 generator => $generator,
177 format => "json"
178 };
179 if(definedscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/defined(). $namespace_key){
180 $a->{ $namespace_key } = $namespaces->[0];
181 }
182 #will work with generator in the future
183 deletescip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/delete(). $a->{rvlimit};
184 $res = $mw->apiscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/api().($a);
185 if(!$res){
186 if ( $mw->{error}->{details} =~ /gapunknown_gapnamespace/o || $mw->{error}->{details} =~ /API has returned an empty array reference/o ){
187 shiftscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/shift(). @$namespaces;
188 return unless scalarscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/scalar().(@$namespaces);
189 next;
190 }
191 _failscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/_fail().($mw->{error});
192 }else{
193 last;
194 }
195 }
196
197
198 return unless definedscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/defined(). $res;
199
200 $cont_args->{ $namespaces->[0] } = $res->{'continue'};
201
202 if(existsscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/exists().($res->{'query'}->{'pageids'})){
203
204 for my $pageid(@{ $res->{'query'}->{'pageids'} }){
205 #'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.'
206 #which means: cannot repeat pageids when asking for full history
207 my $page = $res->{'query'}->{'pages'}->{$pageid};
208
209 #add source url
210 my $title = $page->{title};
211 $title =~ s/\s/_/go;
212
213 {
214 my $articlepath = $siteinfo->{general}->{articlepath};
215 $articlepath =~ s/\$1/${title}/;
216
217 #server: http://localhost:8000
218 my $server = $siteinfo->{general}->{server};
219 #servername: localhost:8000
220 my $servername = $siteinfo->{general}->{servername};
221
222 my $url;
223 if( is_stringscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/is_string().($server) ){
224
225 $url = $server.$articlepath;
226
227 }
228 else{
229
230 my $base = $siteinfo->{general}->{base};
231 my $protocol = $base =~ /^https/o ? "https" : "http";
232 $url = "${protocol}://".$siteinfo->{general}->{servername}.$articlepath;
233
234 }
235
236 $page->{_url} = $url;
237
238 }
239
240 if(is_stringscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/is_string().($args->{rvlimit})){
241
242 my $a = {
243 action => "query",
244 format => "json",
245 pageids => $pageid,
246 prop => "revisions",
247 rvprop => $args->{rvprop},
248 rvlimit => $args->{rvlimit}
249 };
250 my $res2 = $mw->apiscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/api().($a) or _failscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/_fail().($mw->{error});
251 $page->{revisions} = $res2->{'query'}->{'pages'}->{$pageid}->{revisions} if $res2->{'query'}->{'pages'}->{$pageid}->{revisions};
252
253 #add source url
254 for my $revision(@{ $page->{revisions} } ){
255
256 $revision->{_url} = $page->{_url}."?oldid=".$revision->{revid};
257
258 }
259
260 }
261
262 pushscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/push(). @$pages,$page;
263 }
264 }
265 }
266
267 shiftscip-perl cpan Catmandu-MediaWiki 0.021 Catmandu/Importer/MediaWiki/shift(). @$pages;
268 };
269 }
270
271 =head1 NAME
272
273 Catmandu::Importer::MediaWiki - Catmandu importer that imports pages from mediawiki
274
275 =head1 DESCRIPTION
276
277 This importer uses the query api from mediawiki to get a list of pages
278 that match certain requirements.
279
280 It retrieves a list of pages and their content by using the generators
281 from mediawiki:
282
283 L<http://www.mediawiki.org/wiki/API:Query#Generators>
284
285 The default generator is 'allpages'.
286
287 The list could also be retrieved with the module 'list':
288
289 L<http://www.mediawiki.org/wiki/API:Lists>
290
291 But this module 'list' is very limited. It retrieves a list of pages
292 with a limited set of attributes (pageid, ns and title).
293
294 The module 'properties' on the other hand lets you add properties:
295
296 L<http://www.mediawiki.org/wiki/API:Properties>
297
298 But the selecting parameters (titles, pageids and revids) are too specific
299 to execute a query in one call. One should execute a list query, and then
300 use the pageids to feed them to the submodule 'properties'.
301
302 To execute a query, and add properties to the pages in one call can be
303 accomplished by use of generators.
304
305 L<http://www.mediawiki.org/wiki/API:Query#Generators>
306
307 These parameters are set automatically, and cannot be overwritten:
308
309 action = "query"
310 indexpageids = 1
311 generator = <generate>
312 format = "json"
313
314 Additional parameters can be set in the constructor argument 'args'.
315 Arguments for a generator origin from the list module with the same name,
316 but must be prepended with 'g'.
317
318 =head1 ARGUMENTS
319
320 =over 4
321
322 =item generate
323
324 type: string
325
326 explanation: type of generator to use. For a complete list, see L<http://www.mediawiki.org/wiki/API:Lists>.
327 because Catmandu::Iterable already defines 'generator', this parameter has been renamed
328 to 'generate'.
329
330 default: 'allpages'.
331
332 =item args
333
334 type: hash
335
336 explanation: extra arguments. These arguments are merged with the defaults.
337
338 default:
339
340 {
341 prop => "revisions",
342 rvprop => "ids|flags|timestamp|user|comment|size|content",
343 gaplimit => 100,
344 gapfilterredir => "nonredirects"
345 }
346
347 which means:
348
349 prop add revisions to the list of page attributes
350 rvprop specific properties for the list of revisions
351 gaplimit limit for generator 'allpages' (every 'generator' has its own limit).
352 gapfilterredir filter out redirect pages
353
354 =item lgname
355
356 type: string
357
358 explanation: login name. Only used when both lgname and lgpassword are set.
359
360 L<https://www.mediawiki.org/wiki/API:Login>
361
362 =item lgpassword
363
364 type: string
365
366 explanation: login password. Only used when both lgname and lgpassword are set.
367
368 =back
369
370 =head1 SYNOPSIS
371
372 use Catmandu::Sane;
373 use Catmandu::Importer::MediaWiki;
374
375 binmode STDOUT,":utf8";
376
377 my $importer = Catmandu::Importer::MediaWiki->new(
378 url => "http://en.wikipedia.org/w/api.php",
379 generate => "allpages",
380 args => {
381 prop => "revisions",
382 rvprop => "ids|flags|timestamp|user|comment|size|content",
383 gaplimit => 100,
384 gapprefix => "plato",
385 gapfilterredir => "nonredirects"
386 }
387 );
388 $importer->each(sub{
389 my $r = shift;
390 my $content = $r->{revisions}->[0]->{"*"};
391 say $r->{title};
392 });
393
394 =head1 AUTHORS
395
396 Nicolas Franck C<< <nicolas.franck at ugent.be> >>
397
398 =head1 SEE ALSO
399
400 L<Catmandu>, L<MediaWiki::API>
401
402 =cut
403
404 1;