Wiki.pm

filter: Statementstmt 13.9% 41 / 295 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 40.0% 12 / 30 Podpod 0.0% 0 / 15 totaltotal 11.9% 53 / 444 CC 51 SCAR 35.6
CC
51
Cov
10%
parse_search
110.0
_save_page
72.0
preview_page
42.0
CRAP 35.1
↑ Summary Wiki/Indexer.pm »
1 package AxKit::XSP::Wiki;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 #use Apache::AxKit::Language::XSP::TaglibHelper;
6 1 ▶use AxKit::XSP::Wiki::Indexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Text::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use vars qw($VERSION $NS @ISA @EXPORT_TAGLIB $EmailAlerts $EmailHost);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 $VERSION = '0.07';
13  
14 # The namespace associated with this taglib.
15 1 $NS = 'http://axkit.org/NS/xsp/wiki/1';
16 # Using TaglibHelper:
17 1 @ISA = qw(Apache::AxKit::Language::XSP::TaglibHelper);
18  
19 1 @EXPORT_TAGLIB = (
20 'display_page($dbpath,$db,$page,$action;$id):as_xml=1',
21 'preview_page($dbpath,$db,$page,$text,$texttype):as_xml=1',
22 'search($dbpath,$db,$query):as_xml=1',
23 );
24  
25 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use XML::SAX::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Pod::SAX;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use XML::LibXML::SAX::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Text::WikiFormat::SAX;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 0 ▶sub _mkdb {
Subroutine _mkdb: not called CC=2 SCAR=17.9
32 0 my ($dbpath, $dbname) = @_;
33 0 my $db = DBI->connect(
34 'DBI:SQLite:dbname='. $dbpath . '/wiki-' . $dbname . '.db',
35 '', '', { AutoCommit => 1, RaiseError => 1 }
36 );
37
38 0 eval {
39 0 $db->do('select * from Page, Formatter, History where 1 = 2');
40 };
41 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
42 0 create_db($db);
43 }
44
45 0 return $db;
46 }
47  
48 0 ▶sub display_page ($$$$$) {
Subroutine display_page: not called CC=5 SCAR=34.0
Pod undocumented
49 0 my ($dbpath, $dbname, $page, $action, $id) = @_;
50
51 0 my $db = _mkdb($dbpath, $dbname);
52
53 0 ▶ if ($action eq 'edit') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'edit') { } 0 0 0
elsif ($action eq 'history') { } 0 0 0
elsif ($action eq 'historypage') { } 0 0 0
54 0 return edit_page($db, $page);
55 }
56 elsif ($action eq 'history') {
57 0 return show_history($db, $page);
58 }
59 elsif ($action eq 'historypage') {
60 0 return show_history_page($db, $page, $id);
61 }
62 0 ▶ if ($action eq 'view') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq 'view') { } 0 0 0
63 0 return view_page($db, $page);
64 }
65 else {
66 0 warn("Unrecognised action. Falling back to 'view'");
67 0 return view_page($db, $page);
68 }
69 }
70  
71 0 ▶sub preview_page ($$$$$) {
Subroutine preview_page: not called CC=6 SCAR=37.4
Pod undocumented
72 0 my ($dbpath, $dbname, $page, $text, $texttype) = @_;
73 0 my $db = _mkdb($dbpath, $dbname);
74 0 my $sth = $db->prepare(<<'EOT');
75 SELECT Formatter.module
76 FROM Formatter
77 WHERE Formatter.id = ?
78 EOT
79 0 $sth->execute($texttype);
80
81 0 my $output = '';
82 0 my $handler = XML::SAX::Writer->new(Output => \$output);
83 0 ▶ while ( my $row = $sth->fetch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetch) 0 0 0
84 # create the parser
85 0 my $parser = $row->[0]->new(Handler => $handler);
86 0 eval {
87 0 $parser->parse_string($text);
88 };
89 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
90 0 $output = '<pod>
91 <para>
92 Error parsing the page: ' . xml_escape($@) . '
93 </para>
94 </pod>
95 ';
96 }
97 0 last;
98 }
99 0 ▶ if (!$output) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output) 0 0 0
100 0 $output = <<'EOT';
101 <pod>
102 <para>
103 Eek.
104 </para>
105 </pod>
106 EOT
107 }
108  
109 0 $output =~ s/^<\?xml\s.*?\?>//s;
110  
111 # Now add edit stuff
112 0 $output .= '<edit><text>';
113 0 $output .= xml_escape($text);
114 0 $output .= '</text><texttypes>';
115
116 0 $sth = $db->prepare(<<'EOT');
117 SELECT Formatter.id, Formatter.name
118 FROM Formatter
119 EOT
120 0 $sth->execute();
121 0 ▶ while (my $row = $sth->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetch) 0 0 0
122 0 ▶ $output .= '<texttype id="'. xml_escape($row->[0]) .
Branch: 1 branch
BranchTrueFalseTotal
$texttype == $row->[0] ? : 0 0 0
123 ($texttype == $row->[0] ? '" selected="selected">' : '">') .
124 xml_escape($row->[1]) . '</texttype>';
125 }
126 0 $sth->finish;
127
128 0 $output .= '</texttypes></edit>';
129  
130 0 return $output;
131 } # preview
132  
133 0 ▶sub view_page {
Subroutine view_page: not called CC=5 SCAR=34.0
Pod undocumented
134 0 my ($db, $page) = @_;
135 0 my $sth = $db->prepare(<<'EOT');
136 SELECT Page.content, Formatter.module
137 FROM Page, Formatter
138 WHERE Page.formatterid = Formatter.id
139 AND Page.name = ?
140 EOT
141 0 $sth->execute($page);
142
143 0 my $output = '';
144 0 my $handler = XML::SAX::Writer->new(Output => \$output);
145 0 ▶ while ( my $row = $sth->fetch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetch) 0 0 0
146 # create the parser
147 0 my $parser = $row->[1]->new(Handler => $handler);
148 0 eval {
149 0 $parser->parse_string($row->[0]);
150 };
151 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
152 0 $output = '<pod>
153 <para>
154 Error parsing the page: ' . xml_escape($@) . '
155 </para>
156 </pod>
157 ';
158 }
159 0 last;
160 }
161 0 ▶ if (!$output) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output) 0 0 0
162 0 $output = <<'EOT';
163 <newpage/>
164 EOT
165 }
166 0 $output =~ s/^<\?xml\s.*?\?>//s;
167 0 ▶ AxKit::Debug(10, "Wiki Got: $output") if $ENV{MOD_PERL};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOD_PERL"} 0 0 0
168 0 return $output;
169 }
170  
171 0 ▶sub xml_escape {
Subroutine xml_escape: not called CC=1 SCAR=6.9
Pod undocumented
172 0 my $text = shift;
173 0 $text =~ s/&/&amp;/g;
174 0 $text =~ s/</&lt;/g;
175 0 $text =~ s/]]>/]]&gt;/g;
176 0 return $text;
177 }
178  
179 0 ▶sub get_default_formatter {
Subroutine get_default_formatter: not called CC=2 SCAR=17.9
Pod undocumented
180 0 my ($db) = @_;
181 0 my $sth = $db->prepare("SELECT id FROM Formatter WHERE name LIKE ?");
182 0 $sth->execute("pod%");
183 0 ▶ while (my $row = $sth->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetch) 0 0 0
184 0 return $row->[0];
185 }
186 0 die "No rows from Formatter table!";
187 }
188  
189 0 ▶sub edit_page {
Subroutine edit_page: not called CC=4 SCAR=30.0
Pod undocumented
190 0 my ($db, $page) = @_;
191 0 my $sth = $db->prepare(<<'EOT');
192 SELECT Page.content, Page.formatterid
193 FROM Page
194 WHERE Page.name = ?
195 EOT
196 0 $sth->execute($page);
197
198 0 my $output = '<edit><text>';
199 0 my $formatter = get_default_formatter($db);
200 0 ▶ while ( my $row = $sth->fetch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetch) 0 0 0
201 # create the parser
202 0 $output .= xml_escape($row->[0]);
203 0 $formatter = $row->[1];
204 0 last;
205 }
206 0 $sth->finish;
207
208 0 $output .= '</text><texttypes>';
209
210 0 $sth = $db->prepare(<<'EOT');
211 SELECT Formatter.id, Formatter.name
212 FROM Formatter
213 EOT
214 0 $sth->execute();
215 0 ▶ while (my $row = $sth->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetch) 0 0 0
216 0 ▶ $output .= '<texttype id="'. xml_escape($row->[0]) .
Branch: 1 branch
BranchTrueFalseTotal
$formatter == $row->[0] ? : 0 0 0
217 ($formatter == $row->[0] ? '" selected="selected">' : '">') .
218 xml_escape($row->[1]) . '</texttype>';
219 }
220 0 $sth->finish;
221
222 0 $output .= '</texttypes></edit>';
223 0 return $output;
224 }
225  
226 0 ▶sub search {
Subroutine search: not called CC=3 SCAR=21.7
Pod undocumented
227 0 my ($dbpath, $dbname, $query) = @_;
228 0 my $db = _mkdb($dbpath, $dbname);
229 0 my %search = parse_search($query);
230 0 ▶ use Data::Dumper; warn(Dumper(\%search));
Subroutine BEGIN: called CC=1 SCAR=0.2
231 my $results = search_message_index( db => $db,
232 required => $search{required},
233 normal => $search{normal},
234 phrase => $search{phrase},
235 excluded => $search{excluded},
236 0 );
237 0 my $output = '<search-results>';
238 0 ▶ if (!@{$results}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$results;}) 0 0 0
239 0 $output .= '<no-results/>';
240 }
241 0 foreach my $row (sort { $b->[1] <=> $a->[1] } @{$results}) {
242 0 $output .= "<result><page>" . xml_escape($row->[0]) . "</page>";
243 0 $output .= "<rank>" . xml_escape($row->[1]) . "</rank></result>";
244 }
245 0 $output .= "</search-results>";
246 0 warn("Search results: $output\n");
247 0 return $output;
248 }
249  
250 0 ▶sub search_message_index {
Subroutine search_message_index: not called CC=2 SCAR=17.9
Pod undocumented
251 0 my %p = @_;
252
253 0 my $db = $p{db};
254
255 # Excluded words are excluded from all pages
256 0 my $exclude = '';
257 0 ▶ if ( @{$p{excluded}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$p{"excluded"};}) 0 0 0
258 $exclude .= " AND Page.name NOT IN (
259 SELECT DISTINCT Page.name
260 FROM Page, ContentIndex, Word
261 WHERE ContentIndex.page_id = Page.id
262 AND ContentIndex.word_id = Word.id
263 AND Word.word IN (" .
264 0 join(',', map { $db->quote($_) } @{$p{excluded}}) .
265 ")
266 )\n";
267 }
268  
269 my $sql = "
270 SELECT Page.name, SUM(ContentIndex.value) AS value
271 FROM ContentIndex, Page, Word
272 WHERE ContentIndex.page_id = Page.id
273 AND ContentIndex.word_id = Word.id
274 AND (" .
275 join(" OR ", (
276 0 (map { "Word.word = " . $db->quote($_) } @{$p{required}}),
277 0 (map { "Page.content LIKE " . $db->quote("\%$_\%") } @{$p{phrase}}),
278 )) .
279 ")
280 $exclude
281 GROUP BY ContentIndex.page_id
282 ";
283 0 warn("Getting required with:\n$sql\n");
284 0 return $db->selectall_arrayref($sql);
285 }
286  
287  
288  
289 0 ▶sub parse_search {
Subroutine parse_search: not called CC=10 SCAR=47.0
Pod undocumented
290 0 my $query = shift;
291 0 my %search;
292 0 ▶ while (defined $query && $query =~ /\G(\S*)(\s*)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query and $query =~ /\G(\S*)(\s*)/cg) 0 0 0
Condition0%0/3
defined $query and $query =~ /\G(\S*)(\s*)/cg
!ll&&!rl&&r
000
MC/DC0%0/2
defined $query and $query =~ /\G(\S*)(\s*)/cg
defined $query $query =~ /\G(\S*)(\s*)/cg
Truth table0%0/3
A and B
A= defined $queryB= $query =~ /\G(\S*)(\s*)/cg
ABresult
0X0
100
111
293 0 my $term = $1;
294 0 my $space = $2;
295 0 ▶ next unless length($term);
Branch: 1 branch
BranchTrueFalseTotal
unless length $term 0 0 0
296  
297 0 $term = lc($term);
298
299 0 ▶ if ($term =~ s/^\+//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($term =~ s/^\+//) { } 0 0 0
elsif ($term =~ s/^\-//) { } 0 0 0
elsif ($term =~ /^(["'])/) { } 0 0 0
300 0 $search{required}{$term}++;
301 0 warn "Search required: $term\n";
302 }
303 elsif ($term =~ s/^\-//) {
304 0 $search{excluded}{$term}++;
305 0 warn "Search excluded: $term\n";
306 }
307 elsif ($term =~ /^(["'])/) {
308 0 my $quote = $1;
309 0 $term =~ s/^$quote//;
310 0 $term .= $space;
311  
312 0 ▶ if ($query =~ /\G(.*?)\.?$quote\s*/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query =~ /\G(.*?)\.?$quote\s*/cg) 0 0 0
313 0 $term .= $1;
314 0 $search{phrase}{$term}++;
315 0 warn "Search phrase: $term\n";
316 }
317 }
318 else {
319 0 $search{required}{$term}++;
320 0 warn "Search normal: $term\n";
321 }
322 }
323  
324 # turn into arrayrefs
325 0 foreach ( qw( normal required excluded phrase ) )
326 {
327 0 ▶ if ( $search{$_} )
Branch: 1 branch
BranchTrueFalseTotal
if ($search{$_}) { } 0 0 0
328 {
329 0 $search{$_} = [ keys %{ $search{$_} } ]
330 }
331 else
332 {
333 0 $search{$_} = [];
334 }
335 }
336  
337 0 return %search;
338 }
339  
340 0 ▶sub save_page {
Subroutine save_page: not called CC=1 SCAR=6.9
Pod undocumented
341 0 my ($dbpath, $dbname, $page, $contents, $texttype, $ip, $user) = @_;
342 0 my $db = _mkdb($dbpath, $dbname);
343 0 _save_page($db, $page, $contents, $texttype, $ip, $user);
344 }
345  
346 0 ▶sub _save_page {
Subroutine _save_page: not called CC=8 SCAR=42.8
347 0 my ($db, $page, $contents, $texttype, $ip, $user) = @_;
348 # NB fix hard coded formatterid
349 0 my $last_modified = time;
350 0 my @history = $db->selectrow_array('SELECT content FROM History WHERE name = ? ORDER BY modified DESC', {}, $page);
351 0 local $db->{AutoCommit} = 0;
352 0 $db->do(<<'EOT', {}, $page, $texttype, $contents, $last_modified, $ip, $user);
353 INSERT OR REPLACE INTO Page ( name, formatterid, content, last_modified, ip_address, username )
354 VALUES ( ?, ?, ?, ?, ?, ? )
355 EOT
356 0 $db->do(<<'EOT', {}, $page, $texttype, $contents, $last_modified, $ip, $user);
357 INSERT INTO History ( name, formatterid, content, modified, ip_address, username )
358 VALUES ( ?, ?, ?, ?, ?, ? )
359 EOT
360 0 $db->commit;
361 0 _index_page($db, $page);
362 0 ▶ if ($EmailAlerts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($AxKit::XSP::Wiki::EmailAlerts) 0 0 0
363 # create diff using Text::Diff
364 0 ▶ my $prev = @history ? $history[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
@history ? : 0 0 0
365 0 my $diff = diff(\$prev, \$contents, { STYLE => 'Unified' });
366
367 0 ▶ my $host = $EmailHost || 'localhost';
Condition0%0/2
$AxKit::XSP::Wiki::EmailHost || 'localhost'
l!l
00
MC/DC0%0/1
$AxKit::XSP::Wiki::EmailHost || 'localhost'
$AxKit::XSP::Wiki::EmailHost
Truth table0%0/2
A
A= $AxKit::XSP::Wiki::EmailHost
Aresult
11
00
368 0 my $smtp = Net::SMTP->new($host, Timeout => 10);
369 0 ▶ $smtp->mail('axkitwiki') || die "Wiki email alerts: MAIL FROM:<axkitwiki> failed";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->mail("axkitwiki") or die "Wiki email alerts: MAIL FROM:<axkitwiki> failed" 0 0 0
370 0 ▶ $smtp->to($EmailAlerts) || die "Wiki email alerts: RCPT TO:<$EmailAlerts> failed";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->to($AxKit::XSP::Wiki::EmailAlerts) or die "Wiki email alerts: RCPT TO:<$AxKit::XSP::Wiki::EmailAlerts> failed" 0 0 0
371 0 ▶ $smtp->data() || die "Wiki email alerts: DATA failed";
Branch: 1 branch
BranchTrueFalseTotal
$smtp->data or die "Wiki email alerts: DATA failed" 0 0 0
372 0 my $date = strftime('%a, %d %b %Y %H:%M:%S %Z', localtime);
373
374 0 ▶ my $changed_by = $user ? "$user @ $ip" : "someone at IP $ip";
Branch: 1 branch
BranchTrueFalseTotal
$user ? : 0 0 0
375 0 $smtp->datasend(<<"EOT");
376 To: $EmailAlerts
377 From: "AxKit Wiki" <axkitwiki>
378 Subject: New Wiki Content at $page
379 Date: $date
380  
381 Wiki content at $page Changed by $changed_by :
382  
383 $diff
384  
385 EOT
386 0 $smtp->dataend();
387 0 $smtp->quit();
388 }
389 }
390  
391 0 ▶sub _index_page {
Subroutine _index_page: not called CC=3 SCAR=24.8
392 0 my ($db, $page) = @_;
393 0 my $sth = $db->prepare(<<'EOT');
394 SELECT Page.id, Page.content, Formatter.module
395 FROM Page, Formatter
396 WHERE Page.formatterid = Formatter.id
397 AND Page.name = ?
398 EOT
399 0 $sth->execute($page);
400
401 0 my $output = '';
402 0 ▶ while ( my $row = $sth->fetch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetch) 0 0 0
403 0 my $handler = AxKit::XSP::Wiki::Indexer->new(DB => $db, PageId => $row->[0]);
404 # create the parser
405 0 my $parser = $row->[2]->new(Handler => $handler);
406 0 eval {
407 0 $parser->parse_string($row->[1]);
408 };
409 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
410 0 warn("Indexing failed");
411 }
412 0 last;
413 }
414 }
415  
416 0 ▶sub show_history {
Subroutine show_history: not called CC=3 SCAR=24.8
Pod undocumented
417 0 my ($db, $page) = @_;
418 0 ▶ my $sth = $page ? $db->prepare('SELECT * FROM History WHERE name = ? ORDER BY modified DESC LIMIT 50') :
Branch: 1 branch
BranchTrueFalseTotal
$page ? : 0 0 0
419 $db->prepare('SELECT * FROM History ORDER BY modified DESC LIMIT 50');
420 0 $sth->execute($page);
421 0 my $hist = '<history>';
422 0 my %h;
423 0 my $cols = $sth->{NAME_lc};
424 0 ▶ while (my $row = $sth->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetch) 0 0 0
425 0 @h{@$cols} = @$row;
426 0 $hist .= '<entry>';
427 0 $hist .= '<page>' . xml_escape($h{name}) . '</page>';
428 0 $hist .= '<id>' . xml_escape($h{id}) . '</id>';
429 0 $hist .= '<modified>' . xml_escape(scalar gmtime($h{modified})) . '</modified>';
430 0 $hist .= '<ip-address>' . xml_escape($h{ip_address}) . '</ip-address>';
431 0 $hist .= '<username>' . xml_escape($h{username}) . '</username>';
432 0 $hist .= '<bytes>' . xml_escape(length($h{content})) . '</bytes>';
433 0 $hist .= '</entry>';
434 }
435 0 $hist .= '</history>';
436 0 return $hist;
437 }
438  
439 0 ▶sub show_history_page {
Subroutine show_history_page: not called CC=4 SCAR=30.0
Pod undocumented
440 0 my ($db, $page, $id) = @_;
441 0 my $sth = $db->prepare(<<'EOT');
442 SELECT History.content, Formatter.module,
443 History.ip_address, History.modified
444 FROM History, Formatter
445 WHERE History.formatterid = Formatter.id
446 AND History.name = ?
447 AND History.id = ?
448 EOT
449 0 $sth->execute($page, $id);
450
451 0 my $output = '';
452 0 my $handler = XML::SAX::Writer->new(Output => \$output);
453 0 my ($ip, $modified);
454 0 ▶ while ( my $row = $sth->fetch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetch) 0 0 0
455 0 ($ip, $modified) = ($row->[2], scalar(gmtime($row->[3])));
456 # create the parser
457 0 my $parser = $row->[1]->new(Handler => $handler);
458 0 eval {
459 0 $parser->parse_string($row->[0]);
460 };
461 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
462 0 $output = '<pod>
463 <para>
464 Error parsing the page: ' . xml_escape($@) . '
465 </para>
466 </pod>
467 ';
468 }
469 0 last;
470 }
471 0 ▶ if (!$output) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output) 0 0 0
472 0 $output = <<'EOT';
473 <pod>
474 <para>
475 Unable to find that history page, or unable to find formatter module
476 </para>
477 </pod>
478 EOT
479 }
480 0 $output =~ s/^<\?xml\s.*?\?>\s*//s;
481 0 $output = "<?ip-address " . xml_escape($ip) . "?>\n" .
482 "<?modified " . xml_escape($modified) . "?>\n" .
483 $output;
484 0 return $output;
485 }
486  
487 0 ▶sub restore_page {
Subroutine restore_page: not called CC=2 SCAR=17.9
Pod undocumented
488 0 my ($dbpath, $dbname, $page, $ip, $id, $user) = @_;
489
490 0 my $db = _mkdb($dbpath, $dbname);
491 0 my $sth = $db->prepare('SELECT * FROM History WHERE name = ? and id = ?');
492 0 $sth->execute($page, $id);
493 0 my $row = $sth->fetch;
494 0 ▶ die "No such row" unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
495 0 $sth->finish;
496 0 my ($texttype, $contents) = ($row->[2], $row->[3]);
497 0 _save_page($db, $page, $contents, $texttype, $ip, $user);
498 }
499  
500 0 ▶sub create_db {
Subroutine create_db: not called CC=1 SCAR=6.9
Pod undocumented
501 0 my ($db) = @_;
502
503 0 $db->do(q{
504 create table Page (
505 id INTEGER PRIMARY KEY,
506 name NOT NULL,
507 formatterid NOT NULL,
508 content,
509 last_modified,
510 ip_address,
511 username
512 )
513 });
514 0 $db->do(q{
515 create unique index Page_name on Page ( name )
516 });
517 0 $db->do(q{
518 create table History (
519 id INTEGER PRIMARY KEY,
520 name NOT NULL,
521 formatterid NOT NULL,
522 content,
523 modified,
524 ip_address,
525 username
526 )
527 });
528 0 $db->do(q{
529 CREATE TABLE IgnoreWord
530 (
531 id INTEGER PRIMARY KEY,
532 word NOT NULL
533 )
534 });
535 0 $db->do(q{CREATE UNIQUE INDEX IgnoreWord_word on IgnoreWord (word)});
536 0 $db->do(q{
537 CREATE TABLE Word
538 (
539 id INTEGER PRIMARY KEY,
540 word NOT NULL
541 )
542 });
543 0 $db->do(q{CREATE UNIQUE INDEX Word_word on Word (word)});
544 0 $db->do(q{
545 CREATE TABLE ContentIndex
546 (
547 page_id INTEGER NOT NULL,
548 word_id INTEGER NOT NULL,
549 value INTEGER NOT NULL
550 )
551 });
552 0 $db->do(q{
553 create unique index ContentIndex_idx on ContentIndex (page_id, word_id)
554 });
555 0 $db->do(q{
556 create table Formatter ( id INTEGER PRIMARY KEY, module NOT NULL, name NOT NULL)
557 });
558 0 $db->do(q{
559 insert into Formatter (module, name) values ('Pod::SAX', 'pod - plain old documentation')
560 });
561 0 $db->do(q{
562 insert into Formatter (module, name) values ('Text::WikiFormat::SAX', 'wiki text')
563 });
564 0 $db->do(q{
565 insert into Formatter (module, name) values ('XML::LibXML::SAX::Parser', 'xml (freeform)')
566 });
567 0 $db->commit;
568 }
569  
570 0 ▶sub extract_page_info {
Subroutine extract_page_info: not called CC=6 SCAR=37.4
Pod undocumented
571 0 my ($path_info) = @_;
572 0 $path_info =~ s/^\///;
573 0 my ($db, $page) = split("/", $path_info, 2);
574 0 ▶ $page ||= ''; # can't have page named 0. Ah well.
Condition0%0/2
$page ||= ""
l!l
00
MC/DC0%0/1
$page ||= ""
$page
Truth table0%0/2
A
A= $page
Aresult
11
00
575  
576 0 ▶ if (!$db) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $db) { } 0 0 0
elsif (not $db =~ /^[A-Z][A-Za-z0-9:_-]+$/) { } 0 0 0
elsif (length $page and not $page =~ /^[A-Z][A-Za-z0-9:_-]+$/) { } 0 0 0
577 0 return ('', '');
578 }
579 elsif ($db !~ /^[A-Z][A-Za-z0-9:_-]+$/) {
580 0 die "Invalid db name: $db";
581 }
582 ▶ elsif (length($page) && $page !~ /^[A-Z][A-Za-z0-9:_-]+$/) {
Condition0%0/3
length $page and not $page =~ /^[A-Z][A-Za-z0-9:_-]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
length $page and not $page =~ /^[A-Z][A-Za-z0-9:_-]+$/
length $page not $page =~ /^[A-Z][A-Za-z0-9:_-]+$/
Truth table0%0/3
A and not(B)
A= length $pageB= not $page =~ /^[A-Z][A-Za-z0-9:_-]+$/
ABresult
0X0
100
111
583 0 die "Invalid page name: $page";
584 }
585 0 return ($db, $page);
586 }
587  
588 1 1;
589  
590 __END__
↑ Summary Wiki/Indexer.pm »