| 1 | package Catmandu::Importer::MediaWiki; | ||||||||||
| 2 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use MediaWiki::API; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Catmandu::Util qw(:is :check array_includes); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use URI::Escape; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | #only generators that generate pages, and that have generators | ||||||||||
| 9 | 1 | 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 | 1 | 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 | 1 | 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 | has url => ( | ||||||||||
| 56 | is => 'ro', | ||||||||||
| 57 | 0 | ▶ | isa => sub { check_string($_[0]); } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 58 | 1 | ); | |||||||||
| 59 | #cf. https://www.mediawiki.org/wiki/API:Login | ||||||||||
| 60 | 1 | has lgname => ( is => 'ro' ); | |||||||||
| 61 | 1 | has lgpassword => ( is => 'ro' ); | |||||||||
| 62 | #cf. http://www.mediawiki.org/wiki/API:Lists | ||||||||||
| 63 | #cf. http://www.mediawiki.org/wiki/API:Query#Generators | ||||||||||
| 64 | has generate => ( | ||||||||||
| 65 | is => 'ro', | ||||||||||
| 66 | 0 | ▶ | isa => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 67 | 0 | ▶ | array_includes($generators,$_[0]) or die("invalid generator"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | }, | ||||||||||
| 69 | lazy => 1, | ||||||||||
| 70 | 0 | ▶ | default => sub { "allpages"; } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 71 | 1 | ); | |||||||||
| 72 | has args => ( | ||||||||||
| 73 | is => 'ro', | ||||||||||
| 74 | 0 | ▶ | isa => sub { check_hash_ref($_[0]); }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 75 | lazy => 1, | ||||||||||
| 76 | 0 | ▶ | default => sub { $default_args }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 77 | 0 | ▶ | coerce => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 78 | 0 | my $l = $_[0]; | |||||||||
| 79 | 0 | ▶ | my $h = is_hash_ref($l) ? +{ %$default_args,%$l } : $default_args; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | for(keys %$h){ | |||||||||
| 81 | 0 | ▶ | delete $h->{$_} unless defined $h->{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | } | ||||||||||
| 83 | 0 | $h; | |||||||||
| 84 | } | ||||||||||
| 85 | 1 | ); | |||||||||
| 86 | 1 | has mediawiki => ( | |||||||||
| 87 | is => 'ro', | ||||||||||
| 88 | lazy => 1, | ||||||||||
| 89 | builder => '_build_mw' | ||||||||||
| 90 | ); | ||||||||||
| 91 | |||||||||||
| 92 | 1 | with 'Catmandu::Importer'; | |||||||||
| 93 | |||||||||||
| 94 | 0 | ▶ | sub _build_mw { | ||||||||
|
Subroutine
_build_mw: not called CC=2 SCAR=17.9
| |||||||||||
| 95 | 0 | my $self = $_[0]; | |||||||||
| 96 | 0 | my $mw = MediaWiki::API->new( { api_url => $self->url() } ); | |||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | if(is_string($ENV{LWP_TRACE})){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | ▶ | $mw->{ua}->add_handler("request_send", sub { shift->dump; return }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 100 | 0 | ▶ | $mw->{ua}->add_handler("response_done", sub { shift->dump; return }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 0 | $mw; | |||||||||
| 104 | } | ||||||||||
| 105 | 0 | ▶ | sub _fail { | ||||||||
|
Subroutine
_fail: not called CC=1 SCAR=6.9
| |||||||||||
| 106 | 0 | my $err = $_[0]; | |||||||||
| 107 | 0 | die( $err->{code}.': '.$err->{details} ); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | sub generator { | ||||||||||
| 111 | my $self = $_[0]; | ||||||||||
| 112 | |||||||||||
| 113 | my $generator = $self->generate(); | ||||||||||
| 114 | my $args = $self->args(); | ||||||||||
| 115 | |||||||||||
| 116 | sub { | ||||||||||
| 117 | state $mw = $self->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_string($self->lgname) && is_string($self->lgpassword)){ | ||||||||||
| 128 | $mw->login({ lgname => $self->lgname, lgpassword => $self->lgpassword }) or _fail($mw->{error}); | ||||||||||
| 129 | } | ||||||||||
| 130 | $logged_in = 1; | ||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | #store continue arguments per namespace | ||||||||||
| 134 | if(scalar(@$namespaces) == 0){ | ||||||||||
| 135 | #namespaces supported: true | ||||||||||
| 136 | if(defined $namespace_key){ | ||||||||||
| 137 | |||||||||||
| 138 | if(is_string($args->{ $namespace_key })){ | ||||||||||
| 139 | push @$namespaces,split(/\|/o,$args->{ $namespace_key }); | ||||||||||
| 140 | } | ||||||||||
| 141 | else{ | ||||||||||
| 142 | my $r = $mw->api({ action => "query", meta => "siteinfo", "siprop" => "namespaces|general" }) or _fail($mw->{error}); | ||||||||||
| 143 | return unless defined $r; | ||||||||||
| 144 | $siteinfo = $r->{query}; | ||||||||||
| 145 | push @$namespaces,sort keys(%{ $siteinfo->{namespaces} }); | ||||||||||
| 146 | } | ||||||||||
| 147 | $cont_args->{$_} = { continue => '' } for @$namespaces; | ||||||||||
| 148 | } | ||||||||||
| 149 | #namespaces supported: false | ||||||||||
| 150 | else{ | ||||||||||
| 151 | |||||||||||
| 152 | push @$namespaces,"_none_"; | ||||||||||
| 153 | $cont_args->{_none_} = { continue => '' }; | ||||||||||
| 154 | |||||||||||
| 155 | } | ||||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | return unless scalar(@$namespaces); | ||||||||||
| 159 | |||||||||||
| 160 | if(scalar(@$pages)==0){ | ||||||||||
| 161 | #results for namespace are depleted: goto next | ||||||||||
| 162 | unless(defined ($cont_args->{ $namespaces->[0] })){ | ||||||||||
| 163 | shift @$namespaces; | ||||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | return unless scalar(@$namespaces); | ||||||||||
| 167 | |||||||||||
| 168 | #look until you found a decent namespace | ||||||||||
| 169 | my $res; | ||||||||||
| 170 | while(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(defined $namespace_key){ | ||||||||||
| 180 | $a->{ $namespace_key } = $namespaces->[0]; | ||||||||||
| 181 | } | ||||||||||
| 182 | #will work with generator in the future | ||||||||||
| 183 | delete $a->{rvlimit}; | ||||||||||
| 184 | $res = $mw->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 | shift @$namespaces; | ||||||||||
| 188 | return unless scalar(@$namespaces); | ||||||||||
| 189 | next; | ||||||||||
| 190 | } | ||||||||||
| 191 | _fail($mw->{error}); | ||||||||||
| 192 | }else{ | ||||||||||
| 193 | last; | ||||||||||
| 194 | } | ||||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | |||||||||||
| 198 | return unless defined $res; | ||||||||||
| 199 | |||||||||||
| 200 | $cont_args->{ $namespaces->[0] } = $res->{'continue'}; | ||||||||||
| 201 | |||||||||||
| 202 | if(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_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_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->api($a) or _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 | push @$pages,$page; | ||||||||||
| 263 | } | ||||||||||
| 264 | } | ||||||||||
| 265 | } | ||||||||||
| 266 | |||||||||||
| 267 | 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 | 1; | |||||||||