line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MediaWiki::CleanupHTML; |
2
|
|
|
|
|
|
|
$MediaWiki::CleanupHTML::VERSION = '0.0.4'; |
3
|
1
|
|
|
1
|
|
66925
|
use 5.008; |
|
1
|
|
|
|
|
10
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
525
|
use HTML::TreeBuilder::XPath; |
|
1
|
|
|
|
|
65719
|
|
|
1
|
|
|
|
|
10
|
|
9
|
1
|
|
|
1
|
|
43
|
use Scalar::Util (qw(blessed)); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
701
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new |
14
|
|
|
|
|
|
|
{ |
15
|
1
|
|
|
1
|
1
|
11903
|
my $class = shift; |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
4
|
my $self = bless {}, $class; |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
7
|
$self->_init(@_); |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
|
|
6
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _is_processed |
25
|
|
|
|
|
|
|
{ |
26
|
3
|
|
|
3
|
|
9
|
my $self = shift; |
27
|
|
|
|
|
|
|
|
28
|
3
|
100
|
|
|
|
12
|
if (@_) |
29
|
|
|
|
|
|
|
{ |
30
|
2
|
|
|
|
|
64
|
$self->{_is_processed} = shift; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
|
|
12
|
return $self->{_is_processed}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _fh |
37
|
|
|
|
|
|
|
{ |
38
|
2
|
|
|
2
|
|
3
|
my $self = shift; |
39
|
|
|
|
|
|
|
|
40
|
2
|
100
|
|
|
|
6
|
if (@_) |
41
|
|
|
|
|
|
|
{ |
42
|
1
|
|
|
|
|
58
|
$self->{_fh} = shift; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
|
|
13
|
return $self->{_fh}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _tree |
49
|
|
|
|
|
|
|
{ |
50
|
7
|
|
|
7
|
|
15
|
my $self = shift; |
51
|
|
|
|
|
|
|
|
52
|
7
|
100
|
|
|
|
26
|
if (@_) |
53
|
|
|
|
|
|
|
{ |
54
|
2
|
|
|
|
|
64
|
$self->{_tree} = shift; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
7
|
|
|
|
|
1640
|
return $self->{_tree}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _init |
61
|
|
|
|
|
|
|
{ |
62
|
1
|
|
|
1
|
|
4
|
my ( $self, $args ) = @_; |
63
|
|
|
|
|
|
|
|
64
|
1
|
50
|
|
|
|
5
|
if ( !exists( $args->{'fh'} ) ) |
65
|
|
|
|
|
|
|
{ |
66
|
0
|
|
|
|
|
0
|
Carp::confess( |
67
|
|
|
|
|
|
|
"MediaWiki::CleanupHTML->new was not passed a filehandle."); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
6
|
$self->_fh( $args->{fh} ); |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
18
|
my $tree = HTML::TreeBuilder::XPath->new; |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
363
|
$self->_tree($tree); |
75
|
|
|
|
|
|
|
|
76
|
1
|
|
|
|
|
4
|
$self->_tree->parse_file( $self->_fh ); |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
968482
|
$self->_is_processed(0); |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
3
|
return; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _process |
84
|
|
|
|
|
|
|
{ |
85
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
1
|
50
|
|
|
|
3
|
if ( $self->_is_processed() ) |
88
|
|
|
|
|
|
|
{ |
89
|
0
|
|
|
|
|
0
|
return; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
1
|
|
|
|
|
4
|
my $tree = $self->_tree; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
{ |
95
|
1
|
|
|
|
|
14
|
my @nodes = $tree->findnodes('//div[@class="editsection"]'); |
96
|
|
|
|
|
|
|
|
97
|
1
|
|
|
|
|
729027
|
foreach my $n (@nodes) |
98
|
|
|
|
|
|
|
{ |
99
|
0
|
|
|
|
|
0
|
$n->detach(); |
100
|
0
|
|
|
|
|
0
|
$n->delete(); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
{ |
105
|
1
|
|
|
|
|
2
|
my @nodes = map { $tree->findnodes( '//h' . $_ ) } ( 2 .. 4 ); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
472211
|
|
106
|
|
|
|
|
|
|
|
107
|
1
|
|
|
|
|
230319
|
foreach my $h2 (@nodes) |
108
|
|
|
|
|
|
|
{ |
109
|
30
|
|
|
|
|
280
|
my $a_tag = $h2->left(); |
110
|
30
|
50
|
66
|
|
|
4608
|
if ( blessed($a_tag) |
|
|
|
33
|
|
|
|
|
111
|
|
|
|
|
|
|
&& $a_tag->tag() eq "a" |
112
|
|
|
|
|
|
|
&& $a_tag->attr('name') ) |
113
|
|
|
|
|
|
|
{ |
114
|
0
|
|
|
|
|
0
|
my $id = $a_tag->attr('name'); |
115
|
0
|
|
|
|
|
0
|
$h2->attr( 'id', $id ); |
116
|
0
|
|
|
|
|
0
|
$a_tag->detach(); |
117
|
0
|
|
|
|
|
0
|
$a_tag->delete(); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
1
|
|
|
|
|
8
|
my (@divs_to_delete) = ( |
123
|
|
|
|
|
|
|
$tree->findnodes('//div[@class="printfooter"]'), |
124
|
|
|
|
|
|
|
$tree->findnodes('//div[@id="catlinks"]'), |
125
|
|
|
|
|
|
|
$tree->findnodes('//div[@class="visualClear"]'), |
126
|
|
|
|
|
|
|
$tree->findnodes('//div[@id="column-one"]'), |
127
|
|
|
|
|
|
|
$tree->findnodes('//div[@id="footer"]'), |
128
|
|
|
|
|
|
|
$tree->findnodes('//head//style'), |
129
|
|
|
|
|
|
|
$tree->findnodes('//script'), |
130
|
|
|
|
|
|
|
); |
131
|
|
|
|
|
|
|
|
132
|
1
|
|
|
|
|
4096429
|
foreach my $div (@divs_to_delete) |
133
|
|
|
|
|
|
|
{ |
134
|
14
|
|
|
|
|
1272
|
$div->detach(); |
135
|
14
|
|
|
|
|
446
|
$div->delete(); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
1
|
|
|
|
|
16
|
$self->_is_processed(1); |
139
|
|
|
|
|
|
|
|
140
|
1
|
|
|
|
|
8
|
return; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub print_into_fh |
145
|
|
|
|
|
|
|
{ |
146
|
1
|
|
|
1
|
1
|
1843
|
my ( $self, $fh ) = @_; |
147
|
|
|
|
|
|
|
|
148
|
1
|
|
|
|
|
5
|
$self->_process(); |
149
|
|
|
|
|
|
|
|
150
|
1
|
|
|
|
|
2
|
print {$fh} $self->_tree->as_XML(); |
|
1
|
|
|
|
|
7
|
|
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub destroy_resources |
155
|
|
|
|
|
|
|
{ |
156
|
2
|
|
|
2
|
1
|
546639
|
my $self = shift; |
157
|
|
|
|
|
|
|
|
158
|
2
|
|
|
|
|
12
|
$self->_tree->delete(); |
159
|
1
|
|
|
|
|
42765
|
$self->_tree( undef() ); |
160
|
|
|
|
|
|
|
|
161
|
1
|
|
|
|
|
4
|
return; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub DESTROY |
165
|
|
|
|
|
|
|
{ |
166
|
1
|
|
|
1
|
|
633
|
my $self = shift; |
167
|
|
|
|
|
|
|
|
168
|
1
|
|
|
|
|
6
|
$self->destroy_resources(); |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
return; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; # End of MediaWiki::CleanupHTML |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
__END__ |