line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MediaWiki::CleanupHTML; |
2
|
|
|
|
|
|
|
$MediaWiki::CleanupHTML::VERSION = '0.0.6'; |
3
|
1
|
|
|
1
|
|
69815
|
use 5.008; |
|
1
|
|
|
|
|
11
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
539
|
use HTML::TreeBuilder::XPath (); |
|
1
|
|
|
|
|
68096
|
|
|
1
|
|
|
|
|
31
|
|
9
|
1
|
|
|
1
|
|
7
|
use Scalar::Util (qw(blessed)); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
726
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new |
14
|
|
|
|
|
|
|
{ |
15
|
1
|
|
|
1
|
1
|
17136
|
my $class = shift; |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
3
|
my $self = bless {}, $class; |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
5
|
$self->_init(@_); |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
|
|
7
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _is_processed |
25
|
|
|
|
|
|
|
{ |
26
|
3
|
|
|
3
|
|
10
|
my $self = shift; |
27
|
|
|
|
|
|
|
|
28
|
3
|
100
|
|
|
|
12
|
if (@_) |
29
|
|
|
|
|
|
|
{ |
30
|
2
|
|
|
|
|
58
|
$self->{_is_processed} = shift; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
|
|
14
|
return $self->{_is_processed}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _fh |
37
|
|
|
|
|
|
|
{ |
38
|
2
|
|
|
2
|
|
4
|
my $self = shift; |
39
|
|
|
|
|
|
|
|
40
|
2
|
100
|
|
|
|
6
|
if (@_) |
41
|
|
|
|
|
|
|
{ |
42
|
1
|
|
|
|
|
5
|
$self->{_fh} = shift; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
|
|
10
|
return $self->{_fh}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _tree |
49
|
|
|
|
|
|
|
{ |
50
|
8
|
|
|
8
|
|
19
|
my $self = shift; |
51
|
|
|
|
|
|
|
|
52
|
8
|
100
|
|
|
|
27
|
if (@_) |
53
|
|
|
|
|
|
|
{ |
54
|
2
|
|
|
|
|
6
|
$self->{_tree} = shift; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
8
|
|
|
|
|
46
|
return $self->{_tree}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _init |
61
|
|
|
|
|
|
|
{ |
62
|
1
|
|
|
1
|
|
3
|
my ( $self, $args ) = @_; |
63
|
|
|
|
|
|
|
|
64
|
1
|
50
|
|
|
|
4
|
if ( !exists( $args->{'fh'} ) ) |
65
|
|
|
|
|
|
|
{ |
66
|
0
|
|
|
|
|
0
|
Carp::confess( |
67
|
|
|
|
|
|
|
"MediaWiki::CleanupHTML->new was not passed a filehandle."); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
5
|
$self->_fh( $args->{fh} ); |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
10
|
my $tree = HTML::TreeBuilder::XPath->new; |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
326
|
$self->_tree($tree); |
75
|
|
|
|
|
|
|
|
76
|
1
|
|
|
|
|
4
|
$self->_tree->parse_file( $self->_fh ); |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
966905
|
$self->_is_processed(0); |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
2
|
return; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _process |
84
|
|
|
|
|
|
|
{ |
85
|
1
|
|
|
1
|
|
3
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
1
|
50
|
|
|
|
3
|
if ( $self->_is_processed() ) |
88
|
|
|
|
|
|
|
{ |
89
|
0
|
|
|
|
|
0
|
return; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
1
|
|
|
|
|
6
|
my $tree = $self->_tree; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
{ |
95
|
1
|
|
|
|
|
15
|
my @nodes = $tree->findnodes('//div[@class="editsection"]'); |
96
|
|
|
|
|
|
|
|
97
|
1
|
|
|
|
|
724603
|
foreach my $n (@nodes) |
98
|
|
|
|
|
|
|
{ |
99
|
0
|
|
|
|
|
0
|
$n->detach(); |
100
|
0
|
|
|
|
|
0
|
$n->delete(); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
{ |
105
|
1
|
|
|
|
|
3
|
my @nodes = map { $tree->findnodes( '//h' . $_ ) } ( 2 .. 4 ); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
462613
|
|
106
|
|
|
|
|
|
|
|
107
|
1
|
|
|
|
|
227970
|
foreach my $h2 (@nodes) |
108
|
|
|
|
|
|
|
{ |
109
|
30
|
|
|
|
|
262
|
my $a_tag = $h2->left(); |
110
|
30
|
50
|
66
|
|
|
4612
|
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
|
|
|
|
|
7
|
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
|
|
|
|
|
4073306
|
foreach my $div (@divs_to_delete) |
133
|
|
|
|
|
|
|
{ |
134
|
14
|
|
|
|
|
1280
|
$div->detach(); |
135
|
14
|
|
|
|
|
442
|
$div->delete(); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
1
|
|
|
|
|
14
|
$self->_is_processed(1); |
139
|
|
|
|
|
|
|
|
140
|
1
|
|
|
|
|
8
|
return; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub print_into_fh |
145
|
|
|
|
|
|
|
{ |
146
|
1
|
|
|
1
|
1
|
1846
|
my ( $self, $fh ) = @_; |
147
|
|
|
|
|
|
|
|
148
|
1
|
|
|
|
|
5
|
$self->_process(); |
149
|
|
|
|
|
|
|
|
150
|
1
|
|
|
|
|
2
|
print {$fh} $self->_tree->as_XML(); |
|
1
|
|
|
|
|
6
|
|
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub destroy_resources |
155
|
|
|
|
|
|
|
{ |
156
|
2
|
|
|
2
|
1
|
551302
|
my $self = shift; |
157
|
|
|
|
|
|
|
|
158
|
2
|
100
|
|
|
|
8
|
if ( defined( $self->_tree() ) ) |
159
|
|
|
|
|
|
|
{ |
160
|
1
|
|
|
|
|
4
|
$self->_tree->delete(); |
161
|
1
|
|
|
|
|
42476
|
$self->_tree( undef() ); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
2
|
|
|
|
|
57
|
return; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub DESTROY |
168
|
|
|
|
|
|
|
{ |
169
|
1
|
|
|
1
|
|
604
|
my $self = shift; |
170
|
|
|
|
|
|
|
|
171
|
1
|
|
|
|
|
6
|
$self->destroy_resources(); |
172
|
|
|
|
|
|
|
|
173
|
1
|
|
|
|
|
163
|
return; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
1; # End of MediaWiki::CleanupHTML |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
__END__ |