lib/PurpleWiki/Transclusion.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 144 CC 10 SCAR 40.7
CC
10
Cov
0%
get
110.0
new
2.0
getURL
2.0
CRAP 58.8
1 # PurpleWiki::Transclusion.pm
2 # vi:ai:sw=4:ts=4:et:sm
3 #
4 # $Id: Transclusion.pm 397 2004-06-16 19:01:10Z cdent $
5 #
6 # Copyright (c) Blue Oxen Associates 2002-2003. All rights reserved.
7 #
8 # This file is part of PurpleWiki. PurpleWiki is derived from:
9 #
10 # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001
11 # AtisWiki v0.3 (c) Markus Denker 1998
12 # CVWiki CVS-patches (c) Peter Merel 1997
13 # The Original WikiWikiWeb (c) Ward Cunningham
14 #
15 # PurpleWiki is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the
27 # Free Software Foundation, Inc.
28 # 59 Temple Place, Suite 330
29 # Boston, MA 02111-1307 USA
30  
31 package PurpleWiki::Transclusion;
32  
33 use strict;
34 use DB_File;
35 use LWP::UserAgent;
36 use PurpleWiki::Config;
37 use PurpleWiki::Sequence;
38 use PurpleWiki::Parser::WikiText;
39  
40 our $VERSION;
41 $VERSION = sprintf("%d", q$Id: Transclusion.pm 397 2004-06-16 19:01:10Z cdent $ =~ /\s(\d+)\s/);
42  
43 # The name of the index file. Its directory comes from Config.
44 my $INDEX_FILE = 'sequence.index';
45  
46 # Creates a new Transclusion object associated with
47 # the sequence.index in the DataDir. The index is
48 # used to find the URL from which a particular NID
49 # originates. See get() for more.
50 #
51 # A single Transclusion object should be able to do
52 # multiple gets, but this has not been tested.
53 # FIXME: will a DB file opened for a long read be
54 # aware of new writes? Presumably not?
55 sub new {
56 my $class = shift;
57 my $self = {};
58 bless ($self, $class);
59 my %params = @_;
60  
61 $self->{config} = PurpleWiki::Config->instance();
62 $self->{url} = $params{url};
63 $self->{outputType} = $params{outputType};
64  
65 return $self;
66 }
67  
68 # Takes the provided nid, looks it up in the sequence.index
69 # and then uses HTTP to retrieve the page on which that NID
70 # is found. The retrieved page is parsed to gather the
71 # content associated with the NID. A string containing
72 # the content or an error message if it could not be obtained
73 # is returned.
74  
75 sub get {
76 my $self = shift;
77 my $nid = shift;
78 my $nidLong = "nid$nid";
79 my $outputType = $self->{outputType} || '';
80 my $content;
81  
82 # get the URL that hosts this nid out of the the db
83 my $url = $self->getURL($nid);
84  
85 $content = "no URL for $nid" unless $url;
86  
87 if ($url) {
88 # if the page being retrieved is a wiki page
89 # and source and target are the same, don't
90 # try to retrieve. Same url on static content
91 # is fine.
92 # FIXME: assumes that anything not the wiki
93 # is static content
94 my $scriptName = $self->{config}->ScriptName;
95 if ((($url =~ /$scriptName/) || ($url =~ /\.wiki$/)) &&
96 ($url eq $self->{url})) {
97 $content = q(Transclusion loop, please remove.);
98 } elsif ($url =~ $ENV{HTTP_HOST} && $url =~ /$scriptName/) {
99 my ($pageName) = ($url =~ /\?([^&]+)\b/);
100 my $page = new PurpleWiki::Database::Page(id => $pageName);
101 my $parser = new PurpleWiki::Parser::WikiText;
102 if ($page->pageExists()) {
103 $page->openPage();
104 my $tree = $parser->parse($page->getText()->getText(),
105 'add_node_ids' => 0);
106 $content = $tree->view('subtree',
107 'nid' => uc($nid));
108 }
109
110 $content = "transclusion index out of sync" if not $content;
111 } else {
112 # request the content of the URL
113 my $ua = new LWP::UserAgent(agent => ref($self));
114 my $request = new HTTP::Request('GET', $url);
115  
116 # If we have the right config vars for authenticating
117 # trying authenticating the request.
118 if ($self->{config}->HttpUser() && $self->{config}->HttpPass()) {
119 $request->authorization_basic($self->{config}->HttpUser(),
120 $self->{config}->HttpPass());
121 }
122  
123 my $result = $ua->request($request);
124
125 if ($result->is_success()) {
126 $content = $result->content();
127 ($content =~ s/^.*<a name="$nidLong"[^>]+><\/a>//is &&
128 $content =~
129 s/&nbsp;&nbsp;\s*<a class="nid" title="$nid".*$//is )
130 || ($content = "transclusion index out of sync");
131 } else {
132 $content = "unable to retrieve content: " . $result->code();
133 }
134 }
135 }
136  
137
138 if ($outputType !~ /plaintext/ and not ref($content)) {
139 $content = qq(<span id="$nidLong" class="transclusion">) .
140 qq($content&nbsp;<a class="nid" title="$nid" ) .
141 qq(href="$url#$nidLong">T</a></span>);
142 }
143  
144 return $content;
145 }
146  
147 sub getURL {
148 my $self = shift;
149 my $nid = shift;
150  
151 my $sequence = new PurpleWiki::Sequence($self->{config}->LocalSequenceDir(),
152 $self->{config}->RemoteSequenceURL());
153 return $sequence->getURL($nid);
154 }
155  
156 # Attaches to the the DB file which contains the NID:URL index
157 sub _tieHash {
158 my $self = shift;
159 my $file = shift;
160  
161 tie %{$self->{db}}, 'DB_File', $file, O_RDONLY, 0444, $DB_HASH or
162 warn "unable to tie $file: $!";
163 }
164  
165 1;
166 __END__