/usr/share/perl5/WWW/Wikipedia/Entry.pm is in libwww-wikipedia-perl 2.04-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package WWW::Wikipedia::Entry;
use 5.006;
use strict;
use warnings;
use Text::Autoformat;
use WWW::Wikipedia;
our $VERSION = '2.04';
=head1 NAME
WWW::Wikipedia::Entry - A class for representing a Wikipedia Entry
=head1 SYNOPSIS
my $wiki = WWW::Wikipedia->new();
my $entry = $wiki->search( 'Perl' );
print $entry->text();
my $entry_es = $entry->language( 'es' );
print $entry_es->text();
=head1 DESCRIPTION
WWW::Wikipedia::Entry objects are usually created using the search() method
on a WWW::Wikipedia object to search for a term. Once you've got an entry
object you can then extract pieces of information from the entry using
the following methods.
=head1 METHODS
=head2 new()
You probably won't use this one, it's the constructor that is called
behind the scenes with the correct arguments by WWW::Wikipedia::search().
=cut
sub new {
my ( $class, $raw, $src, %ops ) = @_;
return if length( $raw ) == 0;
my $self = bless {
raw => $raw,
src => $src,
text => '',
fulltext => '',
cursor => 0,
related => [],
categories => [],
headings => [],
languages => {},
currentlang => ''
},
ref( $class ) || $class;
$self->_parse();
# store un-"pretty"-ed version of text
$self->{ fulltext_basic } = $self->{ fulltext };
$self->{ text_basic } = $self->{ text };
if ($ops{clean_html}) {
$self->{ fulltext } = _clean_html( $self->{ fulltext });
$self->{ text } = _clean_html( $self->{ text });
}
$self->{ fulltext } = _pretty( $self->{ fulltext } );
$self->{ text } = _pretty( $self->{ text } );
return ( $self );
}
=head2 text()
The brief text for the entry. This will provide the first paragraph of
text; basically everything up to the first heading. Ordinarily this will
be what you want to use. When there doesn't appear to be summary text you
will be returned the fulltext instead.
If text() returns nothing then you probably are looking at a disambiguation
entry, and should use related() to lookup more specific entries.
=cut
sub text {
my $self = shift;
return $self->{ text } if $self->{ text };
return $self->fulltext();
}
=head2 text_basic()
The same as C<text()>, but not run through Text::Autoformat.
=cut
sub text_basic {
my $self = shift;
return $self->{ text_basic } if $self->{ text_basic };
return $self->fulltext_basic();
}
=head2 fulltext()
Returns the full text for the entry, which can be extensive.
=cut
sub fulltext {
my $self = shift;
return $self->{ fulltext };
}
=head2 fulltext_basic()
The same as C<fulltext()>, but not run through Text::Autoformat.
=cut
sub fulltext_basic {
my $self = shift;
return $self->{ fulltext_basic };
}
=head2 title()
Returns a title of the entry.
=cut
sub title {
my $self = shift;
return $self->{ title };
}
=head2 related()
Returns a list of terms in the wikipedia that are mentioned in the
entry text.
=cut
sub related {
return ( @{ shift->{ related } } );
}
=head2 categories()
Returns a list of categories which the entry is part of. So Perl is part
of the Programming languages category.
=cut
sub categories {
return ( @{ shift->{ categories } } );
}
=head2 headings()
Returns a list of headings used in the entry.
=cut
sub headings {
return ( @{ shift->{ headings } } );
}
=head2 raw()
Returns the raw wikitext for the entry.
=cut
sub raw {
my $self = shift;
return $self->{ raw };
}
=head2 language()
With no parameters, it will return the current language of the entry. By
specifying a two-letter language code, it will return the same entry in that
language, if available.
=cut
sub language {
my $self = shift;
my $lang = shift;
return $self->{ currentlang } unless defined $lang;
return undef unless exists $self->{ languages }->{ $lang };
my $wiki = WWW::Wikipedia->new( language => $lang );
return $wiki->search( $self->{ languages }->{ $lang } );
}
=head2 languages()
Returns an array of two letter language codes denoting the languages in which
this entry is available.
=cut
sub languages {
my $self = shift;
return keys %{ $self->{ languages } };
}
## messy internal routine for barebones parsing of wikitext
sub _parse {
my $self = shift;
my $raw = $self->{ raw };
my $src = $self->{ src };
# Add current language
my ( $lang ) = ( $src =~ /http:\/\/(..)/ );
my $title = ( split( /\//, $src ) )[ -1 ];
if( $title =~ m{\?title=} ) {
( $title ) = $src =~ m{\?title=([^\&]+)};
$title =~ s{_}{ }g;
}
$self->{ currentlang } = $lang;
$self->{ languages }->{ $lang } = $title;
$self->{ title } = $title;
for (
$self->{ cursor } = 0;
$self->{ cursor } < length( $raw );
$self->{ cursor }++
)
{
pos( $raw ) = $self->{ cursor };
## [[ ... ]]
if ( $raw =~ /\G\[\[ *(.*?) *\]\]/ ) {
my $directive = $1;
$self->{ cursor } += length( $& ) - 1;
if ( $directive =~ /\:/ ) {
my ( $type, $text ) = split /:/, $directive;
if ( lc( $type ) eq 'category' ) {
push( @{ $self->{ categories } }, $text );
}
# language codes
if ( length( $type ) == 2 and lc( $type ) eq $type ) {
$self->{ languages }->{ $type } = $text;
}
}
elsif ( $directive =~ /\|/ ) {
my ( $lookup, $name ) = split /\|/, $directive;
$self->{ fulltext } .= $name;
push( @{ $self->{ related } }, $lookup ) if $lookup !~ /^#/;
}
else {
$self->{ fulltext } .= $directive;
push( @{ $self->{ related } }, $directive );
}
}
## === heading 2 ===
elsif ( $raw =~ /\G=== *(.*?) *===/ ) {
### don't bother storing these headings
$self->{ fulltext } .= $1;
$self->{ cursor } += length( $& ) - 1;
next;
}
## == heading 1 ==
elsif ( $raw =~ /\G== *(.*?) *==/ ) {
push( @{ $self->{ headings } }, $1 );
$self->{ text } = $self->{ fulltext } if !$self->{ seenHeading };
$self->{ seenHeading } = 1;
$self->{ fulltext } .= $1;
$self->{ cursor } += length( $& ) - 1;
next;
}
## '' italics ''
elsif ( $raw =~ /\G'' *(.*?) *''/ ) {
$self->{ fulltext } .= $1;
$self->{ cursor } += length( $& ) - 1;
next;
}
## {{ disambig }}
elsif ( $raw =~ /\G\{\{ *(.*?) *\}\}/ ) {
## ignore for now
$self->{ cursor } += length( $& ) - 1;
next;
}
else {
$self->{ fulltext } .= substr( $raw, $self->{ cursor }, 1 );
}
}
}
# future versions might clean tag contents for some specific ones.
sub _clean_html {
my $text = shift;
# force first letter so that standalone < might be kept
$text =~ s{<[/a-zA-Z!][^>]+>}{}g;
return $text;
}
sub _pretty {
my $text = shift;
# Text::Autoformat v1.13 chokes on strings that are one or more "\n"
return '' if $text =~ m/^\n+$/;
return autoformat(
$text,
{ left => 0,
right => 80,
justify => 'left',
all => 1
}
);
}
=head1 AUTHORS
Ed Summers E<lt>ehs@pobox.comE<gt>
Brian Cassidy E<lt>bricas@cpan.orgE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2003-2015 by Ed Summers
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
|